structs.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 RateLimit struct holds information related to a specific rate limit.
  273. type RateLimit struct {
  274. Bucket string `json:"bucket"`
  275. Message string `json:"message"`
  276. RetryAfter time.Duration `json:"retry_after"`
  277. }
  278. // A ReadState stores data on the read state of channels.
  279. type ReadState struct {
  280. MentionCount int `json:"mention_count"`
  281. LastMessageID string `json:"last_message_id"`
  282. ID string `json:"id"`
  283. }
  284. // A TypingStart stores data for the typing start websocket event.
  285. type TypingStart struct {
  286. UserID string `json:"user_id"`
  287. ChannelID string `json:"channel_id"`
  288. Timestamp int `json:"timestamp"`
  289. }
  290. // A PresenceUpdate stores data for the presence update websocket event.
  291. type PresenceUpdate struct {
  292. Status string `json:"status"`
  293. GuildID string `json:"guild_id"`
  294. Roles []string `json:"roles"`
  295. User *User `json:"user"`
  296. Game *Game `json:"game"`
  297. }
  298. // A MessageAck stores data for the message ack websocket event.
  299. type MessageAck struct {
  300. MessageID string `json:"message_id"`
  301. ChannelID string `json:"channel_id"`
  302. }
  303. // A GuildIntegrationsUpdate stores data for the guild integrations update
  304. // websocket event.
  305. type GuildIntegrationsUpdate struct {
  306. GuildID string `json:"guild_id"`
  307. }
  308. // A GuildRole stores data for guild role websocket events.
  309. type GuildRole struct {
  310. Role *Role `json:"role"`
  311. GuildID string `json:"guild_id"`
  312. }
  313. // A GuildRoleDelete stores data for the guild role delete websocket event.
  314. type GuildRoleDelete struct {
  315. RoleID string `json:"role_id"`
  316. GuildID string `json:"guild_id"`
  317. }
  318. // A GuildBan stores data for a guild ban.
  319. type GuildBan struct {
  320. User *User `json:"user"`
  321. GuildID string `json:"guild_id"`
  322. }
  323. // A GuildEmojisUpdate stores data for a guild emoji update event.
  324. type GuildEmojisUpdate struct {
  325. GuildID string `json:"guild_id"`
  326. Emojis []*Emoji `json:"emojis"`
  327. }
  328. // A GuildIntegration stores data for a guild integration.
  329. type GuildIntegration struct {
  330. ID string `json:"id"`
  331. Name string `json:"name"`
  332. Type string `json:"type"`
  333. Enabled bool `json:"enabled"`
  334. Syncing bool `json:"syncing"`
  335. RoleID string `json:"role_id"`
  336. ExpireBehavior int `json:"expire_behavior"`
  337. ExpireGracePeriod int `json:"expire_grace_period"`
  338. User *User `json:"user"`
  339. Account *GuildIntegrationAccount `json:"account"`
  340. SyncedAt int `json:"synced_at"`
  341. }
  342. // A GuildIntegrationAccount stores data for a guild integration account.
  343. type GuildIntegrationAccount struct {
  344. ID string `json:"id"`
  345. Name string `json:"name"`
  346. }
  347. // A GuildEmbed stores data for a guild embed.
  348. type GuildEmbed struct {
  349. Enabled bool `json:"enabled"`
  350. ChannelID string `json:"channel_id"`
  351. }
  352. // A UserGuildSettingsChannelOverride stores data for a channel override for a users guild settings.
  353. type UserGuildSettingsChannelOverride struct {
  354. Muted bool `json:"muted"`
  355. MessageNotifications int `json:"message_notifications"`
  356. ChannelID string `json:"channel_id"`
  357. }
  358. // A UserGuildSettings stores data for a users guild settings.
  359. type UserGuildSettings struct {
  360. SupressEveryone bool `json:"suppress_everyone"`
  361. Muted bool `json:"muted"`
  362. MobilePush bool `json:"mobile_push"`
  363. MessageNotifications int `json:"message_notifications"`
  364. GuildID string `json:"guild_id"`
  365. ChannelOverrides []*UserGuildSettingsChannelOverride `json:"channel_overrides"`
  366. }
  367. // Constants for the different bit offsets of text channel permissions
  368. const (
  369. PermissionReadMessages = 1 << (iota + 10)
  370. PermissionSendMessages
  371. PermissionSendTTSMessages
  372. PermissionManageMessages
  373. PermissionEmbedLinks
  374. PermissionAttachFiles
  375. PermissionReadMessageHistory
  376. PermissionMentionEveryone
  377. )
  378. // Constants for the different bit offsets of voice permissions
  379. const (
  380. PermissionVoiceConnect = 1 << (iota + 20)
  381. PermissionVoiceSpeak
  382. PermissionVoiceMuteMembers
  383. PermissionVoiceDeafenMembers
  384. PermissionVoiceMoveMembers
  385. PermissionVoiceUseVAD
  386. )
  387. // Constants for the different bit offsets of general permissions
  388. const (
  389. PermissionCreateInstantInvite = 1 << iota
  390. PermissionKickMembers
  391. PermissionBanMembers
  392. PermissionManageRoles
  393. PermissionManageChannels
  394. PermissionManageServer
  395. PermissionAllText = PermissionReadMessages |
  396. PermissionSendMessages |
  397. PermissionSendTTSMessages |
  398. PermissionManageMessages |
  399. PermissionEmbedLinks |
  400. PermissionAttachFiles |
  401. PermissionReadMessageHistory |
  402. PermissionMentionEveryone
  403. PermissionAllVoice = PermissionVoiceConnect |
  404. PermissionVoiceSpeak |
  405. PermissionVoiceMuteMembers |
  406. PermissionVoiceDeafenMembers |
  407. PermissionVoiceMoveMembers |
  408. PermissionVoiceUseVAD
  409. PermissionAllChannel = PermissionAllText |
  410. PermissionAllVoice |
  411. PermissionCreateInstantInvite |
  412. PermissionManageRoles |
  413. PermissionManageChannels
  414. PermissionAll = PermissionAllChannel |
  415. PermissionKickMembers |
  416. PermissionBanMembers |
  417. PermissionManageServer
  418. )