structs.go 16 KB

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