structs.go 17 KB

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