structs.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // Discordgo - Discord bindings for Go
  2. // Available at https://github.com/bwmarrin/discordgo
  3. // Copyright 2015 Bruce Marriner <bruce@sqls.net>. All rights reserved.
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file.
  6. // This file contains all structures for the discordgo package. These
  7. // may be moved about later into seperate files but I find it easier to have
  8. // them all located together.
  9. package discordgo
  10. import (
  11. "encoding/json"
  12. "fmt"
  13. "net"
  14. "strings"
  15. "time"
  16. "github.com/gorilla/websocket"
  17. )
  18. // A Session represents a connection to the Discord REST API.
  19. // token : The authentication token returned from Discord
  20. // Debug : If set to ture debug logging will be displayed.
  21. type Session struct {
  22. // General configurable settings.
  23. Token string // Authentication token for this session
  24. Debug bool // Debug for printing JSON request/responses
  25. AutoMention bool // if set to True, ChannelSendMessage will auto mention <@ID>
  26. // Settable Callback functions for Websocket Events
  27. OnEvent func(*Session, Event) // should Event be *Event?
  28. OnReady func(*Session, Ready)
  29. OnTypingStart func(*Session, TypingStart)
  30. OnMessageCreate func(*Session, Message)
  31. OnMessageUpdate func(*Session, Message)
  32. OnMessageDelete func(*Session, MessageDelete)
  33. OnMessageAck func(*Session, MessageAck)
  34. OnUserUpdate func(*Session, User)
  35. OnPresenceUpdate func(*Session, PresenceUpdate)
  36. OnVoiceStateUpdate func(*Session, VoiceState)
  37. OnChannelCreate func(*Session, Channel)
  38. OnChannelUpdate func(*Session, Channel)
  39. OnChannelDelete func(*Session, Channel)
  40. OnGuildCreate func(*Session, Guild)
  41. OnGuildUpdate func(*Session, Guild)
  42. OnGuildDelete func(*Session, Guild)
  43. OnGuildMemberAdd func(*Session, Member)
  44. OnGuildMemberRemove func(*Session, Member)
  45. OnGuildMemberDelete func(*Session, Member) // which is it?
  46. OnGuildMemberUpdate func(*Session, Member)
  47. OnGuildRoleCreate func(*Session, GuildRole)
  48. OnGuildRoleUpdate func(*Session, GuildRole)
  49. OnGuildRoleDelete func(*Session, GuildRoleDelete)
  50. OnGuildIntegrationsUpdate func(*Session, GuildIntegrationsUpdate)
  51. OnGuildBanAdd func(*Session, GuildBan)
  52. OnGuildBanRemove func(*Session, GuildBan)
  53. OnUserSettingsUpdate func(*Session, map[string]interface{}) // TODO: Find better way?
  54. // Exposed but should not be modified by User.
  55. SessionID string // from websocket READY packet
  56. DataReady bool // Set to true when Data Websocket is ready
  57. VoiceReady bool // Set to true when Voice Websocket is ready
  58. UDPReady bool // Set to true when UDP Connection is ready
  59. // Other..
  60. wsConn *websocket.Conn
  61. //TODO, add bools for like.
  62. // are we connnected to websocket?
  63. // have we authenticated to login?
  64. // lets put all the general session
  65. // tracking and infos here.. clearly
  66. // Everything below here is used for Voice testing.
  67. // This stuff is almost guarenteed to change a lot
  68. // and is even a bit hackish right now.
  69. VwsConn *websocket.Conn // new for voice
  70. VSessionID string
  71. VToken string
  72. VEndpoint string
  73. VGuildID string
  74. VChannelID string
  75. Vop2 VoiceOP2
  76. UDPConn *net.UDPConn
  77. // Managed state object, updated with events.
  78. State *State
  79. StateEnabled bool
  80. }
  81. // A Message stores all data related to a specific Discord message.
  82. type Message struct {
  83. ID string `json:"id"`
  84. Author User `json:"author"`
  85. Content string `json:"content"`
  86. Attachments []Attachment `json:"attachments"`
  87. Tts bool `json:"tts"`
  88. Embeds []Embed `json:"embeds"`
  89. Timestamp string `json:"timestamp"`
  90. MentionEveryone bool `json:"mention_everyone"`
  91. EditedTimestamp string `json:"edited_timestamp"`
  92. Mentions []User `json:"mentions"`
  93. ChannelID string `json:"channel_id"`
  94. }
  95. // ContentWithMentionsReplaced will replace all @<id> mentions with the
  96. // username of the mention.
  97. func (m *Message) ContentWithMentionsReplaced() string {
  98. content := m.Content
  99. for _, user := range m.Mentions {
  100. content = strings.Replace(content, fmt.Sprintf("<@%s>", user.ID), fmt.Sprintf("@%s", user.Username), -1)
  101. }
  102. return content
  103. }
  104. // An Attachment stores data for message attachments.
  105. type Attachment struct { //TODO figure this out
  106. }
  107. // An Embed stores data for message embeds.
  108. type Embed struct { // TODO figure this out
  109. }
  110. // A VoiceRegion stores data for a specific voice region server.
  111. type VoiceRegion struct {
  112. ID string `json:"id"`
  113. Name string `json:"name"`
  114. Hostname string `json:"sample_hostname"`
  115. Port int `json:"sample_port"`
  116. }
  117. // A VoiceICE stores data for voice ICE servers.
  118. type VoiceICE struct {
  119. TTL string `json:"ttl"`
  120. Servers []ICEServer `json:"servers"`
  121. }
  122. // A ICEServer stores data for a specific voice ICE server.
  123. type ICEServer struct {
  124. URL string `json:"url"`
  125. Username string `json:"username"`
  126. Credential string `json:"credential"`
  127. }
  128. // A Invite stores all data related to a specific Discord Guild or Channel invite.
  129. type Invite struct {
  130. MaxAge int `json:"max_age"`
  131. Code string `json:"code"`
  132. Guild Guild `json:"guild"`
  133. Revoked bool `json:"revoked"`
  134. CreatedAt string `json:"created_at"` // TODO make timestamp
  135. Temporary bool `json:"temporary"`
  136. Uses int `json:"uses"`
  137. MaxUses int `json:"max_uses"`
  138. Inviter User `json:"inviter"`
  139. XkcdPass bool `json:"xkcdpass"`
  140. Channel Channel `json:"channel"`
  141. }
  142. // A Channel holds all data related to an individual Discord channel.
  143. type Channel struct {
  144. ID string `json:"id"`
  145. GuildID string `json:"guild_id"`
  146. Name string `json:"name"`
  147. Topic string `json:"topic"`
  148. Position int `json:"position"`
  149. Type string `json:"type"`
  150. PermissionOverwrites []PermissionOverwrite `json:"permission_overwrites"`
  151. IsPrivate bool `json:"is_private"`
  152. LastMessageID string `json:"last_message_id"`
  153. Recipient User `json:"recipient"`
  154. }
  155. // A PermissionOverwrite holds permission overwrite data for a Channel
  156. type PermissionOverwrite struct {
  157. ID string `json:"id"`
  158. Type string `json:"type"`
  159. Deny int `json:"deny"`
  160. Allow int `json:"allow"`
  161. }
  162. // A Guild holds all data related to a specific Discord Guild. Guilds are also
  163. // sometimes referred to as Servers in the Discord client.
  164. type Guild struct {
  165. ID string `json:"id"`
  166. Name string `json:"name"`
  167. Icon string `json:"icon"`
  168. Region string `json:"region"`
  169. AfkTimeout int `json:"afk_timeout"`
  170. AfkChannelID string `json:"afk_channel_id"`
  171. EmbedChannelID string `json:"embed_channel_id"`
  172. EmbedEnabled bool `json:"embed_enabled"`
  173. OwnerID string `json:"owner_id"`
  174. Large bool `json:"large"` // ??
  175. JoinedAt string `json:"joined_at"` // make this a timestamp
  176. Roles []Role `json:"roles"`
  177. Members []Member `json:"members"`
  178. Presences []Presence `json:"presences"`
  179. Channels []Channel `json:"channels"`
  180. VoiceStates []VoiceState `json:"voice_states"`
  181. }
  182. // A Role stores information about Discord guild member roles.
  183. type Role struct {
  184. ID string `json:"id"`
  185. Name string `json:"name"`
  186. Managed bool `json:"managed"`
  187. Color int `json:"color"`
  188. Hoist bool `json:"hoist"`
  189. Position int `json:"position"`
  190. Permissions int `json:"permissions"`
  191. }
  192. // A VoiceState stores the voice states of Guilds
  193. type VoiceState struct {
  194. UserID string `json:"user_id"`
  195. Suppress bool `json:"suppress"`
  196. SessionID string `json:"session_id"`
  197. SelfMute bool `json:"self_mute"`
  198. SelfDeaf bool `json:"self_deaf"`
  199. Mute bool `json:"mute"`
  200. Deaf bool `json:"deaf"`
  201. ChannelID string `json:"channel_id"`
  202. }
  203. // A Presence stores the online, offline, or idle and game status of Guild members.
  204. type Presence struct {
  205. User User `json:"user"`
  206. Status string `json:"status"`
  207. Game Game `json:"game"`
  208. }
  209. type Game struct {
  210. Name string `json:"name"`
  211. }
  212. // A Member stores user information for Guild members.
  213. type Member struct {
  214. GuildID string `json:"guild_id"`
  215. JoinedAt string `json:"joined_at"`
  216. Deaf bool `json:"deaf"`
  217. Mute bool `json:"mute"`
  218. User User `json:"user"`
  219. Roles []string `json:"roles"`
  220. }
  221. // A User stores all data for an individual Discord user.
  222. type User struct {
  223. ID string `json:"id"`
  224. Email string `json:"email"`
  225. Username string `json:"username"`
  226. Avatar string `json:"Avatar"`
  227. Verified bool `json:"verified"`
  228. //Discriminator int `json:"discriminator,string"` // TODO: See below
  229. }
  230. // TODO: Research issue.
  231. // Discriminator sometimes comes as a string
  232. // and sometimes it comes as a int. Weird.
  233. // to avoid errors I've just commented it out
  234. // but it doesn't seem to just kill the whole
  235. // program. Heartbeat is taken on READY even
  236. // with error and the system continues to read
  237. // it just doesn't seem able to handle this one
  238. // field correctly. Need to research this more.
  239. // A Settings stores data for a specific users Discord client settings.
  240. type Settings struct {
  241. RenderEmbeds bool `json:"render_embeds"`
  242. InlineEmbedMedia bool `json:"inline_embed_media"`
  243. EnableTtsCommand bool `json:"enable_tts_command"`
  244. MessageDisplayCompact bool `json:"message_display_compact"`
  245. Locale string `json:"locale"`
  246. ShowCurrentGame bool `json:"show_current_game"`
  247. Theme string `json:"theme"`
  248. MutedChannels []string `json:"muted_channels"`
  249. }
  250. // An Event provides a basic initial struct for all websocket event.
  251. type Event struct {
  252. Type string `json:"t"`
  253. State int `json:"s"`
  254. Operation int `json:"o"`
  255. Direction int `json:"dir"`
  256. RawData json.RawMessage `json:"d"`
  257. }
  258. // A Ready stores all data for the websocket READY event.
  259. type Ready struct {
  260. Version int `json:"v"`
  261. SessionID string `json:"session_id"`
  262. HeartbeatInterval time.Duration `json:"heartbeat_interval"`
  263. User User `json:"user"`
  264. ReadState []ReadState
  265. PrivateChannels []Channel `json:"private_channels"`
  266. Guilds []Guild `json:"guilds"`
  267. }
  268. // A ReadState stores data on the read state of channels.
  269. type ReadState struct {
  270. MentionCount int
  271. LastMessageID string `json:"last_message_id"`
  272. ID string `json:"id"`
  273. }
  274. // A TypingStart stores data for the typing start websocket event.
  275. type TypingStart struct {
  276. UserID string `json:"user_id"`
  277. ChannelID string `json:"channel_id"`
  278. Timestamp int `json:"timestamp"`
  279. }
  280. // A PresenceUpdate stores data for the pressence update websocket event.
  281. type PresenceUpdate struct {
  282. User User `json:"user"`
  283. Status string `json:"status"`
  284. Roles []string `json:"roles"`
  285. GuildID string `json:"guild_id"`
  286. Game Game `json:"game"`
  287. }
  288. // A MessageAck stores data for the message ack websocket event.
  289. type MessageAck struct {
  290. MessageID string `json:"message_id"`
  291. ChannelID string `json:"channel_id"`
  292. }
  293. // A MessageDelete stores data for the message delete websocket event.
  294. type MessageDelete struct {
  295. ID string `json:"id"`
  296. ChannelID string `json:"channel_id"`
  297. } // so much like MessageAck..
  298. // A GuildIntegrationsUpdate stores data for the guild integrations update
  299. // websocket event.
  300. type GuildIntegrationsUpdate struct {
  301. GuildID string `json:"guild_id"`
  302. }
  303. // A GuildRole stores data for guild role websocket events.
  304. type GuildRole struct {
  305. Role Role `json:"role"`
  306. GuildID string `json:"guild_id"`
  307. }
  308. // A GuildRoleDelete stores data for the guild role delete websocket event.
  309. type GuildRoleDelete struct {
  310. RoleID string `json:"role_id"`
  311. GuildID string `json:"guild_id"`
  312. }
  313. // A GuildBan stores data for a guild ban.
  314. type GuildBan struct {
  315. User User `json:"user"`
  316. GuildID string `json:"guild_id"`
  317. }
  318. // A State contains the current known state.
  319. // As discord sends this in a READY blob, it seems reasonable to simply
  320. // use that struct as the data store.
  321. type State struct {
  322. Ready
  323. }