structs.go 17 KB

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