1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-11-28 07:11:06 +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 DeferredMessageInteractionWithSource
DeferredMessageUpdate DeferredMessageUpdate
UpdateMessage UpdateMessage
AutocompleteResult
) )
// InteractionResponseFlags implements flags for an // InteractionResponseFlags implements flags for an
@ -72,6 +73,11 @@ type InteractionResponseData struct {
// Files represents a list of files to upload. This will not be // Files represents a list of files to upload. This will not be
// JSON-encoded and will only be available through WriteMultipart. // JSON-encoded and will only be available through WriteMultipart.
Files []sendpart.File `json:"-"` 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. // 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) 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 // RespondInteraction responds to an incoming interaction. It is also known as
// an "interaction callback". // an "interaction callback".
func (c *Client) RespondInteraction( 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) return sendpart.POST(c.Client, resp, nil, URL)
} }

View file

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

View file

@ -45,6 +45,8 @@ func (i *Interaction) UnmarshalJSON(p []byte) error {
i.Data = &ComponentInteractionData{} i.Data = &ComponentInteractionData{}
case CommandInteraction: case CommandInteraction:
i.Data = &CommandInteractionData{} i.Data = &CommandInteractionData{}
case AutocompleteInteraction:
i.Data = &AutocompleteInteractionData{}
default: default:
i.Data = &UnknownInteractionData{typ: v.Type} i.Data = &UnknownInteractionData{typ: v.Type}
} }
@ -58,6 +60,7 @@ const (
PingInteraction InteractionType = iota + 1 PingInteraction InteractionType = iota + 1
CommandInteraction CommandInteraction
ComponentInteraction ComponentInteraction
AutocompleteInteraction
) )
// InteractionData holds the data of an interaction. // InteractionData holds the data of an interaction.
@ -87,6 +90,27 @@ func (*CommandInteractionData) Type() InteractionType {
return CommandInteraction 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 { type UnknownInteractionData struct {
json.Raw json.Raw
typ InteractionType typ InteractionType