structs.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 // Deprecated, will be removed.
  25. LogLevel int
  26. // Should the session reconnect the websocket on errors.
  27. ShouldReconnectOnError bool
  28. // Should the session request compressed websocket data.
  29. Compress bool
  30. // Sharding
  31. ShardID int
  32. ShardCount int
  33. // Should state tracking be enabled.
  34. // State tracking is the best way for getting the the users
  35. // active guilds and the members of the guilds.
  36. StateEnabled bool
  37. // Exposed but should not be modified by User.
  38. // Whether the Data Websocket is ready
  39. DataReady bool // NOTE: Maye be deprecated soon
  40. // Max number of REST API retries
  41. MaxRestRetries int
  42. // Status stores the currect status of the websocket connection
  43. // this is being tested, may stay, may go away.
  44. status int32
  45. // Whether the Voice Websocket is ready
  46. VoiceReady bool // NOTE: Deprecated.
  47. // Whether the UDP Connection is ready
  48. UDPReady bool // NOTE: Deprecated
  49. // Stores a mapping of guild id's to VoiceConnections
  50. VoiceConnections map[string]*VoiceConnection
  51. // Managed state object, updated internally with events when
  52. // StateEnabled is true.
  53. State *State
  54. handlersMu sync.RWMutex
  55. // This is a mapping of event struct to a reflected value
  56. // for event handlers.
  57. // We store the reflected value instead of the function
  58. // reference as it is more performant, instead of re-reflecting
  59. // the function each event.
  60. handlers map[interface{}][]reflect.Value
  61. // The websocket connection.
  62. wsConn *websocket.Conn
  63. // When nil, the session is not listening.
  64. listening chan interface{}
  65. // used to deal with rate limits
  66. // may switch to slices later
  67. // TODO: performance test map vs slices
  68. rateLimit rateLimitMutex
  69. // sequence tracks the current gateway api websocket sequence number
  70. sequence int
  71. // stores sessions current Discord Gateway
  72. gateway string
  73. // stores session ID of current Gateway connection
  74. sessionID string
  75. // used to make sure gateway websocket writes do not happen concurrently
  76. wsMutex sync.Mutex
  77. }
  78. type rateLimitMutex struct {
  79. sync.Mutex
  80. url map[string]*sync.Mutex
  81. // bucket map[string]*sync.Mutex // TODO :)
  82. }
  83. // A Resumed struct holds the data received in a RESUMED event
  84. type Resumed struct {
  85. HeartbeatInterval time.Duration `json:"heartbeat_interval"`
  86. Trace []string `json:"_trace"`
  87. }
  88. // A VoiceRegion stores data for a specific voice region server.
  89. type VoiceRegion struct {
  90. ID string `json:"id"`
  91. Name string `json:"name"`
  92. Hostname string `json:"sample_hostname"`
  93. Port int `json:"sample_port"`
  94. }
  95. // A VoiceICE stores data for voice ICE servers.
  96. type VoiceICE struct {
  97. TTL string `json:"ttl"`
  98. Servers []*ICEServer `json:"servers"`
  99. }
  100. // A ICEServer stores data for a specific voice ICE server.
  101. type ICEServer struct {
  102. URL string `json:"url"`
  103. Username string `json:"username"`
  104. Credential string `json:"credential"`
  105. }
  106. // A Invite stores all data related to a specific Discord Guild or Channel invite.
  107. type Invite struct {
  108. Guild *Guild `json:"guild"`
  109. Channel *Channel `json:"channel"`
  110. Inviter *User `json:"inviter"`
  111. Code string `json:"code"`
  112. CreatedAt Timestamp `json:"created_at"`
  113. MaxAge int `json:"max_age"`
  114. Uses int `json:"uses"`
  115. MaxUses int `json:"max_uses"`
  116. XkcdPass string `json:"xkcdpass"`
  117. Revoked bool `json:"revoked"`
  118. Temporary bool `json:"temporary"`
  119. }
  120. // A Channel holds all data related to an individual Discord channel.
  121. type Channel struct {
  122. ID string `json:"id"`
  123. GuildID string `json:"guild_id"`
  124. Name string `json:"name"`
  125. Topic string `json:"topic"`
  126. Type string `json:"type"`
  127. LastMessageID string `json:"last_message_id"`
  128. Position int `json:"position"`
  129. Bitrate int `json:"bitrate"`
  130. IsPrivate bool `json:"is_private"`
  131. Recipient *User `json:"recipient"`
  132. Messages []*Message `json:"-"`
  133. PermissionOverwrites []*PermissionOverwrite `json:"permission_overwrites"`
  134. }
  135. // A PermissionOverwrite holds permission overwrite data for a Channel
  136. type PermissionOverwrite struct {
  137. ID string `json:"id"`
  138. Type string `json:"type"`
  139. Deny int `json:"deny"`
  140. Allow int `json:"allow"`
  141. }
  142. // Emoji struct holds data related to Emoji's
  143. type Emoji struct {
  144. ID string `json:"id"`
  145. Name string `json:"name"`
  146. Roles []string `json:"roles"`
  147. Managed bool `json:"managed"`
  148. RequireColons bool `json:"require_colons"`
  149. }
  150. // VerificationLevel type defination
  151. type VerificationLevel int
  152. // Constants for VerificationLevel levels from 0 to 3 inclusive
  153. const (
  154. VerificationLevelNone VerificationLevel = iota
  155. VerificationLevelLow
  156. VerificationLevelMedium
  157. VerificationLevelHigh
  158. )
  159. // A Guild holds all data related to a specific Discord Guild. Guilds are also
  160. // sometimes referred to as Servers in the Discord client.
  161. type Guild struct {
  162. ID string `json:"id"`
  163. Name string `json:"name"`
  164. Icon string `json:"icon"`
  165. Region string `json:"region"`
  166. AfkChannelID string `json:"afk_channel_id"`
  167. EmbedChannelID string `json:"embed_channel_id"`
  168. OwnerID string `json:"owner_id"`
  169. JoinedAt Timestamp `json:"joined_at"`
  170. Splash string `json:"splash"`
  171. AfkTimeout int `json:"afk_timeout"`
  172. VerificationLevel VerificationLevel `json:"verification_level"`
  173. EmbedEnabled bool `json:"embed_enabled"`
  174. Large bool `json:"large"` // ??
  175. DefaultMessageNotifications int `json:"default_message_notifications"`
  176. Roles []*Role `json:"roles"`
  177. Emojis []*Emoji `json:"emojis"`
  178. Members []*Member `json:"members"`
  179. Presences []*Presence `json:"presences"`
  180. Channels []*Channel `json:"channels"`
  181. VoiceStates []*VoiceState `json:"voice_states"`
  182. Unavailable bool `json:"unavailable"`
  183. }
  184. // A UserGuild holds a brief version of a Guild
  185. type UserGuild struct {
  186. ID string `json:"id"`
  187. Name string `json:"name"`
  188. Icon string `json:"icon"`
  189. Owner bool `json:"owner"`
  190. Permissions int `json:"permissions"`
  191. }
  192. // A GuildParams stores all the data needed to update discord guild settings
  193. type GuildParams struct {
  194. Name string `json:"name"`
  195. Region string `json:"region"`
  196. VerificationLevel *VerificationLevel `json:"verification_level"`
  197. }
  198. // A Role stores information about Discord guild member roles.
  199. type Role struct {
  200. ID string `json:"id"`
  201. Name string `json:"name"`
  202. Managed bool `json:"managed"`
  203. Mentionable bool `json:"mentionable"`
  204. Hoist bool `json:"hoist"`
  205. Color int `json:"color"`
  206. Position int `json:"position"`
  207. Permissions int `json:"permissions"`
  208. }
  209. // A VoiceState stores the voice states of Guilds
  210. type VoiceState struct {
  211. UserID string `json:"user_id"`
  212. SessionID string `json:"session_id"`
  213. ChannelID string `json:"channel_id"`
  214. GuildID string `json:"guild_id"`
  215. Suppress bool `json:"suppress"`
  216. SelfMute bool `json:"self_mute"`
  217. SelfDeaf bool `json:"self_deaf"`
  218. Mute bool `json:"mute"`
  219. Deaf bool `json:"deaf"`
  220. }
  221. // A Presence stores the online, offline, or idle and game status of Guild members.
  222. type Presence struct {
  223. User *User `json:"user"`
  224. Status Status `json:"status"`
  225. Game *Game `json:"game"`
  226. }
  227. // A Game struct holds the name of the "playing .." game for a user
  228. type Game struct {
  229. Name string `json:"name"`
  230. Type int `json:"type"`
  231. URL string `json:"url"`
  232. }
  233. // A Member stores user information for Guild members.
  234. type Member struct {
  235. GuildID string `json:"guild_id"`
  236. JoinedAt string `json:"joined_at"`
  237. Nick string `json:"nick"`
  238. Deaf bool `json:"deaf"`
  239. Mute bool `json:"mute"`
  240. User *User `json:"user"`
  241. Roles []string `json:"roles"`
  242. }
  243. // A User stores all data for an individual Discord user.
  244. type User struct {
  245. ID string `json:"id"`
  246. Email string `json:"email"`
  247. Username string `json:"username"`
  248. Avatar string `json:"Avatar"`
  249. Discriminator string `json:"discriminator"`
  250. Token string `json:"token"`
  251. Verified bool `json:"verified"`
  252. MFAEnabled bool `json:"mfa_enabled"`
  253. Bot bool `json:"bot"`
  254. }
  255. // A Settings stores data for a specific users Discord client settings.
  256. type Settings struct {
  257. RenderEmbeds bool `json:"render_embeds"`
  258. InlineEmbedMedia bool `json:"inline_embed_media"`
  259. InlineAttachmentMedia bool `json:"inline_attachment_media"`
  260. EnableTtsCommand bool `json:"enable_tts_command"`
  261. MessageDisplayCompact bool `json:"message_display_compact"`
  262. ShowCurrentGame bool `json:"show_current_game"`
  263. ConvertEmoticons bool `json:"convert_emoticons"`
  264. Locale string `json:"locale"`
  265. Theme string `json:"theme"`
  266. GuildPositions []string `json:"guild_positions"`
  267. RestrictedGuilds []string `json:"restricted_guilds"`
  268. FriendSourceFlags *FriendSourceFlags `json:"friend_source_flags"`
  269. Status Status `json:"status"`
  270. DetectPlatformAccounts bool `json:"detect_platform_accounts"`
  271. DeveloperMode bool `json:"developer_mode"`
  272. }
  273. // Status type defination
  274. type Status string
  275. // Constants for Status with the different current available status
  276. const (
  277. StatusOnline Status = "online"
  278. StatusIdle Status = "idle"
  279. StatusDoNotDisturb Status = "dnd"
  280. StatusInvisible Status = "invisible"
  281. StatusOffline Status = "offline"
  282. )
  283. // FriendSourceFlags stores ... TODO :)
  284. type FriendSourceFlags struct {
  285. All bool `json:"all"`
  286. MutualGuilds bool `json:"mutual_guilds"`
  287. MutualFriends bool `json:"mutual_friends"`
  288. }
  289. // An Event provides a basic initial struct for all websocket event.
  290. type Event struct {
  291. Operation int `json:"op"`
  292. Sequence int `json:"s"`
  293. Type string `json:"t"`
  294. RawData json.RawMessage `json:"d"`
  295. Struct interface{} `json:"-"`
  296. }
  297. // A Ready stores all data for the websocket READY event.
  298. type Ready struct {
  299. Version int `json:"v"`
  300. SessionID string `json:"session_id"`
  301. HeartbeatInterval time.Duration `json:"heartbeat_interval"`
  302. User *User `json:"user"`
  303. ReadState []*ReadState `json:"read_state"`
  304. PrivateChannels []*Channel `json:"private_channels"`
  305. Guilds []*Guild `json:"guilds"`
  306. // Undocumented fields
  307. Settings *Settings `json:"user_settings"`
  308. UserGuildSettings []*UserGuildSettings `json:"user_guild_settings"`
  309. Relationships []*Relationship `json:"relationships"`
  310. Presences []*Presence `json:"presences"`
  311. }
  312. // A Relationship between the logged in user and Relationship.User
  313. type Relationship struct {
  314. User *User `json:"user"`
  315. Type int `json:"type"` // 1 = friend, 2 = blocked, 3 = incoming friend req, 4 = sent friend req
  316. ID string `json:"id"`
  317. }
  318. // A TooManyRequests struct holds information received from Discord
  319. // when receiving a HTTP 429 response.
  320. type TooManyRequests struct {
  321. Bucket string `json:"bucket"`
  322. Message string `json:"message"`
  323. RetryAfter time.Duration `json:"retry_after"`
  324. }
  325. // A ReadState stores data on the read state of channels.
  326. type ReadState struct {
  327. MentionCount int `json:"mention_count"`
  328. LastMessageID string `json:"last_message_id"`
  329. ID string `json:"id"`
  330. }
  331. // A TypingStart stores data for the typing start websocket event.
  332. type TypingStart struct {
  333. UserID string `json:"user_id"`
  334. ChannelID string `json:"channel_id"`
  335. Timestamp int `json:"timestamp"`
  336. }
  337. // A PresenceUpdate stores data for the presence update websocket event.
  338. type PresenceUpdate struct {
  339. Presence
  340. GuildID string `json:"guild_id"`
  341. Roles []string `json:"roles"`
  342. }
  343. // A MessageAck stores data for the message ack websocket event.
  344. type MessageAck struct {
  345. MessageID string `json:"message_id"`
  346. ChannelID string `json:"channel_id"`
  347. }
  348. // A GuildIntegrationsUpdate stores data for the guild integrations update
  349. // websocket event.
  350. type GuildIntegrationsUpdate struct {
  351. GuildID string `json:"guild_id"`
  352. }
  353. // A GuildRole stores data for guild role websocket events.
  354. type GuildRole struct {
  355. Role *Role `json:"role"`
  356. GuildID string `json:"guild_id"`
  357. }
  358. // A GuildRoleDelete stores data for the guild role delete websocket event.
  359. type GuildRoleDelete struct {
  360. RoleID string `json:"role_id"`
  361. GuildID string `json:"guild_id"`
  362. }
  363. // A GuildBan stores data for a guild ban.
  364. type GuildBan struct {
  365. User *User `json:"user"`
  366. GuildID string `json:"guild_id"`
  367. }
  368. // A GuildEmojisUpdate stores data for a guild emoji update event.
  369. type GuildEmojisUpdate struct {
  370. GuildID string `json:"guild_id"`
  371. Emojis []*Emoji `json:"emojis"`
  372. }
  373. // A GuildMembersChunk stores data for the Guild Members Chunk websocket event.
  374. type GuildMembersChunk struct {
  375. GuildID string `json:"guild_id"`
  376. Members []*Member `json:"members"`
  377. }
  378. // A GuildIntegration stores data for a guild integration.
  379. type GuildIntegration struct {
  380. ID string `json:"id"`
  381. Name string `json:"name"`
  382. Type string `json:"type"`
  383. Enabled bool `json:"enabled"`
  384. Syncing bool `json:"syncing"`
  385. RoleID string `json:"role_id"`
  386. ExpireBehavior int `json:"expire_behavior"`
  387. ExpireGracePeriod int `json:"expire_grace_period"`
  388. User *User `json:"user"`
  389. Account *GuildIntegrationAccount `json:"account"`
  390. SyncedAt int `json:"synced_at"`
  391. }
  392. // A GuildIntegrationAccount stores data for a guild integration account.
  393. type GuildIntegrationAccount struct {
  394. ID string `json:"id"`
  395. Name string `json:"name"`
  396. }
  397. // A GuildEmbed stores data for a guild embed.
  398. type GuildEmbed struct {
  399. Enabled bool `json:"enabled"`
  400. ChannelID string `json:"channel_id"`
  401. }
  402. // A UserGuildSettingsChannelOverride stores data for a channel override for a users guild settings.
  403. type UserGuildSettingsChannelOverride struct {
  404. Muted bool `json:"muted"`
  405. MessageNotifications int `json:"message_notifications"`
  406. ChannelID string `json:"channel_id"`
  407. }
  408. // A UserGuildSettings stores data for a users guild settings.
  409. type UserGuildSettings struct {
  410. SupressEveryone bool `json:"suppress_everyone"`
  411. Muted bool `json:"muted"`
  412. MobilePush bool `json:"mobile_push"`
  413. MessageNotifications int `json:"message_notifications"`
  414. GuildID string `json:"guild_id"`
  415. ChannelOverrides []*UserGuildSettingsChannelOverride `json:"channel_overrides"`
  416. }
  417. // A UserGuildSettingsEdit stores data for editing UserGuildSettings
  418. type UserGuildSettingsEdit struct {
  419. SupressEveryone bool `json:"suppress_everyone"`
  420. Muted bool `json:"muted"`
  421. MobilePush bool `json:"mobile_push"`
  422. MessageNotifications int `json:"message_notifications"`
  423. ChannelOverrides map[string]*UserGuildSettingsChannelOverride `json:"channel_overrides"`
  424. }
  425. // ChannelPinsUpdate stores data for the channel pins update event
  426. type ChannelPinsUpdate struct {
  427. LastPinTimestamp string `json:"last_pin_timestamp"`
  428. ChannelID string `json:"channel_id"`
  429. }
  430. // Constants for the different bit offsets of text channel permissions
  431. const (
  432. PermissionReadMessages = 1 << (iota + 10)
  433. PermissionSendMessages
  434. PermissionSendTTSMessages
  435. PermissionManageMessages
  436. PermissionEmbedLinks
  437. PermissionAttachFiles
  438. PermissionReadMessageHistory
  439. PermissionMentionEveryone
  440. )
  441. // Constants for the different bit offsets of voice permissions
  442. const (
  443. PermissionVoiceConnect = 1 << (iota + 20)
  444. PermissionVoiceSpeak
  445. PermissionVoiceMuteMembers
  446. PermissionVoiceDeafenMembers
  447. PermissionVoiceMoveMembers
  448. PermissionVoiceUseVAD
  449. )
  450. // Constants for the different bit offsets of general permissions
  451. const (
  452. PermissionCreateInstantInvite = 1 << iota
  453. PermissionKickMembers
  454. PermissionBanMembers
  455. PermissionManageRoles
  456. PermissionManageChannels
  457. PermissionManageServer
  458. PermissionAllText = PermissionReadMessages |
  459. PermissionSendMessages |
  460. PermissionSendTTSMessages |
  461. PermissionManageMessages |
  462. PermissionEmbedLinks |
  463. PermissionAttachFiles |
  464. PermissionReadMessageHistory |
  465. PermissionMentionEveryone
  466. PermissionAllVoice = PermissionVoiceConnect |
  467. PermissionVoiceSpeak |
  468. PermissionVoiceMuteMembers |
  469. PermissionVoiceDeafenMembers |
  470. PermissionVoiceMoveMembers |
  471. PermissionVoiceUseVAD
  472. PermissionAllChannel = PermissionAllText |
  473. PermissionAllVoice |
  474. PermissionCreateInstantInvite |
  475. PermissionManageRoles |
  476. PermissionManageChannels
  477. PermissionAll = PermissionAllChannel |
  478. PermissionKickMembers |
  479. PermissionBanMembers |
  480. PermissionManageServer
  481. )