structs.go 12 KB

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