structs.go 16 KB

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