Skip to content

Commit

Permalink
Merge branch 'master' into context-menus
Browse files Browse the repository at this point in the history
  • Loading branch information
FedorLap2006 committed Aug 10, 2021
2 parents e13da20 + 460b371 commit f6231ab
Show file tree
Hide file tree
Showing 6 changed files with 457 additions and 99 deletions.
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(),
})
}

0 comments on commit f6231ab

Please sign in to comment.