structs.go 10 KB

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