structs.go 12 KB

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