structs.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. // 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. type Emoji struct {
  162. Roles []string `json:"roles"`
  163. RequireColons bool `json:"require_colons"`
  164. Name string `json:"name"`
  165. Managed bool `json:"managed"`
  166. ID string `json:"id"`
  167. }
  168. // A Guild holds all data related to a specific Discord Guild. Guilds are also
  169. // sometimes referred to as Servers in the Discord client.
  170. type Guild struct {
  171. ID string `json:"id"`
  172. Name string `json:"name"`
  173. Icon string `json:"icon"`
  174. Region string `json:"region"`
  175. AfkTimeout int `json:"afk_timeout"`
  176. AfkChannelID string `json:"afk_channel_id"`
  177. EmbedChannelID string `json:"embed_channel_id"`
  178. EmbedEnabled bool `json:"embed_enabled"`
  179. OwnerID string `json:"owner_id"`
  180. Large bool `json:"large"` // ??
  181. JoinedAt string `json:"joined_at"` // make this a timestamp
  182. Roles []Role `json:"roles"`
  183. Emojis []Emoji `json:"emojis"`
  184. Members []Member `json:"members"`
  185. Presences []Presence `json:"presences"`
  186. Channels []Channel `json:"channels"`
  187. VoiceStates []VoiceState `json:"voice_states"`
  188. }
  189. // A Role stores information about Discord guild member roles.
  190. type Role struct {
  191. ID string `json:"id"`
  192. Name string `json:"name"`
  193. Managed bool `json:"managed"`
  194. Color int `json:"color"`
  195. Hoist bool `json:"hoist"`
  196. Position int `json:"position"`
  197. Permissions int `json:"permissions"`
  198. }
  199. // A VoiceState stores the voice states of Guilds
  200. type VoiceState struct {
  201. UserID string `json:"user_id"`
  202. Suppress bool `json:"suppress"`
  203. SessionID string `json:"session_id"`
  204. SelfMute bool `json:"self_mute"`
  205. SelfDeaf bool `json:"self_deaf"`
  206. Mute bool `json:"mute"`
  207. Deaf bool `json:"deaf"`
  208. ChannelID string `json:"channel_id"`
  209. }
  210. // A Presence stores the online, offline, or idle and game status of Guild members.
  211. type Presence struct {
  212. User User `json:"user"`
  213. Status string `json:"status"`
  214. Game Game `json:"game"`
  215. }
  216. type Game struct {
  217. Name string `json:"name"`
  218. }
  219. // A Member stores user information for Guild members.
  220. type Member struct {
  221. GuildID string `json:"guild_id"`
  222. JoinedAt string `json:"joined_at"`
  223. Deaf bool `json:"deaf"`
  224. Mute bool `json:"mute"`
  225. User User `json:"user"`
  226. Roles []string `json:"roles"`
  227. }
  228. // A User stores all data for an individual Discord user.
  229. type User struct {
  230. ID string `json:"id"`
  231. Email string `json:"email"`
  232. Username string `json:"username"`
  233. Avatar string `json:"Avatar"`
  234. Verified bool `json:"verified"`
  235. //Discriminator int `json:"discriminator,string"` // TODO: See below
  236. }
  237. // TODO: Research issue.
  238. // Discriminator sometimes comes as a string
  239. // and sometimes it comes as a int. Weird.
  240. // to avoid errors I've just commented it out
  241. // but it doesn't seem to just kill the whole
  242. // program. Heartbeat is taken on READY even
  243. // with error and the system continues to read
  244. // it just doesn't seem able to handle this one
  245. // field correctly. Need to research this more.
  246. // A Settings stores data for a specific users Discord client settings.
  247. type Settings struct {
  248. RenderEmbeds bool `json:"render_embeds"`
  249. InlineEmbedMedia bool `json:"inline_embed_media"`
  250. EnableTtsCommand bool `json:"enable_tts_command"`
  251. MessageDisplayCompact bool `json:"message_display_compact"`
  252. Locale string `json:"locale"`
  253. ShowCurrentGame bool `json:"show_current_game"`
  254. Theme string `json:"theme"`
  255. MutedChannels []string `json:"muted_channels"`
  256. }
  257. // An Event provides a basic initial struct for all websocket event.
  258. type Event struct {
  259. Type string `json:"t"`
  260. State int `json:"s"`
  261. Operation int `json:"o"`
  262. Direction int `json:"dir"`
  263. RawData json.RawMessage `json:"d"`
  264. }
  265. // A Ready stores all data for the websocket READY event.
  266. type Ready struct {
  267. Version int `json:"v"`
  268. SessionID string `json:"session_id"`
  269. HeartbeatInterval time.Duration `json:"heartbeat_interval"`
  270. User User `json:"user"`
  271. ReadState []ReadState
  272. PrivateChannels []Channel `json:"private_channels"`
  273. Guilds []Guild `json:"guilds"`
  274. }
  275. // A ReadState stores data on the read state of channels.
  276. type ReadState struct {
  277. MentionCount int
  278. LastMessageID string `json:"last_message_id"`
  279. ID string `json:"id"`
  280. }
  281. // A TypingStart stores data for the typing start websocket event.
  282. type TypingStart struct {
  283. UserID string `json:"user_id"`
  284. ChannelID string `json:"channel_id"`
  285. Timestamp int `json:"timestamp"`
  286. }
  287. // A PresenceUpdate stores data for the pressence update websocket event.
  288. type PresenceUpdate struct {
  289. User User `json:"user"`
  290. Status string `json:"status"`
  291. Roles []string `json:"roles"`
  292. GuildID string `json:"guild_id"`
  293. Game Game `json:"game"`
  294. }
  295. // A MessageAck stores data for the message ack websocket event.
  296. type MessageAck struct {
  297. MessageID string `json:"message_id"`
  298. ChannelID string `json:"channel_id"`
  299. }
  300. // A MessageDelete stores data for the message delete websocket event.
  301. type MessageDelete struct {
  302. ID string `json:"id"`
  303. ChannelID string `json:"channel_id"`
  304. } // so much like MessageAck..
  305. // A GuildIntegrationsUpdate stores data for the guild integrations update
  306. // websocket event.
  307. type GuildIntegrationsUpdate struct {
  308. GuildID string `json:"guild_id"`
  309. }
  310. // A GuildRole stores data for guild role websocket events.
  311. type GuildRole struct {
  312. Role Role `json:"role"`
  313. GuildID string `json:"guild_id"`
  314. }
  315. // A GuildRoleDelete stores data for the guild role delete websocket event.
  316. type GuildRoleDelete struct {
  317. RoleID string `json:"role_id"`
  318. GuildID string `json:"guild_id"`
  319. }
  320. // A GuildBan stores data for a guild ban.
  321. type GuildBan struct {
  322. User User `json:"user"`
  323. GuildID string `json:"guild_id"`
  324. }
  325. // A GuildEmojisUpdate stores data for a guild emoji update event.
  326. type GuildEmojisUpdate struct {
  327. GuildID string `json:"guild_id"`
  328. Emojis []Emoji `json:"emojis"`
  329. }
  330. // A State contains the current known state.
  331. // As discord sends this in a READY blob, it seems reasonable to simply
  332. // use that struct as the data store.
  333. type State struct {
  334. Ready
  335. }