structs.go 12 KB

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