structs.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. "strconv"
  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. // Sharding
  31. ShardID int
  32. ShardCount int
  33. // Should state tracking be enabled.
  34. // State tracking is the best way for getting the the users
  35. // active guilds and the members of the guilds.
  36. StateEnabled bool
  37. // Exposed but should not be modified by User.
  38. // Whether the Data Websocket is ready
  39. DataReady bool // NOTE: Maye be deprecated soon
  40. // Max number of REST API retries
  41. MaxRestRetries int
  42. // Status stores the currect status of the websocket connection
  43. // this is being tested, may stay, may go away.
  44. status int32
  45. // Whether the Voice Websocket is ready
  46. VoiceReady bool // NOTE: Deprecated.
  47. // Whether the UDP Connection is ready
  48. UDPReady bool // NOTE: Deprecated
  49. // Stores a mapping of guild id's to VoiceConnections
  50. VoiceConnections map[string]*VoiceConnection
  51. // Managed state object, updated internally with events when
  52. // StateEnabled is true.
  53. State *State
  54. // Event handlers
  55. handlersMu sync.RWMutex
  56. handlers map[string][]*eventHandlerInstance
  57. onceHandlers map[string][]*eventHandlerInstance
  58. // The websocket connection.
  59. wsConn *websocket.Conn
  60. // When nil, the session is not listening.
  61. listening chan interface{}
  62. // used to deal with rate limits
  63. ratelimiter *RateLimiter
  64. // sequence tracks the current gateway api websocket sequence number
  65. sequence int
  66. // stores sessions current Discord Gateway
  67. gateway string
  68. // stores session ID of current Gateway connection
  69. sessionID string
  70. // used to make sure gateway websocket writes do not happen concurrently
  71. wsMutex sync.Mutex
  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 Timestamp `json:"created_at"`
  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. // APIName returns an correctly formatted API name for use in the MessageReactions endpoints.
  136. func (e *Emoji) APIName() string {
  137. if e.ID != "" && e.Name != "" {
  138. return e.Name + ":" + e.ID
  139. }
  140. if e.Name != "" {
  141. return e.Name
  142. }
  143. return e.ID
  144. }
  145. // VerificationLevel type defination
  146. type VerificationLevel int
  147. // Constants for VerificationLevel levels from 0 to 3 inclusive
  148. const (
  149. VerificationLevelNone VerificationLevel = iota
  150. VerificationLevelLow
  151. VerificationLevelMedium
  152. VerificationLevelHigh
  153. )
  154. // A Guild holds all data related to a specific Discord Guild. Guilds are also
  155. // sometimes referred to as Servers in the Discord client.
  156. type Guild struct {
  157. ID string `json:"id"`
  158. Name string `json:"name"`
  159. Icon string `json:"icon"`
  160. Region string `json:"region"`
  161. AfkChannelID string `json:"afk_channel_id"`
  162. EmbedChannelID string `json:"embed_channel_id"`
  163. OwnerID string `json:"owner_id"`
  164. JoinedAt Timestamp `json:"joined_at"`
  165. Splash string `json:"splash"`
  166. AfkTimeout int `json:"afk_timeout"`
  167. MemberCount int `json:"member_count"`
  168. VerificationLevel VerificationLevel `json:"verification_level"`
  169. EmbedEnabled bool `json:"embed_enabled"`
  170. Large bool `json:"large"` // ??
  171. DefaultMessageNotifications int `json:"default_message_notifications"`
  172. Roles []*Role `json:"roles"`
  173. Emojis []*Emoji `json:"emojis"`
  174. Members []*Member `json:"members"`
  175. Presences []*Presence `json:"presences"`
  176. Channels []*Channel `json:"channels"`
  177. VoiceStates []*VoiceState `json:"voice_states"`
  178. Unavailable bool `json:"unavailable"`
  179. }
  180. // A UserGuild holds a brief version of a Guild
  181. type UserGuild struct {
  182. ID string `json:"id"`
  183. Name string `json:"name"`
  184. Icon string `json:"icon"`
  185. Owner bool `json:"owner"`
  186. Permissions int `json:"permissions"`
  187. }
  188. // A GuildParams stores all the data needed to update discord guild settings
  189. type GuildParams struct {
  190. Name string `json:"name"`
  191. Region string `json:"region"`
  192. VerificationLevel *VerificationLevel `json:"verification_level"`
  193. }
  194. // A Role stores information about Discord guild member roles.
  195. type Role struct {
  196. ID string `json:"id"`
  197. Name string `json:"name"`
  198. Managed bool `json:"managed"`
  199. Mentionable bool `json:"mentionable"`
  200. Hoist bool `json:"hoist"`
  201. Color int `json:"color"`
  202. Position int `json:"position"`
  203. Permissions int `json:"permissions"`
  204. }
  205. // A VoiceState stores the voice states of Guilds
  206. type VoiceState struct {
  207. UserID string `json:"user_id"`
  208. SessionID string `json:"session_id"`
  209. ChannelID string `json:"channel_id"`
  210. GuildID string `json:"guild_id"`
  211. Suppress bool `json:"suppress"`
  212. SelfMute bool `json:"self_mute"`
  213. SelfDeaf bool `json:"self_deaf"`
  214. Mute bool `json:"mute"`
  215. Deaf bool `json:"deaf"`
  216. }
  217. // A Presence stores the online, offline, or idle and game status of Guild members.
  218. type Presence struct {
  219. User *User `json:"user"`
  220. Status Status `json:"status"`
  221. Game *Game `json:"game"`
  222. Nick string `json:"nick"`
  223. Roles []string `json:"roles"`
  224. }
  225. // A Game struct holds the name of the "playing .." game for a user
  226. type Game struct {
  227. Name string `json:"name"`
  228. Type int `json:"type"`
  229. URL string `json:"url"`
  230. }
  231. // UnmarshalJSON unmarshals json to Game struct
  232. func (g *Game) UnmarshalJSON(bytes []byte) error {
  233. temp := &struct {
  234. Name string `json:"name"`
  235. Type json.RawMessage `json:"type"`
  236. URL string `json:"url"`
  237. }{}
  238. err := json.Unmarshal(bytes, temp)
  239. if err != nil {
  240. return err
  241. }
  242. g.Name = temp.Name
  243. g.URL = temp.URL
  244. if temp.Type != nil {
  245. err = json.Unmarshal(temp.Type, &g.Type)
  246. if err == nil {
  247. return nil
  248. }
  249. s := ""
  250. err = json.Unmarshal(temp.Type, &s)
  251. if err == nil {
  252. g.Type, err = strconv.Atoi(s)
  253. }
  254. return err
  255. }
  256. return nil
  257. }
  258. // A Member stores user information for Guild members.
  259. type Member struct {
  260. GuildID string `json:"guild_id"`
  261. JoinedAt string `json:"joined_at"`
  262. Nick string `json:"nick"`
  263. Deaf bool `json:"deaf"`
  264. Mute bool `json:"mute"`
  265. User *User `json:"user"`
  266. Roles []string `json:"roles"`
  267. }
  268. // A User stores all data for an individual Discord user.
  269. type User struct {
  270. ID string `json:"id"`
  271. Email string `json:"email"`
  272. Username string `json:"username"`
  273. Avatar string `json:"Avatar"`
  274. Discriminator string `json:"discriminator"`
  275. Token string `json:"token"`
  276. Verified bool `json:"verified"`
  277. MFAEnabled bool `json:"mfa_enabled"`
  278. Bot bool `json:"bot"`
  279. }
  280. // A Settings stores data for a specific users Discord client settings.
  281. type Settings struct {
  282. RenderEmbeds bool `json:"render_embeds"`
  283. InlineEmbedMedia bool `json:"inline_embed_media"`
  284. InlineAttachmentMedia bool `json:"inline_attachment_media"`
  285. EnableTtsCommand bool `json:"enable_tts_command"`
  286. MessageDisplayCompact bool `json:"message_display_compact"`
  287. ShowCurrentGame bool `json:"show_current_game"`
  288. ConvertEmoticons bool `json:"convert_emoticons"`
  289. Locale string `json:"locale"`
  290. Theme string `json:"theme"`
  291. GuildPositions []string `json:"guild_positions"`
  292. RestrictedGuilds []string `json:"restricted_guilds"`
  293. FriendSourceFlags *FriendSourceFlags `json:"friend_source_flags"`
  294. Status Status `json:"status"`
  295. DetectPlatformAccounts bool `json:"detect_platform_accounts"`
  296. DeveloperMode bool `json:"developer_mode"`
  297. }
  298. // Status type defination
  299. type Status string
  300. // Constants for Status with the different current available status
  301. const (
  302. StatusOnline Status = "online"
  303. StatusIdle Status = "idle"
  304. StatusDoNotDisturb Status = "dnd"
  305. StatusInvisible Status = "invisible"
  306. StatusOffline Status = "offline"
  307. )
  308. // FriendSourceFlags stores ... TODO :)
  309. type FriendSourceFlags struct {
  310. All bool `json:"all"`
  311. MutualGuilds bool `json:"mutual_guilds"`
  312. MutualFriends bool `json:"mutual_friends"`
  313. }
  314. // A Relationship between the logged in user and Relationship.User
  315. type Relationship struct {
  316. User *User `json:"user"`
  317. Type int `json:"type"` // 1 = friend, 2 = blocked, 3 = incoming friend req, 4 = sent friend req
  318. ID string `json:"id"`
  319. }
  320. // A TooManyRequests struct holds information received from Discord
  321. // when receiving a HTTP 429 response.
  322. type TooManyRequests struct {
  323. Bucket string `json:"bucket"`
  324. Message string `json:"message"`
  325. RetryAfter time.Duration `json:"retry_after"`
  326. }
  327. // A ReadState stores data on the read state of channels.
  328. type ReadState struct {
  329. MentionCount int `json:"mention_count"`
  330. LastMessageID string `json:"last_message_id"`
  331. ID string `json:"id"`
  332. }
  333. // An Ack is used to ack messages
  334. type Ack struct {
  335. Token string `json:"token"`
  336. }
  337. // A GuildRole stores data for guild roles.
  338. type GuildRole struct {
  339. Role *Role `json:"role"`
  340. GuildID string `json:"guild_id"`
  341. }
  342. // A GuildBan stores data for a guild ban.
  343. type GuildBan struct {
  344. Reason string `json:"reason"`
  345. User *User `json:"user"`
  346. }
  347. // A GuildIntegration stores data for a guild integration.
  348. type GuildIntegration struct {
  349. ID string `json:"id"`
  350. Name string `json:"name"`
  351. Type string `json:"type"`
  352. Enabled bool `json:"enabled"`
  353. Syncing bool `json:"syncing"`
  354. RoleID string `json:"role_id"`
  355. ExpireBehavior int `json:"expire_behavior"`
  356. ExpireGracePeriod int `json:"expire_grace_period"`
  357. User *User `json:"user"`
  358. Account *GuildIntegrationAccount `json:"account"`
  359. SyncedAt int `json:"synced_at"`
  360. }
  361. // A GuildIntegrationAccount stores data for a guild integration account.
  362. type GuildIntegrationAccount struct {
  363. ID string `json:"id"`
  364. Name string `json:"name"`
  365. }
  366. // A GuildEmbed stores data for a guild embed.
  367. type GuildEmbed struct {
  368. Enabled bool `json:"enabled"`
  369. ChannelID string `json:"channel_id"`
  370. }
  371. // A UserGuildSettingsChannelOverride stores data for a channel override for a users guild settings.
  372. type UserGuildSettingsChannelOverride struct {
  373. Muted bool `json:"muted"`
  374. MessageNotifications int `json:"message_notifications"`
  375. ChannelID string `json:"channel_id"`
  376. }
  377. // A UserGuildSettings stores data for a users guild settings.
  378. type UserGuildSettings struct {
  379. SupressEveryone bool `json:"suppress_everyone"`
  380. Muted bool `json:"muted"`
  381. MobilePush bool `json:"mobile_push"`
  382. MessageNotifications int `json:"message_notifications"`
  383. GuildID string `json:"guild_id"`
  384. ChannelOverrides []*UserGuildSettingsChannelOverride `json:"channel_overrides"`
  385. }
  386. // A UserGuildSettingsEdit stores data for editing UserGuildSettings
  387. type UserGuildSettingsEdit struct {
  388. SupressEveryone bool `json:"suppress_everyone"`
  389. Muted bool `json:"muted"`
  390. MobilePush bool `json:"mobile_push"`
  391. MessageNotifications int `json:"message_notifications"`
  392. ChannelOverrides map[string]*UserGuildSettingsChannelOverride `json:"channel_overrides"`
  393. }
  394. // An APIErrorMessage is an api error message returned from discord
  395. type APIErrorMessage struct {
  396. Code int `json:"code"`
  397. Message string `json:"message"`
  398. }
  399. // Webhook stores the data for a webhook.
  400. type Webhook struct {
  401. ID string `json:"id"`
  402. GuildID string `json:"guild_id"`
  403. ChannelID string `json:"channel_id"`
  404. User *User `json:"user"`
  405. Name string `json:"name"`
  406. Avatar string `json:"avatar"`
  407. Token string `json:"token"`
  408. }
  409. // WebhookParams is a struct for webhook params, used in the WebhookExecute command.
  410. type WebhookParams struct {
  411. Content string `json:"content,omitempty"`
  412. Username string `json:"username,omitempty"`
  413. AvatarURL string `json:"avatar_url,omitempty"`
  414. TTS bool `json:"tts,omitempty"`
  415. File string `json:"file,omitempty"`
  416. Embeds []*MessageEmbed `json:"embeds,omitempty"`
  417. }
  418. // MessageReaction stores the data for a message reaction.
  419. type MessageReaction struct {
  420. UserID string `json:"user_id"`
  421. MessageID string `json:"message_id"`
  422. Emoji Emoji `json:"emoji"`
  423. ChannelID string `json:"channel_id"`
  424. }
  425. // Constants for the different bit offsets of text channel permissions
  426. const (
  427. PermissionReadMessages = 1 << (iota + 10)
  428. PermissionSendMessages
  429. PermissionSendTTSMessages
  430. PermissionManageMessages
  431. PermissionEmbedLinks
  432. PermissionAttachFiles
  433. PermissionReadMessageHistory
  434. PermissionMentionEveryone
  435. PermissionUseExternalEmojis
  436. )
  437. // Constants for the different bit offsets of voice permissions
  438. const (
  439. PermissionVoiceConnect = 1 << (iota + 20)
  440. PermissionVoiceSpeak
  441. PermissionVoiceMuteMembers
  442. PermissionVoiceDeafenMembers
  443. PermissionVoiceMoveMembers
  444. PermissionVoiceUseVAD
  445. )
  446. // Constants for general management.
  447. const (
  448. PermissionChangeNickname = 1 << (iota + 26)
  449. PermissionManageNicknames
  450. PermissionManageRoles
  451. PermissionManageWebhooks
  452. PermissionManageEmojis
  453. )
  454. // Constants for the different bit offsets of general permissions
  455. const (
  456. PermissionCreateInstantInvite = 1 << iota
  457. PermissionKickMembers
  458. PermissionBanMembers
  459. PermissionAdministrator
  460. PermissionManageChannels
  461. PermissionManageServer
  462. PermissionAllText = PermissionReadMessages |
  463. PermissionSendMessages |
  464. PermissionSendTTSMessages |
  465. PermissionManageMessages |
  466. PermissionEmbedLinks |
  467. PermissionAttachFiles |
  468. PermissionReadMessageHistory |
  469. PermissionMentionEveryone
  470. PermissionAllVoice = PermissionVoiceConnect |
  471. PermissionVoiceSpeak |
  472. PermissionVoiceMuteMembers |
  473. PermissionVoiceDeafenMembers |
  474. PermissionVoiceMoveMembers |
  475. PermissionVoiceUseVAD
  476. PermissionAllChannel = PermissionAllText |
  477. PermissionAllVoice |
  478. PermissionCreateInstantInvite |
  479. PermissionManageRoles |
  480. PermissionManageChannels
  481. PermissionAll = PermissionAllChannel |
  482. PermissionKickMembers |
  483. PermissionBanMembers |
  484. PermissionManageServer
  485. )