structs.go 13 KB

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