structs.go 12 KB

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