structs.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 GuildParams stores all the data needed to update discord guild settings
  185. type GuildParams struct {
  186. Name string `json:"name"`
  187. Region string `json:"region"`
  188. VerificationLevel *VerificationLevel `json:"verification_level"`
  189. }
  190. // A Role stores information about Discord guild member roles.
  191. type Role struct {
  192. ID string `json:"id"`
  193. Name string `json:"name"`
  194. Managed bool `json:"managed"`
  195. Mentionable bool `json:"mentionable"`
  196. Hoist bool `json:"hoist"`
  197. Color int `json:"color"`
  198. Position int `json:"position"`
  199. Permissions int `json:"permissions"`
  200. }
  201. // A VoiceState stores the voice states of Guilds
  202. type VoiceState struct {
  203. UserID string `json:"user_id"`
  204. SessionID string `json:"session_id"`
  205. ChannelID string `json:"channel_id"`
  206. GuildID string `json:"guild_id"`
  207. Suppress bool `json:"suppress"`
  208. SelfMute bool `json:"self_mute"`
  209. SelfDeaf bool `json:"self_deaf"`
  210. Mute bool `json:"mute"`
  211. Deaf bool `json:"deaf"`
  212. }
  213. // A Presence stores the online, offline, or idle and game status of Guild members.
  214. type Presence struct {
  215. User *User `json:"user"`
  216. Status string `json:"status"`
  217. Game *Game `json:"game"`
  218. }
  219. // A Game struct holds the name of the "playing .." game for a user
  220. type Game struct {
  221. Name string `json:"name"`
  222. Type int `json:"type"`
  223. URL string `json:"url"`
  224. }
  225. // A Member stores user information for Guild members.
  226. type Member struct {
  227. GuildID string `json:"guild_id"`
  228. JoinedAt string `json:"joined_at"`
  229. Nick string `json:"nick"`
  230. Deaf bool `json:"deaf"`
  231. Mute bool `json:"mute"`
  232. User *User `json:"user"`
  233. Roles []string `json:"roles"`
  234. }
  235. // A User stores all data for an individual Discord user.
  236. type User struct {
  237. ID string `json:"id"`
  238. Email string `json:"email"`
  239. Username string `json:"username"`
  240. Avatar string `json:"Avatar"`
  241. Discriminator string `json:"discriminator"`
  242. Token string `json:"token"`
  243. Verified bool `json:"verified"`
  244. MFAEnabled bool `json:"mfa_enabled"`
  245. Bot bool `json:"bot"`
  246. }
  247. // A Settings stores data for a specific users Discord client settings.
  248. type Settings struct {
  249. RenderEmbeds bool `json:"render_embeds"`
  250. InlineEmbedMedia bool `json:"inline_embed_media"`
  251. InlineAttachmentMedia bool `json:"inline_attachment_media"`
  252. EnableTtsCommand bool `json:"enable_tts_command"`
  253. MessageDisplayCompact bool `json:"message_display_compact"`
  254. ShowCurrentGame bool `json:"show_current_game"`
  255. AllowEmailFriendRequest bool `json:"allow_email_friend_request"`
  256. ConvertEmoticons bool `json:"convert_emoticons"`
  257. Locale string `json:"locale"`
  258. Theme string `json:"theme"`
  259. GuildPositions []string `json:"guild_positions"`
  260. RestrictedGuilds []string `json:"restricted_guilds"`
  261. FriendSourceFlags *FriendSourceFlags `json:"friend_source_flags"`
  262. }
  263. // FriendSourceFlags stores ... TODO :)
  264. type FriendSourceFlags struct {
  265. All bool `json:"all"`
  266. MutualGuilds bool `json:"mutual_guilds"`
  267. MutualFriends bool `json:"mutual_friends"`
  268. }
  269. // An Event provides a basic initial struct for all websocket event.
  270. type Event struct {
  271. Operation int `json:"op"`
  272. Sequence int `json:"s"`
  273. Type string `json:"t"`
  274. RawData json.RawMessage `json:"d"`
  275. Struct interface{} `json:"-"`
  276. }
  277. // A Ready stores all data for the websocket READY event.
  278. type Ready struct {
  279. Version int `json:"v"`
  280. SessionID string `json:"session_id"`
  281. HeartbeatInterval time.Duration `json:"heartbeat_interval"`
  282. User *User `json:"user"`
  283. ReadState []*ReadState `json:"read_state"`
  284. PrivateChannels []*Channel `json:"private_channels"`
  285. Guilds []*Guild `json:"guilds"`
  286. // Undocumented fields
  287. Settings *Settings `json:"user_settings"`
  288. UserGuildSettings []*UserGuildSettings `json:"user_guild_settings"`
  289. Relationships []*Relationship `json:"relationships"`
  290. Presences []*Presence `json:"presences"`
  291. }
  292. // A Relationship between the logged in user and Relationship.User
  293. type Relationship struct {
  294. User *User `json:"user"`
  295. Type int `json:"type"` // 1 = friend, 2 = blocked, 3 = incoming friend req, 4 = sent friend req
  296. ID string `json:"id"`
  297. }
  298. // A TooManyRequests struct holds information received from Discord
  299. // when receiving a HTTP 429 response.
  300. type TooManyRequests struct {
  301. Bucket string `json:"bucket"`
  302. Message string `json:"message"`
  303. RetryAfter time.Duration `json:"retry_after"`
  304. }
  305. // A ReadState stores data on the read state of channels.
  306. type ReadState struct {
  307. MentionCount int `json:"mention_count"`
  308. LastMessageID string `json:"last_message_id"`
  309. ID string `json:"id"`
  310. }
  311. // A TypingStart stores data for the typing start websocket event.
  312. type TypingStart struct {
  313. UserID string `json:"user_id"`
  314. ChannelID string `json:"channel_id"`
  315. Timestamp int `json:"timestamp"`
  316. }
  317. // A PresenceUpdate stores data for the presence update websocket event.
  318. type PresenceUpdate struct {
  319. Presence
  320. GuildID string `json:"guild_id"`
  321. Roles []string `json:"roles"`
  322. }
  323. // A MessageAck stores data for the message ack websocket event.
  324. type MessageAck struct {
  325. MessageID string `json:"message_id"`
  326. ChannelID string `json:"channel_id"`
  327. }
  328. // A GuildIntegrationsUpdate stores data for the guild integrations update
  329. // websocket event.
  330. type GuildIntegrationsUpdate struct {
  331. GuildID string `json:"guild_id"`
  332. }
  333. // A GuildRole stores data for guild role websocket events.
  334. type GuildRole struct {
  335. Role *Role `json:"role"`
  336. GuildID string `json:"guild_id"`
  337. }
  338. // A GuildRoleDelete stores data for the guild role delete websocket event.
  339. type GuildRoleDelete struct {
  340. RoleID string `json:"role_id"`
  341. GuildID string `json:"guild_id"`
  342. }
  343. // A GuildBan stores data for a guild ban.
  344. type GuildBan struct {
  345. User *User `json:"user"`
  346. GuildID string `json:"guild_id"`
  347. }
  348. // A GuildEmojisUpdate stores data for a guild emoji update event.
  349. type GuildEmojisUpdate struct {
  350. GuildID string `json:"guild_id"`
  351. Emojis []*Emoji `json:"emojis"`
  352. }
  353. // A GuildIntegration stores data for a guild integration.
  354. type GuildIntegration struct {
  355. ID string `json:"id"`
  356. Name string `json:"name"`
  357. Type string `json:"type"`
  358. Enabled bool `json:"enabled"`
  359. Syncing bool `json:"syncing"`
  360. RoleID string `json:"role_id"`
  361. ExpireBehavior int `json:"expire_behavior"`
  362. ExpireGracePeriod int `json:"expire_grace_period"`
  363. User *User `json:"user"`
  364. Account *GuildIntegrationAccount `json:"account"`
  365. SyncedAt int `json:"synced_at"`
  366. }
  367. // A GuildIntegrationAccount stores data for a guild integration account.
  368. type GuildIntegrationAccount struct {
  369. ID string `json:"id"`
  370. Name string `json:"name"`
  371. }
  372. // A GuildEmbed stores data for a guild embed.
  373. type GuildEmbed struct {
  374. Enabled bool `json:"enabled"`
  375. ChannelID string `json:"channel_id"`
  376. }
  377. // A UserGuildSettingsChannelOverride stores data for a channel override for a users guild settings.
  378. type UserGuildSettingsChannelOverride struct {
  379. Muted bool `json:"muted"`
  380. MessageNotifications int `json:"message_notifications"`
  381. ChannelID string `json:"channel_id"`
  382. }
  383. // A UserGuildSettings stores data for a users guild settings.
  384. type UserGuildSettings struct {
  385. SupressEveryone bool `json:"suppress_everyone"`
  386. Muted bool `json:"muted"`
  387. MobilePush bool `json:"mobile_push"`
  388. MessageNotifications int `json:"message_notifications"`
  389. GuildID string `json:"guild_id"`
  390. ChannelOverrides []*UserGuildSettingsChannelOverride `json:"channel_overrides"`
  391. }
  392. // A UserGuildSettingsEdit stores data for editing UserGuildSettings
  393. type UserGuildSettingsEdit struct {
  394. SupressEveryone bool `json:"suppress_everyone"`
  395. Muted bool `json:"muted"`
  396. MobilePush bool `json:"mobile_push"`
  397. MessageNotifications int `json:"message_notifications"`
  398. ChannelOverrides map[string]*UserGuildSettingsChannelOverride `json:"channel_overrides"`
  399. }
  400. // ChannelPinsUpdate stores data for the channel pins update event
  401. type ChannelPinsUpdate struct {
  402. LastPinTimestamp string `json:"last_pin_timestamp"`
  403. ChannelID string `json:"channel_id"`
  404. }
  405. // Constants for the different bit offsets of text channel permissions
  406. const (
  407. PermissionReadMessages = 1 << (iota + 10)
  408. PermissionSendMessages
  409. PermissionSendTTSMessages
  410. PermissionManageMessages
  411. PermissionEmbedLinks
  412. PermissionAttachFiles
  413. PermissionReadMessageHistory
  414. PermissionMentionEveryone
  415. )
  416. // Constants for the different bit offsets of voice permissions
  417. const (
  418. PermissionVoiceConnect = 1 << (iota + 20)
  419. PermissionVoiceSpeak
  420. PermissionVoiceMuteMembers
  421. PermissionVoiceDeafenMembers
  422. PermissionVoiceMoveMembers
  423. PermissionVoiceUseVAD
  424. )
  425. // Constants for the different bit offsets of general permissions
  426. const (
  427. PermissionCreateInstantInvite = 1 << iota
  428. PermissionKickMembers
  429. PermissionBanMembers
  430. PermissionManageRoles
  431. PermissionManageChannels
  432. PermissionManageServer
  433. PermissionAllText = PermissionReadMessages |
  434. PermissionSendMessages |
  435. PermissionSendTTSMessages |
  436. PermissionManageMessages |
  437. PermissionEmbedLinks |
  438. PermissionAttachFiles |
  439. PermissionReadMessageHistory |
  440. PermissionMentionEveryone
  441. PermissionAllVoice = PermissionVoiceConnect |
  442. PermissionVoiceSpeak |
  443. PermissionVoiceMuteMembers |
  444. PermissionVoiceDeafenMembers |
  445. PermissionVoiceMoveMembers |
  446. PermissionVoiceUseVAD
  447. PermissionAllChannel = PermissionAllText |
  448. PermissionAllVoice |
  449. PermissionCreateInstantInvite |
  450. PermissionManageRoles |
  451. PermissionManageChannels
  452. PermissionAll = PermissionAllChannel |
  453. PermissionKickMembers |
  454. PermissionBanMembers |
  455. PermissionManageServer
  456. )