-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilsCommands.cs
2554 lines (2195 loc) · 109 KB
/
UtilsCommands.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.Text.RegularExpressions;
namespace utilities_cs {
/// <summary>
/// The base class of all command-classes for all commands in utilities-cs.
/// </summary>
public class Command {
/// <summary>
/// The primary name of the command.
/// </summary>
public string? CommandName { get; set; }
/// <summary>
/// A command's aliases.
/// </summary>
public string[]? Aliases { get; set; }
/// <summary>
/// A dictionary of command names to methods (For FormattableCommands).
/// </summary>
public static Dictionary<string, Func<string[], bool, bool, string?>> FCommands = [];
/// <summary>
/// A dictionary of command names to methods (For RegularCommands).
/// </summary>
public static Dictionary<string, Action<string[]>> RCommands = [];
/// <summary>
/// Executes a command in either the RCommands dictionary or the FCommands dictionary.
/// </summary>
/// <param name="cmd">The name of the command to be excuted.</param>
/// <param name="args">The command arguments to be used when executing the command.</param>
/// <param name="copy">Controls whether the function is willing to copy text to the clipboard.</param>
/// <param name="notif">Controls whether the function is willing to send a notification.</param>
/// <returns>A string of the output of the command. This can also be null.</returns>
public static string? ExecuteCommand(string[] args, bool copy = true, bool notif = true) {
string cmd = args[0].ToLower();
if (FCommands.TryGetValue(cmd, out var fcommand)) {
string? output = fcommand.Invoke(args, copy, notif);
if (output != null) { return output; } else { return null; }
} else if (RCommands.TryGetValue(cmd, out var rcommand)) {
rcommand.Invoke(args);
return null;
} else if (Force.AreAnyForced()) {
args = Enumerable.Concat(["cmd"], args).ToArray<string>();
string? output = Force.forced!.Function!.Invoke(args, copy, notif);
if (output != null) { return output; } else { return null; }
} else {
Utils.NotifCheck(
true,
[
"Exception", "Invalid command, try 'help' for more info.", "4"
], "executeCommandError"
); return null;
}
}
/// <summary>
/// A simple method that checks if a certain command exists.
/// </summary>
/// <param name="cmd">The name of the command</param>
/// <returns>True or False based on if the command exists, or not.</returns>
public static bool Exists(string cmd) {
if (FCommands.ContainsKey(cmd)) {
return true;
} else if (RCommands.ContainsKey(cmd)) {
return true;
} else {
return false;
}
}
/// <summary>
/// Gets the Method of a Formattable OR Regular Command.
/// </summary>
/// <param name="commandName">The name of the command.</param>
/// <returns>Returns the method of the formattable/regular command.</returns>
public static object? GetMethod(string commandName) {
if (Exists(commandName)) {
if (FCommands.TryGetValue(commandName, out var fname)) {
return fname;
} else if (RCommands.TryGetValue(commandName, out var rname)) {
return rname;
} else {
return null;
}
}
return null;
}
/// <summary>
/// Gets all the aliases for a command.
/// </summary>
/// <param name="commandName">The name of the command.</param>
/// <returns>A list of all the aliases, or null if the command does not exist.</returns>
public static List<string>? GetAliases(string commandName) {
if (FCommands.TryGetValue(commandName, out var fname)) {
var aliases = FCommands.Where(kvp => kvp.Value == fname)
.Select(kvp => kvp.Key)
.ToList();
return aliases;
} else if (RCommands.TryGetValue(commandName, out var rname)) {
var aliases = RCommands.Where(kvp => kvp.Value == rname)
.Select(kvp => kvp.Key)
.ToList();
return aliases;
}
return null;
}
}
/// <summary>
/// The class that supports formattable commands.
/// </summary>
public class FormattableCommand : Command {
/// <summary>
/// The function that will be executed when the command is called.
/// </summary>
public Func<string[], bool, bool, string?>? Function;
/// <summary>
/// Denotes whether this specific command will be used in the all command.
/// </summary>
public bool UseInAllCommand;
/// <summary>
/// If UseInAllCommand is true, then this denotes what all-command-mode the command will be used in.
/// </summary>
public string? AllCommandMode;
/// <summary>
/// List of all registered FormattableCommands.
/// </summary>
public static List<FormattableCommand> FormattableCommands = [];
/// <summary>
/// Initializes a new instance of a FormattableCommand.
/// </summary>
/// <param name="commandName">The commandName for the FormattableCommand.</param>
/// <param name="function">The function for the FormattableCommand.</param>
/// <param name="aliases">The aliases for the FormattableCommand.</param>
/// <param name="useInAllCommand">Denotes whether the command should be included in the all command.</param>
/// <param name="allCommandMode">The mode for the all command that the command is to be included in.</param>
public FormattableCommand(
string commandName,
Func<string[], bool, bool, string?> function,
string[]? aliases = null,
bool useInAllCommand = false,
string allCommandMode = "none"
) {
//* setting all attributes for instance
CommandName = commandName; Function = function; Aliases = aliases;
UseInAllCommand = useInAllCommand; AllCommandMode = allCommandMode;
if (aliases != null) {
FCommands.Add(commandName, function);
foreach (string alias in aliases) { FCommands.Add(alias, function); }
} else { FCommands.Add(commandName, function); }
FormattableCommands.Add(this);
}
/// <summary>
/// A non-static command that allows you to execute a command immediately.
/// </summary>
/// <param name="args">The command arguments to be used when executing the command.</param>
/// <param name="copy">Controls whether the function is willing to copy text to the clipboard.</param>
/// <param name="notif">Controls whether the function is willing to send a notification.</param>
//! Mostly unused method. Only used for testing purposes.
public string? Execute(string[] args, bool copy, bool notif) {
if (Function != null) {
string? output = Function.Invoke(args, copy, notif);
if (output != null) { Console.WriteLine(output); return output; }
}
return null;
}
/// <summary>
/// Lists all currently registered FormattableCommands.
/// </summary>
/// <returns>A string with all currently registered Commands, seperated by newlines.</returns>
public static string ListAllFCommands() {
List<string> fCommandsList = [];
foreach (KeyValuePair<string, Func<string[], bool, bool, string?>> i in FCommands) {
fCommandsList.Add(i.Key);
}
return string.Join("\n", fCommandsList);
}
/// <summary>
/// Finds the command in the fCommands dictionary and then executes it.
/// </summary>
/// <param name="cmd">The command to execute.</param>
/// <param name="args">The command arguments to be used when executing the command.</param>
/// <param name="copy">Controls whether the function is willing to copy text to the clipboard.</param>
/// <param name="notif">Controls whether the function is willing to send a notification.</param>
/// <returns>The output of the method that is ran. Value can be null.</returns>
public static string? FindAndExecute(string cmd, string[] args, bool copy, bool notif) {
if (FCommands.TryGetValue(cmd, out var fcommand)) {
string? output = fcommand.Invoke(args, copy, notif);
if (output != null) { return output; } else { return null; }
} else {
return null;
}
}
/// <summary>
/// Returns every command that supports use in the 'all' command.
/// </summary>
/// <param name="mode">Mode for the command, fancy/encoding</param>
/// <returns></returns>
public static List<FormattableCommand> GetMethodsSupportedByAll(string mode) {
List<FormattableCommand> methodsSupportedByAll = [];
FormattableCommands?.ForEach(
i => { if (i.UseInAllCommand && i.AllCommandMode == mode) { methodsSupportedByAll.Add(i); } }
);
return methodsSupportedByAll;
}
/// <summary>
/// Returns a FormattableCommand using the name of that command.
/// </summary>
/// <param name="cmd">The name of the command that is used to find the method and return it.</param>
/// <returns>The method of that command name.</returns>
public static Func<string[], bool, bool, string?>? GetFMethod(string cmd) {
if (FCommands.TryGetValue(cmd, out var fcommand)) {
Func<string[], bool, bool, string?> func = fcommand;
return func;
} else {
return null;
}
}
/// <summary>
/// Returns a FormattableCommand using the name of that command.
/// </summary>
/// <param name="cmd">The name of the command.</param>
/// <returns>A FormattableCommand based on the "cmd" that is passed.</returns>
public static FormattableCommand? GetFormattableCommand(string cmd) {
foreach (FormattableCommand i in FormattableCommands) {
if (i.CommandName == cmd) {
return i;
} else if (i.Aliases != null) {
if (i.Aliases.Any(x => x == cmd)) { return i; }
}
}
return null;
}
/// <summary>
/// Checks if a FormattableCommand exists using the name of its name.
/// </summary>
/// <param name="cmd">The name of the command.</param>
/// <returns>True if the command exists, else false.</returns>
public static bool FormattableCommandExists(string cmd) { return FCommands.ContainsKey(cmd); }
}
/// <summary>
/// The class that supports regular commands.
/// </summary>
public class RegularCommand : Command {
public Action<string[]>? Function;
public static List<RegularCommand> RegularCommands = [];
/// <summary>
/// Initializes a new instance of a RegularCommand.
/// </summary>
/// <param name="commandName">The name of the regular command.</param>
/// <param name="function">The function to be run.</param>
/// <param name="aliases">The aliases for the command.</param>
public RegularCommand(string commandName, Action<string[]> function, string[]? aliases = null) {
//* setting all attributes for instance
CommandName = commandName.ToLower(); Function = function; Aliases = aliases;
if (aliases != null) {
RCommands.Add(commandName, function);
foreach (string alias in aliases) { RCommands.Add(alias, function); }
} else {
RCommands.Add(commandName, function);
}
RegularCommands.Add(this);
}
/// <summary>
/// Lists all currently registered Regular Commands.
/// </summary>
/// <returns>A string with every RegularCommand, seperated by newlines.</returns>
public static string ListAllRCommands() {
List<string> rCommandsList = [];
foreach (KeyValuePair<string, Action<string[]>> i in Command.RCommands) {
rCommandsList.Add(i.Key);
}
return string.Join("\n", rCommandsList);
}
/// <summary>
/// Gets a RegularCommand using the name of that command.
/// </summary>
/// <param name="commandName">The name of the command.</param>
/// <returns>An instance of the RegularCommand class, or null.</returns>
public static RegularCommand? GetRegularCommand(string commandName) {
foreach (RegularCommand i in RegularCommands!) {
if (i.CommandName == commandName) {
return i;
} else if (i.Aliases != null) {
if (i.Aliases.Any(x => x == commandName)) {
return i;
}
}
}
return null;
}
/// <summary>A non-static method that executes a command immediately.</summary>
/// <param name="args">The command arguments to be used when executing the command.</param>
//! Mostly unused method. Only used for testing purposes.
public void Execute(string[] args) {
Function?.Invoke(args);
}
}
/// <summary>
/// The class containing all methods that are used for registering commands to the dictionaries.
/// </summary>
public partial class RegisterCommands {
[GeneratedRegex(@"(?<root>-?\d+\.\d+|-?\d+)(?:st|nd|rd|th) root of (?<num>-?\d+\.\d+|-?\d+)")]
private static partial Regex RootRegex();
[GeneratedRegex(@"[""'](?<old>.+)[""'] with [""'](?<new>.+|)[""'] in [""'](?<text>.+)[""']")]
private static partial Regex ReplaceRegex();
[GeneratedRegex(@"(?<percent>-?\d+\.\d+|-?\d+)% of (?<number>-?\d+\.\d+|-?\d+)")]
private static partial Regex FindNumberFromPercentageRegex();
[GeneratedRegex(@"get (?<num1>-?\d+\.\d+|-?\d+) and (?<num2>-?\d+\.\d+|-?\d+)")]
private static partial Regex FindPercentageFromNumbersRegex();
/// <summary>
/// The method that registers all regular commands.
/// </summary>
public static void RegisterAllRCommands() {
RegularCommand autoclick = new(
commandName: "autoclick",
Autoclick.Autoclicker
);
RegularCommand send = new(
commandName: "send",
Send.SendMain
);
RegularCommand spam = new(
commandName: "spam",
Spam.SpamMain
);
RegularCommand settings = new(
commandName: "settings",
function: SettingsModification.SettingsMain
);
RegularCommand force = new(
commandName: "force",
function: Force.ForceMain
);
RegularCommand unforce = new(
commandName: "unforce",
function: Force.UnforceMain,
aliases: ["un-force"]
);
RegularCommand format = new(
commandName: "format",
function: Format.FormatMain
);
RegularCommand update = new(
commandName: "update",
function: (string[] args) => {
if (Utils.IndexTest(args)) { return; }
if (args[1] == "check") {
Update.Check();
} else {
Utils.NotifCheck(
true,
["Exception", "Invalid mode, try 'help' for more info.", "3"],
"updateError"
);
}
},
aliases: ["updates"]
);
RegularCommand exit = new(
commandName: "exit",
function: (string[] args) => {
HookManager.UnregisterAllHooks();
Application.Exit();
},
aliases: ["quit"]
);
RegularCommand help = new(
commandName: "help",
function: (string[] args) => {
const string wikiLink = "https://github.com/prokenz101/utilities-cs/wiki/Utilities-Wiki";
var process = new System.Diagnostics.ProcessStartInfo("cmd", $"/c start {wikiLink}")
{ CreateNoWindow = true };
if (Utils.IndexTest(args, sendNotif: false)) {
System.Diagnostics.Process.Start(process);
Utils.NotifCheck(
true,
["Opening wiki...", "Opening wiki in your default browser.", "3"],
"wikiOpen"
); return;
} else {
string searchQuery = args[1].ToLower();
if (Command.Exists(searchQuery)) {
string commandName =
RegularCommand.GetRegularCommand(searchQuery) != null
? RegularCommand.GetRegularCommand(searchQuery)!.CommandName!
: FormattableCommand.GetFormattableCommand(searchQuery) != null
? FormattableCommand.GetFormattableCommand(searchQuery)!.CommandName!
: "Impossible";
process.Arguments = process.Arguments += $"#{commandName}";
Utils.NotifCheck(
true,
["Opening wiki...", $"Opening wiki for \"{commandName}\"", "3"],
"wikiOpen"
); System.Diagnostics.Process.Start(process);
} else {
Utils.NotifCheck(
true,
[ "Exception", @"Invalid command.
Opening wiki anyway...", "3" ],
"wikiError"
);
System.Diagnostics.Process.Start(process);
}
}
},
aliases: ["wiki"]
);
RegularCommand notification = new(
commandName: "notification",
function: (string[] args) => {
string text = string.Join(" ", args[1..]);
Dictionary<System.Text.RegularExpressions.Match, System.Text.RegularExpressions.GroupCollection>?
matchToGroups =
Utils.RegexFind(
text,
@"[""'](?<title>.*?)[""'],? [""'](?<subtitle>.*?)[""'],? (?<duration>\d+)",
useIsMatch: true,
() => {
Utils.NotifCheck(
true,
["Exception", "Invalid parameters, try 'help' for more info.", "3"],
"notificationCommandError"
);
}
);
if (matchToGroups != null) {
foreach (
KeyValuePair<System.Text.RegularExpressions.Match, System.Text.RegularExpressions.GroupCollection>
kvp in matchToGroups
) {
System.Text.RegularExpressions.GroupCollection groups = kvp.Value;
string title = groups["title"].ToString();
string subtitle = groups["subtitle"].ToString();
int duration = int.Parse(groups["duration"].ToString());
Utils.NotifCheck(
true,
[title, subtitle, duration.ToString()],
"notificationCommandSuccess"
); return;
}
}
},
aliases: ["notify", "notif"]
);
RegularCommand remind = new(
commandName: "remind",
function: (string[] args) => {
string text = string.Join(" ", args[1..]);
Dictionary<System.Text.RegularExpressions.Match, System.Text.RegularExpressions.GroupCollection>? matchToGroups =
Utils.RegexFind(
text,
@"(?<time>\d+)(?<unit>h|m|s)(?<text> .*)?",
useIsMatch: true,
() => {
Utils.NotifCheck(
true,
["Exception", "Invalid parameters, try 'help' for more info.", "3"],
"remindCommandError"
);
}
);
if (matchToGroups != null) {
List<int> timeEnumerable = [];
List<char> unitEnumerable = [];
List<string> textEnumerable = [];
foreach (
KeyValuePair<
System.Text.RegularExpressions.Match,
System.Text.RegularExpressions.GroupCollection
> kvp in matchToGroups
) {
timeEnumerable.Add(int.Parse(kvp.Value["time"].ToString())); //* float
unitEnumerable.Add(kvp.Value["unit"].ToString().ToCharArray()[0]); //* char
textEnumerable.Add(kvp.Value["text"].ToString()); //* string
}
int time = timeEnumerable[0];
char unit = unitEnumerable[0];
string reminderText = textEnumerable[0];
Dictionary<char, string[]> timeOptions = new() {
{ 's', new string[] { "1", "second" } },
{ 'm', new string[] { "60", "minute" } },
{ 'h', new string[] { "3600", "hour" } }
};
if (timeOptions.ContainsKey(unit)) {
int multiplier = int.Parse(timeOptions[unit][0]);
string word = timeOptions[unit][1].ToString();
int timeSeconds = time * 1000 * multiplier;
Microsoft.Toolkit.Uwp.Notifications.ToastContentBuilder customReminderToast =
new Microsoft.Toolkit.Uwp.Notifications.ToastContentBuilder()
.AddText("Reminder!");
string timeEquals1 = $"You set a reminder for 1 {word}.";
string timeNotEqualTo1 = $"You set a reminder for {time} {word}s.";
string timeWithMessage = $"Your reminder was: {reminderText}";
if (time == 1 && reminderText == string.Empty) {
customReminderToast.AddText(timeEquals1);
} else if (reminderText == string.Empty) {
customReminderToast.AddText(timeNotEqualTo1);
} else {
customReminderToast.AddText(timeWithMessage);
}
customReminderToast.AddButton(
new Microsoft.Toolkit.Uwp.Notifications.ToastButton()
.SetContent("Dismiss")
.AddArgument("remind", "dismiss")
.SetBackgroundActivation()
);
if (timeSeconds > 10000) {
Utils.NotifCheck(
true,
[
"New reminder added.",
$"A reminder will come in {timeSeconds / 1000} seconds.",
"4"
], "remindCommandInfo"
);
}
customReminderToast.SetToastScenario(
Microsoft.Toolkit.Uwp.Notifications.ToastScenario.Alarm
); Task.Delay(timeSeconds).Wait();
Utils.NotifCheck(customReminderToast, "reminder", clearToast: false);
}
}
},
aliases: ["reminder"]
);
RegularCommand googleSearch = new(
commandName: "gs",
function: (string[] args) => {
string url = System.Web.HttpUtility.UrlEncode(string.Join(" ", args[1..]));
System.Diagnostics.Process.Start(
new System.Diagnostics.ProcessStartInfo(
"cmd", $"/c start https://google.com/search?q={url}"
) { CreateNoWindow = true }
);
}
);
RegularCommand youtubeSearch = new(
commandName: "youtube",
function: (string[] args) => {
string url = System.Web.HttpUtility.UrlEncode(string.Join(" ", args[1..]));
System.Diagnostics.Process.Start(
new System.Diagnostics.ProcessStartInfo(
"cmd", $"/c start https://youtube.com/results?search_query={url}"
) { CreateNoWindow = true }
);
},
aliases: ["yt"]
);
RegularCommand imageSearch = new(
commandName: "images",
function: (string[] args) => {
string url = System.Web.HttpUtility.UrlEncode(string.Join(" ", args[1..]));
System.Diagnostics.Process.Start(
new System.Diagnostics.ProcessStartInfo(
"cmd", $"/c start https://www.google.com/search?tbm=isch&q={url}"
) { CreateNoWindow = true }
);
}
);
RegularCommand translate = new(
commandName: "translate",
function: Translate.TranslateMain
);
RegularCommand getcommandcount = new(
commandName: "getcommandcount",
function: (string[] args) => {
int regularCommandsCount = RegularCommand.RegularCommands.Count;
int formattableCommandsCount = FormattableCommand.FormattableCommands.Count;
Utils.NotifCheck(
true,
[
$"Total Commands: {regularCommandsCount + formattableCommandsCount}",
$@"RegularCommands Count: {regularCommandsCount}
FormattableCommands Count: {formattableCommandsCount}",
"5"
], "getcommandcountSuccess"
);
},
aliases: ["totalcommandcount", "get-commandcount"]
);
}
/// <summary>
/// The method that registers all formattable commands.
/// </summary>
public static void RegisterAllFCommands() {
FormattableCommand all = new(
commandName: "all",
All.AllCommand
);
FormattableCommand getAliases = new(
commandName: "aliases",
function: (string[] args, bool copy, bool notif) => {
if (Utils.IndexTest(args)) { return null; }
string cmd = args[1];
List<string>? aliases = Command.GetAliases(cmd);
if (aliases != null) {
string aliasesString = string.Join(", ", aliases);
Utils.CopyCheck(copy, aliasesString);
Utils.NotifCheck(
notif,
["Success!", "The aliases were copied to your clipboard.", "3"],
"getAliasesSuccess"
); return aliasesString;
} else {
Utils.NotifCheck(
false,
["No aliases found for command: " + cmd],
"getAliasesError"
); return null;
}
},
aliases: ["getaliases", "getalias", "get-alias", "get-aliases"]
);
FormattableCommand escape = new(
commandName: "escape",
function: (string[] args, bool copy, bool notif) => {
if (Utils.IndexTest(args)) { return null; }
string text = string.Join(" ", args[1..]);
string ans = Utils.BulkReplace(
text,
"! @ # $ % ^ & * ( ) _ + , . / ; ' [ ] < > ? : \" { } ` ~ \\",
"\\" + string.Join(" \\", "! @ # $ % ^ & * ( ) _ + , . / ; ' [ ] < > ? : \" { } ` ~ \\".Split(" "))
);
Utils.CopyCheck(copy, ans);
Utils.NotifCheck(
notif, ["Success!", "Message copied to clipboard.", "3"], "escapeSuccess"
); return ans;
}
);
FormattableCommand base32 = new(
commandName: "base32",
function: Base32Convert.Base32ConvertMain,
aliases: ["b32"]
);
FormattableCommand base64 = new(
commandName: "base64",
function: (string[] args, bool copy, bool notif) => {
if (Utils.IndexTest(args)) { return null; }
Func<string, bool> isBase64 = (string s) => {
s = s.Trim();
bool isB64 = (s.Length % 4 == 0) && System.Text.RegularExpressions.Regex.IsMatch(
s, @"^[a-zA-Z0-9\+/]*={0,3}$",
System.Text.RegularExpressions.RegexOptions.None
); return isB64;
};
string text = string.Join(" ", args[1..]);
if (isBase64(text)) {
try {
string ans = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(text));
Utils.CopyCheck(copy, ans);
Utils.NotifCheck(
notif,
["Success!", $"The message was: {ans}", "6"],
"base64Success"
); return ans;
} catch {
Utils.NotifCheck(
true,
["Exception", "Something went wrong while converting this text to Base64.", "4"],
"base64Error"
); return null;
}
} else {
string ans = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text));
Utils.CopyCheck(copy, ans);
Utils.NotifCheck(
notif,
["Success!", "The message was copied to your clipboard.", "3"],
"base64Success"
); return ans;
}
},
aliases: ["b64"],
useInAllCommand: true,
allCommandMode: "encodings"
);
FormattableCommand isBase64 = new(
commandName: "isbase64",
function: (string[] args, bool copy, bool notif) => {
if (Utils.IndexTest(args)) {
return null;
}
string text = string.Join(" ", args[1..]);
Func<string, bool> isBase64 = (string s) => {
s = s.Trim();
bool isB64 = (s.Length % 4 == 0) && System.Text.RegularExpressions.Regex.IsMatch(
s, @"^[a-zA-Z0-9\+/]*={0,3}$",
System.Text.RegularExpressions.RegexOptions.None
); return isB64;
};
if (isBase64(text)) {
Utils.NotifCheck(notif, ["Yes.", "The string is Base64.", "3"], "isBase64Success");
return "Yes";
} else {
Utils.NotifCheck(notif, ["No.", "The string is not Base64.", "3"], "isBase64Success");
return "No";
}
}
);
FormattableCommand base85 = new(
commandName: "base85",
function: Ascii85.Base85Main,
aliases: ["ascii85", "b85"]
);
FormattableCommand urlencode = new(
commandName: "urlencode",
function: (string[] args, bool copy, bool notif) => {
if (Utils.IndexTest(args)) { return null; }
string text = string.Join(" ", args[1..]);
string url = System.Web.HttpUtility.UrlEncode(text);
Utils.CopyCheck(copy, url);
Utils.NotifCheck(
notif,
["Success!", "The URL was copied to your clipboard.", "2"],
"urlEncodeSuccess"
); return url;
}
);
FormattableCommand urldecode = new(
commandName: "urldecode",
function: (string[] args, bool copy, bool notif) => {
if (Utils.IndexTest(args)) { return null; }
string text = string.Join(" ", args[1..]);
string url = System.Web.HttpUtility.UrlDecode(text);
Utils.CopyCheck(copy, url);
Utils.NotifCheck(
notif,
["Success!", "The URL was copied to your clipboard.", "2"],
"urlDecodeSuccess"
); return url;
}
);
FormattableCommand binary = new(
commandName: "binary",
function: (string[] args, bool copy, bool notif) => {
string text = string.Join(" ", args[1..]);
if (Utils.IndexTest(args)) { return null; }
if (!Utils.FormatValid("01 ", text)) {
byte[] ConvertToByteArray(string str, System.Text.Encoding encoding) {
return encoding.GetBytes(str);
}
string ToBinary(byte[] data) {
return string.Join(
" ",
data.Select(
byt => Convert.ToString(byt, 2).PadLeft(8, '0')
)
);
}
string ans = ToBinary(ConvertToByteArray(text, System.Text.Encoding.ASCII));
Utils.CopyCheck(copy, ans);
Utils.NotifCheck(
notif,
["Success!", "Message copied to clipboard.", "3"],
"binarySuccess"
); return ans;
} else {
try {
string[] textList = text.Split(" ");
var chars = from split in textList select ((char)Convert.ToInt32(split, 2)).ToString();
Utils.CopyCheck(copy, string.Join("", chars));
Utils.NotifCheck(
notif,
["Success!", $"The message was: {string.Join("", chars)}", "10"],
"binarySuccess"
); return string.Join("", chars);
} catch {
Utils.NotifCheck(
true,
["Exception", @"Something went wrong while converting this text to binary.", "3"],
"binaryError"
); return null;
}
}
},
aliases: ["bin"],
useInAllCommand: true,
allCommandMode: "encodings"
);
FormattableCommand bubbletext = new(
commandName: "bubbletext",
function: (string[] args, bool copy, bool notif) => {
if (Utils.IndexTest(args)) { return null; }
string result = Utils.TextFormatter(string.Join(" ", args[1..]), Dictionaries.BubbleDict);
Utils.CopyCheck(copy, result);
Utils.NotifCheck(
notif,
["Success!", "Message copied to clipboard.", "3"],
"bubbletextSuccess"
); return result;
},
aliases: ["bubble"],
useInAllCommand: true,
allCommandMode: "fancy"
);
FormattableCommand commaseperator = new(
commandName: "commaseperator",
function: (string[] args, bool copy, bool notif) => {
if (Utils.IndexTest(args)) { return null; }
string input = string.Join(" ", args[1..]);
System.Text.RegularExpressions.Regex re = new(@"(?<num>-?\d+)(?:\.(?<decimals>\d+))?");
if (re.IsMatch(input)) {
System.Numerics.BigInteger num =
System.Numerics.BigInteger.Parse(re.Match(input).Groups["num"].Value);
System.Numerics.BigInteger decimals =
re.Match(input).Groups["decimals"].Value != ""
? System.Numerics.BigInteger.Parse(re.Match(input).Groups["decimals"].Value)
: 0;
string result =
decimals == 0 ? string.Format("{0:n0}", num)
: string.Format("{0:n0}", num) + "." + decimals.ToString();
Utils.CopyCheck(copy, result);
Utils.NotifCheck(
notif,
["Success!", "Message copied to clipboard.", "3"],
"commaseperatorSuccess"
); return result;
} else {
Utils.NotifCheck(
true,
["Exception", "Invalid input, try 'help' for more info.", "2"],
"commaseperatorError"
); return null;
}
},
aliases: ["cms"]
);
FormattableCommand copypaste = new(
commandName: "copypaste",
function: (string[] args, bool copy, bool notif) => {
if (Utils.IndexTest(args)) { return null; }
string text = string.Join(" ", args[1..]);
if (Dictionaries.CopypasteDict.TryGetValue(text, out var val)) {
Utils.CopyCheck(copy, val);
Utils.NotifCheck(
notif,
["Success!", "Message copied to clipboard.", "3"],
"copypasteSuccess"
); return Dictionaries.CopypasteDict[text];
} else {
Utils.NotifCheck(
true,
[
"Exception",
"Invalid input, try 'help' for more info.",
"3"
], "copypasteError"
); return null;
}
},
aliases: ["cp"]
);
FormattableCommand creepy = new(
commandName: "creepy",
function: (string[] args, bool copy, bool notif) => {
if (Utils.IndexTest(args)) { return null; }
string result = Utils.TextFormatter(string.Join(" ", args[1..]), Dictionaries.CreepyDict);
Utils.CopyCheck(copy, result);
Utils.NotifCheck(
notif,
["Success!", "Message copied to clipboard.", "3"],
"creepySuccess"
); return result;
},
useInAllCommand: true,
allCommandMode: "fancy"
);
FormattableCommand wingdings = new(
commandName: "wingdings",
function: (string[] args, bool copy, bool notif) => {
if (Utils.IndexTest(args)) { return null; }
string result = Utils.TextFormatter(string.Join(" ", args[1..]), Dictionaries.WingdingsDict);