forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 5
/
commands.go
827 lines (705 loc) · 24.4 KB
/
commands.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
package moderation
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/jinzhu/gorm"
"github.com/jonas747/dcmd"
"github.com/jonas747/discordgo"
"github.com/jonas747/dstate"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/commands"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/scheduledevents2"
"github.com/pkg/errors"
)
func MBaseCmd(cmdData *dcmd.Data, targetID int64) (config *Config, targetUser *discordgo.User, err error) {
config, err = GetConfig(cmdData.GS.ID)
if err != nil {
return nil, nil, errors.WithMessage(err, "GetConfig")
}
if targetID != 0 {
targetMember, _ := bot.GetMember(cmdData.GS.ID, targetID)
if targetMember != nil {
authorMember := commands.ContextMS(cmdData.Context())
gs := cmdData.GS
gs.RLock()
above := bot.IsMemberAbove(gs, authorMember, targetMember)
gs.RUnlock()
if !above {
return config, targetMember.DGoUser(), commands.NewPublicError("Can't use moderation commands on users ranked the same or higher than you")
}
return config, targetMember.DGoUser(), nil
}
}
return config, &discordgo.User{
Username: "unknown",
Discriminator: "????",
ID: targetID,
}, nil
}
func MBaseCmdSecond(cmdData *dcmd.Data, reason string, reasonArgOptional bool, neededPerm int, additionalPermRoles []int64, enabled bool) (oreason string, err error) {
cmdName := cmdData.Cmd.Trigger.Names[0]
oreason = reason
if !enabled {
return oreason, commands.NewPublicErrorF("The **%s** command is disabled on this server. Enable it in the control panel on the moderation page.", cmdName)
}
if strings.TrimSpace(reason) == "" {
if !reasonArgOptional {
return oreason, commands.NewPublicError("A reason has been set to be required for this command by the server admins, see help for more info.")
}
oreason = "(No reason specified)"
}
// check permissions or role setup for this command
permsMet := false
if len(additionalPermRoles) > 0 {
// Check if the user has one of the required roles
member := commands.ContextMS(cmdData.Context())
for _, r := range member.Roles {
if common.ContainsInt64Slice(additionalPermRoles, r) {
permsMet = true
break
}
}
}
if !permsMet && neededPerm != 0 {
// Fallback to legacy permissions
hasPerms, err := bot.AdminOrPermMS(commands.ContextMS(cmdData.Context()), cmdData.CS.ID, neededPerm)
if err != nil || !hasPerms {
return oreason, commands.NewPublicErrorF("The **%s** command requires the **%s** permission in this channel or additional roles set up by admins, you don't have it. (if you do contact bot support)", cmdName, common.StringPerms[neededPerm])
}
permsMet = true
}
return oreason, nil
}
func SafeArgString(data *dcmd.Data, arg int) string {
if arg >= len(data.Args) || data.Args[arg].Value == nil {
return ""
}
return data.Args[arg].Str()
}
func GenericCmdResp(action ModlogAction, target *discordgo.User, duration time.Duration, zeroDurPermanent bool, noDur bool) string {
durStr := " indefinitely"
if duration > 0 || !zeroDurPermanent {
durStr = " for `" + common.HumanizeDuration(common.DurationPrecisionMinutes, duration) + "`"
}
if noDur {
durStr = ""
}
userStr := target.Username + "#" + target.Discriminator
if target.Discriminator == "????" {
userStr = strconv.FormatInt(target.ID, 10)
}
return fmt.Sprintf("%s %s `%s`%s", action.Emoji, action.Prefix, userStr, durStr)
}
var ModerationCommands = []*commands.YAGCommand{
&commands.YAGCommand{
CustomEnabled: true,
CmdCategory: commands.CategoryModeration,
Name: "Ban",
Aliases: []string{"banid"},
Description: "Bans a member, specify a duration with -d",
RequiredArgs: 1,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "User", Type: dcmd.UserID},
&dcmd.ArgDef{Name: "Reason", Type: dcmd.String},
},
ArgSwitches: []*dcmd.ArgDef{
&dcmd.ArgDef{Switch: "d", Default: time.Duration(0), Name: "Duration", Type: &commands.DurationArg{}},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
config, target, err := MBaseCmd(parsed, parsed.Args[0].Int64())
if err != nil {
return nil, err
}
reason := SafeArgString(parsed, 1)
reason, err = MBaseCmdSecond(parsed, reason, config.BanReasonOptional, discordgo.PermissionBanMembers, config.BanCmdRoles, config.BanEnabled)
if err != nil {
return nil, err
}
err = BanUserWithDuration(config, parsed.GS.ID, parsed.Msg.ChannelID, parsed.Msg.Author, reason, target, parsed.Switches["d"].Value.(time.Duration))
if err != nil {
return nil, err
}
return GenericCmdResp(MABanned, target, parsed.Switch("d").Value.(time.Duration), true, false), nil
},
},
&commands.YAGCommand{
CustomEnabled: true,
CmdCategory: commands.CategoryModeration,
Name: "Kick",
Description: "Kicks a member",
RequiredArgs: 1,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "User", Type: dcmd.UserID},
&dcmd.ArgDef{Name: "Reason", Type: dcmd.String},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
config, target, err := MBaseCmd(parsed, parsed.Args[0].Int64())
if err != nil {
return nil, err
}
reason := SafeArgString(parsed, 1)
reason, err = MBaseCmdSecond(parsed, reason, config.KickReasonOptional, discordgo.PermissionKickMembers, config.KickCmdRoles, config.KickEnabled)
if err != nil {
return nil, err
}
err = KickUser(config, parsed.GS.ID, parsed.Msg.ChannelID, parsed.Msg.Author, reason, target)
if err != nil {
return nil, err
}
return GenericCmdResp(MAKick, target, 0, true, true), nil
},
},
&commands.YAGCommand{
CustomEnabled: true,
CmdCategory: commands.CategoryModeration,
Name: "Mute",
Description: "Mutes a member",
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "User", Type: dcmd.UserID},
&dcmd.ArgDef{Name: "Duration", Default: time.Minute * 10, Type: &commands.DurationArg{Max: time.Hour * 24 * 7}},
&dcmd.ArgDef{Name: "Reason", Type: dcmd.String},
},
ArgumentCombos: [][]int{[]int{0, 1, 2}, []int{0, 2, 1}, []int{0, 1}, []int{0, 2}, []int{0}},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
config, target, err := MBaseCmd(parsed, parsed.Args[0].Int64())
if err != nil {
return nil, err
}
if config.MuteRole == "" {
return "No mute role set up, assign a mute role in the control panel", nil
}
reason := parsed.Args[2].Str()
reason, err = MBaseCmdSecond(parsed, reason, config.MuteReasonOptional, discordgo.PermissionKickMembers, config.MuteCmdRoles, config.MuteEnabled)
if err != nil {
return nil, err
}
muteDuration := int(parsed.Args[1].Value.(time.Duration).Minutes())
member, err := bot.GetMember(parsed.GS.ID, target.ID)
if err != nil || member == nil {
return "Member not found", err
}
err = MuteUnmuteUser(config, true, parsed.GS.ID, parsed.Msg.ChannelID, parsed.Msg.Author, reason, member, muteDuration)
if err != nil {
return nil, err
}
return GenericCmdResp(MAMute, target, time.Duration(muteDuration)*time.Minute, false, false), nil
},
},
&commands.YAGCommand{
CustomEnabled: true,
CmdCategory: commands.CategoryModeration,
Name: "Unmute",
Description: "Unmutes a member",
RequiredArgs: 1,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "User", Type: dcmd.UserID},
&dcmd.ArgDef{Name: "Reason", Type: dcmd.String},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
config, target, err := MBaseCmd(parsed, parsed.Args[0].Int64())
if err != nil {
return nil, err
}
if config.MuteRole == "" {
return "No mute role set up, assign a mute role in the control panel", nil
}
reason := parsed.Args[1].Str()
reason, err = MBaseCmdSecond(parsed, reason, config.UnmuteReasonOptional, discordgo.PermissionKickMembers, config.MuteCmdRoles, config.MuteEnabled)
if err != nil {
return nil, err
}
member, err := bot.GetMember(parsed.GS.ID, target.ID)
if err != nil || member == nil {
return "Member not found", err
}
err = MuteUnmuteUser(config, false, parsed.GS.ID, parsed.Msg.ChannelID, parsed.Msg.Author, reason, member, 0)
if err != nil {
return nil, err
}
return GenericCmdResp(MAUnmute, target, 0, false, true), nil
},
},
&commands.YAGCommand{
CustomEnabled: true,
Cooldown: 5,
CmdCategory: commands.CategoryModeration,
Name: "Report",
Description: "Reports a member to the server's staff",
RequiredArgs: 2,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "User", Type: dcmd.UserID},
&dcmd.ArgDef{Name: "Reason", Type: dcmd.String},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
config, _, err := MBaseCmd(parsed, 0)
if err != nil {
return nil, err
}
_, err = MBaseCmdSecond(parsed, "", true, 0, nil, config.ReportEnabled)
if err != nil {
return nil, err
}
target := parsed.Args[0].Int64()
logLink := CreateLogs(parsed.GS.ID, parsed.CS.ID, parsed.Msg.Author)
channelID := config.IntReportChannel()
if channelID == 0 {
return "No report channel set up", nil
}
reportBody := fmt.Sprintf("<@%d> Reported <@%d> in <#%d> For `%s`\nLast 100 messages from channel: <%s>", parsed.Msg.Author.ID, target, parsed.Msg.ChannelID, parsed.Args[1].Str(), logLink)
_, err = common.BotSession.ChannelMessageSend(channelID, common.EscapeSpecialMentions(reportBody))
if err != nil {
return nil, err
}
// don't bother sending confirmation if it's in the same channel
if channelID != parsed.Msg.ChannelID {
return "User reported to the proper authorities", nil
}
return nil, nil
},
},
&commands.YAGCommand{
CustomEnabled: true,
CmdCategory: commands.CategoryModeration,
Name: "Clean",
Description: "Delete the last number of messages from chat, optionally filtering by user, max age and regex.",
LongDescription: "Specify a regex with \"-r regex_here\" and max age with \"-ma 1h10m\"\nNote: Will only look in the last 1k messages",
Aliases: []string{"clear", "cl"},
RequiredArgs: 1,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "Num", Type: &dcmd.IntArg{Min: 1, Max: 100}},
&dcmd.ArgDef{Name: "User", Type: dcmd.UserID, Default: 0},
},
ArgSwitches: []*dcmd.ArgDef{
&dcmd.ArgDef{Switch: "r", Name: "Regex", Type: dcmd.String},
&dcmd.ArgDef{Switch: "ma", Default: time.Duration(0), Name: "Max age", Type: &commands.DurationArg{}},
&dcmd.ArgDef{Switch: "i", Name: "Regex case insensitive"},
},
ArgumentCombos: [][]int{[]int{0}, []int{0, 1}, []int{1, 0}},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
config, _, err := MBaseCmd(parsed, 0)
if err != nil {
return nil, err
}
_, err = MBaseCmdSecond(parsed, "", true, discordgo.PermissionManageMessages, nil, config.CleanEnabled)
if err != nil {
return nil, err
}
userFilter := parsed.Args[1].Int64()
num := parsed.Args[0].Int()
if userFilter == 0 || userFilter == parsed.Msg.Author.ID {
num++ // Automatically include our own message
}
if num > 100 {
num = 100
}
if num < 1 {
if num < 0 {
return errors.New("Bot is having a stroke <https://www.youtube.com/watch?v=dQw4w9WgXcQ>"), nil
}
return errors.New("Can't delete nothing"), nil
}
filtered := false
// Check if we should regex match this
re := ""
if parsed.Switches["r"].Value != nil {
filtered = true
re = parsed.Switches["r"].Str()
// Add the case insensitive flag if needed
if parsed.Switches["i"].Value != nil && parsed.Switches["i"].Value.(bool) {
if !strings.HasPrefix(re, "(?i)") {
re = "(?i)" + re
}
}
}
// Check if we have a max age
ma := parsed.Switches["ma"].Value.(time.Duration)
if ma != 0 {
filtered = true
}
limitFetch := num
if userFilter != 0 || filtered {
limitFetch = num * 50 // Maybe just change to full fetch?
}
if limitFetch > 1000 {
limitFetch = 1000
}
// Wait a second so the client dosen't gltich out
time.Sleep(time.Second)
numDeleted, err := AdvancedDeleteMessages(parsed.Msg.ChannelID, userFilter, re, ma, num, limitFetch)
return dcmd.NewTemporaryResponse(time.Second*5, fmt.Sprintf("Deleted %d message(s)! :')", numDeleted), true), err
},
},
&commands.YAGCommand{
CustomEnabled: true,
CmdCategory: commands.CategoryModeration,
Name: "Reason",
Description: "Add/Edit a modlog reason",
RequiredArgs: 2,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "Message ID", Type: dcmd.Int},
&dcmd.ArgDef{Name: "Reason", Type: dcmd.String},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
config, _, err := MBaseCmd(parsed, 0)
if err != nil {
return nil, err
}
_, err = MBaseCmdSecond(parsed, "", true, discordgo.PermissionKickMembers, nil, true)
if err != nil {
return nil, err
}
if config.ActionChannel == "" {
return "No mod log channel set up", nil
}
msg, err := common.BotSession.ChannelMessage(config.IntActionChannel(), parsed.Args[0].Int64())
if err != nil {
return nil, err
}
if msg.Author.ID != common.BotUser.ID {
return "I didn't make that message", nil
}
if len(msg.Embeds) < 1 {
return "This entry is either too old or you're trying to mess with me...", nil
}
embed := msg.Embeds[0]
updateEmbedReason(parsed.Msg.Author, parsed.Args[1].Str(), embed)
_, err = common.BotSession.ChannelMessageEditEmbed(config.IntActionChannel(), msg.ID, embed)
if err != nil {
return nil, err
}
return "👌", nil
},
},
&commands.YAGCommand{
CustomEnabled: true,
CmdCategory: commands.CategoryModeration,
Name: "Warn",
Description: "Warns a user, warnings are saved using the bot. Use -warnings to view them.",
RequiredArgs: 2,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "User", Type: dcmd.UserID},
&dcmd.ArgDef{Name: "Reason", Type: dcmd.String},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
config, target, err := MBaseCmd(parsed, parsed.Args[0].Int64())
if err != nil {
return nil, err
}
_, err = MBaseCmdSecond(parsed, "", true, discordgo.PermissionManageMessages, config.WarnCmdRoles, config.WarnCommandsEnabled)
if err != nil {
return nil, err
}
err = WarnUser(config, parsed.GS.ID, parsed.CS.ID, parsed.Msg.Author, target, parsed.Args[1].Str())
if err != nil {
return nil, err
}
return GenericCmdResp(MAWarned, target, 0, false, true), nil
},
},
&commands.YAGCommand{
CustomEnabled: true,
CmdCategory: commands.CategoryModeration,
Name: "Warnings",
Description: "Lists warning of a user.",
Aliases: []string{"Warns"},
RequiredArgs: 1,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "User", Type: dcmd.UserID},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
config, _, err := MBaseCmd(parsed, 0)
if err != nil {
return nil, err
}
_, err = MBaseCmdSecond(parsed, "", true, discordgo.PermissionManageMessages, config.WarnCmdRoles, true)
if err != nil {
return nil, err
}
userID := parsed.Args[0].Int64()
var result []*WarningModel
err = common.GORM.Where("user_id = ? AND guild_id = ?", userID, parsed.GS.ID).Order("id desc").Find(&result).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
if len(result) < 1 {
return "This user has not received any warnings", nil
}
out := ""
for _, entry := range result {
out += fmt.Sprintf("#%d: `%20s` **%s** (%13s) - **%s**\n", entry.ID, entry.CreatedAt.Format(time.RFC822), entry.AuthorUsernameDiscrim, entry.AuthorID, entry.Message)
if entry.LogsLink != "" {
out += "^logs: <" + entry.LogsLink + ">\n"
}
}
return out, nil
},
},
&commands.YAGCommand{
CustomEnabled: true,
CmdCategory: commands.CategoryModeration,
Name: "EditWarning",
Description: "Edit a warning, id is the first number of each warning from the warnings command",
RequiredArgs: 2,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "Id", Type: dcmd.Int},
&dcmd.ArgDef{Name: "NewMessage", Type: dcmd.String},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
config, _, err := MBaseCmd(parsed, 0)
if err != nil {
return nil, err
}
_, err = MBaseCmdSecond(parsed, "", true, discordgo.PermissionManageMessages, config.WarnCmdRoles, config.WarnCommandsEnabled)
if err != nil {
return nil, err
}
rows := common.GORM.Model(WarningModel{}).Where("guild_id = ? AND id = ?", parsed.GS.ID, parsed.Args[0].Int()).Update(
"message", fmt.Sprintf("%s (updated by %s#%s (%d))", parsed.Args[1].Str(), parsed.Msg.Author.Username, parsed.Msg.Author.Discriminator, parsed.Msg.Author.ID)).RowsAffected
if rows < 1 {
return "Failed updating, most likely couldn't find the warning", nil
}
return "👌", nil
},
},
&commands.YAGCommand{
CustomEnabled: true,
CmdCategory: commands.CategoryModeration,
Name: "DelWarning",
Aliases: []string{"dw"},
Description: "Deletes a warning, id is the first number of each warning from the warnings command",
RequiredArgs: 1,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "Id", Type: dcmd.Int},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
config, _, err := MBaseCmd(parsed, 0)
if err != nil {
return nil, err
}
_, err = MBaseCmdSecond(parsed, "", true, discordgo.PermissionManageMessages, config.WarnCmdRoles, config.WarnCommandsEnabled)
if err != nil {
return nil, err
}
rows := common.GORM.Where("guild_id = ? AND id = ?", parsed.GS.ID, parsed.Args[0].Int()).Delete(WarningModel{}).RowsAffected
if rows < 1 {
return "Failed deleting, most likely couldn't find the warning", nil
}
return "👌", nil
},
},
&commands.YAGCommand{
CustomEnabled: true,
CmdCategory: commands.CategoryModeration,
Name: "ClearWarnings",
Aliases: []string{"clw"},
Description: "Clears the warnings of a user",
RequiredArgs: 1,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "User", Type: dcmd.UserID},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
config, _, err := MBaseCmd(parsed, 0)
if err != nil {
return nil, err
}
_, err = MBaseCmdSecond(parsed, "", true, discordgo.PermissionManageMessages, config.WarnCmdRoles, config.WarnCommandsEnabled)
if err != nil {
return nil, err
}
userID := parsed.Args[0].Int64()
rows := common.GORM.Where("guild_id = ? AND user_id = ?", parsed.GS.ID, userID).Delete(WarningModel{}).RowsAffected
return fmt.Sprintf("Deleted %d warnings.", rows), nil
},
},
&commands.YAGCommand{
CustomEnabled: true,
CmdCategory: commands.CategoryModeration,
Name: "GiveRole",
Aliases: []string{"grole", "arole", "addrole"},
Description: "Gives a role to the specified member, with optional expiry",
RequiredArgs: 2,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "User", Type: dcmd.UserID},
&dcmd.ArgDef{Name: "Role", Type: dcmd.String},
},
ArgSwitches: []*dcmd.ArgDef{
&dcmd.ArgDef{Switch: "d", Default: time.Duration(0), Name: "Duration", Type: &commands.DurationArg{}},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
config, target, err := MBaseCmd(parsed, parsed.Args[0].Int64())
if err != nil {
return nil, err
}
_, err = MBaseCmdSecond(parsed, "", true, discordgo.PermissionManageRoles, config.GiveRoleCmdRoles, config.GiveRoleCmdEnabled)
if err != nil {
return nil, err
}
member, err := bot.GetMember(parsed.GS.ID, target.ID)
if err != nil || member == nil {
return "Member not found", err
}
role := FindRole(parsed.GS, parsed.Args[1].Str())
if role == nil {
return "Couldn't find the specified role", nil
}
authorMember := commands.ContextMS(parsed.Context())
parsed.GS.RLock()
if !bot.IsMemberAboveRole(parsed.GS, authorMember, role) {
parsed.GS.RUnlock()
return "Can't give roles above you", nil
}
parsed.GS.RUnlock()
dur := parsed.Switches["d"].Value.(time.Duration)
// no point if the user has the role and is not updating the expiracy
if common.ContainsInt64Slice(member.Roles, role.ID) && dur <= 0 {
return "That user already has that role", nil
}
err = common.AddRoleDS(member, role.ID)
if err != nil {
return nil, err
}
// schedule the expirey
if dur > 0 {
err := scheduledevents2.ScheduleRemoveRole(parsed.Context(), parsed.GS.ID, target.ID, role.ID, time.Now().Add(dur))
if err != nil {
return nil, err
}
}
action := MAGiveRole
action.Prefix = "Gave the role " + role.Name + " to "
if config.GiveRoleCmdModlog && config.IntActionChannel() != 0 {
if dur > 0 {
action.Footer = "Duration: " + common.HumanizeDuration(common.DurationPrecisionMinutes, dur)
}
CreateModlogEmbed(config.IntActionChannel(), parsed.Msg.Author, action, target, "", "")
}
return GenericCmdResp(action, target, dur, true, dur <= 0), nil
},
},
&commands.YAGCommand{
CustomEnabled: true,
CmdCategory: commands.CategoryModeration,
Name: "RemoveRole",
Aliases: []string{"rrole", "takerole", "trole"},
Description: "Removes the specified role from the target",
RequiredArgs: 2,
Arguments: []*dcmd.ArgDef{
&dcmd.ArgDef{Name: "User", Type: dcmd.UserID},
&dcmd.ArgDef{Name: "Role", Type: dcmd.String},
},
RunFunc: func(parsed *dcmd.Data) (interface{}, error) {
config, target, err := MBaseCmd(parsed, parsed.Args[0].Int64())
if err != nil {
return nil, err
}
_, err = MBaseCmdSecond(parsed, "", true, discordgo.PermissionManageRoles, config.GiveRoleCmdRoles, config.GiveRoleCmdEnabled)
if err != nil {
return nil, err
}
member, err := bot.GetMember(parsed.GS.ID, target.ID)
if err != nil || member == nil {
return "Member not found", err
}
role := FindRole(parsed.GS, parsed.Args[1].Str())
if role == nil {
return "Couldn't find the specified role", nil
}
authorMember := commands.ContextMS(parsed.Context())
parsed.GS.RLock()
if !bot.IsMemberAboveRole(parsed.GS, authorMember, role) {
parsed.GS.RUnlock()
return "Can't remove roles above you", nil
}
parsed.GS.RUnlock()
err = common.RemoveRoleDS(member, role.ID)
if err != nil {
return nil, err
}
// cancel the event to remove the role
scheduledevents2.CancelRemoveRole(parsed.Context(), parsed.GS.ID, parsed.Msg.Author.ID, role.ID)
action := MARemoveRole
action.Prefix = "Removed the role " + role.Name + " from "
if config.GiveRoleCmdModlog && config.IntActionChannel() != 0 {
CreateModlogEmbed(config.IntActionChannel(), parsed.Msg.Author, action, target, "", "")
}
return GenericCmdResp(action, target, 0, true, true), nil
},
},
}
func AdvancedDeleteMessages(channelID int64, filterUser int64, regex string, maxAge time.Duration, deleteNum, fetchNum int) (int, error) {
var compiledRegex *regexp.Regexp
if regex != "" {
// Start by compiling the regex
var err error
compiledRegex, err = regexp.Compile(regex)
if err != nil {
return 0, err
}
}
msgs, err := bot.GetMessages(channelID, fetchNum, false)
if err != nil {
return 0, err
}
toDelete := make([]int64, 0)
now := time.Now()
for i := len(msgs) - 1; i >= 0; i-- {
if filterUser != 0 && msgs[i].Author.ID != filterUser {
continue
}
// Can only bulk delete messages up to 2 weeks (but add 1 minute buffer account for time sync issues and other smallies)
if now.Sub(msgs[i].ParsedCreated) > (time.Hour*24*14)-time.Minute {
continue
}
// Check regex
if compiledRegex != nil {
if !compiledRegex.MatchString(msgs[i].Content) {
continue
}
}
// Check max age
if maxAge != 0 && now.Sub(msgs[i].ParsedCreated) > maxAge {
continue
}
toDelete = append(toDelete, msgs[i].ID)
//log.Println("Deleting", msgs[i].ContentWithMentionsReplaced())
if len(toDelete) >= deleteNum || len(toDelete) >= 100 {
break
}
}
if len(toDelete) < 1 {
return 0, nil
}
if len(toDelete) < 1 {
return 0, nil
} else if len(toDelete) == 1 {
err = common.BotSession.ChannelMessageDelete(channelID, toDelete[0])
} else {
err = common.BotSession.ChannelMessagesBulkDelete(channelID, toDelete)
}
return len(toDelete), err
}
func FindRole(gs *dstate.GuildState, roleS string) *discordgo.Role {
parsedNumber, parseErr := strconv.ParseInt(roleS, 10, 64)
gs.RLock()
defer gs.RUnlock()
if parseErr == nil {
// was a number, try looking by id
r := gs.RoleCopy(false, parsedNumber)
if r != nil {
return r
}
}
// otherwise search by name
for _, v := range gs.Guild.Roles {
trimmed := strings.TrimSpace(v.Name)
if strings.EqualFold(trimmed, roleS) {
return v
}
}
// couldn't find the role :(
return nil
}