-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Valkyrja-commands.cs
1047 lines (933 loc) · 41.6 KB
/
Valkyrja-commands.cs
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
using System;
using System.Collections;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Discord;
using Discord.Rest;
using Valkyrja.entities;
using Discord.WebSocket;
using guid = System.UInt64;
namespace Valkyrja.coreLite
{
public partial class ValkyrjaClient<T>: IValkyrjaClient, IDisposable where T: BaseConfig, new()
{
private readonly Regex RegexMentionHelp = new Regex(".*(help|commands).*", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private readonly Regex RegexPrefixHelp = new Regex(".*(command character|prefix).*", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private async Task HandleMentionResponse(Server server, SocketTextChannel channel, SocketMessage message)
{
if( this.CoreConfig.Debug )
Console.WriteLine("ValkyrjaClient: MentionReceived");
string responseString = "";
if( this.RegexMentionHelp.Match(message.Content).Success )
responseString = $"Use `{this.CoreConfig.CommandPrefix}help` command to search for things, or the manual pages (`{this.CoreConfig.CommandPrefix}man`) for specific and detailed information about a command";
else if( this.RegexPrefixHelp.Match(message.Content).Success )
responseString = string.IsNullOrEmpty(this.CoreConfig.CommandPrefix) ? "Command prefix is empty. Someone forgot to set it up?" : $"Try this: `{this.CoreConfig.CommandPrefix}`";
else
responseString = "<:ValkyrjaNomPing:509482352028942358>";
if( !string.IsNullOrEmpty(responseString) )
await SendRawMessageToChannel(channel, responseString);
}
private async Task InitSlashCommands()
{
try
{
SlashCommandBuilder pingCommand = new SlashCommandBuilder().WithName("ping").WithDescription("Verify basic functionality.")
.WithNameLocalizations(new Dictionary<string, string>()).WithDescriptionLocalizations(new Dictionary<string, string>()); //D.NET bug #2453
await this.DiscordClient.CreateGlobalApplicationCommandAsync(pingCommand.Build());
}
catch( Exception e )
{
await LogException(e, "InitSlashCommands");
}
}
private async Task ExecuteSlashCommand(SocketSlashCommand command)
{
if( command.CommandName == "ping" && command.GuildId.HasValue && this.Servers.ContainsKey(command.GuildId.Value) )
{
TimeSpan time = DateTime.UtcNow - Utils.GetTimeFromId(command.Id);
await command.RespondAsync(GetStatusString(time, this.Servers[command.GuildId.Value]), ephemeral: true);
}
}
private Task InitCommands()
{
Command newCommand = null;
// !restart
newCommand = new Command("restart");
newCommand.Type = CommandType.Standard;
newCommand.IsCoreCommand = true;
newCommand.Description = "Shut down the bot.";
newCommand.ManPage = new ManPage("", "");
newCommand.RequiredPermissions = PermissionType.OwnerOnly;
newCommand.OnExecute += async e => {
await e.SendReplySafe("bai");
await Task.Delay(1000);
Environment.Exit(0);
};
this.Commands.Add(newCommand.Id.ToLower(), newCommand);
this.Commands.Add("shutdown", newCommand.CreateAlias("shutdown"));
// !operations
newCommand = new Command("operations");
newCommand.Type = CommandType.Standard;
newCommand.IsCoreCommand = true;
newCommand.Description = "Display info about all queued or running operations on your server.";
newCommand.ManPage = new ManPage("", "");
newCommand.RequiredPermissions = PermissionType.ServerOwner | PermissionType.Admin;
newCommand.OnExecute += async e => {
StringBuilder response = new StringBuilder();
bool allOperations = IsAdmin(e.Message.Author.Id);
response.AppendLine($"Total operations in the queue: `{this.CurrentOperations.Count}`");
if( allOperations )
response.AppendLine($"Currently allocated data Memory: `{(GC.GetTotalMemory(false) / 1000000f):#0.00} MB`");
response.AppendLine();
lock( this.OperationsLock )
{
foreach( Operation op in this.CurrentOperations )
{
if( !allOperations && op.CommandArgs.Server.Id != e.Server.Id )
continue;
response.AppendLine(op.ToString());
if( allOperations )
response.AppendLine($"Server: `{op.CommandArgs.Server.Guild.Name}`\n" +
$"ServerID: `{op.CommandArgs.Server.Id}`\n" +
$"Allocated DataMemory: `{op.AllocatedMemoryStarted:#0.00} MB`\n");
}
}
string responseString = response.ToString();
if( string.IsNullOrEmpty(responseString) )
responseString = "There are no operations running.";
await e.SendReplySafe(responseString);
};
this.Commands.Add(newCommand.Id.ToLower(), newCommand);
// !cancel
newCommand = new Command("cancel");
newCommand.Type = CommandType.Standard;
newCommand.IsCoreCommand = true;
newCommand.Description = "Cancel queued or running operation - use in the same channel. (nuke, promoteEveryone, etc...)";
newCommand.ManPage = new ManPage("<CommandId>", "`<CommandId>` - running operation type command which this will interrupt.");
newCommand.RequiredPermissions = PermissionType.ServerOwner | PermissionType.Admin;
newCommand.OnExecute += async e => {
string responseString = "Operation not found.";
Operation operation = null;
if( !string.IsNullOrEmpty(e.TrimmedMessage) &&
(operation = this.CurrentOperations.FirstOrDefault(
op => op.CommandArgs.Channel.Id == e.Channel.Id &&
op.CommandArgs.Command.Id == e.TrimmedMessage)) != null )
responseString = "Operation canceled:\n\n" + operation.ToString();
await e.SendReplySafe(responseString);
};
this.Commands.Add(newCommand.Id.ToLower(), newCommand);
// !say
newCommand = new Command("say");
newCommand.Type = CommandType.Standard;
newCommand.Description = "Make the bot say something!";
newCommand.ManPage = new ManPage("<text>", "`<text>` - Text which the bot will repeat.");
newCommand.RequiredPermissions = PermissionType.ServerOwner | PermissionType.Admin | PermissionType.Moderator | PermissionType.SubModerator;
newCommand.DeleteRequest = true;
newCommand.IsBonusCommand = true;
newCommand.IsBonusAdminCommand = true;
newCommand.IsSupportCommand = true;
newCommand.OnExecute += async e => {
if( string.IsNullOrWhiteSpace(e.TrimmedMessage) )
{
await e.SendReplySafe("Say what?");
return;
}
await e.SendReplySafe(e.TrimmedMessage);
};
this.Commands.Add(newCommand.Id.ToLower(), newCommand);
// !edit
newCommand = new Command("edit");
newCommand.Type = CommandType.Standard;
newCommand.Description = "Edit a message the bot previously said!";
newCommand.ManPage = new ManPage("<MessageId> <text>", "`<MessageId>` - An ID of a message that will be edited.\n\n`<text>` - Text which the bot will repeat.");
newCommand.RequiredPermissions = PermissionType.ServerOwner | PermissionType.Admin | PermissionType.Moderator | PermissionType.SubModerator;
newCommand.DeleteRequest = true;
newCommand.IsBonusCommand = true;
newCommand.IsBonusAdminCommand = true;
newCommand.IsSupportCommand = true;
newCommand.OnExecute += async e => {
IMessage msg = null;
if( e.MessageArgs == null || e.MessageArgs.Length < 2 || !guid.TryParse(e.MessageArgs[0], out guid id) || (msg = await e.Channel.GetMessageAsync(id)) == null )
{
await e.SendReplySafe("Edit what?");
return;
}
switch( msg ) {
case RestUserMessage message:
await message?.ModifyAsync(m => m.Content = e.TrimmedMessage.Substring(e.MessageArgs[0].Length+1));
break;
case SocketUserMessage message:
await message?.ModifyAsync(m => m.Content = e.TrimmedMessage.Substring(e.MessageArgs[0].Length+1));
break;
default:
await e.SendReplySafe("GetMessage went bork.");
break;
}
};
this.Commands.Add(newCommand.Id.ToLower(), newCommand);
// !status
newCommand = new Command("status");
newCommand.Type = CommandType.Standard;
newCommand.IsCoreCommand = true;
newCommand.Description = "Display basic server status.";
newCommand.ManPage = new ManPage("", "");
newCommand.RequiredPermissions = PermissionType.Everyone;
newCommand.OnExecute += async e => {
TimeSpan time = DateTime.UtcNow - Utils.GetTimeFromId(e.Message.Id);
await e.SendReplySafe(GetStatusString(time, e.Server));
};
this.Commands.Add(newCommand.Id.ToLower(), newCommand);
this.Commands.Add("ping", newCommand.CreateAlias("ping"));
// !man
newCommand = new Command("man");
newCommand.Type = CommandType.Standard;
newCommand.Description = "Show detailed manual page for a command.";
newCommand.ManPage = new ManPage("<command>", "`<command>` - Command ID for which to display .");
newCommand.RequiredPermissions = PermissionType.Everyone;
newCommand.OnExecute += async e => {
string commandId = e.TrimmedMessage.ToLower();
string response = "I ain't got no real command like that. (This feature isn't a thing for Custom Commands!)";
if( string.IsNullOrEmpty(commandId) || (!e.Server.Commands.ContainsKey(commandId) && (!e.Server.CustomAliases.ContainsKey(commandId) || !e.Server.Commands.ContainsKey(commandId = e.Server.CustomAliases[commandId].CommandId))) )
{
await e.SendReplySafe(response);
return;
}
Command cmd = e.Server.Commands[commandId];
if( !string.IsNullOrEmpty(cmd.ParentId) && e.Server.Commands.ContainsKey(cmd.ParentId) )
cmd = e.Server.Commands[cmd.ParentId];
Embed embed = e.Server.GetManPage(cmd);
await e.Channel.SendMessageSafe(null, embed);
};
this.Commands.Add(newCommand.Id.ToLower(), newCommand);
this.Commands.Add("manual", newCommand.CreateAlias("manual"));
// !help
newCommand = new Command("help");
newCommand.Type = CommandType.Standard;
newCommand.Description = "PMs a list of Custom Commands for the server if used without arguments, or search for specific commands.";
newCommand.ManPage = new ManPage("[search expression]", "[search expression] - Optional argument to search for specific commands.");
newCommand.RequiredPermissions = PermissionType.Everyone;
newCommand.OnExecute += async e => {
await e.SendReplySafe($"`{this.CoreConfig.CommandPrefix}help` is merely a search. Use `{this.CoreConfig.CommandPrefix}man` for detailed manual page.");
StringBuilder response = new StringBuilder();
StringBuilder commandStrings = new StringBuilder();
bool isSpecific = !string.IsNullOrWhiteSpace(e.TrimmedMessage);
string prefix = this.CoreConfig.CommandPrefix;
List<string> includedCommandIds = new List<string>();
int count = 0;
bool cantPm = false;
async Task Append(string newString)
{
string pm = commandStrings.ToString();
if( !isSpecific && pm.Length + newString.Length >= BaseConfig.MessageCharacterLimit )
{
try
{
if( this.CoreConfig.HelpPrintsEverything )
await e.SendReplySafe(pm);
else
await e.Message.Author.SendMessageAsync(pm);
}
catch( Exception )
{
cantPm = true;
}
commandStrings.Clear();
}
commandStrings.AppendLine(newString);
}
async Task AddCustomAlias(string commandId)
{
string newString = "";
List<CustomAlias> aliases = e.Server.CustomAliases.Values.Where(a => a.CommandId == commandId).ToList();
int aliasCount = aliases.Count;
if( aliasCount > 0 )
{
newString = aliasCount == 1 ? " **-** Custom Alias: " : " **-** Custom Aliases: ";
for( int i = 0; i < aliasCount; i++ )
newString += $"{(i == 0 ? "`" : i == aliasCount - 1 ? " and `" : ", `")}{prefix}{aliases[i].Alias}`";
await Append(newString);
}
}
async Task AddCommand(Command cmd)
{
if( includedCommandIds.Contains(cmd.Id) )
return;
includedCommandIds.Add(cmd.Id);
string newString = $"\n```diff\n{(cmd.CanExecute(this, e.Server, e.Channel, e.Message.Author as SocketGuildUser) ? "+" : "-")}" +
$" {prefix}{cmd.Id}```" +
$" **-** {cmd.Description}";
if( cmd.Aliases != null && cmd.Aliases.Any() )
{
int aliasCount = cmd.Aliases.Count;
newString += aliasCount == 1 ? "\n **-** Alias: " : "\n **-** Aliases: ";
for( int i = 0; i < aliasCount; i++ )
newString += $"{(i == 0 ? "`" : i == aliasCount - 1 ? " and `" : ", `")}{prefix}{cmd.Aliases[i]}`";
}
await Append(newString);
await AddCustomAlias(cmd.Id);
}
async Task AddCustomCommand(CustomCommand cmd)
{
if( includedCommandIds.Contains(cmd.CommandId) )
return;
includedCommandIds.Add(cmd.CommandId);
string newString = $"\n```diff\n{(cmd.CanExecute(this, e.Server, e.Channel, e.Message.Author as SocketGuildUser) ? "+" : "-")}" +
$" {prefix}{cmd.CommandId}```";
if( !string.IsNullOrWhiteSpace(cmd.Description) )
newString += $"\n **-** {cmd.Description}";
await Append(newString);
await AddCustomAlias(cmd.CommandId);
}
if( isSpecific )
{
string expression = e.TrimmedMessage.Replace(" ", "|") + ")\\w*";
if( e.MessageArgs.Length > 1 )
expression += "(" + expression;
Regex regex = new Regex($"\\w*({expression}", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(10f));
foreach( Command cmd in e.Server.Commands.Values )
{
if( !cmd.IsHidden &&
cmd.RequiredPermissions != PermissionType.OwnerOnly &&
regex.Match(cmd.Id).Success )
{
Command command = cmd;
if( cmd.IsAlias && e.Server.Commands.ContainsKey(cmd.ParentId.ToLower()) )
command = e.Server.Commands[cmd.ParentId.ToLower()];
if( includedCommandIds.Contains(command.Id) )
continue;
if( ++count > 5 )
break;
await AddCommand(cmd);
}
}
foreach( CustomCommand cmd in e.Server.CustomCommands.Values )
{
if( regex.Match(cmd.CommandId).Success ) //Chances are that it's gonna fail more often.
{
if( ++count > 5 )
break;
await AddCustomCommand(cmd);
}
}
foreach( CustomAlias alias in e.Server.CustomAliases.Values )
{
if( regex.Match(alias.Alias).Success ) //Chances are that it's gonna fail more often.
{
if( ++count > 5 )
break;
if( e.Server.Commands.ContainsKey(alias.CommandId.ToLower()) )
await AddCommand(e.Server.Commands[alias.CommandId.ToLower()]);
else if( e.Server.CustomCommands.ContainsKey(alias.CommandId.ToLower()) )
{
await AddCustomCommand(e.Server.CustomCommands[alias.CommandId.ToLower()]);
}
}
}
if( count == 0 )
response.AppendLine("I did not find any commands matching your search expression.");
else
{
if( count > 5 )
response.AppendLine("I found too many commands matching your search expression. **Here are the first five:**");
response.Append(commandStrings.ToString());
}
}
else if( this.CoreConfig.HelpPrintsEverything )
{
foreach( Command cmd in e.Server.Commands.Values.Where(cmd => !cmd.IsAlias) )
{
await AddCommand(cmd);
}
response.Append(commandStrings.ToString());
}
else if( e.Server.CustomCommands.Any() ) //Not specific - PM CustomCommands.
{
foreach( CustomCommand cmd in e.Server.CustomCommands.Values )
{
await AddCustomCommand(cmd);
}
try
{
await e.Message.Author.SendMessageSafe(commandStrings.ToString());
response.AppendLine("I've PMed you the Custom Commands for this server.");
}
catch( Exception )
{
cantPm = true;
}
}
if( cantPm )
response.AppendLine("And I was unable to PM you the Custom Commands for this server. (Fix your privacy settings or unblock me.)");
await e.SendReplySafe(response.ToString());
};
this.Commands.Add(newCommand.Id.ToLower(), newCommand);
// !alias
/*newCommand = new Command("alias");
newCommand.Type = CommandType.Standard;
newCommand.Description = "Manage command aliases, use without parameters for more details.";
newCommand.ManPage = new ManPage("<list|create|delete> [alias] [command]", "`list` - Display the list of custom aliases.\n\n`create alias command` - Create a new `alias` to the `command`.\n\n`delete alias` - Delete the `alias`.");
newCommand.RequiredPermissions = PermissionType.ServerOwner | PermissionType.Admin;
newCommand.OnExecute += async e => {
if(e.MessageArgs == null || e.MessageArgs.Length == 0 || !(
(e.MessageArgs.Length == 1 && e.MessageArgs[0] == "list") ||
(e.MessageArgs.Length == 2 && (e.MessageArgs[0] == "remove" || e.MessageArgs[0] == "delete")) ||
(e.MessageArgs.Length == 3 && (e.MessageArgs[0] == "add" || e.MessageArgs[0] == "create")) ))
{
await e.SendReplySafe(string.Format(
"Use this command with the following parameters:\n" +
" `{0}{1} list` - Display the list of your custom aliases.\n" +
" `{0}{1} create alias command` - Create a new `alias` to the old `command`.\n" +
" `{0}{1} delete alias` - Delete the `alias`.\n",
e.Server.Config.CommandPrefix,
e.Command.Id
));
return;
}
string responseString = "";
switch(e.MessageArgs[0])
{
case "list":
{
if( e.Server.CustomAliases == null || !e.Server.CustomAliases.Any() )
{
responseString = "There aren't any! O_O";
break;
}
StringBuilder response = new StringBuilder();
response.AppendLine("Command-Aliases on this server:\n```http\nexampleAlias: command\n---------------------");
foreach( CustomAlias alias in e.Server.CustomAliases.Values )
{
string line = alias.Alias + ": " + alias.CommandId;
if( line.Length + response.Length + 5 > GlobalConfig.MessageCharacterLimit )
{
await e.SendReplySafe(response.ToString() + "\n```");
response.Clear().AppendLine("```http\nexampleAlias: command\n---------------------");
}
response.AppendLine(line);
}
response.Append("```");
responseString = response.ToString();
}
break;
case "create":
case "add":
{
CustomAlias alias = new CustomAlias(){
Alias = e.MessageArgs[1],
CommandId = e.MessageArgs[2],
ServerId = e.Server.Id
};
if( e.Server.Commands.ContainsKey(alias.Alias.ToLower()) ||
e.Server.CustomCommands.ContainsKey(alias.Alias.ToLower()) ||
e.Server.CustomAliases.ContainsKey(alias.Alias.ToLower()) )
{
responseString = $"I already have a command with this name (`{alias.Alias}`)";
break;
}
if( !e.Server.Commands.ContainsKey(alias.CommandId.ToLower()) &&
!e.Server.CustomCommands.ContainsKey(alias.CommandId.ToLower()) )
{
responseString = $"Target command not found (`{alias.CommandId}`)";
break;
}
if( e.Server.Commands.ContainsKey(alias.CommandId.ToLower()) )
{
Command cmd = e.Server.Commands[alias.CommandId.ToLower()];
if( cmd.IsAlias && !string.IsNullOrEmpty(cmd.ParentId) )
alias.CommandId = cmd.ParentId;
}
ServerContext dbContext = ServerContext.Create(this.DbConnectionString);
dbContext.CustomAliases.Add(alias);
dbContext.SaveChanges();
dbContext.Dispose();
responseString = $"Alias `{e.Server.Config.CommandPrefix}{alias.Alias}` created.";
}
break;
case "delete":
case "remove":
{
if( e.Server.CustomAliases == null || !e.Server.CustomAliases.ContainsKey(e.MessageArgs[1].ToLower()) )
{
responseString = $"Alias not found. (`{e.MessageArgs[1]}`)";
break;
}
ServerContext dbContext = ServerContext.Create(this.DbConnectionString);
CustomAlias alias = new CustomAlias{ServerId = e.Server.Id, Alias = e.MessageArgs[1]};
dbContext.CustomAliases.Attach(alias);
dbContext.CustomAliases.Remove(alias);
dbContext.SaveChanges();
dbContext.Dispose();
responseString = $"RIP `{e.Server.Config.CommandPrefix}{alias.Alias}`.";
}
break;
default:
responseString = "Unknown property.";
return;
}
await e.SendReplySafe(responseString);
};
this.Commands.Add(newCommand.Id.ToLower(), newCommand);*/
// !listPermissions
/*newCommand = new Command("listPermissions");
newCommand.Type = CommandType.Standard;
newCommand.IsCoreCommand = true;
newCommand.Description = "List permissions of all the commands.";
newCommand.ManPage = new ManPage("", "");
newCommand.RequiredPermissions = PermissionType.ServerOwner;
newCommand.OnExecute += async e => {
string response = "";
ServerContext dbContext = ServerContext.Create(this.DbConnectionString);
StringBuilder responseBuilder = new StringBuilder();
foreach( Command cmd in this.Commands.Values.OrderBy(c => c.RequiredPermissions) )
{
if( cmd.IsAlias || cmd.IsHidden || cmd.IsCoreCommand || cmd.RequiredPermissions == PermissionType.OwnerOnly )
continue;
CommandOptions options = dbContext.GetOrAddCommandOptions(e.Server, cmd.Id);
responseBuilder.Append($"`{cmd.Id}`: `{options.PermissionOverrides.ToString()}`");
if( options.PermissionOverrides == PermissionOverrides.Default )
{
PermissionOverrides permissions = PermissionOverrides.Default;
switch(cmd.RequiredPermissions)
{
case PermissionType.ServerOwner:
permissions = PermissionOverrides.ServerOwner;
break;
case PermissionType.ServerOwner | PermissionType.Admin:
permissions = PermissionOverrides.Admins;
break;
case PermissionType.ServerOwner | PermissionType.Admin | PermissionType.Moderator:
permissions = PermissionOverrides.Moderators;
break;
case PermissionType.ServerOwner | PermissionType.Admin | PermissionType.Moderator | PermissionType.SubModerator:
permissions = PermissionOverrides.SubModerators;
break;
case PermissionType.ServerOwner | PermissionType.Admin | PermissionType.Moderator | PermissionType.SubModerator | PermissionType.Member:
permissions = PermissionOverrides.Members;
break;
default:
permissions = PermissionOverrides.Everyone;
break;
}
responseBuilder.AppendLine($" -> `{permissions.ToString()}`");
}
else
responseBuilder.AppendLine();
}
response = responseBuilder.ToString();
dbContext.Dispose();
await e.SendReplySafe(response);
};
this.Commands.Add(newCommand.Id.ToLower(), newCommand);*/
// !permissions
/*newCommand = new Command("permissions");
newCommand.Type = CommandType.Standard;
newCommand.IsCoreCommand = true;
newCommand.Description = "Configure permission groups for every command. Use without parameters for help.";
newCommand.ManPage = new ManPage("<CommandId> [PermissionGroup]", "`<CommandId>` - name of the command for which you would like to display or change permissions.\n\n" +
"`[PermissionGroup]` - Optional argument to change the permissions of the CommandId - one of:\n" +
"* `ServerOwner`, `Admins`, `Moderators`, `SubModerators` or `Members`, which are configured on the website.\n" +
"* `Everyone`, `Nobody` and `Default` (will set default permissions as seen in the webdocs.)\n");
newCommand.RequiredPermissions = PermissionType.ServerOwner;
newCommand.OnExecute += async e => {
string response = string.Format(
"Use this command with the following parameters:\n" +
" `{0}{1} CommandID PermissionGroup` - where `CommandID` is name of the command, and `PermissionGroups` can be:\n" +
" `ServerOwner`, `Admins`, `Moderators`, `SubModerators`, `Members`, `Everyone` - Look at the docs for reference: <https://valkyrja.app/docs>\n" +
" `Nobody` - Block this command from execusion even by Server Owner.\n" +
" `Default` - will set default permissions as seen in the docs.\n"+
" For example `{0}{1} nuke ServerOwner`",
e.Server.Config.CommandPrefix,
e.Command.Id);
if( e.MessageArgs == null || e.MessageArgs.Length < 1)
{
await e.SendReplySafe(response);
return;
}
string commandId = "";
ServerContext dbContext = ServerContext.Create(this.DbConnectionString);
if( string.IsNullOrEmpty(commandId = e.Server.GetCommandOptionsId(e.MessageArgs[0])) )
{
if( commandId == null )
{
response = "I'm sorry but you can not restrict this command.";
}
else if( commandId == "" )
{
response = $"Command `{e.MessageArgs[0]}` not found.";
}
}
else if( e.MessageArgs.Length == 1 )
{
StringBuilder responseBuilder = new StringBuilder();
CommandOptions options = dbContext.GetOrAddCommandOptions(e.Server, commandId);
responseBuilder.Append($"Current permissions for `{commandId}` are:\n" +
$"`{options.PermissionOverrides.ToString()}`");
if( options.PermissionOverrides == PermissionOverrides.Default )
{
Command command = null;
CustomCommand customCommand = null;
if( e.Server.Commands.ContainsKey(commandId.ToLower()) && (command = e.Server.Commands[commandId.ToLower()]) != null )
{
PermissionOverrides permissions = PermissionOverrides.Default;
switch(command.RequiredPermissions)
{
case PermissionType.ServerOwner:
permissions = PermissionOverrides.ServerOwner;
break;
case PermissionType.ServerOwner | PermissionType.Admin:
permissions = PermissionOverrides.Admins;
break;
case PermissionType.ServerOwner | PermissionType.Admin | PermissionType.Moderator:
permissions = PermissionOverrides.Moderators;
break;
case PermissionType.ServerOwner | PermissionType.Admin | PermissionType.Moderator | PermissionType.SubModerator:
permissions = PermissionOverrides.SubModerators;
break;
case PermissionType.ServerOwner | PermissionType.Admin | PermissionType.Moderator | PermissionType.SubModerator | PermissionType.Member:
permissions = PermissionOverrides.Members;
break;
default:
permissions = PermissionOverrides.Everyone;
break;
}
responseBuilder.Append($" -> `{permissions.ToString()}`");
}
else if( e.Server.CustomCommands.ContainsKey(commandId.ToLower()) && (customCommand = e.Server.CustomCommands[commandId.ToLower()]) != null )
responseBuilder.Append($" -> `{PermissionOverrides.Everyone}`");
}
if( options.DeleteReply && options.DeleteRequest )
responseBuilder.Append("\n+ This command will attempt to delete both, the message that issued the command and my response.");
else if( options.DeleteReply )
responseBuilder.Append("\n+ This command will attempt to delete my response.");
else if( options.DeleteRequest )
responseBuilder.Append("\n+ This command will attempt to delete the message that issued the command.");
IEnumerable<CommandChannelOptions> channelBlacklist = dbContext.CommandChannelOptions.AsQueryable().Where(c => c.ServerId == e.Server.Id && c.CommandId == commandId && c.Blacklisted);
IEnumerable<CommandChannelOptions> channelWhitelist = dbContext.CommandChannelOptions.AsQueryable().Where(c => c.ServerId == e.Server.Id && c.CommandId == commandId && c.Whitelisted);
if( channelBlacklist.Any() )
{
responseBuilder.Append("\n+ This command can not be invoked in any of the following channels:");
foreach( CommandChannelOptions channelOptions in channelBlacklist )
{
responseBuilder.Append($"\n <#{channelOptions.ChannelId}>");
}
}
if( channelWhitelist.Any() )
{
responseBuilder.Append("\n+ This command can be invoked only in the following channels:");
foreach( CommandChannelOptions channelOptions in channelWhitelist )
{
if( channelBlacklist.Any(c => c.ChannelId == channelOptions.ChannelId) )
continue;
responseBuilder.Append($"\n<#{channelOptions.ChannelId}>");
}
}
response = responseBuilder.ToString();
}
else if( e.MessageArgs.Length == 2 && Enum.TryParse(e.MessageArgs[1], true, out PermissionOverrides permissionOverrides) )
{
CommandOptions options = dbContext.GetOrAddCommandOptions(e.Server, commandId);
options.PermissionOverrides = permissionOverrides;
dbContext.SaveChanges();
response = "All set!";
}
dbContext.Dispose();
await e.SendReplySafe(response);
};
this.Commands.Add(newCommand.Id.ToLower(), newCommand);*/
// !deleteRequest
/*newCommand = new Command("deleteRequest");
newCommand.Type = CommandType.Standard;
newCommand.Description = "Set a command to have the issuing request message deleted automatically.";
newCommand.RequiredPermissions = PermissionType.ServerOwner | PermissionType.Admin;
newCommand.ManPage = new ManPage("<CommandId> <true|false>", "`<CommandId>` - The command for which to change the delete settings.\n\n`<true|false>` - True to delete the issuing request message.");
newCommand.OnExecute += async e => {
if( e.MessageArgs == null || e.MessageArgs.Length < 2 ||
!bool.TryParse(e.MessageArgs[1], out bool deleteRequest) )
{
await e.SendReplySafe("Invalid parameters...\n" + e.Command.Description);
return;
}
string commandId = e.Server.GetCommandOptionsId(e.MessageArgs[0]);
if( commandId == null )
{
await e.SendReplySafe("I'm sorry but you can not restrict this command.");
return;
}
if( commandId == "" )
{
await e.SendReplySafe($"Command `{e.MessageArgs[0]}` not found.");
return;
}
ServerContext dbContext = ServerContext.Create(this.DbConnectionString);
CommandOptions options = dbContext.GetOrAddCommandOptions(e.Server, commandId);
options.DeleteRequest = deleteRequest;
dbContext.SaveChanges();
dbContext.Dispose();
await e.SendReplySafe("Okay...");
};
this.Commands.Add(newCommand.Id.ToLower(), newCommand);
this.Commands.Add("removerequest", newCommand.CreateAlias("removeRequest"));*/
// !deleteReply
/*newCommand = new Command("deleteReply");
newCommand.Type = CommandType.Standard;
newCommand.Description = "Set a command to delete bot's replies in a few seconds. _(Only some commands support this, if you find one that doesn't, submit a feature request!)_";
newCommand.ManPage = new ManPage("<CommandId> <true|false>", "`<CommandId>` - The command for which to change the delete settings.\n\n`<true|false>` - True to delete the bots response message.");
newCommand.RequiredPermissions = PermissionType.ServerOwner | PermissionType.Admin;
newCommand.OnExecute += async e => {
if( e.MessageArgs == null || e.MessageArgs.Length < 2 ||
!bool.TryParse(e.MessageArgs[1], out bool deleteReply) )
{
await e.SendReplySafe("Invalid parameters...\n" + e.Command.Description);
return;
}
string commandId = e.Server.GetCommandOptionsId(e.MessageArgs[0]);
if( commandId == null )
{
await e.SendReplySafe("I'm sorry but you can not restrict this command.");
return;
}
if( commandId == "" )
{
await e.SendReplySafe($"Command `{e.MessageArgs[0]}` not found.");
return;
}
ServerContext dbContext = ServerContext.Create(this.DbConnectionString);
CommandOptions options = dbContext.GetOrAddCommandOptions(e.Server, commandId);
options.DeleteReply = deleteReply;
dbContext.SaveChanges();
dbContext.Dispose();
await e.SendReplySafe("Okay...");
};
this.Commands.Add(newCommand.Id.ToLower(), newCommand);
this.Commands.Add("removereply", newCommand.CreateAlias("removeReply"));*/
// !cmdChannelWhitelist
/*newCommand = new Command("cmdChannelWhitelist");
newCommand.Type = CommandType.Standard;
newCommand.Description = "Allow a command to be ran only in certain channels.";
newCommand.ManPage = new ManPage("<CommandId> <add|remove> <ChannelId>", "`<CommandId>` - The command for which to change the restriction settings.\n\n`<add|remove>` - Add or remove to/from the restriction list.\n\n`<ChannelId>` - Id or mention of the channel in which to allow the execution of the Command");
newCommand.RequiredPermissions = PermissionType.ServerOwner | PermissionType.Admin;
newCommand.OnExecute += async e => {
if( e.MessageArgs == null || e.MessageArgs.Length < 2 ||
(e.MessageArgs[1].ToLower() != "add" && e.MessageArgs[1].ToLower() != "remove") ||
!guid.TryParse(e.MessageArgs[2].Trim('<', '#', '>'), out guid channelId) || e.Server.Guild.GetChannel(channelId) == null )
{
await e.SendReplySafe("Invalid parameters...\n" + e.Command.Description);
return;
}
string commandId = e.Server.GetCommandOptionsId(e.MessageArgs[0]);
if( commandId == null )
{
await e.SendReplySafe("I'm sorry but you can not restrict this command.");
return;
}
if( commandId == "" )
{
await e.SendReplySafe($"Command `{e.MessageArgs[0]}` not found.");
return;
}
ServerContext dbContext = ServerContext.Create(this.DbConnectionString);
CommandChannelOptions commandOptions = dbContext.GetOrAddCommandChannelOptions(e.Server.Id, channelId, commandId);
string responseString = "Success! \\o/";
switch(e.MessageArgs[1].ToLower())
{
case "add":
commandOptions.Allowed = true;
dbContext.SaveChanges();
break;
case "remove":
commandOptions.Allowed = false;
dbContext.SaveChanges();
break;
default:
responseString = "Invalid parameters...\n" + e.Command.Description;
break;
}
dbContext.Dispose();
await e.SendReplySafe(responseString);
};
this.Commands.Add(newCommand.Id.ToLower(), newCommand);
// !cmdChannelWhitelistAllCC
newCommand = new Command("cmdChannelWhitelistAllCC");
newCommand.Type = CommandType.Standard;
newCommand.Description = "Allow all custom commands to be ran only in certain channels.";
newCommand.ManPage = new ManPage("<add|remove> <ChannelId>", "`<add|remove>` - Add or remove to/from the restriction list.\n\n`<ChannelId>` - Id or mention of the channel in which to allow the execution of the CustomCommands");
newCommand.RequiredPermissions = PermissionType.ServerOwner | PermissionType.Admin;
newCommand.OnExecute += async e => {
if( e.MessageArgs == null || e.MessageArgs.Length < 2 ||
(e.MessageArgs[0].ToLower() != "add" && e.MessageArgs[0].ToLower() != "remove") ||
!guid.TryParse(e.MessageArgs[1].Trim('<', '#', '>'), out guid channelId) || e.Server.Guild.GetChannel(channelId) == null )
{
await e.SendReplySafe("Invalid parameters...\n" + e.Command.Description);
return;
}
string responseString = "Success! \\o/";
ServerContext dbContext = ServerContext.Create(this.DbConnectionString);
foreach( CustomCommand cmd in e.Server.CustomCommands.Values )
{
CommandChannelOptions commandOptions = dbContext.GetOrAddCommandChannelOptions(e.Server.Id, channelId, cmd.CommandId);
switch( e.MessageArgs[0].ToLower() )
{
case "add":
commandOptions.Allowed = true;
dbContext.SaveChanges();
break;
case "remove":
commandOptions.Allowed = false;
dbContext.SaveChanges();
break;
default:
responseString = "Invalid parameters...\n" + e.Command.Description;
break;
}
}
dbContext.Dispose();
await e.SendReplySafe(responseString);
};
this.Commands.Add(newCommand.Id.ToLower(), newCommand);
// !cmdChannelBlacklist
newCommand = new Command("cmdChannelBlacklist");
newCommand.Type = CommandType.Standard;
newCommand.Description = "Block a command from certain channels.";
newCommand.ManPage = new ManPage("<CommandId> <add|remove> <ChannelId>", "`<CommandId>` - The command for which to change the restriction settings.\n\n`<add|remove>` - Add or remove to/from the restriction list.\n\n`<ChannelId>` - Id or mention of the channel in which to deny the execution of the Command");
newCommand.RequiredPermissions = PermissionType.ServerOwner | PermissionType.Admin;
newCommand.OnExecute += async e => {
if( e.MessageArgs == null || e.MessageArgs.Length < 2 ||
(e.MessageArgs[1].ToLower() != "add" && e.MessageArgs[1].ToLower() != "remove") ||
!guid.TryParse(e.MessageArgs[2].Trim('<', '#', '>'), out guid channelId) || e.Server.Guild.GetChannel(channelId) == null )
{
await e.SendReplySafe("Invalid parameters...\n" + e.Command.Description);
return;
}
string commandId = e.Server.GetCommandOptionsId(e.MessageArgs[0]);
if( commandId == null )
{
await e.SendReplySafe("I'm sorry but you can not restrict this command.");
return;
}
if( commandId == "" )
{
await e.SendReplySafe($"Command `{e.MessageArgs[0]}` not found.");
return;
}
ServerContext dbContext = ServerContext.Create(this.DbConnectionString);
CommandChannelOptions commandOptions = dbContext.GetOrAddCommandChannelOptions(e.Server.Id, channelId, commandId);
string responseString = "Success! \\o/";
switch(e.MessageArgs[1].ToLower())
{
case "add":
commandOptions.Blocked = true;
dbContext.SaveChanges();
break;
case "remove":
commandOptions.Blocked = false;
dbContext.SaveChanges();
break;
default:
responseString = "Invalid parameters...\n" + e.Command.Description;
break;
}
dbContext.Dispose();
await e.SendReplySafe(responseString);
};
this.Commands.Add(newCommand.Id.ToLower(), newCommand);
// !cmdChannelBlacklistAllCC
newCommand = new Command("cmdChannelBlacklistAllCC");
newCommand.Type = CommandType.Standard;
newCommand.Description = "Block all custom commands from certain channels.";
newCommand.ManPage = new ManPage("<add|remove> <ChannelId>", "`<add|remove>` - Add or remove to/from the restriction list.\n\n`<ChannelId>` - Id or mention of the channel in which to deny the execution of the CustomCommands");
newCommand.RequiredPermissions = PermissionType.ServerOwner | PermissionType.Admin;
newCommand.OnExecute += async e => {
if( e.MessageArgs == null || e.MessageArgs.Length < 2 ||
(e.MessageArgs[0].ToLower() != "add" && e.MessageArgs[0].ToLower() != "remove") ||
!guid.TryParse(e.MessageArgs[1].Trim('<', '#', '>'), out guid channelId) || e.Server.Guild.GetChannel(channelId) == null )
{
await e.SendReplySafe("Invalid parameters...\n" + e.Command.Description);
return;
}
string responseString = "Success! \\o/";
ServerContext dbContext = ServerContext.Create(this.DbConnectionString);
foreach( CustomCommand cmd in e.Server.CustomCommands.Values )
{
CommandChannelOptions commandOptions = dbContext.GetOrAddCommandChannelOptions(e.Server.Id, channelId, cmd.CommandId);
switch( e.MessageArgs[0].ToLower() )
{
case "add":
commandOptions.Blocked = true;
dbContext.SaveChanges();
break;
case "remove":
commandOptions.Blocked = false;
dbContext.SaveChanges();
break;
default:
responseString = "Invalid parameters...\n" + e.Command.Description;
break;
}
}
dbContext.Dispose();
await e.SendReplySafe(responseString);
};
this.Commands.Add(newCommand.Id.ToLower(), newCommand);
// !cmdResetRestrictions
newCommand = new Command("cmdResetRestrictions");
newCommand.Type = CommandType.Standard;
newCommand.Description = "Reset restrictions placed on a command by the _cmdChannelWhitelist_ and _cmdChannelBlacklist_ commands. Use with the `CommandID` as parameter.";
newCommand.RequiredPermissions = PermissionType.ServerOwner | PermissionType.Admin;
newCommand.OnExecute += async e => {
if( e.MessageArgs == null || e.MessageArgs.Length < 1 )
{
await e.SendReplySafe("Invalid parameters...\n" + e.Command.Description);
return;
}
string commandId = e.Server.GetCommandOptionsId(e.MessageArgs[0]);
if( string.IsNullOrEmpty(commandId) )
{
await e.SendReplySafe($"Command `{e.MessageArgs[0]}` not found.");
return;
}
ServerContext dbContext = ServerContext.Create(this.DbConnectionString);
await dbContext.CommandChannelOptions.AsQueryable().Where(c => c.ServerId == e.Server.Id && c.CommandId == commandId)
.ForEachAsync(c => c.Blacklisted = c.Whitelisted = false);