structs.go 12 KB

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