structs.go 12 KB

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