interactions.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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(iota + 1)
  27. ApplicationCommandOptionSubCommandGroup
  28. ApplicationCommandOptionString
  29. ApplicationCommandOptionInteger
  30. ApplicationCommandOptionBoolean
  31. ApplicationCommandOptionUser
  32. ApplicationCommandOptionChannel
  33. ApplicationCommandOptionRole
  34. ApplicationCommandOptionMentionable
  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(iota + 1)
  58. InteractionApplicationCommand
  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. Options []*ApplicationCommandInteractionDataOption `json:"options"`
  85. }
  86. // ApplicationCommandInteractionDataOption represents an option of a slash command.
  87. type ApplicationCommandInteractionDataOption struct {
  88. Name string `json:"name"`
  89. // NOTE: Contains the value specified by InteractionType.
  90. Value interface{} `json:"value,omitempty"`
  91. Options []*ApplicationCommandInteractionDataOption `json:"options,omitempty"`
  92. }
  93. // IntValue is a utility function for casting option value to integer
  94. func (o ApplicationCommandInteractionDataOption) IntValue() int64 {
  95. if v, ok := o.Value.(float64); ok {
  96. return int64(v)
  97. }
  98. return 0
  99. }
  100. // UintValue is a utility function for casting option value to unsigned integer
  101. func (o ApplicationCommandInteractionDataOption) UintValue() uint64 {
  102. if v, ok := o.Value.(float64); ok {
  103. return uint64(v)
  104. }
  105. return 0
  106. }
  107. // FloatValue is a utility function for casting option value to float
  108. func (o ApplicationCommandInteractionDataOption) FloatValue() float64 {
  109. if v, ok := o.Value.(float64); ok {
  110. return v
  111. }
  112. return 0.0
  113. }
  114. // StringValue is a utility function for casting option value to string
  115. func (o ApplicationCommandInteractionDataOption) StringValue() string {
  116. if v, ok := o.Value.(string); ok {
  117. return v
  118. }
  119. return ""
  120. }
  121. // BoolValue is a utility function for casting option value to bool
  122. func (o ApplicationCommandInteractionDataOption) BoolValue() bool {
  123. if v, ok := o.Value.(bool); ok {
  124. return v
  125. }
  126. return false
  127. }
  128. // ChannelValue is a utility function for casting option value to channel object.
  129. // s : Session object, if not nil, function additionally fetches all channel's data
  130. func (o ApplicationCommandInteractionDataOption) ChannelValue(s *Session) *Channel {
  131. chanID := o.StringValue()
  132. if chanID == "" {
  133. return nil
  134. }
  135. if s == nil {
  136. return &Channel{ID: chanID}
  137. }
  138. ch, err := s.State.Channel(chanID)
  139. if err != nil {
  140. ch, err = s.Channel(chanID)
  141. if err != nil {
  142. return &Channel{ID: chanID}
  143. }
  144. }
  145. return ch
  146. }
  147. // RoleValue is a utility function for casting option value to role object.
  148. // s : Session object, if not nil, function additionally fetches all role's data
  149. func (o ApplicationCommandInteractionDataOption) RoleValue(s *Session, gID string) *Role {
  150. roleID := o.StringValue()
  151. if roleID == "" {
  152. return nil
  153. }
  154. if s == nil || gID == "" {
  155. return &Role{ID: roleID}
  156. }
  157. r, err := s.State.Role(roleID, gID)
  158. if err != nil {
  159. roles, err := s.GuildRoles(gID)
  160. if err == nil {
  161. for _, r = range roles {
  162. if r.ID == roleID {
  163. return r
  164. }
  165. }
  166. }
  167. return &Role{ID: roleID}
  168. }
  169. return r
  170. }
  171. // UserValue is a utility function for casting option value to user object.
  172. // s : Session object, if not nil, function additionally fetches all user's data
  173. func (o ApplicationCommandInteractionDataOption) UserValue(s *Session) *User {
  174. userID := o.StringValue()
  175. if userID == "" {
  176. return nil
  177. }
  178. if s == nil {
  179. return &User{ID: userID}
  180. }
  181. u, err := s.User(userID)
  182. if err != nil {
  183. return &User{ID: userID}
  184. }
  185. return u
  186. }
  187. // InteractionResponseType is type of interaction response.
  188. type InteractionResponseType uint8
  189. // Interaction response types.
  190. const (
  191. // InteractionResponsePong is for ACK ping event.
  192. InteractionResponsePong InteractionResponseType = 1
  193. // InteractionResponseChannelMessageWithSource is for responding with a message, showing the user's input.
  194. InteractionResponseChannelMessageWithSource InteractionResponseType = 4
  195. // InteractionResponseDeferredChannelMessageWithSource acknowledges that the event was received, and that a follow-up will come later.
  196. InteractionResponseDeferredChannelMessageWithSource InteractionResponseType = 5
  197. )
  198. // InteractionResponse represents a response for an interaction event.
  199. type InteractionResponse struct {
  200. Type InteractionResponseType `json:"type,omitempty"`
  201. Data *InteractionApplicationCommandResponseData `json:"data,omitempty"`
  202. }
  203. // InteractionApplicationCommandResponseData is response data for a slash command interaction.
  204. type InteractionApplicationCommandResponseData struct {
  205. TTS bool `json:"tts,omitempty"`
  206. Content string `json:"content,omitempty"`
  207. Embeds []*MessageEmbed `json:"embeds,omitempty"`
  208. AllowedMentions *MessageAllowedMentions `json:"allowed_mentions,omitempty"`
  209. // NOTE: Undocumented feature, be careful with it.
  210. Flags uint64 `json:"flags,omitempty"`
  211. }
  212. // VerifyInteraction implements message verification of the discord interactions api
  213. // signing algorithm, as documented here:
  214. // https://discord.com/developers/docs/interactions/slash-commands#security-and-authorization
  215. func VerifyInteraction(r *http.Request, key ed25519.PublicKey) bool {
  216. var msg bytes.Buffer
  217. signature := r.Header.Get("X-Signature-Ed25519")
  218. if signature == "" {
  219. return false
  220. }
  221. sig, err := hex.DecodeString(signature)
  222. if err != nil {
  223. return false
  224. }
  225. if len(sig) != ed25519.SignatureSize {
  226. return false
  227. }
  228. timestamp := r.Header.Get("X-Signature-Timestamp")
  229. if timestamp == "" {
  230. return false
  231. }
  232. msg.WriteString(timestamp)
  233. defer r.Body.Close()
  234. var body bytes.Buffer
  235. // at the end of the function, copy the original body back into the request
  236. defer func() {
  237. r.Body = ioutil.NopCloser(&body)
  238. }()
  239. // copy body into buffers
  240. _, err = io.Copy(&msg, io.TeeReader(r.Body, &body))
  241. if err != nil {
  242. return false
  243. }
  244. return ed25519.Verify(key, msg.Bytes(), sig)
  245. }