structs.go 18 KB

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