structs.go 12 KB

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