2021-01-27 17:43:38 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-06-09 17:29:03 +00:00
|
|
|
"mime/multipart"
|
2021-08-03 18:44:20 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-06-09 17:29:03 +00:00
|
|
|
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v3/utils/json/option"
|
2021-06-09 17:29:03 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/utils/sendpart"
|
2021-01-27 17:43:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var EndpointInteractions = Endpoint + "interactions/"
|
|
|
|
|
|
|
|
type InteractionResponseType uint
|
|
|
|
|
2021-08-03 18:44:20 +00:00
|
|
|
// https://discord.com/developers/docs/interactions/slash-commands#interaction-response-object-interaction-callback-type
|
2021-01-27 17:43:38 +00:00
|
|
|
const (
|
|
|
|
PongInteraction InteractionResponseType = iota + 1
|
2021-05-30 04:28:37 +00:00
|
|
|
_
|
|
|
|
_
|
2021-01-27 17:43:38 +00:00
|
|
|
MessageInteractionWithSource
|
2021-05-30 04:28:37 +00:00
|
|
|
DeferredMessageInteractionWithSource
|
2021-05-12 05:36:03 +00:00
|
|
|
DeferredMessageUpdate
|
|
|
|
UpdateMessage
|
2021-10-01 18:48:17 +00:00
|
|
|
AutocompleteResult
|
2022-02-14 03:15:28 +00:00
|
|
|
ModalResponse
|
2021-01-27 17:43:38 +00:00
|
|
|
)
|
|
|
|
|
2021-08-03 18:44:20 +00:00
|
|
|
// InteractionResponseFlags implements flags for an
|
|
|
|
// InteractionApplicationCommandCallbackData.
|
|
|
|
//
|
|
|
|
// https://discord.com/developers/docs/interactions/slash-commands#interaction-response-object-interaction-application-command-callback-data-flags
|
|
|
|
type InteractionResponseFlags uint
|
|
|
|
|
|
|
|
const EphemeralResponse InteractionResponseFlags = 64
|
|
|
|
|
2021-01-27 17:43:38 +00:00
|
|
|
type InteractionResponse struct {
|
|
|
|
Type InteractionResponseType `json:"type"`
|
|
|
|
Data *InteractionResponseData `json:"data,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-08-03 18:44:20 +00:00
|
|
|
// NeedsMultipart returns true if the InteractionResponse has files.
|
|
|
|
func (resp InteractionResponse) NeedsMultipart() bool {
|
|
|
|
return resp.Data != nil && resp.Data.NeedsMultipart()
|
|
|
|
}
|
2021-06-12 08:49:43 +00:00
|
|
|
|
2021-08-03 18:44:20 +00:00
|
|
|
func (resp InteractionResponse) WriteMultipart(body *multipart.Writer) error {
|
|
|
|
return sendpart.Write(body, resp, resp.Data.Files)
|
|
|
|
}
|
2021-06-12 08:49:43 +00:00
|
|
|
|
2021-01-27 17:43:38 +00:00
|
|
|
// InteractionResponseData is InteractionApplicationCommandCallbackData in the
|
|
|
|
// official documentation.
|
|
|
|
type InteractionResponseData struct {
|
2021-08-03 18:44:20 +00:00
|
|
|
// Content are the message contents (up to 2000 characters).
|
|
|
|
//
|
|
|
|
// Required: one of content, file, embeds
|
2021-08-08 20:20:54 +00:00
|
|
|
Content option.NullableString `json:"content,omitempty"`
|
2021-08-03 18:44:20 +00:00
|
|
|
// TTS is true if this is a TTS message.
|
|
|
|
TTS bool `json:"tts,omitempty"`
|
|
|
|
// Embeds contains embedded rich content.
|
|
|
|
//
|
|
|
|
// Required: one of content, file, embeds
|
2021-08-08 20:20:54 +00:00
|
|
|
Embeds *[]discord.Embed `json:"embeds,omitempty"`
|
2021-08-03 18:44:20 +00:00
|
|
|
// Components is the list of components (such as buttons) to be attached to
|
|
|
|
// the message.
|
2021-10-10 22:44:31 +00:00
|
|
|
Components *discord.ContainerComponents `json:"components,omitempty"`
|
2021-08-03 18:44:20 +00:00
|
|
|
// AllowedMentions are the allowed mentions for the message.
|
|
|
|
AllowedMentions *AllowedMentions `json:"allowed_mentions,omitempty"`
|
|
|
|
// Flags are the interaction application command callback data flags.
|
|
|
|
Flags InteractionResponseFlags `json:"flags,omitempty"`
|
|
|
|
|
|
|
|
// Files represents a list of files to upload. This will not be
|
|
|
|
// JSON-encoded and will only be available through WriteMultipart.
|
|
|
|
Files []sendpart.File `json:"-"`
|
2021-10-01 18:48:17 +00:00
|
|
|
|
|
|
|
// Choices are the results to display on autocomplete interaction events.
|
|
|
|
//
|
|
|
|
// During all other events, this should not be provided.
|
|
|
|
Choices *[]AutocompleteChoice `json:"choices"`
|
2022-02-14 03:15:28 +00:00
|
|
|
|
|
|
|
// CustomID used with the modal
|
|
|
|
CustomID option.NullableString `json:"custom_id,omitempty"`
|
|
|
|
// Title is the heading of the modal window
|
|
|
|
Title option.NullableString `json:"title,omitempty"`
|
2021-08-03 18:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NeedsMultipart returns true if the InteractionResponseData has files.
|
|
|
|
func (d InteractionResponseData) NeedsMultipart() bool {
|
|
|
|
return len(d.Files) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d InteractionResponseData) WriteMultipart(body *multipart.Writer) error {
|
|
|
|
return sendpart.Write(body, d, d.Files)
|
2021-01-27 17:43:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 18:48:17 +00:00
|
|
|
// AutocompleteChoice is the choice in ApplicationCommandAutocompleteResult in
|
|
|
|
// the official documentation.
|
|
|
|
type AutocompleteChoice struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
}
|
|
|
|
|
2021-01-27 17:43:38 +00:00
|
|
|
// RespondInteraction responds to an incoming interaction. It is also known as
|
|
|
|
// an "interaction callback".
|
|
|
|
func (c *Client) RespondInteraction(
|
2021-06-09 17:29:03 +00:00
|
|
|
id discord.InteractionID, token string, resp InteractionResponse) error {
|
2021-08-03 18:44:20 +00:00
|
|
|
|
2021-08-08 20:20:54 +00:00
|
|
|
if resp.Data != nil {
|
2022-01-03 21:38:38 +00:00
|
|
|
switch resp.Type {
|
|
|
|
case MessageInteractionWithSource:
|
2021-08-08 20:20:54 +00:00
|
|
|
// A new message is being created, make sure none of the fields
|
|
|
|
// are null or empty.
|
|
|
|
if (resp.Data.Content == nil || resp.Data.Content.Val == "") &&
|
|
|
|
(resp.Data.Embeds == nil || len(*resp.Data.Embeds) == 0) &&
|
|
|
|
len(resp.Data.Files) == 0 {
|
|
|
|
return ErrEmptyMessage
|
|
|
|
}
|
2022-01-03 21:38:38 +00:00
|
|
|
case UpdateMessage:
|
2021-08-08 20:20:54 +00:00
|
|
|
// A component is being updated. We therefore don't know what
|
|
|
|
// fields are filled. The only thing we can check is if content,
|
|
|
|
// embeds and files are null.
|
|
|
|
if (resp.Data.Content != nil && !resp.Data.Content.Init) &&
|
|
|
|
(resp.Data.Embeds != nil && *resp.Data.Embeds == nil) && len(resp.Data.Files) == 0 {
|
|
|
|
return ErrEmptyMessage
|
|
|
|
}
|
2021-08-03 18:44:20 +00:00
|
|
|
}
|
|
|
|
|
2021-08-08 20:20:54 +00:00
|
|
|
if resp.Data.AllowedMentions != nil {
|
|
|
|
if err := resp.Data.AllowedMentions.Verify(); err != nil {
|
|
|
|
return errors.Wrap(err, "allowedMentions error")
|
|
|
|
}
|
2021-08-03 18:44:20 +00:00
|
|
|
}
|
2021-08-08 20:20:54 +00:00
|
|
|
|
|
|
|
if resp.Data.Embeds != nil {
|
|
|
|
sum := 0
|
|
|
|
for i, embed := range *resp.Data.Embeds {
|
|
|
|
if err := embed.Validate(); err != nil {
|
|
|
|
return errors.Wrap(err, "embed error at "+strconv.Itoa(i))
|
|
|
|
}
|
|
|
|
sum += embed.Length()
|
|
|
|
if sum > 6000 {
|
|
|
|
return &discord.OverboundError{Count: sum, Max: 6000, Thing: "sum of all text in embeds"}
|
|
|
|
}
|
2021-08-24 18:23:49 +00:00
|
|
|
|
|
|
|
(*resp.Data.Embeds)[i] = embed // embed.Validate changes fields
|
2021-08-08 20:20:54 +00:00
|
|
|
}
|
2021-08-03 18:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 18:48:17 +00:00
|
|
|
URL := EndpointInteractions + id.String() + "/" + token + "/callback"
|
2021-06-09 17:29:03 +00:00
|
|
|
return sendpart.POST(c.Client, resp, nil, URL)
|
|
|
|
}
|
|
|
|
|
2021-08-03 18:44:20 +00:00
|
|
|
// InteractionResponse returns the initial interaction response.
|
|
|
|
func (c *Client) InteractionResponse(
|
|
|
|
appID discord.AppID, token string) (*discord.Message, error) {
|
|
|
|
|
|
|
|
var m *discord.Message
|
|
|
|
return m, c.RequestJSON(
|
|
|
|
&m, "GET",
|
|
|
|
EndpointWebhooks+appID.String()+"/"+token+"/messages/@original")
|
2021-06-09 17:29:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 18:44:20 +00:00
|
|
|
type EditInteractionResponseData struct {
|
2021-08-08 20:20:54 +00:00
|
|
|
// Content are the new message contents (up to 2000 characters).
|
2021-08-03 18:44:20 +00:00
|
|
|
Content option.NullableString `json:"content,omitempty"`
|
|
|
|
// Embeds contains embedded rich content.
|
|
|
|
Embeds *[]discord.Embed `json:"embeds,omitempty"`
|
|
|
|
// Components contains the new components to attach.
|
2021-10-10 22:44:31 +00:00
|
|
|
Components *discord.ContainerComponents `json:"components,omitempty"`
|
2021-08-08 20:20:54 +00:00
|
|
|
// AllowedMentions are the allowed mentions for the message.
|
2021-08-03 18:44:20 +00:00
|
|
|
AllowedMentions *AllowedMentions `json:"allowed_mentions,omitempty"`
|
2021-08-08 20:20:54 +00:00
|
|
|
// Attachments are the attached files to keep.
|
2021-08-03 18:44:20 +00:00
|
|
|
Attachments *[]discord.Attachment `json:"attachments,omitempty"`
|
|
|
|
|
|
|
|
// Files represents a list of files to upload. This will not be
|
|
|
|
// JSON-encoded and will only be available through WriteMultipart.
|
|
|
|
Files []sendpart.File `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NeedsMultipart returns true if the SendMessageData has files.
|
|
|
|
func (data EditInteractionResponseData) NeedsMultipart() bool {
|
|
|
|
return len(data.Files) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (data EditInteractionResponseData) WriteMultipart(body *multipart.Writer) error {
|
|
|
|
return sendpart.Write(body, data, data.Files)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EditInteractionResponse edits the initial Interaction response.
|
|
|
|
func (c *Client) EditInteractionResponse(
|
|
|
|
appID discord.AppID,
|
|
|
|
token string, data EditInteractionResponseData) (*discord.Message, error) {
|
|
|
|
|
|
|
|
if data.AllowedMentions != nil {
|
|
|
|
if err := data.AllowedMentions.Verify(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "allowedMentions error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.Embeds != nil {
|
|
|
|
sum := 0
|
2021-08-24 18:23:49 +00:00
|
|
|
for i, e := range *data.Embeds {
|
2021-08-03 18:44:20 +00:00
|
|
|
if err := e.Validate(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "embed error")
|
|
|
|
}
|
|
|
|
sum += e.Length()
|
|
|
|
if sum > 6000 {
|
|
|
|
return nil, &discord.OverboundError{Count: sum, Max: 6000, Thing: "sum of text in embeds"}
|
|
|
|
}
|
2021-08-24 18:23:49 +00:00
|
|
|
|
|
|
|
(*data.Embeds)[i] = e // e.Validate changes fields
|
2021-08-03 18:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg *discord.Message
|
|
|
|
return msg, sendpart.PATCH(c.Client, data, &msg,
|
|
|
|
EndpointWebhooks+appID.String()+"/"+token+"/messages/@original")
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteInteractionResponse deletes the initial interaction response.
|
|
|
|
func (c *Client) DeleteInteractionResponse(appID discord.AppID, token string) error {
|
|
|
|
return c.FastRequest("DELETE",
|
|
|
|
EndpointWebhooks+appID.String()+"/"+token+"/messages/@original")
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateInteractionFollowup creates a followup message for an interaction.
|
|
|
|
func (c *Client) CreateInteractionFollowup(
|
|
|
|
appID discord.AppID, token string, data InteractionResponseData) (*discord.Message, error) {
|
|
|
|
|
2021-08-08 20:20:54 +00:00
|
|
|
if (data.Content == nil || data.Content.Val == "") &&
|
|
|
|
(data.Embeds == nil || len(*data.Embeds) == 0) && len(data.Files) == 0 {
|
2021-08-03 18:44:20 +00:00
|
|
|
return nil, ErrEmptyMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.AllowedMentions != nil {
|
|
|
|
if err := data.AllowedMentions.Verify(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "allowedMentions error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-08 20:20:54 +00:00
|
|
|
if data.Embeds != nil {
|
|
|
|
sum := 0
|
|
|
|
for i, embed := range *data.Embeds {
|
|
|
|
if err := embed.Validate(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "embed error at "+strconv.Itoa(i))
|
|
|
|
}
|
|
|
|
sum += embed.Length()
|
|
|
|
if sum > 6000 {
|
|
|
|
return nil, &discord.OverboundError{Count: sum, Max: 6000, Thing: "sum of all text in embeds"}
|
|
|
|
}
|
2021-08-24 18:23:49 +00:00
|
|
|
|
|
|
|
(*data.Embeds)[i] = embed // embed.Validate changes fields
|
2021-08-03 18:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg *discord.Message
|
|
|
|
return msg, sendpart.POST(
|
2021-11-24 06:53:27 +00:00
|
|
|
c.Client, data, &msg, EndpointWebhooks+appID.String()+"/"+token+"?")
|
2021-08-03 18:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) EditInteractionFollowup(
|
|
|
|
appID discord.AppID, messageID discord.MessageID,
|
|
|
|
token string, data EditInteractionResponseData) (*discord.Message, error) {
|
|
|
|
|
|
|
|
if data.AllowedMentions != nil {
|
|
|
|
if err := data.AllowedMentions.Verify(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "allowedMentions error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.Embeds != nil {
|
|
|
|
sum := 0
|
2021-08-24 18:23:49 +00:00
|
|
|
for i, e := range *data.Embeds {
|
2021-08-03 18:44:20 +00:00
|
|
|
if err := e.Validate(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "embed error")
|
|
|
|
}
|
|
|
|
sum += e.Length()
|
|
|
|
if sum > 6000 {
|
|
|
|
return nil, &discord.OverboundError{Count: sum, Max: 6000, Thing: "sum of text in embeds"}
|
|
|
|
}
|
2021-08-24 18:23:49 +00:00
|
|
|
|
|
|
|
(*data.Embeds)[i] = e // e.Validate changes fields
|
2021-08-03 18:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg *discord.Message
|
|
|
|
return msg, sendpart.PATCH(c.Client, data, &msg,
|
|
|
|
EndpointWebhooks+appID.String()+"/"+token+"/messages/"+messageID.String())
|
|
|
|
}
|
|
|
|
|
2021-08-15 16:33:33 +00:00
|
|
|
// DeleteInteractionFollowup deletes a followup message for an interaction.
|
2021-08-03 18:44:20 +00:00
|
|
|
func (c *Client) DeleteInteractionFollowup(
|
|
|
|
appID discord.AppID, messageID discord.MessageID, token string) error {
|
|
|
|
|
|
|
|
return c.FastRequest("DELETE",
|
|
|
|
EndpointWebhooks+appID.String()+"/"+token+"/messages/"+messageID.String())
|
2021-01-27 17:43:38 +00:00
|
|
|
}
|