structs.go 12 KB

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