structs.go 12 KB

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