structs.go 16 KB

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