structs.go 11 KB

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