interactions.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package discordgo
  2. import (
  3. "bytes"
  4. "crypto/ed25519"
  5. "encoding/hex"
  6. "io"
  7. "io/ioutil"
  8. "net/http"
  9. "time"
  10. )
  11. // InteractionDeadline is the time allowed to respond to an interaction.
  12. const InteractionDeadline = time.Second * 3
  13. // ApplicationCommand represents an application's slash command.
  14. type ApplicationCommand struct {
  15. ID string `json:"id,omitempty"`
  16. ApplicationID string `json:"application_id,omitempty"`
  17. Name string `json:"name"`
  18. Description string `json:"description,omitempty"`
  19. Version string `json:"version,omitempty"`
  20. Options []*ApplicationCommandOption `json:"options"`
  21. }
  22. // ApplicationCommandOptionType indicates the type of a slash command's option.
  23. type ApplicationCommandOptionType uint8
  24. // Application command option types.
  25. const (
  26. ApplicationCommandOptionSubCommand ApplicationCommandOptionType = 1
  27. ApplicationCommandOptionSubCommandGroup ApplicationCommandOptionType = 2
  28. ApplicationCommandOptionString ApplicationCommandOptionType = 3
  29. ApplicationCommandOptionInteger ApplicationCommandOptionType = 4
  30. ApplicationCommandOptionBoolean ApplicationCommandOptionType = 5
  31. ApplicationCommandOptionUser ApplicationCommandOptionType = 6
  32. ApplicationCommandOptionChannel ApplicationCommandOptionType = 7
  33. ApplicationCommandOptionRole ApplicationCommandOptionType = 8
  34. ApplicationCommandOptionMentionable ApplicationCommandOptionType = 9
  35. )
  36. // ApplicationCommandOption represents an option/subcommand/subcommands group.
  37. type ApplicationCommandOption struct {
  38. Type ApplicationCommandOptionType `json:"type"`
  39. Name string `json:"name"`
  40. Description string `json:"description,omitempty"`
  41. // NOTE: This feature was on the API, but at some point developers decided to remove it.
  42. // So I commented it, until it will be officially on the docs.
  43. // Default bool `json:"default"`
  44. Required bool `json:"required"`
  45. Choices []*ApplicationCommandOptionChoice `json:"choices"`
  46. Options []*ApplicationCommandOption `json:"options"`
  47. }
  48. // ApplicationCommandOptionChoice represents a slash command option choice.
  49. type ApplicationCommandOptionChoice struct {
  50. Name string `json:"name"`
  51. Value interface{} `json:"value"`
  52. }
  53. // InteractionType indicates the type of an interaction event.
  54. type InteractionType uint8
  55. // Interaction types
  56. const (
  57. InteractionPing InteractionType = 1
  58. InteractionApplicationCommand InteractionType = 2
  59. )
  60. // Interaction represents an interaction event created via a slash command.
  61. type Interaction struct {
  62. ID string `json:"id"`
  63. Type InteractionType `json:"type"`
  64. Data ApplicationCommandInteractionData `json:"data"`
  65. GuildID string `json:"guild_id"`
  66. ChannelID string `json:"channel_id"`
  67. // The member who invoked this interaction.
  68. // NOTE: this field is only filled when the slash command was invoked in a guild;
  69. // if it was invoked in a DM, the `User` field will be filled instead.
  70. // Make sure to check for `nil` before using this field.
  71. Member *Member `json:"member"`
  72. // The user who invoked this interaction.
  73. // NOTE: this field is only filled when the slash command was invoked in a DM;
  74. // if it was invoked in a guild, the `Member` field will be filled instead.
  75. // Make sure to check for `nil` before using this field.
  76. User *User `json:"user"`
  77. Token string `json:"token"`
  78. Version int `json:"version"`
  79. }
  80. // ApplicationCommandInteractionData contains data received in an interaction event.
  81. type ApplicationCommandInteractionData struct {
  82. ID string `json:"id"`
  83. Name string `json:"name"`
  84. Resolved *ApplicationCommandInteractionDataResolved `json:"resolved"`
  85. Options []*ApplicationCommandInteractionDataOption `json:"options"`
  86. }
  87. // ApplicationCommandInteractionDataResolved contains resolved data for command arguments.
  88. // Partial Member objects are missing user, deaf and mute fields.
  89. // Partial Channel objects only have id, name, type and permissions fields.
  90. type ApplicationCommandInteractionDataResolved struct {
  91. Users map[string]*User `json:"users"`
  92. Members map[string]*Member `json:"members"`
  93. Roles map[string]*Role `json:"roles"`
  94. Channels map[string]*Channel `json:"channels"`
  95. }
  96. // ApplicationCommandInteractionDataOption represents an option of a slash command.
  97. type ApplicationCommandInteractionDataOption struct {
  98. Name string `json:"name"`
  99. // NOTE: Contains the value specified by InteractionType.
  100. Value interface{} `json:"value,omitempty"`
  101. Options []*ApplicationCommandInteractionDataOption `json:"options,omitempty"`
  102. }
  103. // IntValue is a utility function for casting option value to integer
  104. func (o ApplicationCommandInteractionDataOption) IntValue() int64 {
  105. if v, ok := o.Value.(float64); ok {
  106. return int64(v)
  107. }
  108. return 0
  109. }
  110. // UintValue is a utility function for casting option value to unsigned integer
  111. func (o ApplicationCommandInteractionDataOption) UintValue() uint64 {
  112. if v, ok := o.Value.(float64); ok {
  113. return uint64(v)
  114. }
  115. return 0
  116. }
  117. // FloatValue is a utility function for casting option value to float
  118. func (o ApplicationCommandInteractionDataOption) FloatValue() float64 {
  119. if v, ok := o.Value.(float64); ok {
  120. return v
  121. }
  122. return 0.0
  123. }
  124. // StringValue is a utility function for casting option value to string
  125. func (o ApplicationCommandInteractionDataOption) StringValue() string {
  126. if v, ok := o.Value.(string); ok {
  127. return v
  128. }
  129. return ""
  130. }
  131. // BoolValue is a utility function for casting option value to bool
  132. func (o ApplicationCommandInteractionDataOption) BoolValue() bool {
  133. if v, ok := o.Value.(bool); ok {
  134. return v
  135. }
  136. return false
  137. }
  138. // ChannelValue is a utility function for casting option value to channel object.
  139. // s : Session object, if not nil, function additionally fetches all channel's data
  140. func (o ApplicationCommandInteractionDataOption) ChannelValue(s *Session) *Channel {
  141. chanID := o.StringValue()
  142. if chanID == "" {
  143. return nil
  144. }
  145. if s == nil {
  146. return &Channel{ID: chanID}
  147. }
  148. ch, err := s.State.Channel(chanID)
  149. if err != nil {
  150. ch, err = s.Channel(chanID)
  151. if err != nil {
  152. return &Channel{ID: chanID}
  153. }
  154. }
  155. return ch
  156. }
  157. // RoleValue is a utility function for casting option value to role object.
  158. // s : Session object, if not nil, function additionally fetches all role's data
  159. func (o ApplicationCommandInteractionDataOption) RoleValue(s *Session, gID string) *Role {
  160. roleID := o.StringValue()
  161. if roleID == "" {
  162. return nil
  163. }
  164. if s == nil || gID == "" {
  165. return &Role{ID: roleID}
  166. }
  167. r, err := s.State.Role(roleID, gID)
  168. if err != nil {
  169. roles, err := s.GuildRoles(gID)
  170. if err == nil {
  171. for _, r = range roles {
  172. if r.ID == roleID {
  173. return r
  174. }
  175. }
  176. }
  177. return &Role{ID: roleID}
  178. }
  179. return r
  180. }
  181. // UserValue is a utility function for casting option value to user object.
  182. // s : Session object, if not nil, function additionally fetches all user's data
  183. func (o ApplicationCommandInteractionDataOption) UserValue(s *Session) *User {
  184. userID := o.StringValue()
  185. if userID == "" {
  186. return nil
  187. }
  188. if s == nil {
  189. return &User{ID: userID}
  190. }
  191. u, err := s.User(userID)
  192. if err != nil {
  193. return &User{ID: userID}
  194. }
  195. return u
  196. }
  197. // InteractionResponseType is type of interaction response.
  198. type InteractionResponseType uint8
  199. // Interaction response types.
  200. const (
  201. // InteractionResponsePong is for ACK ping event.
  202. InteractionResponsePong InteractionResponseType = 1
  203. // InteractionResponseChannelMessageWithSource is for responding with a message, showing the user's input.
  204. InteractionResponseChannelMessageWithSource InteractionResponseType = 4
  205. // InteractionResponseDeferredChannelMessageWithSource acknowledges that the event was received, and that a follow-up will come later.
  206. InteractionResponseDeferredChannelMessageWithSource InteractionResponseType = 5
  207. )
  208. // InteractionResponse represents a response for an interaction event.
  209. type InteractionResponse struct {
  210. Type InteractionResponseType `json:"type,omitempty"`
  211. Data *InteractionApplicationCommandResponseData `json:"data,omitempty"`
  212. }
  213. // InteractionApplicationCommandResponseData is response data for a slash command interaction.
  214. type InteractionApplicationCommandResponseData struct {
  215. TTS bool `json:"tts,omitempty"`
  216. Content string `json:"content,omitempty"`
  217. Embeds []*MessageEmbed `json:"embeds,omitempty"`
  218. AllowedMentions *MessageAllowedMentions `json:"allowed_mentions,omitempty"`
  219. // NOTE: Undocumented feature, be careful with it.
  220. Flags uint64 `json:"flags,omitempty"`
  221. }
  222. // VerifyInteraction implements message verification of the discord interactions api
  223. // signing algorithm, as documented here:
  224. // https://discord.com/developers/docs/interactions/slash-commands#security-and-authorization
  225. func VerifyInteraction(r *http.Request, key ed25519.PublicKey) bool {
  226. var msg bytes.Buffer
  227. signature := r.Header.Get("X-Signature-Ed25519")
  228. if signature == "" {
  229. return false
  230. }
  231. sig, err := hex.DecodeString(signature)
  232. if err != nil {
  233. return false
  234. }
  235. if len(sig) != ed25519.SignatureSize {
  236. return false
  237. }
  238. timestamp := r.Header.Get("X-Signature-Timestamp")
  239. if timestamp == "" {
  240. return false
  241. }
  242. msg.WriteString(timestamp)
  243. defer r.Body.Close()
  244. var body bytes.Buffer
  245. // at the end of the function, copy the original body back into the request
  246. defer func() {
  247. r.Body = ioutil.NopCloser(&body)
  248. }()
  249. // copy body into buffers
  250. _, err = io.Copy(&msg, io.TeeReader(r.Body, &body))
  251. if err != nil {
  252. return false
  253. }
  254. return ed25519.Verify(key, msg.Bytes(), sig)
  255. }