structs.go 15 KB

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