structs.go 17 KB

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