structs.go 16 KB

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