structs.go 12 KB

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