structs.go 17 KB

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