1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-11-27 22:56:13 +00:00

api: Implement autocomplete components

This commit is contained in:
Hamza Ali 2021-10-02 01:48:17 +07:00 committed by diamondburned
parent 028a341597
commit 6fba21d704
No known key found for this signature in database
GPG key ID: D78C4471CE776659
3 changed files with 47 additions and 7 deletions

View file

@ -24,6 +24,7 @@ const (
DeferredMessageInteractionWithSource
DeferredMessageUpdate
UpdateMessage
AutocompleteResult
)
// InteractionResponseFlags implements flags for an
@ -72,6 +73,11 @@ type InteractionResponseData struct {
// 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:"-"`
// Choices are the results to display on autocomplete interaction events.
//
// During all other events, this should not be provided.
Choices *[]AutocompleteChoice `json:"choices"`
}
// NeedsMultipart returns true if the InteractionResponseData has files.
@ -83,6 +89,13 @@ func (d InteractionResponseData) WriteMultipart(body *multipart.Writer) error {
return sendpart.Write(body, d, d.Files)
}
// AutocompleteChoice is the choice in ApplicationCommandAutocompleteResult in
// the official documentation.
type AutocompleteChoice struct {
Name string `json:"name"`
Value string `json:"value"`
}
// RespondInteraction responds to an incoming interaction. It is also known as
// an "interaction callback".
func (c *Client) RespondInteraction(
@ -129,7 +142,7 @@ func (c *Client) RespondInteraction(
}
}
var URL = EndpointInteractions + id.String() + "/" + token + "/callback"
URL := EndpointInteractions + id.String() + "/" + token + "/callback"
return sendpart.POST(c.Client, resp, nil, URL)
}

View file

@ -181,12 +181,15 @@ func (c Command) CreatedAt() time.Time {
}
type CommandOption struct {
Type CommandOptionType `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
Required bool `json:"required"`
Choices []CommandOptionChoice `json:"choices,omitempty"`
Options []CommandOption `json:"options,omitempty"`
Type CommandOptionType `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
Required bool `json:"required"`
// Choices cannot be present if Autocomplete is true.
Choices []CommandOptionChoice `json:"choices,omitempty"`
Autocomplete bool `json:"autocomplete"`
Options []CommandOption `json:"options,omitempty"`
// If this option is a channel type, the channels shown will be restricted to these types
ChannelTypes []ChannelType `json:"-"`

View file

@ -45,6 +45,8 @@ func (i *Interaction) UnmarshalJSON(p []byte) error {
i.Data = &ComponentInteractionData{}
case CommandInteraction:
i.Data = &CommandInteractionData{}
case AutocompleteInteraction:
i.Data = &AutocompleteInteractionData{}
default:
i.Data = &UnknownInteractionData{typ: v.Type}
}
@ -58,6 +60,7 @@ const (
PingInteraction InteractionType = iota + 1
CommandInteraction
ComponentInteraction
AutocompleteInteraction
)
// InteractionData holds the data of an interaction.
@ -87,6 +90,27 @@ func (*CommandInteractionData) Type() InteractionType {
return CommandInteraction
}
type AutocompleteInteractionData struct {
ID CommandID `json:"id"`
// Name of command autocomplete is triggered for.
Name string `json:"name"`
CommandType CommandType `json:"type"`
Version string `json:"version"`
Options []AutocompleteCommandOption `json:"options"`
}
type AutocompleteCommandOption struct {
Type CommandOptionType `json:"type"`
Name string `json:"name"`
Value string `json:"value"`
Focused bool `json:"focused"`
}
func (*AutocompleteInteractionData) Type() InteractionType {
return AutocompleteInteraction
}
type UnknownInteractionData struct {
json.Raw
typ InteractionType