From 8c6fd79fee5d2942d18050c3f8b908b9772e6200 Mon Sep 17 00:00:00 2001 From: Cuken Date: Wed, 10 Feb 2016 20:30:19 -0700 Subject: [PATCH] Added PermissionHelper.cs, Permissons.cs, changed project requirmenets, and added PermissionModule() to main cs file. --- NadekoBot/Classes/PermissionHelper.cs | 44 +++++ NadekoBot/Modules/Permissions.cs | 226 ++++++++++++++++++++++++++ NadekoBot/NadekoBot.cs | 1 + 3 files changed, 271 insertions(+) create mode 100644 NadekoBot/Classes/PermissionHelper.cs create mode 100644 NadekoBot/Modules/Permissions.cs diff --git a/NadekoBot/Classes/PermissionHelper.cs b/NadekoBot/Classes/PermissionHelper.cs new file mode 100644 index 00000000..adf7a643 --- /dev/null +++ b/NadekoBot/Classes/PermissionHelper.cs @@ -0,0 +1,44 @@ +using NadekoBot.Extensions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + + +namespace NadekoBot.Classes +{ + static class PermissionHelper + { + public static bool ValidateBool(string passedArg) + { + if (string.IsNullOrEmpty(passedArg.Trim())) + { + throw new System.ArgumentException("No value supplied! Missing argument"); + } + switch (passedArg.ToLower()) + { + case "t": + case "true": + case "enabled": + return true; + case "f": + case "false": + case "disabled": + return false; + default: + throw new System.ArgumentException("Did not receive a valid boolean value"); + } + } + + internal static void ValidateModule(string v) + { + return; + } + + internal static void ValidateCommand(string v) + { + return; + } + } +} diff --git a/NadekoBot/Modules/Permissions.cs b/NadekoBot/Modules/Permissions.cs new file mode 100644 index 00000000..f8ad344c --- /dev/null +++ b/NadekoBot/Modules/Permissions.cs @@ -0,0 +1,226 @@ +using System; +using System.Threading.Tasks; +using Discord.Modules; +using System.Net; +using System.IO; +using Newtonsoft.Json.Linq; +using System.Text.RegularExpressions; +using Newtonsoft.Json; +using Discord.Commands; +using NadekoBot.Extensions; +using System.Collections.Generic; +using NadekoBot.Classes.PermissionCheckers; +using NadekoBot.Classes; + +namespace NadekoBot.Modules +{ + class PermissionModule : DiscordModule + { + string trigger = "*"; + public PermissionModule() : base() + { + //Empty for now + } + + public override void Install(ModuleManager manager) + { + var client = NadekoBot.client; + manager.CreateCommands("", cgb => { + + commands.ForEach(cmd => cmd.Init(cgb)); + + cgb.CreateCommand(trigger + "ssm").Alias(trigger + "setservermodule") + .Parameter("module", ParameterType.Required) + .Parameter("bool", ParameterType.Required) + .Description("Sets a module's permission at the server level.") + // .AddCheck() -> fix this + .Do(async e => + { + try + { + PermissionHelper.ValidateModule(e.GetArg("module")); + bool state = PermissionHelper.ValidateBool(e.GetArg("bool")); + + await e.Send("I'm setting " + e.GetArg("module") + " to " + state); + } + catch (ArgumentException exArg) + { + await e.Send(exArg.Message); + } + catch (Exception ex) + { + await e.Send("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(trigger + "ssc").Alias(trigger + "setservercommand") + .Parameter("command", ParameterType.Required) + .Parameter("bool", ParameterType.Required) + .Description("Sets a command's permission at the server level.") + // .AddCheck() -> fix this + .Do(async e => + { + try + { + PermissionHelper.ValidateCommand(e.GetArg("command")); + bool state = PermissionHelper.ValidateBool(e.GetArg("bool")); + + await e.Send("I'm setting " + e.GetArg("command") + " to " + state); + } + catch (ArgumentException exArg) + { + await e.Send(exArg.Message); + } + catch (Exception ex) + { + await e.Send("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(trigger + "srm").Alias(trigger + "setrollmodule") + .Parameter("module", ParameterType.Required) + .Parameter("bool", ParameterType.Required) + .Description("Sets a module's permission at the roll level.") + // .AddCheck() -> fix this + .Do(async e => + { + try + { + PermissionHelper.ValidateModule(e.GetArg("module")); + bool state = PermissionHelper.ValidateBool(e.GetArg("bool")); + + await e.Send("I'm setting " + e.GetArg("module") + " to " + state); + } + catch (ArgumentException exArg) + { + await e.Send(exArg.Message); + } + catch (Exception ex) + { + await e.Send("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(trigger + "src").Alias(trigger + "setrollcommand") + .Parameter("command", ParameterType.Required) + .Parameter("bool", ParameterType.Required) + .Description("Sets a command's permission at the roll level.") + // .AddCheck() -> fix this + .Do(async e => + { + try + { + PermissionHelper.ValidateCommand(e.GetArg("command")); + bool state = PermissionHelper.ValidateBool(e.GetArg("bool")); + + await e.Send("I'm setting " + e.GetArg("command") + " to " + state); + } + catch (ArgumentException exArg) + { + await e.Send(exArg.Message); + } + catch (Exception ex) + { + await e.Send("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(trigger + "scm").Alias(trigger + "setchannelmodule") + .Parameter("module", ParameterType.Required) + .Parameter("bool", ParameterType.Required) + .Description("Sets a module's permission at the channel level.") + // .AddCheck() -> fix this + .Do(async e => + { + try + { + PermissionHelper.ValidateModule(e.GetArg("module")); + bool state = PermissionHelper.ValidateBool(e.GetArg("bool")); + + await e.Send("I'm setting " + e.GetArg("module") + " to " + state); + } + catch (ArgumentException exArg) + { + await e.Send(exArg.Message); + } + catch (Exception ex) + { + await e.Send("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(trigger + "scc").Alias(trigger + "setchannelcommand") + .Parameter("command", ParameterType.Required) + .Parameter("bool", ParameterType.Required) + .Description("Sets a command's permission at the channel level.") + // .AddCheck() -> fix this + .Do(async e => + { + try + { + PermissionHelper.ValidateCommand(e.GetArg("command")); + bool state = PermissionHelper.ValidateBool(e.GetArg("bool")); + + await e.Send("I'm setting " + e.GetArg("command") + " to " + state); + } + catch (ArgumentException exArg) + { + await e.Send(exArg.Message); + } + catch (Exception ex) + { + await e.Send("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(trigger + "sum").Alias(trigger + "setusermodule") + .Parameter("module", ParameterType.Required) + .Parameter("bool", ParameterType.Required) + .Description("Sets a module's permission at the user level.") + // .AddCheck() -> fix this + .Do(async e => + { + try + { + PermissionHelper.ValidateModule(e.GetArg("module")); + bool state = PermissionHelper.ValidateBool(e.GetArg("bool")); + + await e.Send("I'm setting " + e.GetArg("module") + " to " + state); + } + catch (ArgumentException exArg) + { + await e.Send(exArg.Message); + } + catch (Exception ex) + { + await e.Send("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(trigger + "suc").Alias(trigger + "setuserchannel") + .Parameter("command", ParameterType.Required) + .Parameter("bool", ParameterType.Required) + .Description("Sets a command's permission at the user level.") + // .AddCheck() -> fix this + .Do(async e => + { + try + { + PermissionHelper.ValidateModule(e.GetArg("command")); + bool state = PermissionHelper.ValidateBool(e.GetArg("bool")); + + await e.Send("I'm setting " + e.GetArg("command") + " to " + state); + } + catch (ArgumentException exArg) + { + await e.Send(exArg.Message); + } + catch (Exception ex) + { + await e.Send("Something went terribly wrong - " + ex.Message); + } + }); + }); + } + } +} diff --git a/NadekoBot/NadekoBot.cs b/NadekoBot/NadekoBot.cs index f35d1421..014b73a1 100644 --- a/NadekoBot/NadekoBot.cs +++ b/NadekoBot/NadekoBot.cs @@ -90,6 +90,7 @@ namespace NadekoBot { //install modules modules.Add(new Administration(), "Administration", ModuleFilter.None); + modules.Add(new PermissionModule(), "Permissions", ModuleFilter.None); modules.Add(new Conversations(), "Conversations", ModuleFilter.None); modules.Add(new Gambling(), "Gambling", ModuleFilter.None); modules.Add(new Games(), "Games", ModuleFilter.None);