123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- package discordgo
- import (
- "encoding/json"
- "fmt"
- )
- type ComponentType uint
- const (
- ActionsRowComponent ComponentType = 1
- ButtonComponent ComponentType = 2
- SelectMenuComponent ComponentType = 3
- TextInputComponent ComponentType = 4
- )
- type MessageComponent interface {
- json.Marshaler
- Type() ComponentType
- }
- type unmarshalableMessageComponent struct {
- MessageComponent
- }
- func (umc *unmarshalableMessageComponent) UnmarshalJSON(src []byte) error {
- var v struct {
- Type ComponentType `json:"type"`
- }
- err := json.Unmarshal(src, &v)
- if err != nil {
- return err
- }
- switch v.Type {
- case ActionsRowComponent:
- umc.MessageComponent = &ActionsRow{}
- case ButtonComponent:
- umc.MessageComponent = &Button{}
- case SelectMenuComponent:
- umc.MessageComponent = &SelectMenu{}
- case TextInputComponent:
- umc.MessageComponent = &TextInput{}
- default:
- return fmt.Errorf("unknown component type: %d", v.Type)
- }
- return json.Unmarshal(src, umc.MessageComponent)
- }
- func MessageComponentFromJSON(b []byte) (MessageComponent, error) {
- var u unmarshalableMessageComponent
- err := u.UnmarshalJSON(b)
- if err != nil {
- return nil, fmt.Errorf("failed to unmarshal into MessageComponent: %w", err)
- }
- return u.MessageComponent, nil
- }
- type ActionsRow struct {
- Components []MessageComponent `json:"components"`
- }
- func (r ActionsRow) MarshalJSON() ([]byte, error) {
- type actionsRow ActionsRow
- return json.Marshal(struct {
- actionsRow
- Type ComponentType `json:"type"`
- }{
- actionsRow: actionsRow(r),
- Type: r.Type(),
- })
- }
- func (r *ActionsRow) UnmarshalJSON(data []byte) error {
- var v struct {
- RawComponents []unmarshalableMessageComponent `json:"components"`
- }
- err := json.Unmarshal(data, &v)
- if err != nil {
- return err
- }
- r.Components = make([]MessageComponent, len(v.RawComponents))
- for i, v := range v.RawComponents {
- r.Components[i] = v.MessageComponent
- }
- return err
- }
- func (r ActionsRow) Type() ComponentType {
- return ActionsRowComponent
- }
- type ButtonStyle uint
- const (
-
- PrimaryButton ButtonStyle = 1
-
- SecondaryButton ButtonStyle = 2
-
- SuccessButton ButtonStyle = 3
-
- DangerButton ButtonStyle = 4
-
- LinkButton ButtonStyle = 5
- )
- type ComponentEmoji struct {
- Name string `json:"name,omitempty"`
- ID string `json:"id,omitempty"`
- Animated bool `json:"animated,omitempty"`
- }
- type Button struct {
- Label string `json:"label"`
- Style ButtonStyle `json:"style"`
- Disabled bool `json:"disabled"`
- Emoji ComponentEmoji `json:"emoji"`
-
- URL string `json:"url,omitempty"`
- CustomID string `json:"custom_id,omitempty"`
- }
- func (b Button) MarshalJSON() ([]byte, error) {
- type button Button
- if b.Style == 0 {
- b.Style = PrimaryButton
- }
- return json.Marshal(struct {
- button
- Type ComponentType `json:"type"`
- }{
- button: button(b),
- Type: b.Type(),
- })
- }
- func (Button) Type() ComponentType {
- return ButtonComponent
- }
- type SelectMenuOption struct {
- Label string `json:"label,omitempty"`
- Value string `json:"value"`
- Description string `json:"description"`
- Emoji ComponentEmoji `json:"emoji"`
-
- Default bool `json:"default"`
- }
- type SelectMenu struct {
- CustomID string `json:"custom_id,omitempty"`
-
- Placeholder string `json:"placeholder"`
-
- MinValues *int `json:"min_values,omitempty"`
-
-
- MaxValues int `json:"max_values,omitempty"`
- Options []SelectMenuOption `json:"options"`
- Disabled bool `json:"disabled"`
- }
- func (SelectMenu) Type() ComponentType {
- return SelectMenuComponent
- }
- func (m SelectMenu) MarshalJSON() ([]byte, error) {
- type selectMenu SelectMenu
- return json.Marshal(struct {
- selectMenu
- Type ComponentType `json:"type"`
- }{
- selectMenu: selectMenu(m),
- Type: m.Type(),
- })
- }
- type TextInput struct {
- CustomID string `json:"custom_id"`
- Label string `json:"label"`
- Style TextInputStyle `json:"style"`
- Placeholder string `json:"placeholder,omitempty"`
- Value string `json:"value,omitempty"`
- Required bool `json:"required,omitempty"`
- MinLength int `json:"min_length,omitempty"`
- MaxLength int `json:"max_length,omitempty"`
- }
- func (TextInput) Type() ComponentType {
- return TextInputComponent
- }
- func (m TextInput) MarshalJSON() ([]byte, error) {
- type inputText TextInput
- return json.Marshal(struct {
- inputText
- Type ComponentType `json:"type"`
- }{
- inputText: inputText(m),
- Type: m.Type(),
- })
- }
- type TextInputStyle uint
- const (
- TextInputShort TextInputStyle = 1
- TextInputParagraph TextInputStyle = 2
- )
|