structs.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. // Sharding
  30. ShardID int
  31. NumShards int
  32. // Should state tracking be enabled.
  33. // State tracking is the best way for getting the the users
  34. // active guilds and the members of the guilds.
  35. StateEnabled bool
  36. // Exposed but should not be modified by User.
  37. // Whether the Data Websocket is ready
  38. DataReady bool
  39. // Whether the Voice Websocket is ready
  40. VoiceReady bool
  41. // Whether the UDP Connection is ready
  42. UDPReady bool
  43. // Stores a mapping of guild id's to VoiceConnections
  44. VoiceConnections map[string]*VoiceConnection
  45. // Managed state object, updated internally with events when
  46. // StateEnabled is true.
  47. State *State
  48. handlersMu sync.RWMutex
  49. // This is a mapping of event struct to a reflected value
  50. // for event handlers.
  51. // We store the reflected value instead of the function
  52. // reference as it is more performant, instead of re-reflecting
  53. // the function each event.
  54. handlers map[interface{}][]reflect.Value
  55. // The websocket connection.
  56. wsConn *websocket.Conn
  57. // When nil, the session is not listening.
  58. listening chan interface{}
  59. }
  60. // A VoiceRegion stores data for a specific voice region server.
  61. type VoiceRegion struct {
  62. ID string `json:"id"`
  63. Name string `json:"name"`
  64. Hostname string `json:"sample_hostname"`
  65. Port int `json:"sample_port"`
  66. }
  67. // A VoiceICE stores data for voice ICE servers.
  68. type VoiceICE struct {
  69. TTL string `json:"ttl"`
  70. Servers []*ICEServer `json:"servers"`
  71. }
  72. // A ICEServer stores data for a specific voice ICE server.
  73. type ICEServer struct {
  74. URL string `json:"url"`
  75. Username string `json:"username"`
  76. Credential string `json:"credential"`
  77. }
  78. // A Invite stores all data related to a specific Discord Guild or Channel invite.
  79. type Invite struct {
  80. Guild *Guild `json:"guild"`
  81. Channel *Channel `json:"channel"`
  82. Inviter *User `json:"inviter"`
  83. Code string `json:"code"`
  84. CreatedAt string `json:"created_at"` // TODO make timestamp
  85. MaxAge int `json:"max_age"`
  86. Uses int `json:"uses"`
  87. MaxUses int `json:"max_uses"`
  88. XkcdPass bool `json:"xkcdpass"`
  89. Revoked bool `json:"revoked"`
  90. Temporary bool `json:"temporary"`
  91. }
  92. // A Channel holds all data related to an individual Discord channel.
  93. type Channel struct {
  94. ID string `json:"id"`
  95. GuildID string `json:"guild_id"`
  96. Name string `json:"name"`
  97. Topic string `json:"topic"`
  98. Type string `json:"type"`
  99. LastMessageID string `json:"last_message_id"`
  100. Position int `json:"position"`
  101. Bitrate int `json:"bitrate"`
  102. IsPrivate bool `json:"is_private"`
  103. Recipient *User `json:"recipient"`
  104. Messages []*Message `json:"-"`
  105. PermissionOverwrites []*PermissionOverwrite `json:"permission_overwrites"`
  106. }
  107. // A PermissionOverwrite holds permission overwrite data for a Channel
  108. type PermissionOverwrite struct {
  109. ID string `json:"id"`
  110. Type string `json:"type"`
  111. Deny int `json:"deny"`
  112. Allow int `json:"allow"`
  113. }
  114. // Emoji struct holds data related to Emoji's
  115. type Emoji struct {
  116. ID string `json:"id"`
  117. Name string `json:"name"`
  118. Roles []string `json:"roles"`
  119. Managed bool `json:"managed"`
  120. RequireColons bool `json:"require_colons"`
  121. }
  122. // VerificationLevel type defination
  123. type VerificationLevel int
  124. // Constants for VerificationLevel levels from 0 to 3 inclusive
  125. const (
  126. VerificationLevelNone VerificationLevel = iota
  127. VerificationLevelLow
  128. VerificationLevelMedium
  129. VerificationLevelHigh
  130. )
  131. // A Guild holds all data related to a specific Discord Guild. Guilds are also
  132. // sometimes referred to as Servers in the Discord client.
  133. type Guild struct {
  134. ID string `json:"id"`
  135. Name string `json:"name"`
  136. Icon string `json:"icon"`
  137. Region string `json:"region"`
  138. AfkChannelID string `json:"afk_channel_id"`
  139. EmbedChannelID string `json:"embed_channel_id"`
  140. OwnerID string `json:"owner_id"`
  141. JoinedAt string `json:"joined_at"` // make this a timestamp
  142. Splash string `json:"splash"`
  143. AfkTimeout int `json:"afk_timeout"`
  144. VerificationLevel VerificationLevel `json:"verification_level"`
  145. EmbedEnabled bool `json:"embed_enabled"`
  146. Large bool `json:"large"` // ??
  147. Roles []*Role `json:"roles"`
  148. Emojis []*Emoji `json:"emojis"`
  149. Members []*Member `json:"members"`
  150. Presences []*Presence `json:"presences"`
  151. Channels []*Channel `json:"channels"`
  152. VoiceStates []*VoiceState `json:"voice_states"`
  153. Unavailable *bool `json:"unavailable"`
  154. }
  155. // A GuildParams stores all the data needed to update discord guild settings
  156. type GuildParams struct {
  157. Name string `json:"name"`
  158. Region string `json:"region"`
  159. VerificationLevel *VerificationLevel `json:"verification_level"`
  160. }
  161. // A Role stores information about Discord guild member roles.
  162. type Role struct {
  163. ID string `json:"id"`
  164. Name string `json:"name"`
  165. Managed bool `json:"managed"`
  166. Hoist bool `json:"hoist"`
  167. Color int `json:"color"`
  168. Position int `json:"position"`
  169. Permissions int `json:"permissions"`
  170. }
  171. // A VoiceState stores the voice states of Guilds
  172. type VoiceState struct {
  173. UserID string `json:"user_id"`
  174. SessionID string `json:"session_id"`
  175. ChannelID string `json:"channel_id"`
  176. GuildID string `json:"guild_id"`
  177. Suppress bool `json:"suppress"`
  178. SelfMute bool `json:"self_mute"`
  179. SelfDeaf bool `json:"self_deaf"`
  180. Mute bool `json:"mute"`
  181. Deaf bool `json:"deaf"`
  182. }
  183. // A Presence stores the online, offline, or idle and game status of Guild members.
  184. type Presence struct {
  185. User *User `json:"user"`
  186. Status string `json:"status"`
  187. Game *Game `json:"game"`
  188. }
  189. // A Game struct holds the name of the "playing .." game for a user
  190. type Game struct {
  191. Name string `json:"name"`
  192. }
  193. // A Member stores user information for Guild members.
  194. type Member struct {
  195. GuildID string `json:"guild_id"`
  196. JoinedAt string `json:"joined_at"`
  197. Deaf bool `json:"deaf"`
  198. Mute bool `json:"mute"`
  199. User *User `json:"user"`
  200. Roles []string `json:"roles"`
  201. }
  202. // A User stores all data for an individual Discord user.
  203. type User struct {
  204. ID string `json:"id"`
  205. Email string `json:"email"`
  206. Username string `json:"username"`
  207. Avatar string `json:"Avatar"`
  208. Discriminator string `json:"discriminator"`
  209. Token string `json:"token"`
  210. Verified bool `json:"verified"`
  211. Bot bool `json:"bot"`
  212. }
  213. // A Settings stores data for a specific users Discord client settings.
  214. type Settings struct {
  215. RenderEmbeds bool `json:"render_embeds"`
  216. InlineEmbedMedia bool `json:"inline_embed_media"`
  217. EnableTtsCommand bool `json:"enable_tts_command"`
  218. MessageDisplayCompact bool `json:"message_display_compact"`
  219. ShowCurrentGame bool `json:"show_current_game"`
  220. Locale string `json:"locale"`
  221. Theme string `json:"theme"`
  222. MutedChannels []string `json:"muted_channels"`
  223. }
  224. // An Event provides a basic initial struct for all websocket event.
  225. type Event struct {
  226. Type string `json:"t"`
  227. State int `json:"s"`
  228. Operation int `json:"op"`
  229. Direction int `json:"dir"`
  230. RawData json.RawMessage `json:"d"`
  231. Struct interface{} `json:"-"`
  232. }
  233. // A Ready stores all data for the websocket READY event.
  234. type Ready struct {
  235. Version int `json:"v"`
  236. SessionID string `json:"session_id"`
  237. HeartbeatInterval time.Duration `json:"heartbeat_interval"`
  238. User *User `json:"user"`
  239. ReadState []*ReadState `json:"read_state"`
  240. PrivateChannels []*Channel `json:"private_channels"`
  241. Guilds []*Guild `json:"guilds"`
  242. }
  243. // A RateLimit struct holds information related to a specific rate limit.
  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 `json:"mention_count"`
  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 presence update websocket event.
  262. type PresenceUpdate struct {
  263. Status string `json:"status"`
  264. GuildID string `json:"guild_id"`
  265. Roles []string `json:"roles"`
  266. User *User `json:"user"`
  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 GuildIntegrationsUpdate stores data for the guild integrations update
  275. // websocket event.
  276. type GuildIntegrationsUpdate struct {
  277. GuildID string `json:"guild_id"`
  278. }
  279. // A GuildRole stores data for guild role websocket events.
  280. type GuildRole struct {
  281. Role *Role `json:"role"`
  282. GuildID string `json:"guild_id"`
  283. }
  284. // A GuildRoleDelete stores data for the guild role delete websocket event.
  285. type GuildRoleDelete struct {
  286. RoleID string `json:"role_id"`
  287. GuildID string `json:"guild_id"`
  288. }
  289. // A GuildBan stores data for a guild ban.
  290. type GuildBan struct {
  291. User *User `json:"user"`
  292. GuildID string `json:"guild_id"`
  293. }
  294. // A GuildEmojisUpdate stores data for a guild emoji update event.
  295. type GuildEmojisUpdate struct {
  296. GuildID string `json:"guild_id"`
  297. Emojis []*Emoji `json:"emojis"`
  298. }
  299. // A GuildIntegration stores data for a guild integration.
  300. type GuildIntegration struct {
  301. ID string `json:"id"`
  302. Name string `json:"name"`
  303. Type string `json:"type"`
  304. Enabled bool `json:"enabled"`
  305. Syncing bool `json:"syncing"`
  306. RoleID string `json:"role_id"`
  307. ExpireBehavior int `json:"expire_behavior"`
  308. ExpireGracePeriod int `json:"expire_grace_period"`
  309. User *User `json:"user"`
  310. Account *GuildIntegrationAccount `json:"account"`
  311. SyncedAt int `json:"synced_at"`
  312. }
  313. // A GuildIntegrationAccount stores data for a guild integration account.
  314. type GuildIntegrationAccount struct {
  315. ID string `json:"id"`
  316. Name string `json:"name"`
  317. }
  318. // A GuildEmbed stores data for a guild embed.
  319. type GuildEmbed struct {
  320. Enabled bool `json:"enabled"`
  321. ChannelID string `json:"channel_id"`
  322. }
  323. // A UserGuildSettingsChannelOverride stores data for a channel override for a users guild settings.
  324. type UserGuildSettingsChannelOverride struct {
  325. Muted bool `json:"muted"`
  326. MessageNotifications int `json:"message_notifications"`
  327. ChannelID string `json:"channel_id"`
  328. }
  329. // A UserGuildSettings stores data for a users guild settings.
  330. type UserGuildSettings struct {
  331. SupressEveryone bool `json:"suppress_everyone"`
  332. Muted bool `json:"muted"`
  333. MobilePush bool `json:"mobile_push"`
  334. MessageNotifications int `json:"message_notifications"`
  335. GuildID string `json:"guild_id"`
  336. ChannelOverrides []*UserGuildSettingsChannelOverride `json:"channel_overrides"`
  337. }
  338. // A State contains the current known state.
  339. // As discord sends this in a READY blob, it seems reasonable to simply
  340. // use that struct as the data store.
  341. type State struct {
  342. sync.RWMutex
  343. Ready
  344. MaxMessageCount int
  345. guildMap map[string]*Guild
  346. channelMap map[string]*Channel
  347. }
  348. // Constants for the different bit offsets of text channel permissions
  349. const (
  350. PermissionReadMessages = 1 << (iota + 10)
  351. PermissionSendMessages
  352. PermissionSendTTSMessages
  353. PermissionManageMessages
  354. PermissionEmbedLinks
  355. PermissionAttachFiles
  356. PermissionReadMessageHistory
  357. PermissionMentionEveryone
  358. )
  359. // Constants for the different bit offsets of voice permissions
  360. const (
  361. PermissionVoiceConnect = 1 << (iota + 20)
  362. PermissionVoiceSpeak
  363. PermissionVoiceMuteMembers
  364. PermissionVoiceDeafenMembers
  365. PermissionVoiceMoveMembers
  366. PermissionVoiceUseVAD
  367. )
  368. // Constants for the different bit offsets of general permissions
  369. const (
  370. PermissionCreateInstantInvite = 1 << iota
  371. PermissionKickMembers
  372. PermissionBanMembers
  373. PermissionManageRoles
  374. PermissionManageChannels
  375. PermissionManageServer
  376. PermissionAllText = PermissionReadMessages |
  377. PermissionSendMessages |
  378. PermissionSendTTSMessages |
  379. PermissionManageMessages |
  380. PermissionEmbedLinks |
  381. PermissionAttachFiles |
  382. PermissionReadMessageHistory |
  383. PermissionMentionEveryone
  384. PermissionAllVoice = PermissionVoiceConnect |
  385. PermissionVoiceSpeak |
  386. PermissionVoiceMuteMembers |
  387. PermissionVoiceDeafenMembers |
  388. PermissionVoiceMoveMembers |
  389. PermissionVoiceUseVAD
  390. PermissionAllChannel = PermissionAllText |
  391. PermissionAllVoice |
  392. PermissionCreateInstantInvite |
  393. PermissionManageRoles |
  394. PermissionManageChannels
  395. PermissionAll = PermissionAllChannel |
  396. PermissionKickMembers |
  397. PermissionBanMembers |
  398. PermissionManageServer
  399. )