-
Notifications
You must be signed in to change notification settings - Fork 927
/
yagcommmand.go
1050 lines (855 loc) · 31.6 KB
/
yagcommmand.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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package commands
import (
"context"
"fmt"
"strings"
"sync"
"sync/atomic"
"time"
"emperror.dev/errors"
"github.com/botlabs-gg/yagpdb/v2/analytics"
"github.com/botlabs-gg/yagpdb/v2/bot"
"github.com/botlabs-gg/yagpdb/v2/commands/models"
"github.com/botlabs-gg/yagpdb/v2/common"
"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"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/sirupsen/logrus"
"github.com/volatiletech/sqlboiler/queries/qm"
)
type ContextKey int
const (
CtxKeyRedisClient ContextKey = iota
)
var (
CategoryGeneral = &dcmd.Category{
Name: "General",
Description: "General & informational commands",
HelpEmoji: "ℹ️",
EmbedColor: 0xe53939,
}
CategoryTool = &dcmd.Category{
Name: "Tools & Utilities",
Description: "Various miscellaneous commands",
HelpEmoji: "🔨",
EmbedColor: 0xeaed40,
}
CategoryModeration = &dcmd.Category{
Name: "Moderation",
Description: "Moderation commands",
HelpEmoji: "👮",
EmbedColor: 0xdb0606,
}
CategoryFun = &dcmd.Category{
Name: "Fun",
Description: "Various commands meant for entertainment",
HelpEmoji: "🎉",
EmbedColor: 0x5ae26c,
}
CategoryDebug = &dcmd.Category{
Name: "Debug & Maintenance",
Description: "Debug and other commands to inspect the bot",
HelpEmoji: "🖥",
EmbedColor: 0,
}
)
var (
RKeyCommandCooldown = func(uID int64, cmd string) string { return "cmd_cd:" + discordgo.StrID(uID) + ":" + cmd }
RKeyCommandCooldownGuild = func(gID int64, cmd string) string { return "cmd_guild_cd:" + discordgo.StrID(gID) + ":" + cmd }
RKeyCommandLock = func(uID int64, cmd string) string { return "cmd_lock:" + discordgo.StrID(uID) + ":" + cmd }
CommandExecTimeout = time.Minute
runningCommands = make([]*RunningCommand, 0)
runningcommandsLock sync.Mutex
shuttingDown = new(int32)
)
type RunningCommand struct {
GuildID int64
ChannelID int64
AuthorID int64
Command *YAGCommand
}
type RolesRunFunc func(gs *dstate.GuildSet) ([]int64, error)
// Slight extension to the simplecommand, it will check if the command is enabled in the HandleCommand func
// And invoke a custom handlerfunc with provided redis client
type YAGCommand struct {
Name string // Name of command, what its called from
Aliases []string // Aliases which it can also be called from
Description string // Description shown in non targetted help
LongDescription string // Longer description when this command was targetted
Arguments []*dcmd.ArgDef // Slice of argument definitions, ctx.Args will always be the same size as this slice (although the data may be nil)
RequiredArgs int // Number of reuquired arguments, ignored if combos is specified
ArgumentCombos [][]int // Slice of argument pairs, will override RequiredArgs if specified
ArgSwitches []*dcmd.ArgDef // Switches for the commadn to use
AllowEveryoneMention bool
HideFromCommandsPage bool // Set to hide this command from the commands page
Key string // GuildId is appended to the key, e.g if key is "test:", it will check for "test:<guildid>"
CustomEnabled bool // Set to true to handle the enable check itself
Default bool // The default enabled state of this command
Cooldown int // Cooldown in seconds before user can use it again
CmdCategory *dcmd.Category
GuildScopeCooldown int
RunInDM bool // Set to enable this commmand in DM's
HideFromHelp bool // Set to hide from help
RequireDiscordPerms []int64 // Require users to have one of these permission sets to run the command
RequiredDiscordPermsHelp string // Optional message that shows up when users run the help command that documents user permission requirements for the command
RequireBotPerms [][]int64 // Discord permissions that the bot needs to run the command, (([0][0] && [0][1] && [0][2]) || ([1][0] && [1][1]...))
Middlewares []dcmd.MiddleWareFunc
// Run is ran the the command has sucessfully been parsed
// It returns a reply and an error
// the reply can have a type of string, *MessageEmbed or error
RunFunc dcmd.RunFunc
Plugin common.Plugin
// Slash commands integration (this is unused on sub commands)
//
// Note about channel overrides:
// Since the slash commands permissions is limited to roles/users only and can't be per channel, it takes the common set of roles required to run the command between all overrides
// e.g if the command does not require a role in one channel, but it requires one in another channel, then the required permission for the slash command will be none set,
// note that these settings are still checked when the command is run, but they just show the command in the client even if you can't use it in this case, so its just a visual limitation of slash commands.
//
// If it's disabled in all chanels, then for default_enabled = true commands, it adds the everyone role to the blacklist, otherwise it adds no role to the whitelist (does this even work? can i use the everyone role in this context?)
SlashCommandEnabled bool
// Wether the command is enabled in all guilds by default or not
DefaultEnabled bool
// If default enabled = false
// then this returns the roles that CAN use the command
// if default enabled = true
// then this returns the roles that CAN'T use the command
RolesRunFunc RolesRunFunc
slashCommandID int64
IsResponseEphemeral bool
NSFW bool
}
// CmdWithCategory puts the command in a category, mostly used for the help generation
func (yc *YAGCommand) Category() *dcmd.Category {
return yc.CmdCategory
}
func (yc *YAGCommand) Descriptions(data *dcmd.Data) (short, long string) {
return yc.Description, yc.Description + "\n" + yc.LongDescription
}
func (yc *YAGCommand) ArgDefs(data *dcmd.Data) (args []*dcmd.ArgDef, required int, combos [][]int) {
return yc.Arguments, yc.RequiredArgs, yc.ArgumentCombos
}
func (yc *YAGCommand) Switches() []*dcmd.ArgDef {
return yc.ArgSwitches
}
var metricsExcecutedCommands = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "bot_commands_total",
Help: "Commands the bot executed",
}, []string{"name", "trigger_type"})
func (yc *YAGCommand) Run(data *dcmd.Data) (interface{}, error) {
if !yc.RunInDM && data.Source == dcmd.TriggerSourceDM {
return nil, nil
}
if yc.NSFW {
channel := data.GuildData.GS.GetChannelOrThread(data.ChannelID)
if !channel.NSFW {
return "This command can be used only in age-restricted channels", nil
}
}
// Send typing to indicate the bot's working
if confSetTyping.GetBool() && data.TriggerType != dcmd.TriggerTypeSlashCommands {
common.BotSession.ChannelTyping(data.ChannelID)
}
logger := yc.Logger(data)
// Track how long execution of a command took
started := time.Now()
rawCommand := ""
triggerType := ""
switch data.TriggerType {
case dcmd.TriggerTypeSlashCommands:
rawCommand = yc.Name + " (slashcommand)"
triggerType = "slashcommand"
default:
rawCommand = data.TraditionalTriggerData.Message.Content
triggerType = "message"
}
defer func() {
yc.logExecutionTime(time.Since(started), rawCommand, data.Author.Username)
}()
guildID := int64(0)
if data.GuildData != nil {
guildID = data.GuildData.GS.ID
}
cmdFullName := yc.Name
if len(data.ContainerChain) > 1 {
lastContainer := data.ContainerChain[len(data.ContainerChain)-1]
cmdFullName = lastContainer.Names[0] + " " + cmdFullName
}
// Set up log entry for later use
logEntry := &common.LoggedExecutedCommand{
UserID: discordgo.StrID(data.Author.ID),
ChannelID: discordgo.StrID(data.ChannelID),
Command: cmdFullName,
RawCommand: rawCommand,
TimeStamp: time.Now(),
}
if data.GuildData != nil {
logEntry.GuildID = discordgo.StrID(data.GuildData.GS.ID)
}
metricsExcecutedCommands.With(prometheus.Labels{"name": "(other)", "trigger_type": triggerType}).Inc()
logger.Info("Handling command: " + rawCommand)
runCtx, cancelExec := context.WithTimeout(data.Context(), CommandExecTimeout)
defer cancelExec()
// Run the command
r, cmdErr := yc.RunFunc(data.WithContext(runCtx))
logEntry.ResponseTime = int64(time.Since(started))
if cmdErr != nil {
if errors.Cause(cmdErr) == context.Canceled || errors.Cause(cmdErr) == context.DeadlineExceeded {
r = &EphemeralOrGuild{Content: "Took longer than " + CommandExecTimeout.String() + " to handle command: `" + rawCommand + "`, Cancelled the command."}
}
if r == nil || r == "" {
r = &EphemeralOrGuild{Content: yc.humanizeError(cmdErr)}
}
// set cmdErr to nil if this was a user error top stop it from being recorded and logged as an actual error
if _, isUserErr := errors.Cause(cmdErr).(dcmd.UserError); isUserErr {
cmdErr = nil
}
} else {
// set cooldowns
err := yc.SetCooldowns(data.ContainerChain, data.Author.ID, guildID)
if err != nil {
logger.WithError(err).Error("Failed setting cooldown")
}
if yc.Plugin != nil {
go analytics.RecordActiveUnit(guildID, yc.Plugin, "cmd_executed_"+strings.ToLower(cmdFullName))
}
}
// Create command log entry
err := common.GORM.Create(logEntry).Error
if err != nil {
logger.WithError(err).Error("Failed creating command execution log")
}
return r, cmdErr
}
func (yc *YAGCommand) humanizeError(err error) string {
cause := errors.Cause(err)
switch t := cause.(type) {
case PublicError:
return "The command returned an error: " + t.Error()
case UserError:
return "Unable to run the command: " + t.Error()
case *discordgo.RESTError:
if t.Message != nil && t.Message.Message != "" {
if t.Message.Message == "Unknown Message" {
return "The bot was not able to perform the action, Discord responded with: " + t.Message.Message + ". Please be sure you ran the command in the same channel as the message."
} else if t.Response != nil && t.Response.StatusCode == 403 {
return "The bot permissions has been incorrectly set up on this server for it to run this command: " + t.Message.Message
}
return "The bot was not able to perform the action, discord responded with: " + t.Message.Message
}
}
return "Something went wrong when running this command, either discord or the bot may be having issues."
}
// PostCommandExecuted sends the response and handles the trigger and response deletions
func (yc *YAGCommand) PostCommandExecuted(settings *CommandSettings, cmdData *dcmd.Data, resp interface{}, err error) {
if err != nil {
yc.Logger(cmdData).WithError(err).Error("Command returned error")
}
if cmdData.GuildData != nil {
if resp == nil && err != nil {
err = errors.New(FilterResp(err.Error(), cmdData.GuildData.GS.ID).(string))
} else if resp != nil {
resp = FilterResp(resp, cmdData.GuildData.GS.ID)
}
}
if (settings.DelResponse && settings.DelResponseDelay < 1) && cmdData.TraditionalTriggerData != nil {
// Set up the trigger deletion if set
if settings.DelTrigger {
go func() {
time.Sleep(time.Duration(settings.DelTriggerDelay) * time.Second)
common.BotSession.ChannelMessageDelete(cmdData.ChannelID, cmdData.TraditionalTriggerData.Message.ID)
}()
}
return // Don't bother sending the reponse if it has no delete delay
}
// Use the error as the response if no response was provided
if resp == nil && err != nil {
resp = fmt.Sprintf("'%s' command returned an error: %s", cmdData.Cmd.FormatNames(false, "/"), err)
}
// send a alternative message in case of embeds in channels with no embeds perms
if cmdData.GuildData != nil && cmdData.TriggerType != dcmd.TriggerTypeSlashCommands {
switch resp.(type) {
case *discordgo.MessageEmbed, []*discordgo.MessageEmbed:
if hasPerms, _ := bot.BotHasPermissionGS(cmdData.GuildData.GS, cmdData.ChannelID, discordgo.PermissionEmbedLinks); !hasPerms {
resp = "This command returned an embed but the bot does not have embed links permissions in this channel, cannot send the response."
}
}
}
// Send the response
var replies []*discordgo.Message
if resp == nil && cmdData.TriggerType == dcmd.TriggerTypeSlashCommands {
common.BotSession.DeleteInteractionResponse(common.BotApplication.ID, cmdData.SlashCommandTriggerData.Interaction.Token)
} else if resp != nil {
replies, err = dcmd.SendResponseInterface(cmdData, resp, true)
if err != nil {
logger.WithError(err).Error("failed sending command response")
}
}
if settings.DelResponse {
go func() {
time.Sleep(time.Second * time.Duration(settings.DelResponseDelay))
ids := make([]int64, 0, len(replies))
for _, v := range replies {
if v == nil {
continue
}
ids = append(ids, v.ID)
}
// If trigger deletion had the same delay, delete the trigger in the same batch
if settings.DelTrigger && settings.DelTriggerDelay == settings.DelResponseDelay && cmdData.TraditionalTriggerData != nil {
ids = append(ids, cmdData.TraditionalTriggerData.Message.ID)
}
if len(ids) == 1 {
common.BotSession.ChannelMessageDelete(cmdData.ChannelID, ids[0])
} else if len(ids) > 1 {
common.BotSession.ChannelMessagesBulkDelete(cmdData.ChannelID, ids)
}
}()
}
// If were deleting the trigger in a seperate call from the response deletion
if settings.DelTrigger && (!settings.DelResponse || settings.DelTriggerDelay != settings.DelResponseDelay) && cmdData.TraditionalTriggerData != nil {
go func() {
time.Sleep(time.Duration(settings.DelTriggerDelay) * time.Second)
common.BotSession.ChannelMessageDelete(cmdData.ChannelID, cmdData.TraditionalTriggerData.Message.ID)
}()
}
}
type CanExecuteType int
const (
// ReasonError = "An error occured"
// ReasonCommandDisabaledSettings = "Command is disabled in the settings"
// ReasonMissingRole = "Missing a required role for this command"
// ReasonIgnoredRole = "Has a ignored role for this command"
// ReasonUserMissingPerms = "User is missing one or more permissions to run this command"
// ReasonCooldown = "This command is on cooldown"
ReasonError CanExecuteType = iota
ReasonCommandDisabaledSettings
ReasonMissingRole
ReasonIgnoredRole
ReasonUserMissingPerms
ReasonBotMissingPerms
ReasonCooldown
)
type CanExecuteError struct {
Type CanExecuteType
Message string
}
// checks if the specified user can execute the command, and if so returns the settings for said command
func (yc *YAGCommand) checkCanExecuteCommand(data *dcmd.Data) (canExecute bool, resp *CanExecuteError, settings *CommandSettings, err error) {
// Check guild specific settings if not triggered from a DM
if data.GuildData != nil {
guild := data.GuildData.GS
if data.TriggerType != dcmd.TriggerTypeSlashCommands {
if hasPerms, _ := bot.BotHasPermissionGS(guild, data.ChannelID, discordgo.PermissionReadMessages|discordgo.PermissionSendMessages); !hasPerms {
return false, nil, nil, nil
}
}
channel_id := data.GuildData.CS.ID
parent_id := data.GuildData.CS.ParentID
// in case the channel is a thread, get the parent channel from parent id and check for the overrides
if data.GuildData.CS.Type.IsThread() {
channel := data.GuildData.GS.GetChannel(parent_id)
channel_id = channel.ID
parent_id = channel.ParentID
}
settings, err = yc.GetSettings(data.ContainerChain, channel_id, parent_id, guild.ID)
if err != nil {
resp = &CanExecuteError{
Type: ReasonError,
Message: "Failed retrieving cs.settings",
}
return false, resp, settings, errors.WithMessage(err, "cs.GetSettings")
}
if !settings.Enabled {
resp = &CanExecuteError{
Type: ReasonCommandDisabaledSettings,
Message: "Command is disabled in this channel by server admins",
}
return false, resp, settings, nil
}
member := data.GuildData.MS
guildRoles := roleNames(guild)
if missingWhitelistErr := checkWhitelistRoles(guildRoles, settings.RequiredRoles, data); missingWhitelistErr != nil {
return false, missingWhitelistErr, settings, nil
}
if blacklistErr := checkBlacklistRoles(guildRoles, settings.IgnoreRoles, data); blacklistErr != nil {
return false, blacklistErr, settings, nil
}
if userPermsErr := yc.checkRequiredMemberPerms(guild, member, data.ChannelID); userPermsErr != nil {
return false, userPermsErr, settings, nil
}
if userPermsErr := yc.checkRequiredBotPerms(guild, data.ChannelID); userPermsErr != nil {
return false, userPermsErr, settings, nil
}
} else {
settings = &CommandSettings{
Enabled: true,
}
}
guildID := int64(0)
if data.GuildData != nil {
guildID = data.GuildData.GS.ID
}
// Check the command cooldown
cdLeft, err := yc.LongestCooldownLeft(data.ContainerChain, data.Author.ID, guildID)
if err != nil {
// Just pretend the cooldown is off...
yc.Logger(data).Error("Failed checking command cooldown")
}
if cdLeft > 0 {
resp = &CanExecuteError{
Type: ReasonCooldown,
Message: "Command is on cooldown",
}
return false, resp, settings, nil
}
// If we got here then we can execute the command
return true, nil, settings, nil
}
func checkWhitelistRoles(guildRoles map[int64]string, whitelistRoles []int64, data *dcmd.Data) *CanExecuteError {
member := data.GuildData.MS
if len(whitelistRoles) < 1 {
// no whitelist roles
return nil
}
for _, r := range member.Member.Roles {
if common.ContainsInt64Slice(whitelistRoles, r) {
// we have a whitelist role!
return nil
}
}
var humanizedRoles strings.Builder
for i, v := range whitelistRoles {
if i != 0 {
humanizedRoles.WriteString(", ")
}
if i >= 20 {
left := len(whitelistRoles) - i
if left > 1 {
// if there's only 1 role left then just finished, otherwise add this
humanizedRoles.WriteString(fmt.Sprintf("(+%d roles)", left))
break
}
}
name := "unknown-role"
if v, ok := guildRoles[v]; ok {
name = v
}
humanizedRoles.WriteString(name)
}
return &CanExecuteError{
Type: ReasonMissingRole,
Message: "You need at least one of the server whitelist roles: " + humanizedRoles.String(),
}
}
func checkBlacklistRoles(guildRoles map[int64]string, blacklistRoles []int64, data *dcmd.Data) *CanExecuteError {
member := data.GuildData.MS
if len(blacklistRoles) < 1 {
// no blacklist roles
return nil
}
hasRole := int64(0)
for _, r := range member.Member.Roles {
if common.ContainsInt64Slice(blacklistRoles, r) {
// we have a blacklist role!
hasRole = r
break
}
}
if hasRole == 0 {
// We don't have a blacklist role!
return nil
}
// we do have a blacklist roles :(
humanizedRole := "unknown-role"
if v, ok := guildRoles[hasRole]; ok {
humanizedRole = v
}
return &CanExecuteError{
Type: ReasonIgnoredRole,
Message: "You have one of the server blacklist roles: " + humanizedRole,
}
}
func (yc *YAGCommand) checkRequiredMemberPerms(gs *dstate.GuildSet, ms *dstate.MemberState, channelID int64) *CanExecuteError {
// This command has permission sets required, if the user has one of them then allow this command to be used
if len(yc.RequireDiscordPerms) < 1 {
return nil
}
perms, err := gs.GetMemberPermissions(channelID, ms.User.ID, ms.Member.Roles)
if err != nil {
return &CanExecuteError{
Type: ReasonError,
Message: "Failed fetching member perms?",
}
}
for _, permSet := range yc.RequireDiscordPerms {
if permSet&int64(perms) == permSet {
// we have one of the required perms!
return nil
}
}
humanizedPerms := make([]string, 0, len(yc.RequireDiscordPerms))
for _, v := range yc.RequireDiscordPerms {
h := common.HumanizePermissions(v)
joined := strings.Join(h, " and ")
humanizedPerms = append(humanizedPerms, "("+joined+")")
}
return &CanExecuteError{
Type: ReasonUserMissingPerms,
Message: "You need at least one of the following permissions to run this command: " + strings.Join(humanizedPerms, " or "),
}
}
func (yc *YAGCommand) checkRequiredBotPerms(gs *dstate.GuildSet, channelID int64) *CanExecuteError {
// This command has permission sets required, if the user has one of them then allow this command to be used
if len(yc.RequireBotPerms) < 1 {
return nil
}
perms, err := bot.BotPermissions(gs, channelID)
if err != nil {
return &CanExecuteError{
Type: ReasonError,
Message: "Failed fetching bot perms",
}
}
// need all the perms within atleast one group
OUTER:
for _, permGroup := range yc.RequireBotPerms {
for _, v := range permGroup {
if perms&v != v {
continue OUTER
}
}
// if we got here we had them all in the group
return nil
}
humanizedPerms := make([]string, 0, len(yc.RequireDiscordPerms))
for _, group := range yc.RequireBotPerms {
gHumanized := make([]string, 0, len(group))
for _, v := range group {
h := common.HumanizePermissions(v)
joined := strings.Join(h, " and ")
gHumanized = append(gHumanized, joined)
}
humanizedPerms = append(humanizedPerms, "("+strings.Join(gHumanized, " and ")+")")
}
return &CanExecuteError{
Type: ReasonBotMissingPerms,
Message: "The bot needs at least one of the following permissions to run this command: " + strings.Join(humanizedPerms, " or "),
}
}
func roleNames(gs *dstate.GuildSet) map[int64]string {
result := make(map[int64]string)
for _, v := range gs.Roles {
result[v.ID] = v.Name
}
return result
}
func (cs *YAGCommand) logExecutionTime(dur time.Duration, raw string, sender string) {
logger.Infof("Handled Command [%4dms] %s: %s", int(dur.Seconds()*1000), sender, raw)
}
// customEnabled returns wether the command is enabled by it's custom key or not
func (cs *YAGCommand) customEnabled(guildID int64) (bool, error) {
// No special key so it's automatically enabled
if cs.Key == "" || cs.CustomEnabled {
return true, nil
}
// Check redis for settings
var enabled bool
err := common.RedisPool.Do(radix.Cmd(&enabled, "GET", cs.Key+discordgo.StrID(guildID)))
if err != nil {
return false, err
}
if cs.Default {
enabled = !enabled
}
if !enabled {
return false, nil
}
return enabled, nil
}
type CommandSettings struct {
Enabled bool
DelTrigger bool
DelResponse bool
DelTriggerDelay int
DelResponseDelay int
RequiredRoles []int64
IgnoreRoles []int64
}
func GetOverridesForChannel(channelID, channelParentID, guildID int64) ([]*models.CommandsChannelsOverride, error) {
// Fetch the overrides from the database, we treat the global settings as an override for simplicity
channelOverrides, err := models.CommandsChannelsOverrides(qm.Where("(? = ANY (channels) OR global=true OR ? = ANY (channel_categories)) AND guild_id=?", channelID, channelParentID, guildID), qm.Load("CommandsCommandOverrides")).AllG(context.Background())
if err != nil {
return nil, err
}
return channelOverrides, nil
}
// GetSettings returns the settings from the command, generated from the servers channel and command overrides
func (cs *YAGCommand) GetSettings(containerChain []*dcmd.Container, channelID, channelParentID, guildID int64) (settings *CommandSettings, err error) {
// Fetch the overrides from the database, we treat the global settings as an override for simplicity
channelOverrides, err := GetOverridesForChannel(channelID, channelParentID, guildID)
if err != nil {
err = errors.WithMessage(err, "GetOverridesForChannel")
return
}
return cs.GetSettingsWithLoadedOverrides(containerChain, guildID, channelOverrides)
}
func (cs *YAGCommand) GetSettingsWithLoadedOverrides(containerChain []*dcmd.Container, guildID int64, channelOverrides []*models.CommandsChannelsOverride) (settings *CommandSettings, err error) {
settings = &CommandSettings{}
// Some commands have custom places to toggle their enabled status
ce, err := cs.customEnabled(guildID)
if err != nil {
err = errors.WithMessage(err, "customEnabled")
return
}
if !ce {
return
}
if cs.HideFromCommandsPage {
settings.Enabled = true
return
}
if len(channelOverrides) < 1 {
settings.Enabled = true
return // No overrides
}
// Find the global and per channel override
var global *models.CommandsChannelsOverride
var channelOverride *models.CommandsChannelsOverride
for _, v := range channelOverrides {
if v.Global {
global = v
} else {
channelOverride = v
}
}
cmdFullName := cs.Name
if len(containerChain) > 1 {
lastContainer := containerChain[len(containerChain)-1]
cmdFullName = lastContainer.Names[0] + " " + cmdFullName
}
// Assign the global settings, if existing
if global != nil {
cs.fillSettings(cmdFullName, global, settings)
}
// Assign the channel override, if existing
if channelOverride != nil {
cs.fillSettings(cmdFullName, channelOverride, settings)
}
return
}
// Fills the command settings from a channel override, and if a matching command override is found, the command override
func (cs *YAGCommand) fillSettings(cmdFullName string, override *models.CommandsChannelsOverride, settings *CommandSettings) {
settings.Enabled = override.CommandsEnabled
settings.IgnoreRoles = override.IgnoreRoles
settings.RequiredRoles = override.RequireRoles
settings.DelResponse = override.AutodeleteResponse
settings.DelTrigger = override.AutodeleteTrigger
settings.DelResponseDelay = override.AutodeleteResponseDelay
settings.DelTriggerDelay = override.AutodeleteTriggerDelay
OUTER:
for _, cmdOverride := range override.R.CommandsCommandOverrides {
for _, cmd := range cmdOverride.Commands {
if strings.EqualFold(cmd, cmdFullName) {
settings.Enabled = cmdOverride.CommandsEnabled
settings.IgnoreRoles = cmdOverride.IgnoreRoles
settings.RequiredRoles = cmdOverride.RequireRoles
settings.DelResponse = cmdOverride.AutodeleteResponse
settings.DelTrigger = cmdOverride.AutodeleteTrigger
settings.DelResponseDelay = cmdOverride.AutodeleteResponseDelay
settings.DelTriggerDelay = cmdOverride.AutodeleteTriggerDelay
break OUTER
}
}
}
}
// LongestCooldownLeft returns the longest cooldown for this command, either user scoped or guild scoped
func (cs *YAGCommand) LongestCooldownLeft(cc []*dcmd.Container, userID int64, guildID int64) (int, error) {
cdUser, err := cs.UserScopeCooldownLeft(cc, userID)
if err != nil {
return 0, err
}
cdGuild, err := cs.GuildScopeCooldownLeft(cc, guildID)
if err != nil {
return 0, err
}
if cdUser > cdGuild {
return cdUser, nil
}
return cdGuild, nil
}
// UserScopeCooldownLeft returns the number of seconds before a command can be used again by this user
func (cs *YAGCommand) UserScopeCooldownLeft(cc []*dcmd.Container, userID int64) (int, error) {
if cs.Cooldown < 1 {
return 0, nil
}
var ttl int
err := common.RedisPool.Do(radix.Cmd(&ttl, "TTL", RKeyCommandCooldown(userID, cs.FindNameFromContainerChain(cc))))
if err != nil {
return 0, errors.WithStackIf(err)
}
return ttl, nil
}
// GuildScopeCooldownLeft returns the number of seconds before a command can be used again on this server
func (cs *YAGCommand) GuildScopeCooldownLeft(cc []*dcmd.Container, guildID int64) (int, error) {
if cs.GuildScopeCooldown < 1 {
return 0, nil
}
var ttl int
err := common.RedisPool.Do(radix.Cmd(&ttl, "TTL", RKeyCommandCooldownGuild(guildID, cs.FindNameFromContainerChain(cc))))
if err != nil {
return 0, errors.WithStackIf(err)
}
return ttl, nil
}
// SetCooldowns is a helper that serts both User and Guild cooldown
func (cs *YAGCommand) SetCooldowns(cc []*dcmd.Container, userID int64, guildID int64) error {
err := cs.SetCooldownUser(cc, userID)
if err != nil {
return errors.WithStackIf(err)
}
err = cs.SetCooldownGuild(cc, guildID)
if err != nil {
return errors.WithStackIf(err)
}
return nil
}
// SetCooldownUser sets the user scoped cooldown of the command as it's defined in the struct
func (cs *YAGCommand) SetCooldownUser(cc []*dcmd.Container, userID int64) error {
if cs.Cooldown < 1 {
return nil
}
now := time.Now().Unix()
err := common.RedisPool.Do(radix.FlatCmd(nil, "SET", RKeyCommandCooldown(userID, cs.FindNameFromContainerChain(cc)), now, "EX", cs.Cooldown))
return errors.WithStackIf(err)
}
// SetCooldownGuild sets the guild scoped cooldown of the command as it's defined in the struct
func (cs *YAGCommand) SetCooldownGuild(cc []*dcmd.Container, guildID int64) error {
if cs.GuildScopeCooldown < 1 {
return nil
}
now := time.Now().Unix()
err := common.RedisPool.Do(radix.FlatCmd(nil, "SET", RKeyCommandCooldownGuild(guildID, cs.FindNameFromContainerChain(cc)), now, "EX", cs.GuildScopeCooldown))
return errors.WithStackIf(err)
}
func (yc *YAGCommand) Logger(data *dcmd.Data) *logrus.Entry {
l := logger.WithField("cmd", yc.FindNameFromContainerChain(data.ContainerChain))
if data != nil {
if data.Author != nil {
l = l.WithField("user_n", data.Author.Username)
l = l.WithField("user_id", data.Author.ID)
}
if data.GuildData != nil && data.GuildData.CS != nil {
l = l.WithField("channel", data.GuildData.CS.ID)
}
if data.GuildData != nil && data.GuildData.GS != nil {
l = l.WithField("guild", data.GuildData.GS.ID)
}
}
return l
}
func (yc *YAGCommand) GetTrigger() *dcmd.Trigger {
trigger := dcmd.NewTrigger(yc.Name, yc.Aliases...).SetEnableInDM(yc.RunInDM).SetEnableInGuildChannels(true)
trigger = trigger.SetHideFromHelp(yc.HideFromHelp)
if len(yc.Middlewares) > 0 {
trigger = trigger.SetMiddlewares(yc.Middlewares...)
}
return trigger
}
// Keys and other sensitive information shouldnt be sent in error messages, but just in case it is
func CensorError(err error) string {
toCensor := []string{
common.BotSession.Token,
common.ConfClientSecret.GetString(),
}
out := err.Error()
for _, c := range toCensor {
out = strings.Replace(out, c, "", -1)
}
return out
}
func BlockingAddRunningCommand(guildID int64, channelID int64, authorID int64, cmd *YAGCommand, timeout time.Duration) bool {
started := time.Now()
for {
if tryAddRunningCommand(guildID, channelID, authorID, cmd) {
return true
}
if time.Since(started) > timeout {
return false
}
if atomic.LoadInt32(shuttingDown) == 1 {
return false
}
time.Sleep(time.Second)
if atomic.LoadInt32(shuttingDown) == 1 {
return false
}
}
}
func tryAddRunningCommand(guildID int64, channelID int64, authorID int64, cmd *YAGCommand) bool {
runningcommandsLock.Lock()
for _, v := range runningCommands {
if v.GuildID == guildID && v.ChannelID == channelID && v.AuthorID == authorID && v.Command == cmd {
runningcommandsLock.Unlock()
return false
}
}
runningCommands = append(runningCommands, &RunningCommand{
GuildID: guildID,
ChannelID: channelID,
AuthorID: authorID,
Command: cmd,
})
runningcommandsLock.Unlock()
return true
}
func removeRunningCommand(guildID, channelID, authorID int64, cmd *YAGCommand) {
runningcommandsLock.Lock()
for i, v := range runningCommands {
if v.GuildID == guildID && v.ChannelID == channelID && v.AuthorID == authorID && v.Command == cmd {
runningCommands = append(runningCommands[:i], runningCommands[i+1:]...)
runningcommandsLock.Unlock()
return
}
}