structs.go 16 KB

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