structs.go 11 KB

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