structs.go 18 KB

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