structs.go 12 KB

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