-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication_cmds.go
499 lines (423 loc) · 11.8 KB
/
application_cmds.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package sevcord
import "github.com/bwmarrin/discordgo"
type SlashCommandObject interface {
name() string
dg() *discordgo.ApplicationCommandOption
isGroup() bool
permissions() *int64
}
// NOTE: Can only have 2 levels of subcommands
type SlashCommandGroup struct {
Name string
Description string
Children []SlashCommandObject
Permissions *int
}
func NewSlashCommandGroup(name string, description string, children ...SlashCommandObject) *SlashCommandGroup {
return &SlashCommandGroup{Name: name, Description: description, Children: children}
}
// RequirePermissions accepts a discordgo permissions bit mask
func (s *SlashCommandGroup) RequirePermissions(p int) *SlashCommandGroup {
s.Permissions = &p
return s
}
type SlashCommand struct {
Name string
Description string
Options []Option
Permissions *int
Handler SlashCommandHandler
}
func NewSlashCommand(name, description string, handler SlashCommandHandler, options ...Option) *SlashCommand {
return &SlashCommand{Name: name, Description: description, Options: options, Handler: handler}
}
// RequirePermissions accepts a discordgo permissions bit mask
func (s *SlashCommand) RequirePermissions(p int) *SlashCommand {
s.Permissions = &p
return s
}
func (s *SlashCommandGroup) name() string { return s.Name }
func (s *SlashCommandGroup) dg() *discordgo.ApplicationCommandOption {
children := make([]*discordgo.ApplicationCommandOption, len(s.Children))
for i, child := range s.Children {
children[i] = child.dg()
if child.isGroup() {
children[i].Type = discordgo.ApplicationCommandOptionSubCommandGroup
} else {
children[i].Type = discordgo.ApplicationCommandOptionSubCommand
}
}
return &discordgo.ApplicationCommandOption{
Name: s.Name,
Description: s.Description,
Options: children,
}
}
func (s *SlashCommandGroup) isGroup() bool { return true }
func (s *SlashCommandGroup) permissions() *int64 {
if s.Permissions != nil {
v := int64(*s.Permissions)
return &v
}
return nil
}
func (s *SlashCommand) name() string { return s.Name }
func (s *SlashCommand) dg() *discordgo.ApplicationCommandOption {
opts := make([]*discordgo.ApplicationCommandOption, len(s.Options))
for i, opt := range s.Options {
opts[i] = &discordgo.ApplicationCommandOption{
Name: opt.Name,
Description: opt.Description,
Type: opt.Kind.dg(),
Required: opt.Required,
Autocomplete: opt.Autocomplete != nil,
ChannelTypes: opt.ChannelTypes,
}
if opt.Choices != nil {
opts[i].Choices = make([]*discordgo.ApplicationCommandOptionChoice, len(opt.Choices))
for j, choice := range opt.Choices {
opts[i].Choices[j] = &discordgo.ApplicationCommandOptionChoice{
Name: choice.Name,
Value: choice.Value,
}
}
}
if opt.MinVal != nil {
switch opt.Kind {
case OptionKindInt, OptionKindFloat:
opts[i].MinValue = opt.MinVal
opts[i].MaxValue = opt.MaxVal
case OptionKindString:
v := int(*opt.MinVal)
opts[i].MinLength = &v
opts[i].MaxLength = int(opt.MaxVal)
}
}
}
return &discordgo.ApplicationCommandOption{
Name: s.Name,
Description: s.Description,
Options: opts,
}
}
func (s *SlashCommand) isGroup() bool { return false }
func (s *SlashCommand) permissions() *int64 {
if s.Permissions != nil {
v := int64(*s.Permissions)
return &v
}
return nil
}
type SlashCommandAttachment struct {
Filename string
URL string
ProxyURL string // Use this to download
ContentType string
}
type OptionKind int
const (
OptionKindString OptionKind = iota // string
OptionKindInt // int
OptionKindBool // bool
OptionKindUser // *User
OptionKindChannel // channel id (string)
OptionKindRole // role id (string)
OptionKindFloat // float64
OptionKindAttachment // *SlashCommandAttachment
)
func (o OptionKind) dg() discordgo.ApplicationCommandOptionType {
return [...]discordgo.ApplicationCommandOptionType{discordgo.ApplicationCommandOptionString, discordgo.ApplicationCommandOptionInteger, discordgo.ApplicationCommandOptionBoolean, discordgo.ApplicationCommandOptionUser, discordgo.ApplicationCommandOptionChannel, discordgo.ApplicationCommandOptionRole, discordgo.ApplicationCommandOptionNumber, discordgo.ApplicationCommandOptionAttachment}[o]
}
type Option struct {
Name string
Description string
Kind OptionKind
Required bool
Choices []Choice // Optional
Autocomplete AutocompleteHandler // Optional
MinVal *float64
MaxVal float64
ChannelTypes []discordgo.ChannelType
}
func NewOption(name, description string, kind OptionKind, required bool) Option {
return Option{Name: name, Description: description, Kind: kind, Required: required}
}
func (o Option) AddChoices(c ...Choice) Option {
if o.Choices == nil {
o.Choices = make([]Choice, 0)
}
o.Choices = append(o.Choices, c...)
return o
}
func (o Option) AutoComplete(a AutocompleteHandler) Option {
o.Autocomplete = a
return o
}
// ChannelFilter allows specific channels to be shown in a channel option
func (o Option) ChannelFilter(allowed ...discordgo.ChannelType) Option {
o.ChannelTypes = allowed
return o
}
// MinMax allows you to set the min and max values for number options, or the min and max length for string options. Use 0 for the max value to leave no upper limit
func (o Option) MinMax(min, max float64) Option {
o.MinVal = &min
o.MaxVal = max
return o
}
type Choice struct {
Name string
Value string
}
func NewChoice(name, value string) Choice {
return Choice{Name: name, Value: value}
}
type AutocompleteHandler func(Ctx, any) []Choice
type SlashCommandHandler func(Ctx, []any)
type ContextMenuHandler func(Ctx, string)
type ContextMenuKind int
const (
ContextMenuKindMessage = ContextMenuKind(discordgo.MessageApplicationCommand) // The string passed is message ID
ContextMenuKindUser = ContextMenuKind(discordgo.UserApplicationCommand) // The string passed is User ID
)
type ContextMenuCommand struct {
Kind ContextMenuKind
Name string
Handler ContextMenuHandler
}
// Components
type ButtonHandler func(ctx Ctx, params string)
type Button struct {
Label string
Style ButtonStyle
// Handler
Handler string // ID of handler
Params string // Params to pass to handler
// Optional
Emoji *ComponentEmoji
URL string // Only link button can have this
Disabled bool
}
func NewButton(label string, style ButtonStyle, handler string, params string) *Button {
return &Button{
Label: label,
Style: style,
Handler: handler,
Params: params,
Disabled: false,
}
}
func (b *Button) WithEmoji(emoji ComponentEmoji) *Button {
b.Emoji = &emoji
return b
}
func (b *Button) SetURL(url string) *Button {
b.URL = url
return b
}
func (b *Button) SetDisabled(disabled bool) *Button {
b.Disabled = disabled
return b
}
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,
}
}
func (c ComponentEmoji) Dg() discordgo.ComponentEmoji {
return discordgo.ComponentEmoji{
Name: c.name,
ID: c.id,
Animated: c.animated,
}
}
const componentSeperator = "|"
type ButtonStyle int
const (
ButtonStylePrimary ButtonStyle = 1
ButtonStyleSecondary ButtonStyle = 2
ButtonStyleSuccess ButtonStyle = 3
ButtonStyleDanger ButtonStyle = 4
ButtonStyleLink ButtonStyle = 5
)
func (b *Button) Dg() discordgo.MessageComponent {
v := discordgo.Button{
Label: b.Label,
Style: discordgo.ButtonStyle(b.Style),
Disabled: b.Disabled,
URL: b.URL,
CustomID: b.Handler + componentSeperator + b.Params,
}
if b.Emoji != nil {
v.Emoji = b.Emoji.Dg()
}
if b.Style == ButtonStyleLink {
v.CustomID = ""
}
return v
}
type SelectHandler func(ctx Ctx, params string, selected []string)
type SelectKind int
const (
SelectKindString SelectKind = iota
SelectKindUser
SelectKindRole
SelectKindMentionable // Users or Roles
SelectKindChannel
)
func (s SelectKind) Dg() discordgo.SelectMenuType {
return [...]discordgo.SelectMenuType{discordgo.StringSelectMenu, discordgo.UserSelectMenu, discordgo.RoleSelectMenu, discordgo.MentionableSelectMenu, discordgo.ChannelSelectMenu}[s]
}
type Select struct {
Kind SelectKind
Placeholder string
Options []SelectOption
// Handler
Handler string // ID of handler
Params string // Params to pass to handler
// Optional
MinValues int
MaxValues int
Disabled bool
// Fill this for channel select menu
ChannelFilter []discordgo.ChannelType
}
func NewSelect(placeholder string, handler string, params string) *Select {
return &Select{
Placeholder: placeholder,
Handler: handler,
Params: params,
Disabled: false,
MinValues: 1,
MaxValues: 1,
}
}
func (s *Select) SetKind(kind SelectKind) *Select {
s.Kind = kind
return s
}
func (s *Select) Option(option SelectOption) *Select {
s.Options = append(s.Options, option)
return s
}
func (s *Select) SetRange(min, max int) *Select {
s.MinValues = min
s.MaxValues = max
return s
}
func (s *Select) SetDisabled(disabled bool) *Select {
s.Disabled = disabled
return s
}
func (s *Select) ChannelMenuFilter(allowed ...discordgo.ChannelType) *Select {
s.ChannelFilter = allowed
return s
}
func (s *Select) Dg() discordgo.MessageComponent {
v := discordgo.SelectMenu{
MenuType: s.Kind.Dg(),
Placeholder: s.Placeholder,
Options: make([]discordgo.SelectMenuOption, len(s.Options)),
MinValues: &s.MinValues,
MaxValues: s.MaxValues,
Disabled: s.Disabled,
CustomID: s.Handler + componentSeperator + s.Params,
ChannelTypes: s.ChannelFilter,
}
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 = opt.Emoji.Dg()
}
}
return v
}
type SelectOption struct {
Label string
Description string
ID string // Must be unique
// Optional
Emoji *ComponentEmoji
Default bool // Whether it is automatically ticked
}
func NewSelectOption(label, description, id string) SelectOption {
return SelectOption{
Label: label,
Description: description,
ID: id,
}
}
func (s SelectOption) WithEmoji(emoji ComponentEmoji) SelectOption {
s.Emoji = &emoji
return s
}
func (s SelectOption) SetDefault(defaulted bool) SelectOption {
s.Default = defaulted
return s
}
// Modals
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
}
func NewModalInput(label, placeholder string, style ModalInputStyle, maxLength int) ModalInput {
return ModalInput{
Label: label,
Placeholder: placeholder,
Style: style,
Required: true,
MinLength: 1,
MaxLength: maxLength,
}
}
func (m ModalInput) SetRequired(required bool) ModalInput {
m.Required = required
return m
}
func (m ModalInput) SetLength(min, max int) ModalInput {
m.MinLength = min
m.MaxLength = max
return m
}
func NewModal(title string, handler ModalHandler) Modal {
return Modal{
Title: title,
Handler: handler,
Inputs: make([]ModalInput, 0),
}
}
func (m Modal) Input(inp ModalInput) Modal {
m.Inputs = append(m.Inputs, inp)
return m
}