-
Notifications
You must be signed in to change notification settings - Fork 0
/
components.go
154 lines (130 loc) · 3.07 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package sevcord
import "github.com/bwmarrin/discordgo"
type ButtonHandler func(ctx Ctx)
type Button struct {
Label string
Style ButtonStyle
Handler ButtonHandler
// Optional
Emoji *ComponentEmoji
URL string // Only link button can have this
Disabled bool
}
type ComponentEmoji struct {
name string // Make this the raw emoji for a builtin emoji, otherwise the custom emoji name and use ID
id string
animated bool
}
func ComponentEmojiDefault(emoji rune) *ComponentEmoji {
return &ComponentEmoji{
name: string(emoji),
}
}
func ComponentEmojiCustom(name, id string, animated bool) *ComponentEmoji {
return &ComponentEmoji{
name: name,
id: id,
animated: animated,
}
}
type ButtonStyle int
const (
ButtonStylePrimary ButtonStyle = 1
ButtonStyleSecondary ButtonStyle = 2
ButtonStyleSuccess ButtonStyle = 3
ButtonStyleDanger ButtonStyle = 4
ButtonStyleLink ButtonStyle = 5
)
func (b *Button) build(customid string) discordgo.MessageComponent {
v := discordgo.Button{
Label: b.Label,
Style: discordgo.ButtonStyle(b.Style),
Disabled: b.Disabled,
URL: b.URL,
CustomID: customid,
}
if b.Emoji != nil {
v.Emoji = discordgo.ComponentEmoji{
Name: b.Emoji.name,
ID: b.Emoji.id,
Animated: b.Emoji.animated,
}
}
if b.Style == ButtonStyleLink {
v.CustomID = ""
}
return v
}
func (b *Button) handler() any {
return b.Handler
}
type SelectHandler func(ctx Ctx, opts []string) // opts is the IDs of the options that are selected
type Select struct {
Placeholder string
Options []SelectOption
Handler SelectHandler
// Optional
MinValues int
MaxValues int
Disabled bool
}
func (s *Select) build(customid string) discordgo.MessageComponent {
v := discordgo.SelectMenu{
Placeholder: s.Placeholder,
Options: make([]discordgo.SelectMenuOption, len(s.Options)),
MinValues: &s.MinValues,
MaxValues: s.MaxValues,
Disabled: s.Disabled,
CustomID: customid,
}
for i, opt := range s.Options {
v.Options[i] = discordgo.SelectMenuOption{
Label: opt.Label,
Value: opt.ID,
Description: opt.Description,
Default: opt.Default,
}
if opt.Emoji != nil {
v.Options[i].Emoji = discordgo.ComponentEmoji{
Name: opt.Emoji.name,
ID: opt.Emoji.id,
Animated: opt.Emoji.animated,
}
}
}
return v
}
func (s *Select) handler() any {
return s.Handler
}
type SelectOption struct {
Label string
Description string
ID string // Must be unique
// Optional
Emoji *ComponentEmoji
Default bool // Whether it is automatically ticked
}
type Component interface {
build(customid string) discordgo.MessageComponent
handler() any
}
type ModalHandler func(Ctx, []string)
type Modal struct {
Title string
Inputs []ModalInput
Handler ModalHandler
}
type ModalInputStyle int
const (
ModalInputStyleSentence ModalInputStyle = 1
ModalInputStyleParagraph ModalInputStyle = 2
)
type ModalInput struct {
Label string
Placeholder string
Style ModalInputStyle
Required bool
MinLength int
MaxLength int
}