structs.go 12 KB

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