structs.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. "fmt"
  13. "net/http"
  14. "strings"
  15. "sync"
  16. "time"
  17. "github.com/gorilla/websocket"
  18. )
  19. // A Session represents a connection to the Discord API.
  20. type Session struct {
  21. sync.RWMutex
  22. // General configurable settings.
  23. // Authentication token for this session
  24. Token string
  25. MFA bool
  26. // Debug for printing JSON request/responses
  27. Debug bool // Deprecated, will be removed.
  28. LogLevel int
  29. // Should the session reconnect the websocket on errors.
  30. ShouldReconnectOnError bool
  31. // Identify is sent during initial handshake with the discord gateway.
  32. // https://discordapp.com/developers/docs/topics/gateway#identify
  33. Identify Identify
  34. // TODO: REMOVE Below, DEPRECATED, Use IDENTITY struct
  35. // Should the session request compressed websocket data.
  36. Compress bool
  37. // Sharding
  38. ShardID int
  39. ShardCount int
  40. // Should state tracking be enabled.
  41. // State tracking is the best way for getting the the users
  42. // active guilds and the members of the guilds.
  43. StateEnabled bool
  44. // Whether or not to call event handlers synchronously.
  45. // e.g false = launch event handlers in their own goroutines.
  46. SyncEvents bool
  47. // Exposed but should not be modified by User.
  48. // Whether the Data Websocket is ready
  49. DataReady bool // NOTE: Maye be deprecated soon
  50. // Max number of REST API retries
  51. MaxRestRetries int
  52. // Status stores the currect status of the websocket connection
  53. // this is being tested, may stay, may go away.
  54. status int32
  55. // Whether the Voice Websocket is ready
  56. VoiceReady bool // NOTE: Deprecated.
  57. // Whether the UDP Connection is ready
  58. UDPReady bool // NOTE: Deprecated
  59. // Stores a mapping of guild id's to VoiceConnections
  60. VoiceConnections map[string]*VoiceConnection
  61. // Managed state object, updated internally with events when
  62. // StateEnabled is true.
  63. State *State
  64. // The http client used for REST requests
  65. Client *http.Client
  66. // The user agent used for REST APIs
  67. UserAgent string
  68. // Stores the last HeartbeatAck that was recieved (in UTC)
  69. LastHeartbeatAck time.Time
  70. // Stores the last Heartbeat sent (in UTC)
  71. LastHeartbeatSent time.Time
  72. // used to deal with rate limits
  73. Ratelimiter *RateLimiter
  74. // Event handlers
  75. handlersMu sync.RWMutex
  76. handlers map[string][]*eventHandlerInstance
  77. onceHandlers map[string][]*eventHandlerInstance
  78. // The websocket connection.
  79. wsConn *websocket.Conn
  80. // When nil, the session is not listening.
  81. listening chan interface{}
  82. // sequence tracks the current gateway api websocket sequence number
  83. sequence *int64
  84. // stores sessions current Discord Gateway
  85. gateway string
  86. // stores session ID of current Gateway connection
  87. sessionID string
  88. // used to make sure gateway websocket writes do not happen concurrently
  89. wsMutex sync.Mutex
  90. }
  91. // UserConnection is a Connection returned from the UserConnections endpoint
  92. type UserConnection struct {
  93. ID string `json:"id"`
  94. Name string `json:"name"`
  95. Type string `json:"type"`
  96. Revoked bool `json:"revoked"`
  97. Integrations []*Integration `json:"integrations"`
  98. }
  99. // Integration stores integration information
  100. type Integration struct {
  101. ID string `json:"id"`
  102. Name string `json:"name"`
  103. Type string `json:"type"`
  104. Enabled bool `json:"enabled"`
  105. Syncing bool `json:"syncing"`
  106. RoleID string `json:"role_id"`
  107. ExpireBehavior int `json:"expire_behavior"`
  108. ExpireGracePeriod int `json:"expire_grace_period"`
  109. User *User `json:"user"`
  110. Account IntegrationAccount `json:"account"`
  111. SyncedAt Timestamp `json:"synced_at"`
  112. }
  113. // IntegrationAccount is integration account information
  114. // sent by the UserConnections endpoint
  115. type IntegrationAccount struct {
  116. ID string `json:"id"`
  117. Name string `json:"name"`
  118. }
  119. // A VoiceRegion stores data for a specific voice region server.
  120. type VoiceRegion struct {
  121. ID string `json:"id"`
  122. Name string `json:"name"`
  123. Hostname string `json:"sample_hostname"`
  124. Port int `json:"sample_port"`
  125. }
  126. // A VoiceICE stores data for voice ICE servers.
  127. type VoiceICE struct {
  128. TTL string `json:"ttl"`
  129. Servers []*ICEServer `json:"servers"`
  130. }
  131. // A ICEServer stores data for a specific voice ICE server.
  132. type ICEServer struct {
  133. URL string `json:"url"`
  134. Username string `json:"username"`
  135. Credential string `json:"credential"`
  136. }
  137. // A Invite stores all data related to a specific Discord Guild or Channel invite.
  138. type Invite struct {
  139. Guild *Guild `json:"guild"`
  140. Channel *Channel `json:"channel"`
  141. Inviter *User `json:"inviter"`
  142. Code string `json:"code"`
  143. CreatedAt Timestamp `json:"created_at"`
  144. MaxAge int `json:"max_age"`
  145. Uses int `json:"uses"`
  146. MaxUses int `json:"max_uses"`
  147. Revoked bool `json:"revoked"`
  148. Temporary bool `json:"temporary"`
  149. Unique bool `json:"unique"`
  150. // will only be filled when using InviteWithCounts
  151. ApproximatePresenceCount int `json:"approximate_presence_count"`
  152. ApproximateMemberCount int `json:"approximate_member_count"`
  153. }
  154. // ChannelType is the type of a Channel
  155. type ChannelType int
  156. // Block contains known ChannelType values
  157. const (
  158. ChannelTypeGuildText ChannelType = iota
  159. ChannelTypeDM
  160. ChannelTypeGuildVoice
  161. ChannelTypeGroupDM
  162. ChannelTypeGuildCategory
  163. ChannelTypeGuildNews
  164. ChannelTypeGuildStore
  165. )
  166. // A Channel holds all data related to an individual Discord channel.
  167. type Channel struct {
  168. // The ID of the channel.
  169. ID string `json:"id"`
  170. // The ID of the guild to which the channel belongs, if it is in a guild.
  171. // Else, this ID is empty (e.g. DM channels).
  172. GuildID string `json:"guild_id"`
  173. // The name of the channel.
  174. Name string `json:"name"`
  175. // The topic of the channel.
  176. Topic string `json:"topic"`
  177. // The type of the channel.
  178. Type ChannelType `json:"type"`
  179. // The ID of the last message sent in the channel. This is not
  180. // guaranteed to be an ID of a valid message.
  181. LastMessageID string `json:"last_message_id"`
  182. // The timestamp of the last pinned message in the channel.
  183. // Empty if the channel has no pinned messages.
  184. LastPinTimestamp Timestamp `json:"last_pin_timestamp"`
  185. // Whether the channel is marked as NSFW.
  186. NSFW bool `json:"nsfw"`
  187. // Icon of the group DM channel.
  188. Icon string `json:"icon"`
  189. // The position of the channel, used for sorting in client.
  190. Position int `json:"position"`
  191. // The bitrate of the channel, if it is a voice channel.
  192. Bitrate int `json:"bitrate"`
  193. // The recipients of the channel. This is only populated in DM channels.
  194. Recipients []*User `json:"recipients"`
  195. // The messages in the channel. This is only present in state-cached channels,
  196. // and State.MaxMessageCount must be non-zero.
  197. Messages []*Message `json:"-"`
  198. // A list of permission overwrites present for the channel.
  199. PermissionOverwrites []*PermissionOverwrite `json:"permission_overwrites"`
  200. // The user limit of the voice channel.
  201. UserLimit int `json:"user_limit"`
  202. // The ID of the parent channel, if the channel is under a category
  203. ParentID string `json:"parent_id"`
  204. // Amount of seconds a user has to wait before sending another message (0-21600)
  205. // bots, as well as users with the permission manage_messages or manage_channel, are unaffected
  206. RateLimitPerUser int `json:"rate_limit_per_user"`
  207. }
  208. // Mention returns a string which mentions the channel
  209. func (c *Channel) Mention() string {
  210. return fmt.Sprintf("<#%s>", c.ID)
  211. }
  212. // A ChannelEdit holds Channel Field data for a channel edit.
  213. type ChannelEdit struct {
  214. Name string `json:"name,omitempty"`
  215. Topic string `json:"topic,omitempty"`
  216. NSFW bool `json:"nsfw,omitempty"`
  217. Position int `json:"position"`
  218. Bitrate int `json:"bitrate,omitempty"`
  219. UserLimit int `json:"user_limit,omitempty"`
  220. PermissionOverwrites []*PermissionOverwrite `json:"permission_overwrites,omitempty"`
  221. ParentID string `json:"parent_id,omitempty"`
  222. RateLimitPerUser int `json:"rate_limit_per_user,omitempty"`
  223. }
  224. // A PermissionOverwrite holds permission overwrite data for a Channel
  225. type PermissionOverwrite struct {
  226. ID string `json:"id"`
  227. Type string `json:"type"`
  228. Deny int `json:"deny"`
  229. Allow int `json:"allow"`
  230. }
  231. // Emoji struct holds data related to Emoji's
  232. type Emoji struct {
  233. ID string `json:"id"`
  234. Name string `json:"name"`
  235. Roles []string `json:"roles"`
  236. Managed bool `json:"managed"`
  237. RequireColons bool `json:"require_colons"`
  238. Animated bool `json:"animated"`
  239. Available bool `json:"available"`
  240. }
  241. // MessageFormat returns a correctly formatted Emoji for use in Message content and embeds
  242. func (e *Emoji) MessageFormat() string {
  243. if e.ID != "" && e.Name != "" {
  244. if e.Animated {
  245. return "<a:" + e.APIName() + ">"
  246. }
  247. return "<:" + e.APIName() + ">"
  248. }
  249. return e.APIName()
  250. }
  251. // APIName returns an correctly formatted API name for use in the MessageReactions endpoints.
  252. func (e *Emoji) APIName() string {
  253. if e.ID != "" && e.Name != "" {
  254. return e.Name + ":" + e.ID
  255. }
  256. if e.Name != "" {
  257. return e.Name
  258. }
  259. return e.ID
  260. }
  261. // VerificationLevel type definition
  262. type VerificationLevel int
  263. // Constants for VerificationLevel levels from 0 to 4 inclusive
  264. const (
  265. VerificationLevelNone VerificationLevel = iota
  266. VerificationLevelLow
  267. VerificationLevelMedium
  268. VerificationLevelHigh
  269. VerificationLevelVeryHigh
  270. )
  271. // ExplicitContentFilterLevel type definition
  272. type ExplicitContentFilterLevel int
  273. // Constants for ExplicitContentFilterLevel levels from 0 to 2 inclusive
  274. const (
  275. ExplicitContentFilterDisabled ExplicitContentFilterLevel = iota
  276. ExplicitContentFilterMembersWithoutRoles
  277. ExplicitContentFilterAllMembers
  278. )
  279. // MfaLevel type definition
  280. type MfaLevel int
  281. // Constants for MfaLevel levels from 0 to 1 inclusive
  282. const (
  283. MfaLevelNone MfaLevel = iota
  284. MfaLevelElevated
  285. )
  286. // PremiumTier type definition
  287. type PremiumTier int
  288. // Constants for PremiumTier levels from 0 to 3 inclusive
  289. const (
  290. PremiumTierNone PremiumTier = iota
  291. PremiumTier1
  292. PremiumTier2
  293. PremiumTier3
  294. )
  295. // A Guild holds all data related to a specific Discord Guild. Guilds are also
  296. // sometimes referred to as Servers in the Discord client.
  297. type Guild struct {
  298. // The ID of the guild.
  299. ID string `json:"id"`
  300. // The name of the guild. (2–100 characters)
  301. Name string `json:"name"`
  302. // The hash of the guild's icon. Use Session.GuildIcon
  303. // to retrieve the icon itself.
  304. Icon string `json:"icon"`
  305. // The voice region of the guild.
  306. Region string `json:"region"`
  307. // The ID of the AFK voice channel.
  308. AfkChannelID string `json:"afk_channel_id"`
  309. // The ID of the embed channel ID, used for embed widgets.
  310. EmbedChannelID string `json:"embed_channel_id"`
  311. // The user ID of the owner of the guild.
  312. OwnerID string `json:"owner_id"`
  313. // The time at which the current user joined the guild.
  314. // This field is only present in GUILD_CREATE events and websocket
  315. // update events, and thus is only present in state-cached guilds.
  316. JoinedAt Timestamp `json:"joined_at"`
  317. // The hash of the guild's splash.
  318. Splash string `json:"splash"`
  319. // The timeout, in seconds, before a user is considered AFK in voice.
  320. AfkTimeout int `json:"afk_timeout"`
  321. // The number of members in the guild.
  322. // This field is only present in GUILD_CREATE events and websocket
  323. // update events, and thus is only present in state-cached guilds.
  324. MemberCount int `json:"member_count"`
  325. // The verification level required for the guild.
  326. VerificationLevel VerificationLevel `json:"verification_level"`
  327. // Whether the guild has embedding enabled.
  328. EmbedEnabled bool `json:"embed_enabled"`
  329. // Whether the guild is considered large. This is
  330. // determined by a member threshold in the identify packet,
  331. // and is currently hard-coded at 250 members in the library.
  332. Large bool `json:"large"`
  333. // The default message notification setting for the guild.
  334. // 0 == all messages, 1 == mentions only.
  335. DefaultMessageNotifications int `json:"default_message_notifications"`
  336. // A list of roles in the guild.
  337. Roles []*Role `json:"roles"`
  338. // A list of the custom emojis present in the guild.
  339. Emojis []*Emoji `json:"emojis"`
  340. // A list of the members in the guild.
  341. // This field is only present in GUILD_CREATE events and websocket
  342. // update events, and thus is only present in state-cached guilds.
  343. Members []*Member `json:"members"`
  344. // A list of partial presence objects for members in the guild.
  345. // This field is only present in GUILD_CREATE events and websocket
  346. // update events, and thus is only present in state-cached guilds.
  347. Presences []*Presence `json:"presences"`
  348. // A list of channels in the guild.
  349. // This field is only present in GUILD_CREATE events and websocket
  350. // update events, and thus is only present in state-cached guilds.
  351. Channels []*Channel `json:"channels"`
  352. // A list of voice states for the guild.
  353. // This field is only present in GUILD_CREATE events and websocket
  354. // update events, and thus is only present in state-cached guilds.
  355. VoiceStates []*VoiceState `json:"voice_states"`
  356. // Whether this guild is currently unavailable (most likely due to outage).
  357. // This field is only present in GUILD_CREATE events and websocket
  358. // update events, and thus is only present in state-cached guilds.
  359. Unavailable bool `json:"unavailable"`
  360. // The explicit content filter level
  361. ExplicitContentFilter ExplicitContentFilterLevel `json:"explicit_content_filter"`
  362. // The list of enabled guild features
  363. Features []string `json:"features"`
  364. // Required MFA level for the guild
  365. MfaLevel MfaLevel `json:"mfa_level"`
  366. // Whether or not the Server Widget is enabled
  367. WidgetEnabled bool `json:"widget_enabled"`
  368. // The Channel ID for the Server Widget
  369. WidgetChannelID string `json:"widget_channel_id"`
  370. // The Channel ID to which system messages are sent (eg join and leave messages)
  371. SystemChannelID string `json:"system_channel_id"`
  372. // the vanity url code for the guild
  373. VanityURLCode string `json:"vanity_url_code"`
  374. // the description for the guild
  375. Description string `json:"description"`
  376. // The hash of the guild's banner
  377. Banner string `json:"banner"`
  378. // The premium tier of the guild
  379. PremiumTier PremiumTier `json:"premium_tier"`
  380. // The total number of users currently boosting this server
  381. PremiumSubscriptionCount int `json:"premium_subscription_count"`
  382. }
  383. // IconURL returns a URL to the guild's icon.
  384. func (g *Guild) IconURL() string {
  385. if g.Icon == "" {
  386. return ""
  387. }
  388. if strings.HasPrefix(g.Icon, "a_") {
  389. return EndpointGuildIconAnimated(g.ID, g.Icon)
  390. }
  391. return EndpointGuildIcon(g.ID, g.Icon)
  392. }
  393. // A UserGuild holds a brief version of a Guild
  394. type UserGuild struct {
  395. ID string `json:"id"`
  396. Name string `json:"name"`
  397. Icon string `json:"icon"`
  398. Owner bool `json:"owner"`
  399. Permissions int `json:"permissions"`
  400. }
  401. // A GuildParams stores all the data needed to update discord guild settings
  402. type GuildParams struct {
  403. Name string `json:"name,omitempty"`
  404. Region string `json:"region,omitempty"`
  405. VerificationLevel *VerificationLevel `json:"verification_level,omitempty"`
  406. DefaultMessageNotifications int `json:"default_message_notifications,omitempty"` // TODO: Separate type?
  407. AfkChannelID string `json:"afk_channel_id,omitempty"`
  408. AfkTimeout int `json:"afk_timeout,omitempty"`
  409. Icon string `json:"icon,omitempty"`
  410. OwnerID string `json:"owner_id,omitempty"`
  411. Splash string `json:"splash,omitempty"`
  412. }
  413. // A Role stores information about Discord guild member roles.
  414. type Role struct {
  415. // The ID of the role.
  416. ID string `json:"id"`
  417. // The name of the role.
  418. Name string `json:"name"`
  419. // Whether this role is managed by an integration, and
  420. // thus cannot be manually added to, or taken from, members.
  421. Managed bool `json:"managed"`
  422. // Whether this role is mentionable.
  423. Mentionable bool `json:"mentionable"`
  424. // Whether this role is hoisted (shows up separately in member list).
  425. Hoist bool `json:"hoist"`
  426. // The hex color of this role.
  427. Color int `json:"color"`
  428. // The position of this role in the guild's role hierarchy.
  429. Position int `json:"position"`
  430. // The permissions of the role on the guild (doesn't include channel overrides).
  431. // This is a combination of bit masks; the presence of a certain permission can
  432. // be checked by performing a bitwise AND between this int and the permission.
  433. Permissions int `json:"permissions"`
  434. }
  435. // Mention returns a string which mentions the role
  436. func (r *Role) Mention() string {
  437. return fmt.Sprintf("<@&%s>", r.ID)
  438. }
  439. // Roles are a collection of Role
  440. type Roles []*Role
  441. func (r Roles) Len() int {
  442. return len(r)
  443. }
  444. func (r Roles) Less(i, j int) bool {
  445. return r[i].Position > r[j].Position
  446. }
  447. func (r Roles) Swap(i, j int) {
  448. r[i], r[j] = r[j], r[i]
  449. }
  450. // A VoiceState stores the voice states of Guilds
  451. type VoiceState struct {
  452. UserID string `json:"user_id"`
  453. SessionID string `json:"session_id"`
  454. ChannelID string `json:"channel_id"`
  455. GuildID string `json:"guild_id"`
  456. Suppress bool `json:"suppress"`
  457. SelfMute bool `json:"self_mute"`
  458. SelfDeaf bool `json:"self_deaf"`
  459. Mute bool `json:"mute"`
  460. Deaf bool `json:"deaf"`
  461. }
  462. // A Presence stores the online, offline, or idle and game status of Guild members.
  463. type Presence struct {
  464. User *User `json:"user"`
  465. Status Status `json:"status"`
  466. Game *Game `json:"game"`
  467. Nick string `json:"nick"`
  468. Roles []string `json:"roles"`
  469. Since *int `json:"since"`
  470. }
  471. // GameType is the type of "game" (see GameType* consts) in the Game struct
  472. type GameType int
  473. // Valid GameType values
  474. const (
  475. GameTypeGame GameType = iota
  476. GameTypeStreaming
  477. GameTypeListening
  478. GameTypeWatching
  479. )
  480. // A Game struct holds the name of the "playing .." game for a user
  481. type Game struct {
  482. Name string `json:"name"`
  483. Type GameType `json:"type"`
  484. URL string `json:"url,omitempty"`
  485. Details string `json:"details,omitempty"`
  486. State string `json:"state,omitempty"`
  487. TimeStamps TimeStamps `json:"timestamps,omitempty"`
  488. Assets Assets `json:"assets,omitempty"`
  489. ApplicationID string `json:"application_id,omitempty"`
  490. Instance int8 `json:"instance,omitempty"`
  491. // TODO: Party and Secrets (unknown structure)
  492. }
  493. // A TimeStamps struct contains start and end times used in the rich presence "playing .." Game
  494. type TimeStamps struct {
  495. EndTimestamp int64 `json:"end,omitempty"`
  496. StartTimestamp int64 `json:"start,omitempty"`
  497. }
  498. // UnmarshalJSON unmarshals JSON into TimeStamps struct
  499. func (t *TimeStamps) UnmarshalJSON(b []byte) error {
  500. temp := struct {
  501. End float64 `json:"end,omitempty"`
  502. Start float64 `json:"start,omitempty"`
  503. }{}
  504. err := json.Unmarshal(b, &temp)
  505. if err != nil {
  506. return err
  507. }
  508. t.EndTimestamp = int64(temp.End)
  509. t.StartTimestamp = int64(temp.Start)
  510. return nil
  511. }
  512. // An Assets struct contains assets and labels used in the rich presence "playing .." Game
  513. type Assets struct {
  514. LargeImageID string `json:"large_image,omitempty"`
  515. SmallImageID string `json:"small_image,omitempty"`
  516. LargeText string `json:"large_text,omitempty"`
  517. SmallText string `json:"small_text,omitempty"`
  518. }
  519. // A Member stores user information for Guild members. A guild
  520. // member represents a certain user's presence in a guild.
  521. type Member struct {
  522. // The guild ID on which the member exists.
  523. GuildID string `json:"guild_id"`
  524. // The time at which the member joined the guild, in ISO8601.
  525. JoinedAt Timestamp `json:"joined_at"`
  526. // The nickname of the member, if they have one.
  527. Nick string `json:"nick"`
  528. // Whether the member is deafened at a guild level.
  529. Deaf bool `json:"deaf"`
  530. // Whether the member is muted at a guild level.
  531. Mute bool `json:"mute"`
  532. // The underlying user on which the member is based.
  533. User *User `json:"user"`
  534. // A list of IDs of the roles which are possessed by the member.
  535. Roles []string `json:"roles"`
  536. // When the user used their Nitro boost on the server
  537. PremiumSince Timestamp `json:"premium_since"`
  538. }
  539. // Mention creates a member mention
  540. func (m *Member) Mention() string {
  541. return "<@!" + m.User.ID + ">"
  542. }
  543. // A Settings stores data for a specific users Discord client settings.
  544. type Settings struct {
  545. RenderEmbeds bool `json:"render_embeds"`
  546. InlineEmbedMedia bool `json:"inline_embed_media"`
  547. InlineAttachmentMedia bool `json:"inline_attachment_media"`
  548. EnableTTSCommand bool `json:"enable_tts_command"`
  549. MessageDisplayCompact bool `json:"message_display_compact"`
  550. ShowCurrentGame bool `json:"show_current_game"`
  551. ConvertEmoticons bool `json:"convert_emoticons"`
  552. Locale string `json:"locale"`
  553. Theme string `json:"theme"`
  554. GuildPositions []string `json:"guild_positions"`
  555. RestrictedGuilds []string `json:"restricted_guilds"`
  556. FriendSourceFlags *FriendSourceFlags `json:"friend_source_flags"`
  557. Status Status `json:"status"`
  558. DetectPlatformAccounts bool `json:"detect_platform_accounts"`
  559. DeveloperMode bool `json:"developer_mode"`
  560. }
  561. // Status type definition
  562. type Status string
  563. // Constants for Status with the different current available status
  564. const (
  565. StatusOnline Status = "online"
  566. StatusIdle Status = "idle"
  567. StatusDoNotDisturb Status = "dnd"
  568. StatusInvisible Status = "invisible"
  569. StatusOffline Status = "offline"
  570. )
  571. // FriendSourceFlags stores ... TODO :)
  572. type FriendSourceFlags struct {
  573. All bool `json:"all"`
  574. MutualGuilds bool `json:"mutual_guilds"`
  575. MutualFriends bool `json:"mutual_friends"`
  576. }
  577. // A Relationship between the logged in user and Relationship.User
  578. type Relationship struct {
  579. User *User `json:"user"`
  580. Type int `json:"type"` // 1 = friend, 2 = blocked, 3 = incoming friend req, 4 = sent friend req
  581. ID string `json:"id"`
  582. }
  583. // A TooManyRequests struct holds information received from Discord
  584. // when receiving a HTTP 429 response.
  585. type TooManyRequests struct {
  586. Bucket string `json:"bucket"`
  587. Message string `json:"message"`
  588. RetryAfter time.Duration `json:"retry_after"`
  589. }
  590. // A ReadState stores data on the read state of channels.
  591. type ReadState struct {
  592. MentionCount int `json:"mention_count"`
  593. LastMessageID string `json:"last_message_id"`
  594. ID string `json:"id"`
  595. }
  596. // An Ack is used to ack messages
  597. type Ack struct {
  598. Token string `json:"token"`
  599. }
  600. // A GuildRole stores data for guild roles.
  601. type GuildRole struct {
  602. Role *Role `json:"role"`
  603. GuildID string `json:"guild_id"`
  604. }
  605. // A GuildBan stores data for a guild ban.
  606. type GuildBan struct {
  607. Reason string `json:"reason"`
  608. User *User `json:"user"`
  609. }
  610. // A GuildEmbed stores data for a guild embed.
  611. type GuildEmbed struct {
  612. Enabled bool `json:"enabled"`
  613. ChannelID string `json:"channel_id"`
  614. }
  615. // A GuildAuditLog stores data for a guild audit log.
  616. type GuildAuditLog struct {
  617. Webhooks []struct {
  618. ChannelID string `json:"channel_id"`
  619. GuildID string `json:"guild_id"`
  620. ID string `json:"id"`
  621. Avatar string `json:"avatar"`
  622. Name string `json:"name"`
  623. } `json:"webhooks,omitempty"`
  624. Users []struct {
  625. Username string `json:"username"`
  626. Discriminator string `json:"discriminator"`
  627. Bot bool `json:"bot"`
  628. ID string `json:"id"`
  629. Avatar string `json:"avatar"`
  630. } `json:"users,omitempty"`
  631. AuditLogEntries []struct {
  632. TargetID string `json:"target_id"`
  633. Changes []struct {
  634. NewValue interface{} `json:"new_value"`
  635. OldValue interface{} `json:"old_value"`
  636. Key string `json:"key"`
  637. } `json:"changes,omitempty"`
  638. UserID string `json:"user_id"`
  639. ID string `json:"id"`
  640. ActionType int `json:"action_type"`
  641. Options struct {
  642. DeleteMembersDay string `json:"delete_member_days"`
  643. MembersRemoved string `json:"members_removed"`
  644. ChannelID string `json:"channel_id"`
  645. Count string `json:"count"`
  646. ID string `json:"id"`
  647. Type string `json:"type"`
  648. RoleName string `json:"role_name"`
  649. } `json:"options,omitempty"`
  650. Reason string `json:"reason"`
  651. } `json:"audit_log_entries"`
  652. }
  653. // Block contains Discord Audit Log Action Types
  654. const (
  655. AuditLogActionGuildUpdate = 1
  656. AuditLogActionChannelCreate = 10
  657. AuditLogActionChannelUpdate = 11
  658. AuditLogActionChannelDelete = 12
  659. AuditLogActionChannelOverwriteCreate = 13
  660. AuditLogActionChannelOverwriteUpdate = 14
  661. AuditLogActionChannelOverwriteDelete = 15
  662. AuditLogActionMemberKick = 20
  663. AuditLogActionMemberPrune = 21
  664. AuditLogActionMemberBanAdd = 22
  665. AuditLogActionMemberBanRemove = 23
  666. AuditLogActionMemberUpdate = 24
  667. AuditLogActionMemberRoleUpdate = 25
  668. AuditLogActionRoleCreate = 30
  669. AuditLogActionRoleUpdate = 31
  670. AuditLogActionRoleDelete = 32
  671. AuditLogActionInviteCreate = 40
  672. AuditLogActionInviteUpdate = 41
  673. AuditLogActionInviteDelete = 42
  674. AuditLogActionWebhookCreate = 50
  675. AuditLogActionWebhookUpdate = 51
  676. AuditLogActionWebhookDelete = 52
  677. AuditLogActionEmojiCreate = 60
  678. AuditLogActionEmojiUpdate = 61
  679. AuditLogActionEmojiDelete = 62
  680. AuditLogActionMessageDelete = 72
  681. )
  682. // A UserGuildSettingsChannelOverride stores data for a channel override for a users guild settings.
  683. type UserGuildSettingsChannelOverride struct {
  684. Muted bool `json:"muted"`
  685. MessageNotifications int `json:"message_notifications"`
  686. ChannelID string `json:"channel_id"`
  687. }
  688. // A UserGuildSettings stores data for a users guild settings.
  689. type UserGuildSettings struct {
  690. SupressEveryone bool `json:"suppress_everyone"`
  691. Muted bool `json:"muted"`
  692. MobilePush bool `json:"mobile_push"`
  693. MessageNotifications int `json:"message_notifications"`
  694. GuildID string `json:"guild_id"`
  695. ChannelOverrides []*UserGuildSettingsChannelOverride `json:"channel_overrides"`
  696. }
  697. // A UserGuildSettingsEdit stores data for editing UserGuildSettings
  698. type UserGuildSettingsEdit struct {
  699. SupressEveryone bool `json:"suppress_everyone"`
  700. Muted bool `json:"muted"`
  701. MobilePush bool `json:"mobile_push"`
  702. MessageNotifications int `json:"message_notifications"`
  703. ChannelOverrides map[string]*UserGuildSettingsChannelOverride `json:"channel_overrides"`
  704. }
  705. // An APIErrorMessage is an api error message returned from discord
  706. type APIErrorMessage struct {
  707. Code int `json:"code"`
  708. Message string `json:"message"`
  709. }
  710. // Webhook stores the data for a webhook.
  711. type Webhook struct {
  712. ID string `json:"id"`
  713. GuildID string `json:"guild_id"`
  714. ChannelID string `json:"channel_id"`
  715. User *User `json:"user"`
  716. Name string `json:"name"`
  717. Avatar string `json:"avatar"`
  718. Token string `json:"token"`
  719. }
  720. // WebhookParams is a struct for webhook params, used in the WebhookExecute command.
  721. type WebhookParams struct {
  722. Content string `json:"content,omitempty"`
  723. Username string `json:"username,omitempty"`
  724. AvatarURL string `json:"avatar_url,omitempty"`
  725. TTS bool `json:"tts,omitempty"`
  726. File string `json:"file,omitempty"`
  727. Embeds []*MessageEmbed `json:"embeds,omitempty"`
  728. }
  729. // MessageReaction stores the data for a message reaction.
  730. type MessageReaction struct {
  731. UserID string `json:"user_id"`
  732. MessageID string `json:"message_id"`
  733. Emoji Emoji `json:"emoji"`
  734. ChannelID string `json:"channel_id"`
  735. GuildID string `json:"guild_id,omitempty"`
  736. }
  737. // GatewayBotResponse stores the data for the gateway/bot response
  738. type GatewayBotResponse struct {
  739. URL string `json:"url"`
  740. Shards int `json:"shards"`
  741. }
  742. // Gateway Status Update is sent by the client to indicate a presence or status update
  743. // https://discordapp.com/developers/docs/topics/gateway#update-status-gateway-status-update-structure
  744. type GatewayStatusUpdate struct {
  745. Since int `json:"since"`
  746. Game Activity `json:"game"`
  747. Status string `json:"status"`
  748. AFK bool `json:"afk"`
  749. }
  750. type Activity struct {
  751. Name string
  752. Type ActivityType
  753. URL string
  754. }
  755. // GameType is the type of "game" (see GameType* consts) in the Game struct
  756. // https://discordapp.com/developers/docs/topics/gateway#activity-object-activity-types
  757. type ActivityType int
  758. // Valid GameType values
  759. const (
  760. ActivityTypeGame GameType = iota
  761. ActivityTypeStreaming
  762. ActivityTypeListening
  763. // ActivityTypeWatching // not valid in the use?
  764. ActivityTypeCustom = 4
  765. )
  766. // Identify is sent during initial handshake with the discord gateway.
  767. // https://discordapp.com/developers/docs/topics/gateway#identify
  768. type Identify struct {
  769. Token string `json:"token"`
  770. Properties IdentifyProperties `json:"properties"`
  771. Compress bool `json:"compress"`
  772. LargeThreshold int `json:"large_threshold"`
  773. Shard *[2]int `json:"shard,omitempty"`
  774. Presence GatewayStatusUpdate `json:"presence,omitempty"`
  775. GuildSubscriptions bool `json:"guild_subscriptions"`
  776. }
  777. type IdentifyProperties struct {
  778. OS string `json:"$os"`
  779. Browser string `json:"$browser"`
  780. Device string `json:"$device"`
  781. Referer string `json:"$referer"`
  782. ReferringDomain string `json:"$referring_domain"`
  783. }
  784. // Constants for the different bit offsets of text channel permissions
  785. const (
  786. PermissionReadMessages = 1 << (iota + 10)
  787. PermissionSendMessages
  788. PermissionSendTTSMessages
  789. PermissionManageMessages
  790. PermissionEmbedLinks
  791. PermissionAttachFiles
  792. PermissionReadMessageHistory
  793. PermissionMentionEveryone
  794. PermissionUseExternalEmojis
  795. )
  796. // Constants for the different bit offsets of voice permissions
  797. const (
  798. PermissionVoiceConnect = 1 << (iota + 20)
  799. PermissionVoiceSpeak
  800. PermissionVoiceMuteMembers
  801. PermissionVoiceDeafenMembers
  802. PermissionVoiceMoveMembers
  803. PermissionVoiceUseVAD
  804. PermissionVoicePrioritySpeaker = 1 << (iota + 2)
  805. )
  806. // Constants for general management.
  807. const (
  808. PermissionChangeNickname = 1 << (iota + 26)
  809. PermissionManageNicknames
  810. PermissionManageRoles
  811. PermissionManageWebhooks
  812. PermissionManageEmojis
  813. )
  814. // Constants for the different bit offsets of general permissions
  815. const (
  816. PermissionCreateInstantInvite = 1 << iota
  817. PermissionKickMembers
  818. PermissionBanMembers
  819. PermissionAdministrator
  820. PermissionManageChannels
  821. PermissionManageServer
  822. PermissionAddReactions
  823. PermissionViewAuditLogs
  824. PermissionAllText = PermissionReadMessages |
  825. PermissionSendMessages |
  826. PermissionSendTTSMessages |
  827. PermissionManageMessages |
  828. PermissionEmbedLinks |
  829. PermissionAttachFiles |
  830. PermissionReadMessageHistory |
  831. PermissionMentionEveryone
  832. PermissionAllVoice = PermissionVoiceConnect |
  833. PermissionVoiceSpeak |
  834. PermissionVoiceMuteMembers |
  835. PermissionVoiceDeafenMembers |
  836. PermissionVoiceMoveMembers |
  837. PermissionVoiceUseVAD |
  838. PermissionVoicePrioritySpeaker
  839. PermissionAllChannel = PermissionAllText |
  840. PermissionAllVoice |
  841. PermissionCreateInstantInvite |
  842. PermissionManageRoles |
  843. PermissionManageChannels |
  844. PermissionAddReactions |
  845. PermissionViewAuditLogs
  846. PermissionAll = PermissionAllChannel |
  847. PermissionKickMembers |
  848. PermissionBanMembers |
  849. PermissionManageServer |
  850. PermissionAdministrator |
  851. PermissionManageWebhooks |
  852. PermissionManageEmojis
  853. )
  854. // Block contains Discord JSON Error Response codes
  855. const (
  856. ErrCodeUnknownAccount = 10001
  857. ErrCodeUnknownApplication = 10002
  858. ErrCodeUnknownChannel = 10003
  859. ErrCodeUnknownGuild = 10004
  860. ErrCodeUnknownIntegration = 10005
  861. ErrCodeUnknownInvite = 10006
  862. ErrCodeUnknownMember = 10007
  863. ErrCodeUnknownMessage = 10008
  864. ErrCodeUnknownOverwrite = 10009
  865. ErrCodeUnknownProvider = 10010
  866. ErrCodeUnknownRole = 10011
  867. ErrCodeUnknownToken = 10012
  868. ErrCodeUnknownUser = 10013
  869. ErrCodeUnknownEmoji = 10014
  870. ErrCodeUnknownWebhook = 10015
  871. ErrCodeBotsCannotUseEndpoint = 20001
  872. ErrCodeOnlyBotsCanUseEndpoint = 20002
  873. ErrCodeMaximumGuildsReached = 30001
  874. ErrCodeMaximumFriendsReached = 30002
  875. ErrCodeMaximumPinsReached = 30003
  876. ErrCodeMaximumGuildRolesReached = 30005
  877. ErrCodeTooManyReactions = 30010
  878. ErrCodeUnauthorized = 40001
  879. ErrCodeMissingAccess = 50001
  880. ErrCodeInvalidAccountType = 50002
  881. ErrCodeCannotExecuteActionOnDMChannel = 50003
  882. ErrCodeEmbedDisabled = 50004
  883. ErrCodeCannotEditFromAnotherUser = 50005
  884. ErrCodeCannotSendEmptyMessage = 50006
  885. ErrCodeCannotSendMessagesToThisUser = 50007
  886. ErrCodeCannotSendMessagesInVoiceChannel = 50008
  887. ErrCodeChannelVerificationLevelTooHigh = 50009
  888. ErrCodeOAuth2ApplicationDoesNotHaveBot = 50010
  889. ErrCodeOAuth2ApplicationLimitReached = 50011
  890. ErrCodeInvalidOAuthState = 50012
  891. ErrCodeMissingPermissions = 50013
  892. ErrCodeInvalidAuthenticationToken = 50014
  893. ErrCodeNoteTooLong = 50015
  894. ErrCodeTooFewOrTooManyMessagesToDelete = 50016
  895. ErrCodeCanOnlyPinMessageToOriginatingChannel = 50019
  896. ErrCodeCannotExecuteActionOnSystemMessage = 50021
  897. ErrCodeMessageProvidedTooOldForBulkDelete = 50034
  898. ErrCodeInvalidFormBody = 50035
  899. ErrCodeInviteAcceptedToGuildApplicationsBotNotIn = 50036
  900. ErrCodeReactionBlocked = 90001
  901. )