From f7880b91ee2fdb86e12941f52680d0d1d35830bc Mon Sep 17 00:00:00 2001 From: starshines Date: Tue, 10 Aug 2021 23:02:30 +0200 Subject: [PATCH] discord: Add select component type (#260) --- discord/component.go | 37 +++++++++++++++++++++++++++++++++++++ gateway/events.go | 5 ++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/discord/component.go b/discord/component.go index 7e027cd..258109c 100644 --- a/discord/component.go +++ b/discord/component.go @@ -14,6 +14,7 @@ type ComponentType uint const ( ActionRowComponentType ComponentType = iota + 1 ButtonComponentType + SelectComponentType ) // ComponentWrap wraps Component for the purpose of JSON unmarshalling. @@ -187,6 +188,42 @@ func (b ButtonComponent) MarshalJSON() ([]byte, error) { }) } +// Select is a clickable button that may be added to an interaction response. +type SelectComponent struct { + CustomID string `json:"custom_id"` + Options []SelectComponentOption `json:"options"` + Placeholder string `json:"placeholder,omitempty"` + MinValues int `json:"min_values,omitempty"` + MaxValues int `json:"max_values,omitempty"` + Disabled bool `json:"disabled,omitempty"` +} + +type SelectComponentOption struct { + Label string `json:"label"` + Value string `json:"value"` + Description string `json:"description,omitempty"` + Emoji *ButtonEmoji `json:"emoji,omitempty"` + Default bool `json:"default,omitempty"` +} + +// Type implements the Component interface. +func (SelectComponent) Type() ComponentType { + return SelectComponentType +} + +// MarshalJSON marshals the select in the format Discord expects. +func (s SelectComponent) MarshalJSON() ([]byte, error) { + type selectComponent SelectComponent + + return json.Marshal(struct { + selectComponent + Type ComponentType `json:"type"` + }{ + selectComponent: selectComponent(s), + Type: SelectComponentType, + }) +} + // UnknownComponent is reserved for components with unknown or not yet // implemented components types. type UnknownComponent struct { diff --git a/gateway/events.go b/gateway/events.go index 324e437..a6c9210 100644 --- a/gateway/events.go +++ b/gateway/events.go @@ -435,9 +435,12 @@ type InteractionData struct { Name string `json:"name"` Options []InteractionOption `json:"options"` - // Button + // Button and select CustomID string `json:"custom_id"` ComponentType discord.ComponentType `json:"component_type"` + + // Select + Values []string `json:"values"` } type InteractionOption struct {