/
component.go
464 lines (384 loc) · 12.2 KB
/
component.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package discord
import (
"fmt"
"github.com/disgoorg/json"
"github.com/disgoorg/snowflake/v2"
)
// ComponentType defines different Component(s)
type ComponentType int
// Supported ComponentType(s)
const (
ComponentTypeActionRow ComponentType = iota + 1
ComponentTypeButton
ComponentTypeStringSelectMenu
ComponentTypeTextInput
ComponentTypeUserSelectMenu
ComponentTypeRoleSelectMenu
ComponentTypeMentionableSelectMenu
ComponentTypeChannelSelectMenu
)
type Component interface {
json.Marshaler
Type() ComponentType
component()
}
type ContainerComponent interface {
Component
Components() []InteractiveComponent
containerComponent()
}
type InteractiveComponent interface {
Component
ID() string
interactiveComponent()
}
type UnmarshalComponent struct {
Component
}
func (u *UnmarshalComponent) UnmarshalJSON(data []byte) error {
var cType struct {
Type ComponentType `json:"type"`
}
if err := json.Unmarshal(data, &cType); err != nil {
return err
}
var (
component Component
err error
)
switch cType.Type {
case ComponentTypeActionRow:
v := ActionRowComponent{}
err = json.Unmarshal(data, &v)
component = v
case ComponentTypeButton:
v := ButtonComponent{}
err = json.Unmarshal(data, &v)
component = v
case ComponentTypeStringSelectMenu:
v := StringSelectMenuComponent{}
err = json.Unmarshal(data, &v)
component = v
case ComponentTypeTextInput:
v := TextInputComponent{}
err = json.Unmarshal(data, &v)
component = v
case ComponentTypeUserSelectMenu:
v := UserSelectMenuComponent{}
err = json.Unmarshal(data, &v)
component = v
case ComponentTypeRoleSelectMenu:
v := RoleSelectMenuComponent{}
err = json.Unmarshal(data, &v)
component = v
case ComponentTypeMentionableSelectMenu:
v := MentionableSelectMenuComponent{}
err = json.Unmarshal(data, &v)
component = v
case ComponentTypeChannelSelectMenu:
v := ChannelSelectMenuComponent{}
err = json.Unmarshal(data, &v)
component = v
default:
err = fmt.Errorf("unknown component with type %d received", cType.Type)
}
if err != nil {
return err
}
u.Component = component
return nil
}
type ComponentEmoji struct {
ID snowflake.ID `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Animated bool `json:"animated,omitempty"`
}
var (
_ Component = (*ActionRowComponent)(nil)
_ ContainerComponent = (*ActionRowComponent)(nil)
)
func NewActionRow(components ...InteractiveComponent) ActionRowComponent {
return components
}
type ActionRowComponent []InteractiveComponent
func (c ActionRowComponent) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Type ComponentType `json:"type"`
Components []InteractiveComponent `json:"components"`
}{
Type: c.Type(),
Components: c,
})
}
func (c *ActionRowComponent) UnmarshalJSON(data []byte) error {
var actionRow struct {
Components []UnmarshalComponent `json:"components"`
}
if err := json.Unmarshal(data, &actionRow); err != nil {
return err
}
if len(actionRow.Components) > 0 {
*c = make([]InteractiveComponent, len(actionRow.Components))
for i, component := range actionRow.Components {
(*c)[i] = component.Component.(InteractiveComponent)
}
}
return nil
}
func (ActionRowComponent) Type() ComponentType {
return ComponentTypeActionRow
}
func (ActionRowComponent) component() {}
func (ActionRowComponent) containerComponent() {}
func (c ActionRowComponent) Components() []InteractiveComponent {
return c
}
// Buttons returns all ButtonComponent(s) in the ActionRowComponent
func (c ActionRowComponent) Buttons() []ButtonComponent {
var buttons []ButtonComponent
for i := range c {
if button, ok := c[i].(ButtonComponent); ok {
buttons = append(buttons, button)
}
}
return buttons
}
// SelectMenus returns all SelectMenuComponent(s) in the ActionRowComponent
func (c ActionRowComponent) SelectMenus() []SelectMenuComponent {
var selectMenus []SelectMenuComponent
for i := range c {
if selectMenu, ok := c[i].(SelectMenuComponent); ok {
selectMenus = append(selectMenus, selectMenu)
}
}
return selectMenus
}
// TextInputs returns all TextInputComponent(s) in the ActionRowComponent
func (c ActionRowComponent) TextInputs() []TextInputComponent {
var textInputs []TextInputComponent
for i := range c {
if textInput, ok := c[i].(TextInputComponent); ok {
textInputs = append(textInputs, textInput)
}
}
return textInputs
}
// UpdateComponent returns a new ActionRowComponent with the Component which has the customID replaced
func (c ActionRowComponent) UpdateComponent(customID string, component InteractiveComponent) ActionRowComponent {
for i, cc := range c {
if cc.ID() == customID {
c[i] = component
return c
}
}
return c
}
// AddComponents returns a new ActionRowComponent with the provided Component(s) added
func (c ActionRowComponent) AddComponents(components ...InteractiveComponent) ActionRowComponent {
return append(c, components...)
}
// RemoveComponent returns a new ActionRowComponent with the provided Component at the index removed
func (c ActionRowComponent) RemoveComponent(index int) ActionRowComponent {
if len(c) > index {
return append(c[:index], c[index+1:]...)
}
return c
}
// ButtonStyle defines how the ButtonComponent looks like (https://discord.com/assets/7bb017ce52cfd6575e21c058feb3883b.png)
type ButtonStyle int
// Supported ButtonStyle(s)
const (
ButtonStylePrimary ButtonStyle = iota + 1
ButtonStyleSecondary
ButtonStyleSuccess
ButtonStyleDanger
ButtonStyleLink
)
// NewButton creates a new ButtonComponent with the provided parameters. Link ButtonComponent(s) need a URL and other ButtonComponent(s) need a customID
func NewButton(style ButtonStyle, label string, customID string, url string) ButtonComponent {
return ButtonComponent{
Style: style,
CustomID: customID,
URL: url,
Label: label,
}
}
// NewPrimaryButton creates a new ButtonComponent with ButtonStylePrimary & the provided parameters
func NewPrimaryButton(label string, customID string) ButtonComponent {
return NewButton(ButtonStylePrimary, label, customID, "")
}
// NewSecondaryButton creates a new ButtonComponent with ButtonStyleSecondary & the provided parameters
func NewSecondaryButton(label string, customID string) ButtonComponent {
return NewButton(ButtonStyleSecondary, label, customID, "")
}
// NewSuccessButton creates a new ButtonComponent with ButtonStyleSuccess & the provided parameters
func NewSuccessButton(label string, customID string) ButtonComponent {
return NewButton(ButtonStyleSuccess, label, customID, "")
}
// NewDangerButton creates a new ButtonComponent with ButtonStyleDanger & the provided parameters
func NewDangerButton(label string, customID string) ButtonComponent {
return NewButton(ButtonStyleDanger, label, customID, "")
}
// NewLinkButton creates a new link ButtonComponent with ButtonStyleLink & the provided parameters
func NewLinkButton(label string, url string) ButtonComponent {
return NewButton(ButtonStyleLink, label, "", url)
}
var (
_ Component = (*ButtonComponent)(nil)
_ InteractiveComponent = (*ButtonComponent)(nil)
)
type ButtonComponent struct {
Style ButtonStyle `json:"style"`
Label string `json:"label,omitempty"`
Emoji *ComponentEmoji `json:"emoji,omitempty"`
CustomID string `json:"custom_id,omitempty"`
URL string `json:"url,omitempty"`
Disabled bool `json:"disabled,omitempty"`
}
func (c ButtonComponent) MarshalJSON() ([]byte, error) {
type buttonComponent ButtonComponent
return json.Marshal(struct {
Type ComponentType `json:"type"`
buttonComponent
}{
Type: c.Type(),
buttonComponent: buttonComponent(c),
})
}
func (ButtonComponent) Type() ComponentType {
return ComponentTypeButton
}
func (c ButtonComponent) ID() string {
return c.CustomID
}
func (c ButtonComponent) SetID(id string) InteractiveComponent {
c.CustomID = id
return c
}
func (ButtonComponent) component() {}
func (ButtonComponent) interactiveComponent() {}
// WithStyle returns a new ButtonComponent with the provided style
func (c ButtonComponent) WithStyle(style ButtonStyle) ButtonComponent {
c.Style = style
return c
}
// WithLabel returns a new ButtonComponent with the provided label
func (c ButtonComponent) WithLabel(label string) ButtonComponent {
c.Label = label
return c
}
// WithEmoji returns a new ButtonComponent with the provided Emoji
func (c ButtonComponent) WithEmoji(emoji ComponentEmoji) ButtonComponent {
c.Emoji = &emoji
return c
}
// WithCustomID returns a new ButtonComponent with the provided custom id
func (c ButtonComponent) WithCustomID(customID string) ButtonComponent {
c.CustomID = customID
return c
}
// WithURL returns a new ButtonComponent with the provided URL
func (c ButtonComponent) WithURL(url string) ButtonComponent {
c.URL = url
return c
}
// AsEnabled returns a new ButtonComponent but enabled
func (c ButtonComponent) AsEnabled() ButtonComponent {
c.Disabled = false
return c
}
// AsDisabled returns a new ButtonComponent but disabled
func (c ButtonComponent) AsDisabled() ButtonComponent {
c.Disabled = true
return c
}
// WithDisabled returns a new ButtonComponent but disabled/enabled
func (c ButtonComponent) WithDisabled(disabled bool) ButtonComponent {
c.Disabled = disabled
return c
}
var (
_ Component = (*TextInputComponent)(nil)
_ InteractiveComponent = (*TextInputComponent)(nil)
)
func NewTextInput(customID string, style TextInputStyle, label string) TextInputComponent {
return TextInputComponent{
CustomID: customID,
Style: style,
Label: label,
}
}
func NewShortTextInput(customID string, label string) TextInputComponent {
return NewTextInput(customID, TextInputStyleShort, label)
}
func NewParagraphTextInput(customID string, label string) TextInputComponent {
return NewTextInput(customID, TextInputStyleParagraph, label)
}
type TextInputComponent struct {
CustomID string `json:"custom_id"`
Style TextInputStyle `json:"style"`
Label string `json:"label"`
MinLength *int `json:"min_length,omitempty"`
MaxLength int `json:"max_length,omitempty"`
Required bool `json:"required"`
Placeholder string `json:"placeholder,omitempty"`
Value string `json:"value,omitempty"`
}
func (c TextInputComponent) MarshalJSON() ([]byte, error) {
type textInputComponent TextInputComponent
return json.Marshal(struct {
Type ComponentType `json:"type"`
textInputComponent
}{
Type: c.Type(),
textInputComponent: textInputComponent(c),
})
}
func (TextInputComponent) Type() ComponentType {
return ComponentTypeTextInput
}
func (c TextInputComponent) ID() string {
return c.CustomID
}
func (TextInputComponent) component() {}
func (TextInputComponent) interactiveComponent() {}
// WithCustomID returns a new SelectMenuComponent with the provided customID
func (c TextInputComponent) WithCustomID(customID string) TextInputComponent {
c.CustomID = customID
return c
}
// WithStyle returns a new SelectMenuComponent with the provided TextInputStyle
func (c TextInputComponent) WithStyle(style TextInputStyle) TextInputComponent {
c.Style = style
return c
}
// WithMinLength returns a new TextInputComponent with the provided minLength
func (c TextInputComponent) WithMinLength(minLength int) TextInputComponent {
c.MinLength = &minLength
return c
}
// WithMaxLength returns a new TextInputComponent with the provided maxLength
func (c TextInputComponent) WithMaxLength(maxLength int) TextInputComponent {
c.MaxLength = maxLength
return c
}
// WithRequired returns a new TextInputComponent with the provided required
func (c TextInputComponent) WithRequired(required bool) TextInputComponent {
c.Required = required
return c
}
// WithPlaceholder returns a new TextInputComponent with the provided placeholder
func (c TextInputComponent) WithPlaceholder(placeholder string) TextInputComponent {
c.Placeholder = placeholder
return c
}
// WithValue returns a new TextInputComponent with the provided value
func (c TextInputComponent) WithValue(value string) TextInputComponent {
c.Value = value
return c
}
type TextInputStyle int
const (
TextInputStyleShort TextInputStyle = iota + 1
TextInputStyleParagraph
)