forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slashcommands.go
788 lines (655 loc) · 22.2 KB
/
slashcommands.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
package commands
import (
"bytes"
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"strings"
"sync/atomic"
"time"
"github.com/botlabs-gg/yagpdb/v2/bot"
"github.com/botlabs-gg/yagpdb/v2/bot/eventsystem"
"github.com/botlabs-gg/yagpdb/v2/commands/models"
"github.com/botlabs-gg/yagpdb/v2/common"
"github.com/botlabs-gg/yagpdb/v2/common/pubsub"
"github.com/botlabs-gg/yagpdb/v2/lib/dcmd"
"github.com/botlabs-gg/yagpdb/v2/lib/discordgo"
"github.com/botlabs-gg/yagpdb/v2/lib/dstate"
"github.com/mediocregopher/radix/v3"
)
var (
slashCommandsContainers []*slashCommandsContainer
slashCommandsIdsSet = new(int32)
)
type slashCommandsContainer struct {
container *dcmd.Container
defaultPermissions bool
rolesRunFunc RolesRunFunc
slashCommandID int64
}
// register containers seperately as they need special handling
//
// note: we could infer all the info from the members of the container
// but i felt that this explicit method was better and less quirky
func RegisterSlashCommandsContainer(container *dcmd.Container, defaultPermissions bool, rolesRunFunc RolesRunFunc) {
slashCommandsContainers = append(slashCommandsContainers, &slashCommandsContainer{
container: container,
defaultPermissions: defaultPermissions,
rolesRunFunc: rolesRunFunc,
})
}
func (p *Plugin) startSlashCommandsUpdater() {
p.updateGlobalCommands()
}
func (p *Plugin) updateGlobalCommands() {
result := make([]*discordgo.CreateApplicationCommandRequest, 0)
for _, v := range CommandSystem.Root.Commands {
if cmd := p.yagCommandToSlashCommand(v); cmd != nil {
logger.Infof("%s is a global slash command: default enabled: %v", cmd.Name, cmd.DefaultPermission)
result = append(result, cmd)
}
}
for _, v := range slashCommandsContainers {
logger.Infof("%s is a slash command container: default enabled: %v", v.container.Names[0], v.defaultPermissions)
result = append(result, p.containerToSlashCommand(v))
}
encoded, _ := json.MarshalIndent(result, "", " ")
current := ""
err := common.RedisPool.Do(radix.Cmd(¤t, "GET", "slash_commands_current"))
if err != nil {
logger.WithError(err).Error("failed retrieving current saved slash commands")
return
}
if bytes.Equal([]byte(current), encoded) {
logger.Info("Slash commands identical, skipping update")
return
}
// fmt.Println(string(encoded))
logger.Info("Slash commands changed, updating....")
ret, err := common.BotSession.BulkOverwriteGlobalApplicationCommands(common.BotApplication.ID, result)
// ret, err := common.BotSession.BulkOverwriteGuildApplicationCommands(common.BotApplication.ID, 614909558585819162, result)
if err != nil {
logger.WithError(err).Error("failed updating global slash commands")
return
}
// assign the id's
OUTER:
for _, v := range ret {
for _, rs := range CommandSystem.Root.Commands {
if cast, ok := rs.Command.(*YAGCommand); ok {
if cast.SlashCommandEnabled && strings.EqualFold(v.Name, cast.Name) {
cast.slashCommandID = v.ID
continue OUTER
}
}
}
// top level command not found
for _, c := range slashCommandsContainers {
if strings.EqualFold(c.container.Names[0], v.Name) {
c.slashCommandID = v.ID
continue OUTER
}
}
}
atomic.StoreInt32(slashCommandsIdsSet, 1)
err = common.RedisPool.Do(radix.Cmd(nil, "SET", "slash_commands_current", string(encoded)))
if err != nil {
logger.WithError(err).Error("failed setting current slash commands in redis")
}
}
func (p *Plugin) containerToSlashCommand(container *slashCommandsContainer) *discordgo.CreateApplicationCommandRequest {
t := true
req := &discordgo.CreateApplicationCommandRequest{
Name: strings.ToLower(container.container.Names[0]),
Description: common.CutStringShort(container.container.Description, 100),
DefaultPermission: &t,
}
for _, v := range container.container.Commands {
cast, ok := v.Command.(*YAGCommand)
if !ok {
panic("Not a yag command? what is this a triple nested command or something?")
}
isSub, innerOpts := cast.slashCommandOptions()
kind := discordgo.ApplicationCommandOptionSubCommand
if isSub {
kind = discordgo.ApplicationCommandOptionSubCommandGroup
}
opt := &discordgo.ApplicationCommandOption{
Name: strings.ToLower(cast.Name),
Description: common.CutStringShort(cast.Description, 100),
Type: kind,
Options: innerOpts,
}
req.Options = append(req.Options, opt)
}
return req
}
func (p *Plugin) yagCommandToSlashCommand(cmd *dcmd.RegisteredCommand) *discordgo.CreateApplicationCommandRequest {
cast, ok := cmd.Command.(*YAGCommand)
if !ok {
// probably a container, which is handled seperately, see RegisterSlashCommandsContainer
return nil
}
if !cast.SlashCommandEnabled {
// not enabled for slash commands
return nil
}
t := true
_, opts := cast.slashCommandOptions()
return &discordgo.CreateApplicationCommandRequest{
Name: strings.ToLower(cmd.Trigger.Names[0]),
Description: common.CutStringShort(cast.Description, 100),
DefaultPermission: &t,
Options: opts,
NSFW: cast.NSFW,
}
}
func (yc *YAGCommand) slashCommandOptions() (turnedIntoSubCommands bool, result []*discordgo.ApplicationCommandOption) {
var subCommands []*discordgo.ApplicationCommandOption
for i, v := range yc.Arguments {
opts := v.Type.SlashCommandOptions(v)
for _, v := range opts {
v.Name = strings.ToLower(v.Name)
}
if len(opts) > 1 && i == 0 {
// turn this command into a container
turnedIntoSubCommands = true
kind := discordgo.ApplicationCommandOptionSubCommand
for _, opt := range opts {
if i < yc.RequiredArgs {
opt.Required = true
}
subCommands = append(subCommands, &discordgo.ApplicationCommandOption{
Type: kind,
Name: "by-" + opt.Name,
Description: common.CutStringShort(yc.Description, 100),
Options: []*discordgo.ApplicationCommandOption{
opt,
},
})
}
turnedIntoSubCommands = true
} else {
if len(opts) == 1 {
if i < yc.RequiredArgs {
opts[0].Required = true
}
}
result = append(result, opts...)
}
}
sortedResult := make([]*discordgo.ApplicationCommandOption, 0, len(result))
// required args needs to be first
for _, v := range result {
if v.Required {
sortedResult = append(sortedResult, v)
}
}
// add the optional args last
for _, v := range result {
if !v.Required {
sortedResult = append(sortedResult, v)
}
}
for _, v := range yc.ArgSwitches {
if v.Type == nil {
adding := v.StandardSlashCommandOption(discordgo.ApplicationCommandOptionBoolean)
adding.Name = strings.ToLower(adding.Name)
sortedResult = append(sortedResult, adding)
} else {
adding := v.Type.SlashCommandOptions(v)
for _, v := range adding {
v.Name = strings.ToLower(v.Name)
}
sortedResult = append(sortedResult, adding...)
}
}
if turnedIntoSubCommands {
for _, v := range subCommands {
v.Options = append(v.Options, sortedResult...)
}
return true, subCommands
} else {
return false, sortedResult
}
}
func (p *Plugin) handleGuildCreate(evt *eventsystem.EventData) {
// TODO: add queue?
waitForSlashCommandIDs()
gs := bot.State.GetGuild(evt.GuildCreate().ID)
if gs == nil {
panic("gs is nil")
}
_, err := updateSlashCommandGuildPermissions(gs)
if err != nil {
logger.WithError(err).Error("failed updating guild slash command permissions")
}
}
func (p *Plugin) handleDiscordEventUpdateSlashCommandPermissions(evt *eventsystem.EventData) {
if evt.GS == nil {
return
}
waitForSlashCommandIDs()
_, err := updateSlashCommandGuildPermissions(evt.GS)
if err != nil {
logger.WithError(err).Error("failed updating guild slash command permissions")
}
}
func updateSlashCommandGuildPermissions(gs *dstate.GuildSet) (updated bool, err error) {
commandSettings, err := GetAllOverrides(context.Background(), gs.ID)
if err != nil {
return false, err
}
result := make([]*discordgo.GuildApplicationCommandPermissions, 0)
// Start with root commands
for _, v := range CommandSystem.Root.Commands {
if cast, ok := v.Command.(*YAGCommand); ok {
if cast.SlashCommandEnabled {
perms, err := cast.TopLevelSlashCommandPermissions(commandSettings, gs)
if err != nil {
return false, err
}
result = append(result, &discordgo.GuildApplicationCommandPermissions{
ID: cast.slashCommandID,
ApplicationID: common.BotApplication.ID,
GuildID: gs.ID,
Permissions: perms,
})
}
}
}
// do the containers
for _, v := range slashCommandsContainers {
perms, err := ContainerSlashCommandPermissions(v, commandSettings, gs)
if err != nil {
return false, err
}
result = append(result, &discordgo.GuildApplicationCommandPermissions{
ID: v.slashCommandID,
ApplicationID: common.BotApplication.ID,
GuildID: gs.ID,
Permissions: perms,
})
}
serialized, _ := json.MarshalIndent(result, ">", " ")
fmt.Println(string(serialized))
hash := sha256.Sum256(serialized)
oldHash := []byte{}
err = common.RedisPool.Do(radix.Cmd(&oldHash, "GET", fmt.Sprintf("slash_cmd_perms_sum:%d", gs.ID)))
if err != nil {
return false, err
}
fmt.Println("Hash: ", hash, "old", oldHash)
if bytes.Equal(hash[:], oldHash) {
logger.Info("Skipped updating guild slash command perms, hash matched ", gs.ID)
return false, nil
}
ret, err := common.BotSession.BatchEditGuildApplicationCommandsPermissions(common.BotApplication.ID, gs.ID, result)
if err != nil {
return false, err
}
err = common.RedisPool.Do(radix.FlatCmd(nil, "SET", fmt.Sprintf("slash_cmd_perms_sum:%d", gs.ID), hash[:]))
if err != nil {
return false, err
}
serialized, _ = json.MarshalIndent(ret, "perms<", " ")
fmt.Println(string(serialized))
return true, nil
}
func handleInteractionCreate(evt *eventsystem.EventData) {
interaction := evt.InteractionCreate()
if interaction.Type != discordgo.InteractionApplicationCommand {
return
}
if interaction.DataCommand == nil {
logger.Warn("Interaction had no data")
return
}
// serialized, _ := json.MarshalIndent(interaction.Interaction, "", " ")
// logger.Infof("Got interaction %#v", interaction.Interaction)
// fmt.Println(string(serialized))
err := CommandSystem.CheckInteraction(common.BotSession, &interaction.Interaction)
if err != nil {
logger.WithError(err).Error("failed handling command interaction")
}
}
// Since we can't put permissions on subcommands we do a similar thing we do to command settings that is
func ContainerSlashCommandPermissions(container *slashCommandsContainer, overrides []*models.CommandsChannelsOverride, gs *dstate.GuildSet) ([]*discordgo.ApplicationCommandPermissions, error) {
allowRoles, denyRoles, allowAll, denyAll, err := slashCommandPermissionsFromRolesFunc(container.rolesRunFunc, gs, container.defaultPermissions)
if err != nil {
return nil, err
}
childAllows, childDenies, childAllowAll, childDenyAll, err := sumContainerSlashCommandsChildren(container, overrides, gs)
if allowAll {
allowAll = childAllowAll
}
if !denyAll {
denyAll = childDenyAll
}
allowRoles = commonSet(childAllows, allowRoles)
denyRoles = mergeInt64Slice(denyRoles, childDenies)
fmt.Printf("CONTAINER PERMS: %s(%d): allowAll: %v, denyAll: %v, allow: %v, deny: %v \n", container.container.Names[0], container.slashCommandID, allowAll, denyAll, allowRoles, denyRoles)
result := toApplicationCommandPermissions(gs, container.defaultPermissions, allowRoles, denyRoles, allowAll, denyAll)
return result, nil
}
// merges all subcommand allow roles
// and returns a common set of all subcommand deny roles
func sumContainerSlashCommandsChildren(container *slashCommandsContainer, overrides []*models.CommandsChannelsOverride, gs *dstate.GuildSet) (allowRoles []int64, denyRoles []int64, allowAll bool, denyAll bool, err error) {
allowRoles = make([]int64, 0)
denyRoles = make([]int64, 0)
allowAll = false
denyAll = true
for _, v := range container.container.Commands {
cast, ok := v.Command.(*YAGCommand)
if !ok {
continue
}
_allowRoles, _denyRoles, _allowAll, _denyAll, err := cast.SlashCommandPermissions(overrides, container.defaultPermissions, []*dcmd.Container{CommandSystem.Root}, gs)
if err != nil {
return nil, nil, false, false, err
}
if !allowAll {
allowAll = _allowAll
}
allowRoles = mergeInt64Slice(_allowRoles, allowRoles)
if denyAll {
denyAll = _denyAll
denyRoles = _denyRoles
} else {
denyRoles = commonSet(denyRoles, _denyRoles)
}
}
return
}
func toApplicationCommandPermissions(gs *dstate.GuildSet, defaultEnabeld bool, allowRoles, denyRoles []int64, allowAll, denyAll bool) []*discordgo.ApplicationCommandPermissions {
result := make([]*discordgo.ApplicationCommandPermissions, 0, 10)
// allGuildroles := guildRoles(gs)
// Add allow roles
// if allowAll {
// if !defaultEnabeld {
// result = append(result, &discordgo.ApplicationCommandPermissions{
// ID: gs.ID,
// Kind: discordgo.CommandPermissionTypeRole,
// Permission: true,
// })
// }
// } else {
// for _, v := range allowRoles {
// result = append(result, &discordgo.ApplicationCommandPermissions{
// ID: v,
// Kind: discordgo.CommandPermissionTypeRole,
// Permission: true,
// })
// }
// }
// for now were keeping the permissions simple because of limitations with interactions currently
if allowAll || len(allowRoles) > 0 {
if !defaultEnabeld {
result = append(result, &discordgo.ApplicationCommandPermissions{
ID: gs.ID,
Type: discordgo.ApplicationCommandPermissionTypeRole,
Permission: true,
})
}
} else if denyAll {
if defaultEnabeld {
// for _, v := range allGuildroles {
// if v == gs.ID {
// continue
// }
// result = append(result, &discordgo.ApplicationCommandPermissions{
// ID: v,
// Kind: discordgo.CommandPermissionTypeRole,
// Permission: false,
// })
// }
result = append(result, &discordgo.ApplicationCommandPermissions{
ID: gs.ID,
Type: discordgo.ApplicationCommandPermissionTypeRole,
Permission: false,
})
}
} else {
// we cannot do this atm because of the max 10 limit of permission overwrites on commands, so keep it simple
// for _, v := range denyRoles {
// result = append(result, &discordgo.ApplicationCommandPermissions{
// ID: v,
// Kind: discordgo.CommandPermissionTypeRole,
// Permission: false,
// })
// }
}
return result
}
func (yc *YAGCommand) TopLevelSlashCommandPermissions(overrides []*models.CommandsChannelsOverride, gs *dstate.GuildSet) ([]*discordgo.ApplicationCommandPermissions, error) {
allowRoles, denyRoles, allowAll, denyAll, err := yc.SlashCommandPermissions(overrides, yc.DefaultEnabled, []*dcmd.Container{CommandSystem.Root}, gs)
if err != nil {
return nil, err
}
result := toApplicationCommandPermissions(gs, yc.DefaultEnabled, allowRoles, denyRoles, allowAll, denyAll)
return result, nil
}
func (yc *YAGCommand) SlashCommandPermissions(overrides []*models.CommandsChannelsOverride, defaultEnabeld bool, containerChain []*dcmd.Container, gs *dstate.GuildSet) (allowRoles []int64, denyRoles []int64, allowAll bool, denyAll bool, err error) {
allowRoles = make([]int64, 0)
denyRoles = make([]int64, 0)
allowAll = true
// denyAll = true
// generate from custom roles funct first
if yc.RolesRunFunc != nil {
allowRoles, denyRoles, allowAll, denyAll, err = slashCommandPermissionsFromRolesFunc(yc.RolesRunFunc, gs, defaultEnabeld)
if err != nil {
return
}
}
// check fixed required perms on the command
if len(yc.RequireDiscordPerms) > 0 {
roles := findRolesWithDiscordPerms(gs, yc.RequireDiscordPerms, defaultEnabeld)
if defaultEnabeld {
denyRoles = mergeInt64Slice(denyRoles, roles)
} else {
if allowAll {
allowRoles = roles
} else {
allowRoles = commonSet(roles, allowRoles)
}
}
allowAll = false
}
// check guild command settings
fullName := containerChain[len(containerChain)-1].FullName(false)
if !IsSlashCommandPermissionCommandEnabled(fullName, overrides) {
// command is disabled in all channels
return nil, nil, false, true, nil
}
// check overrides
commonRequiredRoles, commonBlacklistedRoles := channelOverridesCommonSet(fullName, overrides)
if len(commonRequiredRoles) > 0 {
if defaultEnabeld {
// Apply the inverse of the required roles to the deny roles, this effectively means were only blacklisting roles.
// which means the permissions need to be updated after any roles are added.
OUTER:
for _, v := range gs.Roles {
for _, required := range commonRequiredRoles {
if v.ID == required {
continue OUTER
}
}
for _, alreadyDenied := range denyRoles {
if alreadyDenied == v.ID {
continue OUTER
}
}
denyRoles = append(denyRoles, v.ID)
}
} else {
if allowAll {
allowRoles = commonRequiredRoles
} else {
allowRoles = commonSet(allowRoles, commonRequiredRoles)
}
}
allowAll = false
}
denyRoles = mergeInt64Slice(denyRoles, commonBlacklistedRoles)
return
}
func findRolesWithDiscordPerms(gs *dstate.GuildSet, requiredPerms []int64, defaultEnabled bool) []int64 {
result := make([]int64, 0)
// check fixed required perms on the command
OUTER:
for _, r := range gs.Roles {
perms := int64(r.Permissions)
for _, rp := range requiredPerms {
if perms&rp == rp || perms&discordgo.PermissionAdministrator == discordgo.PermissionAdministrator || perms&discordgo.PermissionManageServer == discordgo.PermissionManageServer {
// this role can run the command
if !defaultEnabled {
result = append(result, r.ID)
}
continue OUTER
}
}
// this role can not run the command
if defaultEnabled {
result = append(result, r.ID)
}
}
return result
}
// Since permissions in discord permissions is not per channel we can't apply this losslessly, which means i had to make a compromise
// and that compromise was to allow the slash command to be used in cases where it can't (altough during actual execution these will be checked again and you wont be able to run it in the end)
// blacklistRoles is a common set of all the overrides, meaning unless a blacklist role is present in all relevant overrides for the command it wont be applied
// allowRoles is a sum of all the relevant overrides
// this means that if a command is enabled in only a single channel, it will still show up as usable in all channels (but again, execution will be blocked if you try to use it)
func channelOverridesCommonSet(fullName string, overrides []*models.CommandsChannelsOverride) (allowRoles []int64, blacklistRoles []int64) {
first := true
OUTER:
for _, chOverride := range overrides {
for _, cmdOverride := range chOverride.R.CommandsCommandOverrides {
if common.ContainsStringSliceFold(cmdOverride.Commands, fullName) {
if !cmdOverride.CommandsEnabled {
continue OUTER
}
allowRoles = mergeInt64Slice(allowRoles, cmdOverride.RequireRoles)
if first {
first = false
blacklistRoles = cmdOverride.IgnoreRoles
} else {
blacklistRoles = commonSet(blacklistRoles, cmdOverride.IgnoreRoles)
}
continue OUTER
}
}
if !chOverride.CommandsEnabled {
continue OUTER
}
// No command override found for this specific command, default to channel settings
allowRoles = mergeInt64Slice(allowRoles, chOverride.RequireRoles)
if first {
first = false
blacklistRoles = chOverride.IgnoreRoles
} else {
blacklistRoles = commonSet(blacklistRoles, chOverride.IgnoreRoles)
}
}
return
}
// merges the 2 slices, without duplicates
func mergeInt64Slice(a []int64, b []int64) []int64 {
OUTER:
for _, va := range a {
// check if va is in b already
for _, vb := range b {
if va == vb {
continue OUTER
}
}
// va not found in b, add it to b
b = append(b, va)
}
return b
}
func commonSet(a []int64, b []int64) []int64 {
commonSet := make([]int64, 0, len(a))
OUTER:
for _, av := range a {
for _, bv := range b {
if av == bv {
commonSet = append(commonSet, av)
continue OUTER
}
}
}
return commonSet
}
// IsSlashCommandPermissionCommandEnabled following up on the no per channel permissions for slash commands
// This might seem like a mouthfull but essentially for us to be able to disable a command in a guild
// it has to be disabled in all channels. If its enabled it atleast 1 channel override of command override we need to be able to pick it
// from the slash command list
func IsSlashCommandPermissionCommandEnabled(fullName string, overrides []*models.CommandsChannelsOverride) bool {
// check if atleast one command override has it explicitly enabled, this takes precedence
for _, override := range overrides {
if isSlashCommandEnabledChannelOverride(fullName, override) {
return true
}
}
return false
}
func isSlashCommandEnabledChannelOverride(fullName string, override *models.CommandsChannelsOverride) bool {
for _, cmdOverride := range override.R.CommandsCommandOverrides {
if common.ContainsStringSliceFold(cmdOverride.Commands, fullName) {
return cmdOverride.CommandsEnabled
}
}
return override.CommandsEnabled
}
func slashCommandPermissionsFromRolesFunc(rf RolesRunFunc, gs *dstate.GuildSet, defaultEnabled bool) (allow []int64, deny []int64, allowAll bool, denyAll bool, err error) {
roles, err := rf(gs)
if err != nil {
return nil, nil, false, false, err
}
if defaultEnabled {
for _, v := range roles {
if v == gs.ID {
denyAll = true
roles = []int64{}
break
}
}
return nil, roles, false, denyAll, nil
} else {
for _, v := range roles {
if v == gs.ID {
allowAll = true
roles = []int64{}
break
}
}
return roles, nil, allowAll, false, nil
}
}
func (p *Plugin) handleUpdateSlashCommandsPermissions(event *pubsub.Event) {
gs := bot.State.GetGuild(event.TargetGuildInt)
if gs == nil {
return
}
waitForSlashCommandIDs()
_, err := updateSlashCommandGuildPermissions(gs)
if err != nil {
logger.WithError(err).Error("failed updating slash command permissions")
}
}
func waitForSlashCommandIDs() {
for {
if atomic.LoadInt32(slashCommandsIdsSet) == 1 {
break
}
time.Sleep(time.Second)
}
}
func PubsubSendUpdateSlashCommandsPermissions(gID int64) {
err := pubsub.Publish("update_slash_command_permissions", gID, nil)
if err != nil {
logger.WithError(err).Error("failed sending pubsub for update_slash_command_permissions")
}
}