structs.go 12 KB

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