structs.go 11 KB

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