structs.go 11 KB

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