structs.go 16 KB

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