structs.go 11 KB

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