Skip to content

Commit 4ebe5a0

Browse files
FedorLap2006CarsonHoffmanplally
authored
Selects component (bwmarrin#954)
* Interactions: the Buttons (bwmarrin#933) * Interactions: buttons * Doc fix * Gofmt fix * Fix typo * Remaking interaction data into interface * Godoc fix * Gofmt fix * Godoc fix * InteractionData helper functions and some fixes in slash commands example * Fix components example * Yet another fix of components example * Fix interaction unmarshaling * Gofmt fix * Godoc fix * Gofmt fix * Corrected naming and docs * Rolled back API version * Requested fixes * Added support of components to webhook and regular messages * Fix components unmarshaling * Godoc fix * Requested fixes * Fixed unmarshaling issues * Components example: cleanup * Added components tracking to state * Requested fixes * Renaming fix * Remove more named returns * Minor English fixes Co-authored-by: Carson Hoffman <c@rsonhoffman.com> * Doc fix * Gofmt fix * Fix typo * Remaking interaction data into interface * Godoc fix * Gofmt fix * Godoc fix * InteractionData helper functions and some fixes in slash commands example * Fix components example * Yet another fix of components example * Fix interaction unmarshaling * Godoc fix * Gofmt fix * Corrected naming and docs * Rolled back API version * Requested fixes * Added support of components to webhook and regular messages * Interactions: select menus * Example fix * Merge fix * Some fixes * Added missing documentation * Fix components unmarshaling * Godoc fix * Requested fixes * Fixed unmarshaling issues * Components example: cleanup * Gofmt fix * Godoc fix * URL field renaming fix * Added flags to followups * Updated components example * Fixed typo in components example * Merge fix * Improve handling of invalid interaction situations * support allowing webhook edits with files, and responding to interactions with files (bwmarrin#931) * allow files in webhook message edits * add Files to WebhookEdit struct * move the construction of the multipart body for files into a shared function * allow interaction responses to have files * go fmt * fix err shadowing * document MakeFilesBody * rename MakeFilesBody -> EncodeWithFiles. fix InteractionRespond responding twice * use resp in InteractionRespond files, add basic-command-with-files example command * import strings and go fmt * EncodeWithFiles -> MultiPartBodyWithJSON * go fmt * fix example for slash_commands * move files to responsedata * Merge fixes * Fixed rebase consequences Co-authored-by: Carson Hoffman <c@rsonhoffman.com> Co-authored-by: plally <pierce@vulpes.dev>
1 parent ab47f12 commit 4ebe5a0

File tree

6 files changed

+453
-96
lines changed

6 files changed

+453
-96
lines changed

components.go

+50-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type ComponentType uint
1111
const (
1212
ActionsRowComponent ComponentType = 1
1313
ButtonComponent ComponentType = 2
14+
SelectMenuComponent ComponentType = 3
1415
)
1516

1617
// MessageComponent is a base interface for all message components.
@@ -82,6 +83,7 @@ func (r *ActionsRow) UnmarshalJSON(data []byte) error {
8283
for i, v := range v.RawComponents {
8384
r.Components[i] = v.MessageComponent
8485
}
86+
8587
return err
8688
}
8789

@@ -107,19 +109,19 @@ const (
107109
LinkButton ButtonStyle = 5
108110
)
109111

110-
// ButtonEmoji represents button emoji, if it does have one.
111-
type ButtonEmoji struct {
112+
// ComponentEmoji represents button emoji, if it does have one.
113+
type ComponentEmoji struct {
112114
Name string `json:"name,omitempty"`
113115
ID string `json:"id,omitempty"`
114116
Animated bool `json:"animated,omitempty"`
115117
}
116118

117119
// Button represents button component.
118120
type Button struct {
119-
Label string `json:"label"`
120-
Style ButtonStyle `json:"style"`
121-
Disabled bool `json:"disabled"`
122-
Emoji ButtonEmoji `json:"emoji"`
121+
Label string `json:"label"`
122+
Style ButtonStyle `json:"style"`
123+
Disabled bool `json:"disabled"`
124+
Emoji ComponentEmoji `json:"emoji"`
123125

124126
// NOTE: Only button with LinkButton style can have link. Also, URL is mutually exclusive with CustomID.
125127
URL string `json:"url,omitempty"`
@@ -144,6 +146,47 @@ func (b Button) MarshalJSON() ([]byte, error) {
144146
}
145147

146148
// Type is a method to get the type of a component.
147-
func (b Button) Type() ComponentType {
149+
func (Button) Type() ComponentType {
148150
return ButtonComponent
149151
}
152+
153+
// SelectMenuOption represents an option for a select menu.
154+
type SelectMenuOption struct {
155+
Label string `json:"label,omitempty"`
156+
Value string `json:"value"`
157+
Description string `json:"description"`
158+
Emoji ComponentEmoji `json:"emoji"`
159+
// Determines whenever option is selected by default or not.
160+
Default bool `json:"default"`
161+
}
162+
163+
// SelectMenu represents select menu component.
164+
type SelectMenu struct {
165+
CustomID string `json:"custom_id,omitempty"`
166+
// The text which will be shown in the menu if there's no default options or all options was deselected and component was closed.
167+
Placeholder string `json:"placeholder"`
168+
// This value determines the minimal amount of selected items in the menu.
169+
MinValues int `json:"min_values,omitempty"`
170+
// This value determines the maximal amount of selected items in the menu.
171+
// If MaxValues or MinValues are greater than one then the user can select multiple items in the component.
172+
MaxValues int `json:"max_values,omitempty"`
173+
Options []SelectMenuOption `json:"options"`
174+
}
175+
176+
// Type is a method to get the type of a component.
177+
func (SelectMenu) Type() ComponentType {
178+
return SelectMenuComponent
179+
}
180+
181+
// MarshalJSON is a method for marshaling SelectMenu to a JSON object.
182+
func (m SelectMenu) MarshalJSON() ([]byte, error) {
183+
type selectMenu SelectMenu
184+
185+
return json.Marshal(struct {
186+
selectMenu
187+
Type ComponentType `json:"type"`
188+
}{
189+
selectMenu: selectMenu(m),
190+
Type: m.Type(),
191+
})
192+
}

0 commit comments

Comments
 (0)