From 3b3ac5496f7106ddef756e4fa990e6039515a687 Mon Sep 17 00:00:00 2001 From: appelemac Date: Thu, 5 May 2016 11:47:47 +0200 Subject: [PATCH] Moved to seperate file --- .../Administration/AdministrationModule.cs | 98 +------------- .../Commands/CustomReactionsCommands.cs | 123 ++++++++++++++++++ NadekoBot/NadekoBot.csproj | 4 +- 3 files changed, 126 insertions(+), 99 deletions(-) create mode 100644 NadekoBot/Modules/Administration/Commands/CustomReactionsCommands.cs diff --git a/NadekoBot/Modules/Administration/AdministrationModule.cs b/NadekoBot/Modules/Administration/AdministrationModule.cs index e39e67fb..5ea40740 100644 --- a/NadekoBot/Modules/Administration/AdministrationModule.cs +++ b/NadekoBot/Modules/Administration/AdministrationModule.cs @@ -28,6 +28,7 @@ namespace NadekoBot.Modules.Administration commands.Add(new SelfAssignedRolesCommand(this)); commands.Add(new Remind(this)); commands.Add(new InfoCommands(this)); + commands.Add(new CustomReactionsCommands(this)); } public override string Prefix { get; } = NadekoBot.Config.CommandPrefixes.Administration; @@ -851,103 +852,6 @@ namespace NadekoBot.Modules.Administration await e.Channel.SendMessage(":ok:"); }); - cgb.CreateCommand(Prefix + "addcustomreaction") - .Alias(Prefix + "acr") - .Description($"Add a custom reaction. **Owner Only!**\n**Usage**: {Prefix}acr \"hello\" I love saying hello to %user%") - .AddCheck(SimpleCheckers.OwnerOnly()) - .Parameter("name", ParameterType.Required) - .Parameter("message", ParameterType.Unparsed) - .Do(async e => - { - var name = e.GetArg("name"); - var message = e.GetArg("message").Trim(); - if (string.IsNullOrWhiteSpace(message)) - { - await e.Channel.SendMessage($"Incorrect command usage. See -h {Prefix}acr for correct formatting").ConfigureAwait(false); - return; - } - try - { - NadekoBot.Config.CustomReactions[name].Add(message); - } - catch (System.Collections.Generic.KeyNotFoundException) - { - NadekoBot.Config.CustomReactions.Add(name, new System.Collections.Generic.List() { message }); - } - finally - { - Classes.JSONModels.ConfigHandler.SaveConfig(); - } - await e.Channel.SendMessage($"Added {name} : {message}").ConfigureAwait(false); - - }); - - cgb.CreateCommand(Prefix + "listcustomreactions") - .Alias(Prefix + "lcr") - .Description("Lists all current custom reactions. **Owner Only!**") - .AddCheck(SimpleCheckers.OwnerOnly()) - .Do(async e => - { - - string message = $"Custom reactions:"; - foreach (var cr in NadekoBot.Config.CustomReactions) - { - if (message.Length > 1500) - { - await e.Channel.SendMessage(message).ConfigureAwait(false); - message = ""; - } - message += $"\n**\"{Format.Escape(cr.Key)}\"**:"; - int i = 1; - foreach (var reaction in cr.Value) - { - message += "\n " + i++ + "." + Format.Code(reaction); - } - - } - await e.Channel.SendMessage(message); - }); - - cgb.CreateCommand(Prefix + "deletecustomreaction") - .Alias(Prefix + "dcr") - .Description("Deletes a custome reaction with given name (and index)") - .Parameter("name", ParameterType.Required) - .Parameter("index", ParameterType.Optional) - .Do(async e => - { - var name = e.GetArg("name"); - if (!NadekoBot.Config.CustomReactions.ContainsKey(name)) - { - await e.Channel.SendMessage("Could not find given key"); - return; - } - string message = ""; - int index; - if (int.TryParse(e.GetArg("index") ?? "", out index)) - { - try - { - NadekoBot.Config.CustomReactions[name].RemoveAt(index - 1); - if (!NadekoBot.Config.CustomReactions[name].Any()) - { - NadekoBot.Config.CustomReactions.Remove(name); - } - message = $"Deleted response #{index} from {name}"; - } - catch (ArgumentOutOfRangeException) - { - await e.Channel.SendMessage("Index given was out of range").ConfigureAwait(false); - return; - } - } - else - { - NadekoBot.Config.CustomReactions.Remove(name); - message = $"Deleted custom reaction \"{name}\""; - } - Classes.JSONModels.ConfigHandler.SaveConfig(); - await e.Channel.SendMessage(message); - }); }); } diff --git a/NadekoBot/Modules/Administration/Commands/CustomReactionsCommands.cs b/NadekoBot/Modules/Administration/Commands/CustomReactionsCommands.cs new file mode 100644 index 00000000..781226e6 --- /dev/null +++ b/NadekoBot/Modules/Administration/Commands/CustomReactionsCommands.cs @@ -0,0 +1,123 @@ +using NadekoBot.Classes; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Discord.Commands; +using NadekoBot.Modules.Permissions.Classes; +using Discord; + +namespace NadekoBot.Modules.Administration.Commands +{ + class CustomReactionsCommands : DiscordCommand + { + public CustomReactionsCommands(DiscordModule module) : base(module) + { + + } + + internal override void Init(CommandGroupBuilder cgb) + { + var Prefix = Module.Prefix; + + cgb.CreateCommand(Prefix + "addcustomreaction") + .Alias(Prefix + "acr") + .Description($"Add a custom reaction. **Owner Only!**\n**Usage**: {Prefix}acr \"hello\" I love saying hello to %user%") + .AddCheck(SimpleCheckers.OwnerOnly()) + .Parameter("name", ParameterType.Required) + .Parameter("message", ParameterType.Unparsed) + .Do(async e => + { + var name = e.GetArg("name"); + var message = e.GetArg("message").Trim(); + if (string.IsNullOrWhiteSpace(message)) + { + await e.Channel.SendMessage($"Incorrect command usage. See -h {Prefix}acr for correct formatting").ConfigureAwait(false); + return; + } + try + { + NadekoBot.Config.CustomReactions[name].Add(message); + } + catch (KeyNotFoundException) + { + NadekoBot.Config.CustomReactions.Add(name, new System.Collections.Generic.List() { message }); + } + finally + { + Classes.JSONModels.ConfigHandler.SaveConfig(); + } + await e.Channel.SendMessage($"Added {name} : {message}").ConfigureAwait(false); + + }); + + cgb.CreateCommand(Prefix + "listcustomreactions") + .Alias(Prefix + "lcr") + .Description("Lists all current custom reactions. **Owner Only!**") + .AddCheck(SimpleCheckers.OwnerOnly()) + .Do(async e => + { + + string message = $"Custom reactions:"; + foreach (var cr in NadekoBot.Config.CustomReactions) + { + if (message.Length > 1500) + { + await e.Channel.SendMessage(message).ConfigureAwait(false); + message = ""; + } + message += $"\n**\"{Format.Escape(cr.Key)}\"**:"; + int i = 1; + foreach (var reaction in cr.Value) + { + message += "\n " + i++ + "." + Format.Code(reaction); + } + + } + await e.Channel.SendMessage(message); + }); + + cgb.CreateCommand(Prefix + "deletecustomreaction") + .Alias(Prefix + "dcr") + .Description("Deletes a custome reaction with given name (and index)") + .Parameter("name", ParameterType.Required) + .Parameter("index", ParameterType.Optional) + .Do(async e => + { + var name = e.GetArg("name"); + if (!NadekoBot.Config.CustomReactions.ContainsKey(name)) + { + await e.Channel.SendMessage("Could not find given key"); + return; + } + string message = ""; + int index; + if (int.TryParse(e.GetArg("index") ?? "", out index)) + { + try + { + NadekoBot.Config.CustomReactions[name].RemoveAt(index - 1); + if (!NadekoBot.Config.CustomReactions[name].Any()) + { + NadekoBot.Config.CustomReactions.Remove(name); + } + message = $"Deleted response #{index} from {name}"; + } + catch (ArgumentOutOfRangeException) + { + await e.Channel.SendMessage("Index given was out of range").ConfigureAwait(false); + return; + } + } + else + { + NadekoBot.Config.CustomReactions.Remove(name); + message = $"Deleted custom reaction \"{name}\""; + } + Classes.JSONModels.ConfigHandler.SaveConfig(); + await e.Channel.SendMessage(message); + }); + } + } +} diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index a6a84cfa..d9923776 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -120,13 +120,13 @@ + - @@ -483,4 +483,4 @@ --> - + \ No newline at end of file