From 0a44ae99227c0ccb1719e707b74c466c004071f4 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Thu, 11 Feb 2016 12:38:41 +0100 Subject: [PATCH] more work on permissions --- .../PermissionCheckers/PermissionChecker.cs | 17 ----------- .../PermissionsHandler.cs | 14 +++++++--- NadekoBot/Classes/PermissionHelper.cs | 28 ++++++++++++++----- NadekoBot/Modules/Permissions.cs | 10 +++---- 4 files changed, 36 insertions(+), 33 deletions(-) delete mode 100644 NadekoBot/Classes/PermissionCheckers/PermissionChecker.cs rename NadekoBot/Classes/{ => PermissionCheckers}/PermissionsHandler.cs (80%) diff --git a/NadekoBot/Classes/PermissionCheckers/PermissionChecker.cs b/NadekoBot/Classes/PermissionCheckers/PermissionChecker.cs deleted file mode 100644 index 61fa4d7c..00000000 --- a/NadekoBot/Classes/PermissionCheckers/PermissionChecker.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Discord.Commands.Permissions; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Discord; -using Discord.Commands; - -namespace NadekoBot.Classes.PermissionCheckers { - abstract class PermissionChecker : IPermissionChecker where T : new() { - public static readonly T _instance = new T(); - public static T Instance => _instance; - - public abstract bool CanRun(Command command, User user, Channel channel, out string error); - } -} diff --git a/NadekoBot/Classes/PermissionsHandler.cs b/NadekoBot/Classes/PermissionCheckers/PermissionsHandler.cs similarity index 80% rename from NadekoBot/Classes/PermissionsHandler.cs rename to NadekoBot/Classes/PermissionCheckers/PermissionsHandler.cs index 654b497b..dace21b7 100644 --- a/NadekoBot/Classes/PermissionsHandler.cs +++ b/NadekoBot/Classes/PermissionCheckers/PermissionsHandler.cs @@ -1,4 +1,5 @@ using Discord; +using Discord.Commands.Permissions; using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -6,17 +7,22 @@ using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; +using Discord.Commands; -namespace NadekoBot.Classes { - public class PermissionsHandler { +namespace NadekoBot.Classes.Permissions { + public static class PermissionsHandler { public static ConcurrentDictionary _permissionsDict = new ConcurrentDictionary(); + private static void WriteServerToJson(Server server) { + string pathToFile = $"data/permissions/{server.Id}.json"; + File.WriteAllText(pathToFile, Newtonsoft.Json.JsonConvert.SerializeObject(_permissionsDict[server], Newtonsoft.Json.Formatting.Indented)); + } + public static void WriteToJson() { Directory.CreateDirectory("data/permissions/"); foreach (var kvp in _permissionsDict) { - string pathToFile = $"data/permissions/{kvp.Key.Id}.json"; - File.WriteAllText(pathToFile, Newtonsoft.Json.JsonConvert.SerializeObject(_permissionsDict[kvp.Key],Newtonsoft.Json.Formatting.Indented)); + WriteServerToJson(kvp.Key); } } } diff --git a/NadekoBot/Classes/PermissionHelper.cs b/NadekoBot/Classes/PermissionHelper.cs index adf7a643..45afc075 100644 --- a/NadekoBot/Classes/PermissionHelper.cs +++ b/NadekoBot/Classes/PermissionHelper.cs @@ -1,4 +1,6 @@ -using NadekoBot.Extensions; +using Discord.Commands; +using Discord.Modules; +using NadekoBot.Extensions; using System; using System.Collections.Generic; using System.Linq; @@ -20,25 +22,37 @@ namespace NadekoBot.Classes { case "t": case "true": - case "enabled": + case "enable": return true; case "f": case "false": - case "disabled": + case "disable": return false; default: throw new System.ArgumentException("Did not receive a valid boolean value"); } } - internal static void ValidateModule(string v) + internal static bool ValidateModule(string mod) { - return; + if (string.IsNullOrWhiteSpace(mod)) + throw new ArgumentNullException(nameof(mod)); + foreach (var m in NadekoBot.client.Modules().Modules) { + if(m.Name.ToLower().Equals(mod)) + return true; + } + return false; } - internal static void ValidateCommand(string v) + internal static bool ValidateCommand(string commandText) { - return; + if (string.IsNullOrWhiteSpace(commandText)) + throw new ArgumentNullException(nameof(commandText)); + foreach (var com in NadekoBot.client.Commands().AllCommands) { + if (com.Text == commandText) + return true; + } + return false; } } } diff --git a/NadekoBot/Modules/Permissions.cs b/NadekoBot/Modules/Permissions.cs index f8ad344c..a31d927f 100644 --- a/NadekoBot/Modules/Permissions.cs +++ b/NadekoBot/Modules/Permissions.cs @@ -77,10 +77,10 @@ namespace NadekoBot.Modules } }); - cgb.CreateCommand(trigger + "srm").Alias(trigger + "setrollmodule") + cgb.CreateCommand(trigger + "srm").Alias(trigger + "setrolemodule") .Parameter("module", ParameterType.Required) .Parameter("bool", ParameterType.Required) - .Description("Sets a module's permission at the roll level.") + .Description("Sets a module's permission at the role level.") // .AddCheck() -> fix this .Do(async e => { @@ -101,10 +101,10 @@ namespace NadekoBot.Modules } }); - cgb.CreateCommand(trigger + "src").Alias(trigger + "setrollcommand") + cgb.CreateCommand(trigger + "src").Alias(trigger + "setrolecommand") .Parameter("command", ParameterType.Required) .Parameter("bool", ParameterType.Required) - .Description("Sets a command's permission at the roll level.") + .Description("Sets a command's permission at the role level.") // .AddCheck() -> fix this .Do(async e => { @@ -197,7 +197,7 @@ namespace NadekoBot.Modules } }); - cgb.CreateCommand(trigger + "suc").Alias(trigger + "setuserchannel") + cgb.CreateCommand(trigger + "suc").Alias(trigger + "setusercommand") .Parameter("command", ParameterType.Required) .Parameter("bool", ParameterType.Required) .Description("Sets a command's permission at the user level.")