interactions.go 8.9 KB

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