structs.go 12 KB

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