interactions.go 9.3 KB

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