structs.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 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. heartbeatChan chan struct{}
  86. listenLock sync.Mutex
  87. listenChan chan struct{}
  88. }
  89. // A Message stores all data related to a specific Discord message.
  90. type Message struct {
  91. ID string `json:"id"`
  92. Author User `json:"author"`
  93. Content string `json:"content"`
  94. Attachments []Attachment `json:"attachments"`
  95. Tts bool `json:"tts"`
  96. Embeds []Embed `json:"embeds"`
  97. Timestamp string `json:"timestamp"`
  98. MentionEveryone bool `json:"mention_everyone"`
  99. EditedTimestamp string `json:"edited_timestamp"`
  100. Mentions []User `json:"mentions"`
  101. ChannelID string `json:"channel_id"`
  102. }
  103. // ContentWithMentionsReplaced will replace all @<id> mentions with the
  104. // username of the mention.
  105. func (m *Message) ContentWithMentionsReplaced() string {
  106. content := m.Content
  107. for _, user := range m.Mentions {
  108. content = strings.Replace(content, fmt.Sprintf("<@%s>", user.ID), fmt.Sprintf("@%s", user.Username), -1)
  109. }
  110. return content
  111. }
  112. // An Attachment stores data for message attachments.
  113. type Attachment struct { //TODO figure this out
  114. }
  115. // An Embed stores data for message embeds.
  116. type Embed struct { // TODO figure this out
  117. }
  118. // A VoiceRegion stores data for a specific voice region server.
  119. type VoiceRegion struct {
  120. ID string `json:"id"`
  121. Name string `json:"name"`
  122. Hostname string `json:"sample_hostname"`
  123. Port int `json:"sample_port"`
  124. }
  125. // A VoiceICE stores data for voice ICE servers.
  126. type VoiceICE struct {
  127. TTL string `json:"ttl"`
  128. Servers []ICEServer `json:"servers"`
  129. }
  130. // A ICEServer stores data for a specific voice ICE server.
  131. type ICEServer struct {
  132. URL string `json:"url"`
  133. Username string `json:"username"`
  134. Credential string `json:"credential"`
  135. }
  136. // A Invite stores all data related to a specific Discord Guild or Channel invite.
  137. type Invite struct {
  138. MaxAge int `json:"max_age"`
  139. Code string `json:"code"`
  140. Guild Guild `json:"guild"`
  141. Revoked bool `json:"revoked"`
  142. CreatedAt string `json:"created_at"` // TODO make timestamp
  143. Temporary bool `json:"temporary"`
  144. Uses int `json:"uses"`
  145. MaxUses int `json:"max_uses"`
  146. Inviter User `json:"inviter"`
  147. XkcdPass bool `json:"xkcdpass"`
  148. Channel Channel `json:"channel"`
  149. }
  150. // A Channel holds all data related to an individual Discord channel.
  151. type Channel struct {
  152. ID string `json:"id"`
  153. GuildID string `json:"guild_id"`
  154. Name string `json:"name"`
  155. Topic string `json:"topic"`
  156. Position int `json:"position"`
  157. Type string `json:"type"`
  158. PermissionOverwrites []PermissionOverwrite `json:"permission_overwrites"`
  159. IsPrivate bool `json:"is_private"`
  160. LastMessageID string `json:"last_message_id"`
  161. Recipient User `json:"recipient"`
  162. }
  163. // A PermissionOverwrite holds permission overwrite data for a Channel
  164. type PermissionOverwrite struct {
  165. ID string `json:"id"`
  166. Type string `json:"type"`
  167. Deny int `json:"deny"`
  168. Allow int `json:"allow"`
  169. }
  170. type Emoji struct {
  171. Roles []string `json:"roles"`
  172. RequireColons bool `json:"require_colons"`
  173. Name string `json:"name"`
  174. Managed bool `json:"managed"`
  175. ID string `json:"id"`
  176. }
  177. // A Guild holds all data related to a specific Discord Guild. Guilds are also
  178. // sometimes referred to as Servers in the Discord client.
  179. type Guild struct {
  180. ID string `json:"id"`
  181. Name string `json:"name"`
  182. Icon string `json:"icon"`
  183. Region string `json:"region"`
  184. AfkTimeout int `json:"afk_timeout"`
  185. AfkChannelID string `json:"afk_channel_id"`
  186. EmbedChannelID string `json:"embed_channel_id"`
  187. EmbedEnabled bool `json:"embed_enabled"`
  188. OwnerID string `json:"owner_id"`
  189. Large bool `json:"large"` // ??
  190. JoinedAt string `json:"joined_at"` // make this a timestamp
  191. Roles []Role `json:"roles"`
  192. Emojis []Emoji `json:"emojis"`
  193. Members []Member `json:"members"`
  194. Presences []Presence `json:"presences"`
  195. Channels []Channel `json:"channels"`
  196. VoiceStates []VoiceState `json:"voice_states"`
  197. }
  198. // A Role stores information about Discord guild member roles.
  199. type Role struct {
  200. ID string `json:"id"`
  201. Name string `json:"name"`
  202. Managed bool `json:"managed"`
  203. Color int `json:"color"`
  204. Hoist bool `json:"hoist"`
  205. Position int `json:"position"`
  206. Permissions int `json:"permissions"`
  207. }
  208. // A VoiceState stores the voice states of Guilds
  209. type VoiceState struct {
  210. UserID string `json:"user_id"`
  211. Suppress bool `json:"suppress"`
  212. SessionID string `json:"session_id"`
  213. SelfMute bool `json:"self_mute"`
  214. SelfDeaf bool `json:"self_deaf"`
  215. Mute bool `json:"mute"`
  216. Deaf bool `json:"deaf"`
  217. ChannelID string `json:"channel_id"`
  218. }
  219. // A Presence stores the online, offline, or idle and game status of Guild members.
  220. type Presence struct {
  221. User User `json:"user"`
  222. Status string `json:"status"`
  223. Game Game `json:"game"`
  224. }
  225. type Game struct {
  226. Name string `json:"name"`
  227. }
  228. // A Member stores user information for Guild members.
  229. type Member struct {
  230. GuildID string `json:"guild_id"`
  231. JoinedAt string `json:"joined_at"`
  232. Deaf bool `json:"deaf"`
  233. Mute bool `json:"mute"`
  234. User User `json:"user"`
  235. Roles []string `json:"roles"`
  236. }
  237. // A User stores all data for an individual Discord user.
  238. type User struct {
  239. ID string `json:"id"`
  240. Email string `json:"email"`
  241. Username string `json:"username"`
  242. Avatar string `json:"Avatar"`
  243. Verified bool `json:"verified"`
  244. //Discriminator int `json:"discriminator,string"` // TODO: See below
  245. }
  246. // TODO: Research issue.
  247. // Discriminator sometimes comes as a string
  248. // and sometimes it comes as a int. Weird.
  249. // to avoid errors I've just commented it out
  250. // but it doesn't seem to just kill the whole
  251. // program. Heartbeat is taken on READY even
  252. // with error and the system continues to read
  253. // it just doesn't seem able to handle this one
  254. // field correctly. Need to research this more.
  255. // A Settings stores data for a specific users Discord client settings.
  256. type Settings struct {
  257. RenderEmbeds bool `json:"render_embeds"`
  258. InlineEmbedMedia bool `json:"inline_embed_media"`
  259. EnableTtsCommand bool `json:"enable_tts_command"`
  260. MessageDisplayCompact bool `json:"message_display_compact"`
  261. Locale string `json:"locale"`
  262. ShowCurrentGame bool `json:"show_current_game"`
  263. Theme string `json:"theme"`
  264. MutedChannels []string `json:"muted_channels"`
  265. }
  266. // An Event provides a basic initial struct for all websocket event.
  267. type Event struct {
  268. Type string `json:"t"`
  269. State int `json:"s"`
  270. Operation int `json:"o"`
  271. Direction int `json:"dir"`
  272. RawData json.RawMessage `json:"d"`
  273. }
  274. // A Ready stores all data for the websocket READY event.
  275. type Ready struct {
  276. Version int `json:"v"`
  277. SessionID string `json:"session_id"`
  278. HeartbeatInterval time.Duration `json:"heartbeat_interval"`
  279. User User `json:"user"`
  280. ReadState []ReadState
  281. PrivateChannels []Channel `json:"private_channels"`
  282. Guilds []Guild `json:"guilds"`
  283. }
  284. // A ReadState stores data on the read state of channels.
  285. type ReadState struct {
  286. MentionCount int
  287. LastMessageID string `json:"last_message_id"`
  288. ID string `json:"id"`
  289. }
  290. // A TypingStart stores data for the typing start websocket event.
  291. type TypingStart struct {
  292. UserID string `json:"user_id"`
  293. ChannelID string `json:"channel_id"`
  294. Timestamp int `json:"timestamp"`
  295. }
  296. // A PresenceUpdate stores data for the pressence update websocket event.
  297. type PresenceUpdate struct {
  298. User User `json:"user"`
  299. Status string `json:"status"`
  300. Roles []string `json:"roles"`
  301. GuildID string `json:"guild_id"`
  302. Game Game `json:"game"`
  303. }
  304. // A MessageAck stores data for the message ack websocket event.
  305. type MessageAck struct {
  306. MessageID string `json:"message_id"`
  307. ChannelID string `json:"channel_id"`
  308. }
  309. // A MessageDelete stores data for the message delete websocket event.
  310. type MessageDelete struct {
  311. ID string `json:"id"`
  312. ChannelID string `json:"channel_id"`
  313. } // so much like MessageAck..
  314. // A GuildIntegrationsUpdate stores data for the guild integrations update
  315. // websocket event.
  316. type GuildIntegrationsUpdate struct {
  317. GuildID string `json:"guild_id"`
  318. }
  319. // A GuildRole stores data for guild role websocket events.
  320. type GuildRole struct {
  321. Role Role `json:"role"`
  322. GuildID string `json:"guild_id"`
  323. }
  324. // A GuildRoleDelete stores data for the guild role delete websocket event.
  325. type GuildRoleDelete struct {
  326. RoleID string `json:"role_id"`
  327. GuildID string `json:"guild_id"`
  328. }
  329. // A GuildBan stores data for a guild ban.
  330. type GuildBan struct {
  331. User User `json:"user"`
  332. GuildID string `json:"guild_id"`
  333. }
  334. // A GuildEmojisUpdate stores data for a guild emoji update event.
  335. type GuildEmojisUpdate struct {
  336. GuildID string `json:"guild_id"`
  337. Emojis []Emoji `json:"emojis"`
  338. }
  339. // A State contains the current known state.
  340. // As discord sends this in a READY blob, it seems reasonable to simply
  341. // use that struct as the data store.
  342. type State struct {
  343. Ready
  344. }