forked from bwmarrin/discordgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
components.go
97 lines (80 loc) · 2.09 KB
/
components.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package discordgo
import (
"encoding/json"
)
// ComponentType is type of component.
type ComponentType uint
// Component types.
const (
ActionsRowComponent ComponentType = iota + 1
ButtonComponent
)
// Component is a base interface for all components
type Component interface {
json.Marshaler
Type() ComponentType
}
// ActionsRow is a container for components within one row.
type ActionsRow struct {
Components []Component `json:"components"`
}
func (r ActionsRow) MarshalJSON() ([]byte, error) {
type actionRow ActionsRow
return json.Marshal(struct {
actionRow
Type ComponentType `json:"type"`
}{
actionRow: actionRow(r),
Type: r.Type(),
})
}
func (r ActionsRow) Type() ComponentType {
return ActionsRowComponent
}
// ButtonStyle is style of button.
type ButtonStyle uint
// Button styles.
const (
// PrimaryButton is a button with blurple color.
PrimaryButton ButtonStyle = iota + 1
// SecondaryButton is a button with grey color.
SecondaryButton
// SuccessButton is a button with green color.
SuccessButton
// DangerButton is a button with red color.
DangerButton
// LinkButton is a special type of button which navigates to a URL. Has grey color.
LinkButton
)
// ButtonEmoji represents button emoji, if it does have one.
type ButtonEmoji 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"`
// NOTE: Only button with LinkButton style can have link. Also, Link is mutually exclusive with CustomID.
Link 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 (b Button) Type() ComponentType {
return ButtonComponent
}