structs.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // Discordgo - Discord bindings for Go
  2. // Available at https://github.com/bwmarrin/discordgo
  3. // Copyright 2015 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 seperate 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"
  14. "strings"
  15. "sync"
  16. "time"
  17. "github.com/gorilla/websocket"
  18. )
  19. // A Session represents a connection to the Discord REST API.
  20. // token : The authentication token returned from Discord
  21. // Debug : If set to ture debug logging will be displayed.
  22. type Session struct {
  23. // General configurable settings.
  24. Token string // Authentication token for this session
  25. Debug bool // Debug for printing JSON request/responses
  26. AutoMention bool // if set to True, ChannelSendMessage will auto mention <@ID>
  27. // Settable Callback functions for Websocket Events
  28. OnEvent func(*Session, Event) // should Event be *Event?
  29. OnReady func(*Session, Ready)
  30. OnTypingStart func(*Session, TypingStart)
  31. OnMessageCreate func(*Session, Message)
  32. OnMessageUpdate func(*Session, Message)
  33. OnMessageDelete func(*Session, MessageDelete)
  34. OnMessageAck func(*Session, MessageAck)
  35. OnUserUpdate func(*Session, User)
  36. OnPresenceUpdate func(*Session, PresenceUpdate)
  37. OnVoiceStateUpdate func(*Session, VoiceState)
  38. OnChannelCreate func(*Session, Channel)
  39. OnChannelUpdate func(*Session, Channel)
  40. OnChannelDelete func(*Session, Channel)
  41. OnGuildCreate func(*Session, Guild)
  42. OnGuildUpdate func(*Session, Guild)
  43. OnGuildDelete func(*Session, Guild)
  44. OnGuildMemberAdd func(*Session, Member)
  45. OnGuildMemberRemove func(*Session, Member)
  46. OnGuildMemberDelete func(*Session, Member) // which is it?
  47. OnGuildMemberUpdate func(*Session, Member)
  48. OnGuildRoleCreate func(*Session, GuildRole)
  49. OnGuildRoleUpdate func(*Session, GuildRole)
  50. OnGuildRoleDelete func(*Session, GuildRoleDelete)
  51. OnGuildIntegrationsUpdate func(*Session, GuildIntegrationsUpdate)
  52. OnGuildBanAdd func(*Session, GuildBan)
  53. OnGuildBanRemove func(*Session, GuildBan)
  54. OnGuildEmojisUpdate func(*Session, GuildEmojisUpdate)
  55. OnUserSettingsUpdate func(*Session, map[string]interface{}) // TODO: Find better way?
  56. // Exposed but should not be modified by User.
  57. SessionID string // from websocket READY packet
  58. DataReady bool // Set to true when Data Websocket is ready
  59. VoiceReady bool // Set to true when Voice Websocket is ready
  60. UDPReady bool // Set to true when UDP Connection is ready
  61. // Other..
  62. wsConn *websocket.Conn
  63. //TODO, add bools for like.
  64. // are we connnected to websocket?
  65. // have we authenticated to login?
  66. // lets put all the general session
  67. // tracking and infos here.. clearly
  68. // Everything below here is used for Voice testing.
  69. // This stuff is almost guarenteed to change a lot
  70. // and is even a bit hackish right now.
  71. VwsConn *websocket.Conn // new for voice
  72. VSessionID string
  73. VToken string
  74. VEndpoint string
  75. VGuildID string
  76. VChannelID string
  77. Vop2 VoiceOP2
  78. UDPConn *net.UDPConn
  79. // Managed state object, updated with events.
  80. State *State
  81. StateEnabled bool
  82. // Mutex/Bools for locks that prevent accidents.
  83. // TODO: Add channels.
  84. heartbeatLock sync.Mutex
  85. heartbeatRunning bool
  86. }
  87. // A Message stores all data related to a specific Discord message.
  88. type Message struct {
  89. ID string `json:"id"`
  90. Author User `json:"author"`
  91. Content string `json:"content"`
  92. Attachments []Attachment `json:"attachments"`
  93. Tts bool `json:"tts"`
  94. Embeds []Embed `json:"embeds"`
  95. Timestamp string `json:"timestamp"`
  96. MentionEveryone bool `json:"mention_everyone"`
  97. EditedTimestamp string `json:"edited_timestamp"`
  98. Mentions []User `json:"mentions"`
  99. ChannelID string `json:"channel_id"`
  100. }
  101. // ContentWithMentionsReplaced will replace all @<id> mentions with the
  102. // username of the mention.
  103. func (m *Message) ContentWithMentionsReplaced() string {
  104. content := m.Content
  105. for _, user := range m.Mentions {
  106. content = strings.Replace(content, fmt.Sprintf("<@%s>", user.ID), fmt.Sprintf("@%s", user.Username), -1)
  107. }
  108. return content
  109. }
  110. // An Attachment stores data for message attachments.
  111. type Attachment struct { //TODO figure this out
  112. }
  113. // An Embed stores data for message embeds.
  114. type Embed struct { // TODO figure this out
  115. }
  116. // A VoiceRegion stores data for a specific voice region server.
  117. type VoiceRegion struct {
  118. ID string `json:"id"`
  119. Name string `json:"name"`
  120. Hostname string `json:"sample_hostname"`
  121. Port int `json:"sample_port"`
  122. }
  123. // A VoiceICE stores data for voice ICE servers.
  124. type VoiceICE struct {
  125. TTL string `json:"ttl"`
  126. Servers []ICEServer `json:"servers"`
  127. }
  128. // A ICEServer stores data for a specific voice ICE server.
  129. type ICEServer struct {
  130. URL string `json:"url"`
  131. Username string `json:"username"`
  132. Credential string `json:"credential"`
  133. }
  134. // A Invite stores all data related to a specific Discord Guild or Channel invite.
  135. type Invite struct {
  136. MaxAge int `json:"max_age"`
  137. Code string `json:"code"`
  138. Guild Guild `json:"guild"`
  139. Revoked bool `json:"revoked"`
  140. CreatedAt string `json:"created_at"` // TODO make timestamp
  141. Temporary bool `json:"temporary"`
  142. Uses int `json:"uses"`
  143. MaxUses int `json:"max_uses"`
  144. Inviter User `json:"inviter"`
  145. XkcdPass bool `json:"xkcdpass"`
  146. Channel Channel `json:"channel"`
  147. }
  148. // A Channel holds all data related to an individual Discord channel.
  149. type Channel struct {
  150. ID string `json:"id"`
  151. GuildID string `json:"guild_id"`
  152. Name string `json:"name"`
  153. Topic string `json:"topic"`
  154. Position int `json:"position"`
  155. Type string `json:"type"`
  156. PermissionOverwrites []PermissionOverwrite `json:"permission_overwrites"`
  157. IsPrivate bool `json:"is_private"`
  158. LastMessageID string `json:"last_message_id"`
  159. Recipient User `json:"recipient"`
  160. }
  161. // A PermissionOverwrite holds permission overwrite data for a Channel
  162. type PermissionOverwrite struct {
  163. ID string `json:"id"`
  164. Type string `json:"type"`
  165. Deny int `json:"deny"`
  166. Allow int `json:"allow"`
  167. }
  168. type Emoji struct {
  169. Roles []string `json:"roles"`
  170. RequireColons bool `json:"require_colons"`
  171. Name string `json:"name"`
  172. Managed bool `json:"managed"`
  173. ID string `json:"id"`
  174. }
  175. // A Guild holds all data related to a specific Discord Guild. Guilds are also
  176. // sometimes referred to as Servers in the Discord client.
  177. type Guild struct {
  178. ID string `json:"id"`
  179. Name string `json:"name"`
  180. Icon string `json:"icon"`
  181. Region string `json:"region"`
  182. AfkTimeout int `json:"afk_timeout"`
  183. AfkChannelID string `json:"afk_channel_id"`
  184. EmbedChannelID string `json:"embed_channel_id"`
  185. EmbedEnabled bool `json:"embed_enabled"`
  186. OwnerID string `json:"owner_id"`
  187. Large bool `json:"large"` // ??
  188. JoinedAt string `json:"joined_at"` // make this a timestamp
  189. Roles []Role `json:"roles"`
  190. Emojis []Emoji `json:"emojis"`
  191. Members []Member `json:"members"`
  192. Presences []Presence `json:"presences"`
  193. Channels []Channel `json:"channels"`
  194. VoiceStates []VoiceState `json:"voice_states"`
  195. }
  196. // A Role stores information about Discord guild member roles.
  197. type Role struct {
  198. ID string `json:"id"`
  199. Name string `json:"name"`
  200. Managed bool `json:"managed"`
  201. Color int `json:"color"`
  202. Hoist bool `json:"hoist"`
  203. Position int `json:"position"`
  204. Permissions int `json:"permissions"`
  205. }
  206. // A VoiceState stores the voice states of Guilds
  207. type VoiceState struct {
  208. UserID string `json:"user_id"`
  209. Suppress bool `json:"suppress"`
  210. SessionID string `json:"session_id"`
  211. SelfMute bool `json:"self_mute"`
  212. SelfDeaf bool `json:"self_deaf"`
  213. Mute bool `json:"mute"`
  214. Deaf bool `json:"deaf"`
  215. ChannelID string `json:"channel_id"`
  216. }
  217. // A Presence stores the online, offline, or idle and game status of Guild members.
  218. type Presence struct {
  219. User User `json:"user"`
  220. Status string `json:"status"`
  221. Game Game `json:"game"`
  222. }
  223. type Game struct {
  224. Name string `json:"name"`
  225. }
  226. // A Member stores user information for Guild members.
  227. type Member struct {
  228. GuildID string `json:"guild_id"`
  229. JoinedAt string `json:"joined_at"`
  230. Deaf bool `json:"deaf"`
  231. Mute bool `json:"mute"`
  232. User User `json:"user"`
  233. Roles []string `json:"roles"`
  234. }
  235. // A User stores all data for an individual Discord user.
  236. type User struct {
  237. ID string `json:"id"`
  238. Email string `json:"email"`
  239. Username string `json:"username"`
  240. Avatar string `json:"Avatar"`
  241. Verified bool `json:"verified"`
  242. //Discriminator int `json:"discriminator,string"` // TODO: See below
  243. }
  244. // TODO: Research issue.
  245. // Discriminator sometimes comes as a string
  246. // and sometimes it comes as a int. Weird.
  247. // to avoid errors I've just commented it out
  248. // but it doesn't seem to just kill the whole
  249. // program. Heartbeat is taken on READY even
  250. // with error and the system continues to read
  251. // it just doesn't seem able to handle this one
  252. // field correctly. Need to research this more.
  253. // A Settings stores data for a specific users Discord client settings.
  254. type Settings struct {
  255. RenderEmbeds bool `json:"render_embeds"`
  256. InlineEmbedMedia bool `json:"inline_embed_media"`
  257. EnableTtsCommand bool `json:"enable_tts_command"`
  258. MessageDisplayCompact bool `json:"message_display_compact"`
  259. Locale string `json:"locale"`
  260. ShowCurrentGame bool `json:"show_current_game"`
  261. Theme string `json:"theme"`
  262. MutedChannels []string `json:"muted_channels"`
  263. }
  264. // An Event provides a basic initial struct for all websocket event.
  265. type Event struct {
  266. Type string `json:"t"`
  267. State int `json:"s"`
  268. Operation int `json:"o"`
  269. Direction int `json:"dir"`
  270. RawData json.RawMessage `json:"d"`
  271. }
  272. // A Ready stores all data for the websocket READY event.
  273. type Ready struct {
  274. Version int `json:"v"`
  275. SessionID string `json:"session_id"`
  276. HeartbeatInterval time.Duration `json:"heartbeat_interval"`
  277. User User `json:"user"`
  278. ReadState []ReadState
  279. PrivateChannels []Channel `json:"private_channels"`
  280. Guilds []Guild `json:"guilds"`
  281. }
  282. // A ReadState stores data on the read state of channels.
  283. type ReadState struct {
  284. MentionCount int
  285. LastMessageID string `json:"last_message_id"`
  286. ID string `json:"id"`
  287. }
  288. // A TypingStart stores data for the typing start websocket event.
  289. type TypingStart struct {
  290. UserID string `json:"user_id"`
  291. ChannelID string `json:"channel_id"`
  292. Timestamp int `json:"timestamp"`
  293. }
  294. // A PresenceUpdate stores data for the pressence update websocket event.
  295. type PresenceUpdate struct {
  296. User User `json:"user"`
  297. Status string `json:"status"`
  298. Roles []string `json:"roles"`
  299. GuildID string `json:"guild_id"`
  300. Game Game `json:"game"`
  301. }
  302. // A MessageAck stores data for the message ack websocket event.
  303. type MessageAck struct {
  304. MessageID string `json:"message_id"`
  305. ChannelID string `json:"channel_id"`
  306. }
  307. // A MessageDelete stores data for the message delete websocket event.
  308. type MessageDelete struct {
  309. ID string `json:"id"`
  310. ChannelID string `json:"channel_id"`
  311. } // so much like MessageAck..
  312. // A GuildIntegrationsUpdate stores data for the guild integrations update
  313. // websocket event.
  314. type GuildIntegrationsUpdate struct {
  315. GuildID string `json:"guild_id"`
  316. }
  317. // A GuildRole stores data for guild role websocket events.
  318. type GuildRole struct {
  319. Role Role `json:"role"`
  320. GuildID string `json:"guild_id"`
  321. }
  322. // A GuildRoleDelete stores data for the guild role delete websocket event.
  323. type GuildRoleDelete struct {
  324. RoleID string `json:"role_id"`
  325. GuildID string `json:"guild_id"`
  326. }
  327. // A GuildBan stores data for a guild ban.
  328. type GuildBan struct {
  329. User User `json:"user"`
  330. GuildID string `json:"guild_id"`
  331. }
  332. // A GuildEmojisUpdate stores data for a guild emoji update event.
  333. type GuildEmojisUpdate struct {
  334. GuildID string `json:"guild_id"`
  335. Emojis []Emoji `json:"emojis"`
  336. }
  337. // A State contains the current known state.
  338. // As discord sends this in a READY blob, it seems reasonable to simply
  339. // use that struct as the data store.
  340. type State struct {
  341. Ready
  342. }