Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selects component #954

Merged
merged 40 commits into from Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
8c70cbf
Interactions: the Buttons (#933)
FedorLap2006 Jun 27, 2021
6e7c007
Doc fix
FedorLap2006 May 16, 2021
4979e3c
Gofmt fix
FedorLap2006 May 16, 2021
05b6b55
Fix typo
FedorLap2006 May 17, 2021
8738313
Remaking interaction data into interface
FedorLap2006 May 18, 2021
59bc781
Godoc fix
FedorLap2006 May 18, 2021
8299946
Gofmt fix
FedorLap2006 May 18, 2021
64e23e0
Godoc fix
FedorLap2006 May 18, 2021
b51ca56
InteractionData helper functions and some fixes in slash commands exa…
FedorLap2006 May 18, 2021
2153119
Fix components example
FedorLap2006 May 18, 2021
274b9ec
Yet another fix of components example
FedorLap2006 May 18, 2021
c2751aa
Fix interaction unmarshaling
FedorLap2006 May 21, 2021
641b43d
Godoc fix
FedorLap2006 May 21, 2021
b54bdb4
Gofmt fix
FedorLap2006 May 23, 2021
81384d5
Corrected naming and docs
FedorLap2006 May 26, 2021
5cb816c
Rolled back API version
FedorLap2006 May 30, 2021
5b150bd
Requested fixes
FedorLap2006 May 30, 2021
c0d6117
Added support of components to webhook and regular messages
FedorLap2006 Jun 15, 2021
9bd0fe8
Interactions: select menus
FedorLap2006 Jun 17, 2021
789aa19
Example fix
FedorLap2006 Jun 17, 2021
91f8d0a
Merge fix
FedorLap2006 Jun 17, 2021
462151e
Some fixes
FedorLap2006 Jun 17, 2021
2ed8862
Added missing documentation
FedorLap2006 Jun 17, 2021
515f1c9
Fix components unmarshaling
FedorLap2006 Jun 18, 2021
c4079fe
Godoc fix
FedorLap2006 Jun 18, 2021
fd4ad15
Requested fixes
FedorLap2006 Jun 19, 2021
fec9fdf
Fixed unmarshaling issues
FedorLap2006 Jun 22, 2021
9ba0bf7
Components example: cleanup
FedorLap2006 Jun 22, 2021
8c0d715
Gofmt fix
FedorLap2006 Jun 30, 2021
0949c93
Godoc fix
FedorLap2006 Jun 30, 2021
d0a2096
URL field renaming fix
FedorLap2006 Jun 30, 2021
9b0225f
Added flags to followups
FedorLap2006 Jul 26, 2021
bdb76ad
Updated components example
FedorLap2006 Jul 26, 2021
213ca67
Fixed typo in components example
FedorLap2006 Jul 26, 2021
464cb59
Merge fix
FedorLap2006 Jul 30, 2021
6450f39
Improve handling of invalid interaction situations
CarsonHoffman Jul 25, 2021
c6f37ad
support allowing webhook edits with files, and responding to interact…
plally Jul 30, 2021
4ff812c
Merge branch 'master' into selects-component
FedorLap2006 Jul 30, 2021
d44abc7
Merge fixes
FedorLap2006 Jul 30, 2021
072a9ff
Fixed rebase consequences
FedorLap2006 Jul 31, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 50 additions & 7 deletions components.go
Expand Up @@ -11,6 +11,7 @@ type ComponentType uint
const (
ActionsRowComponent ComponentType = 1
ButtonComponent ComponentType = 2
SelectMenuComponent ComponentType = 3
)

// MessageComponent is a base interface for all message components.
Expand Down Expand Up @@ -82,6 +83,7 @@ func (r *ActionsRow) UnmarshalJSON(data []byte) error {
for i, v := range v.RawComponents {
r.Components[i] = v.MessageComponent
}

return err
}

Expand All @@ -107,19 +109,19 @@ const (
LinkButton ButtonStyle = 5
)

// ButtonEmoji represents button emoji, if it does have one.
type ButtonEmoji struct {
// ComponentEmoji represents button emoji, if it does have one.
type ComponentEmoji struct {
Name string `json:"name,omitempty"`
ID string `json:"id,omitempty"`
Animated bool `json:"animated,omitempty"`
}

// Button represents button component.
type Button struct {
Label string `json:"label"`
Style ButtonStyle `json:"style"`
Disabled bool `json:"disabled"`
Emoji ButtonEmoji `json:"emoji"`
Label string `json:"label"`
Style ButtonStyle `json:"style"`
Disabled bool `json:"disabled"`
Emoji ComponentEmoji `json:"emoji"`

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

// Type is a method to get the type of a component.
func (b Button) Type() ComponentType {
func (Button) Type() ComponentType {
return ButtonComponent
}

// SelectMenuOption represents an option for a select menu.
type SelectMenuOption struct {
Label string `json:"label,omitempty"`
Value string `json:"value"`
Description string `json:"description"`
Emoji ComponentEmoji `json:"emoji"`
// Determines whenever option is selected by default or not.
Default bool `json:"default"`
}

// SelectMenu represents select menu component.
type SelectMenu struct {
CustomID string `json:"custom_id,omitempty"`
// The text which will be shown in the menu if there's no default options or all options was deselected and component was closed.
Placeholder string `json:"placeholder"`
// This value determines the minimal amount of selected items in the menu.
MinValues int `json:"min_values,omitempty"`
// This value determines the maximal amount of selected items in the menu.
// If MaxValues or MinValues are greater than one then the user can select multiple items in the component.
MaxValues int `json:"max_values,omitempty"`
Options []SelectMenuOption `json:"options"`
}

// Type is a method to get the type of a component.
func (SelectMenu) Type() ComponentType {
return SelectMenuComponent
}

// MarshalJSON is a method for marshaling SelectMenu to a JSON object.
func (m SelectMenu) MarshalJSON() ([]byte, error) {
type selectMenu SelectMenu

return json.Marshal(struct {
selectMenu
Type ComponentType `json:"type"`
}{
selectMenu: selectMenu(m),
Type: m.Type(),
})
}