structs.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. MaxAge int `json:"max_age"`
  99. Code string `json:"code"`
  100. Guild *Guild `json:"guild"`
  101. Revoked bool `json:"revoked"`
  102. CreatedAt string `json:"created_at"` // TODO make timestamp
  103. Temporary bool `json:"temporary"`
  104. Uses int `json:"uses"`
  105. MaxUses int `json:"max_uses"`
  106. Inviter *User `json:"inviter"`
  107. XkcdPass bool `json:"xkcdpass"`
  108. Channel *Channel `json:"channel"`
  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. type Emoji struct {
  132. Roles []string `json:"roles"`
  133. RequireColons bool `json:"require_colons"`
  134. Name string `json:"name"`
  135. Managed bool `json:"managed"`
  136. ID string `json:"id"`
  137. }
  138. // A Guild holds all data related to a specific Discord Guild. Guilds are also
  139. // sometimes referred to as Servers in the Discord client.
  140. type Guild struct {
  141. ID string `json:"id"`
  142. Name string `json:"name"`
  143. Icon string `json:"icon"`
  144. Region string `json:"region"`
  145. AfkTimeout int `json:"afk_timeout"`
  146. AfkChannelID string `json:"afk_channel_id"`
  147. EmbedChannelID string `json:"embed_channel_id"`
  148. EmbedEnabled bool `json:"embed_enabled"`
  149. OwnerID string `json:"owner_id"`
  150. Large bool `json:"large"` // ??
  151. JoinedAt string `json:"joined_at"` // make this a timestamp
  152. Splash string `json:"splash"`
  153. Roles []*Role `json:"roles"`
  154. Emojis []*Emoji `json:"emojis"`
  155. Members []*Member `json:"members"`
  156. Presences []*Presence `json:"presences"`
  157. Channels []*Channel `json:"channels"`
  158. VoiceStates []*VoiceState `json:"voice_states"`
  159. }
  160. // A Role stores information about Discord guild member roles.
  161. type Role struct {
  162. ID string `json:"id"`
  163. Name string `json:"name"`
  164. Managed bool `json:"managed"`
  165. Color int `json:"color"`
  166. Hoist bool `json:"hoist"`
  167. Position int `json:"position"`
  168. Permissions int `json:"permissions"`
  169. }
  170. // A VoiceState stores the voice states of Guilds
  171. type VoiceState struct {
  172. UserID string `json:"user_id"`
  173. Suppress bool `json:"suppress"`
  174. SessionID string `json:"session_id"`
  175. SelfMute bool `json:"self_mute"`
  176. SelfDeaf bool `json:"self_deaf"`
  177. Mute bool `json:"mute"`
  178. Deaf bool `json:"deaf"`
  179. ChannelID string `json:"channel_id"`
  180. }
  181. // A Presence stores the online, offline, or idle and game status of Guild members.
  182. type Presence struct {
  183. User *User `json:"user"`
  184. Status string `json:"status"`
  185. Game *Game `json:"game"`
  186. }
  187. type Game struct {
  188. Name string `json:"name"`
  189. }
  190. // A Member stores user information for Guild members.
  191. type Member struct {
  192. GuildID string `json:"guild_id"`
  193. JoinedAt string `json:"joined_at"`
  194. Deaf bool `json:"deaf"`
  195. Mute bool `json:"mute"`
  196. User *User `json:"user"`
  197. Roles []string `json:"roles"`
  198. }
  199. // A User stores all data for an individual Discord user.
  200. type User struct {
  201. ID string `json:"id"`
  202. Email string `json:"email"`
  203. Username string `json:"username"`
  204. Avatar string `json:"Avatar"`
  205. Verified bool `json:"verified"`
  206. //Discriminator int `json:"discriminator,string"` // TODO: See below
  207. }
  208. // TODO: Research issue.
  209. // Discriminator sometimes comes as a string
  210. // and sometimes it comes as a int. Weird.
  211. // to avoid errors I've just commented it out
  212. // but it doesn't seem to just kill the whole
  213. // program. Heartbeat is taken on READY even
  214. // with error and the system continues to read
  215. // it just doesn't seem able to handle this one
  216. // field correctly. Need to research this more.
  217. // A Settings stores data for a specific users Discord client settings.
  218. type Settings struct {
  219. RenderEmbeds bool `json:"render_embeds"`
  220. InlineEmbedMedia bool `json:"inline_embed_media"`
  221. EnableTtsCommand bool `json:"enable_tts_command"`
  222. MessageDisplayCompact bool `json:"message_display_compact"`
  223. Locale string `json:"locale"`
  224. ShowCurrentGame bool `json:"show_current_game"`
  225. Theme string `json:"theme"`
  226. MutedChannels []string `json:"muted_channels"`
  227. }
  228. // An Event provides a basic initial struct for all websocket event.
  229. type Event struct {
  230. Type string `json:"t"`
  231. State int `json:"s"`
  232. Operation int `json:"op"`
  233. Direction int `json:"dir"`
  234. RawData json.RawMessage `json:"d"`
  235. }
  236. // A Ready stores all data for the websocket READY event.
  237. type Ready struct {
  238. Version int `json:"v"`
  239. SessionID string `json:"session_id"`
  240. HeartbeatInterval time.Duration `json:"heartbeat_interval"`
  241. User *User `json:"user"`
  242. ReadState []*ReadState `json:"read_state"`
  243. PrivateChannels []*Channel `json:"private_channels"`
  244. Guilds []*Guild `json:"guilds"`
  245. }
  246. type RateLimit struct {
  247. Bucket string `json:"bucket"`
  248. Message string `json:"message"`
  249. RetryAfter time.Duration `json:"retry_after"`
  250. }
  251. // A ReadState stores data on the read state of channels.
  252. type ReadState struct {
  253. MentionCount int
  254. LastMessageID string `json:"last_message_id"`
  255. ID string `json:"id"`
  256. }
  257. // A TypingStart stores data for the typing start websocket event.
  258. type TypingStart struct {
  259. UserID string `json:"user_id"`
  260. ChannelID string `json:"channel_id"`
  261. Timestamp int `json:"timestamp"`
  262. }
  263. // A PresenceUpdate stores data for the pressence update websocket event.
  264. type PresenceUpdate struct {
  265. User *User `json:"user"`
  266. Status string `json:"status"`
  267. Roles []string `json:"roles"`
  268. GuildID string `json:"guild_id"`
  269. Game *Game `json:"game"`
  270. }
  271. // A MessageAck stores data for the message ack websocket event.
  272. type MessageAck struct {
  273. MessageID string `json:"message_id"`
  274. ChannelID string `json:"channel_id"`
  275. }
  276. // A GuildIntegrationsUpdate stores data for the guild integrations update
  277. // websocket event.
  278. type GuildIntegrationsUpdate struct {
  279. GuildID string `json:"guild_id"`
  280. }
  281. // A GuildRole stores data for guild role websocket events.
  282. type GuildRole struct {
  283. Role *Role `json:"role"`
  284. GuildID string `json:"guild_id"`
  285. }
  286. // A GuildRoleDelete stores data for the guild role delete websocket event.
  287. type GuildRoleDelete struct {
  288. RoleID string `json:"role_id"`
  289. GuildID string `json:"guild_id"`
  290. }
  291. // A GuildBan stores data for a guild ban.
  292. type GuildBan struct {
  293. User *User `json:"user"`
  294. GuildID string `json:"guild_id"`
  295. }
  296. // A GuildEmojisUpdate stores data for a guild emoji update event.
  297. type GuildEmojisUpdate struct {
  298. GuildID string `json:"guild_id"`
  299. Emojis []*Emoji `json:"emojis"`
  300. }
  301. // A State contains the current known state.
  302. // As discord sends this in a READY blob, it seems reasonable to simply
  303. // use that struct as the data store.
  304. type State struct {
  305. sync.RWMutex
  306. Ready
  307. MaxMessageCount int
  308. }