structs.go 16 KB

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