more work on permissions

This commit is contained in:
Master Kwoth 2016-02-11 12:38:41 +01:00
parent 8642044a1c
commit 0a44ae9922
4 changed files with 36 additions and 33 deletions

View File

@ -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<T> : 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);
}
}

View File

@ -1,4 +1,5 @@
using Discord; using Discord;
using Discord.Commands.Permissions;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
@ -6,17 +7,22 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord.Commands;
namespace NadekoBot.Classes { namespace NadekoBot.Classes.Permissions {
public class PermissionsHandler { public static class PermissionsHandler {
public static ConcurrentDictionary<Server, ServerPermissions> _permissionsDict = public static ConcurrentDictionary<Server, ServerPermissions> _permissionsDict =
new ConcurrentDictionary<Server, ServerPermissions>(); new ConcurrentDictionary<Server, ServerPermissions>();
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() { public static void WriteToJson() {
Directory.CreateDirectory("data/permissions/"); Directory.CreateDirectory("data/permissions/");
foreach (var kvp in _permissionsDict) { foreach (var kvp in _permissionsDict) {
string pathToFile = $"data/permissions/{kvp.Key.Id}.json"; WriteServerToJson(kvp.Key);
File.WriteAllText(pathToFile, Newtonsoft.Json.JsonConvert.SerializeObject(_permissionsDict[kvp.Key],Newtonsoft.Json.Formatting.Indented));
} }
} }
} }

View File

@ -1,4 +1,6 @@
using NadekoBot.Extensions; using Discord.Commands;
using Discord.Modules;
using NadekoBot.Extensions;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -20,25 +22,37 @@ namespace NadekoBot.Classes
{ {
case "t": case "t":
case "true": case "true":
case "enabled": case "enable":
return true; return true;
case "f": case "f":
case "false": case "false":
case "disabled": case "disable":
return false; return false;
default: default:
throw new System.ArgumentException("Did not receive a valid boolean value"); 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;
} }
} }
} }

View File

@ -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("module", ParameterType.Required)
.Parameter("bool", 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 // .AddCheck() -> fix this
.Do(async e => .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("command", ParameterType.Required)
.Parameter("bool", 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 // .AddCheck() -> fix this
.Do(async e => .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("command", ParameterType.Required)
.Parameter("bool", ParameterType.Required) .Parameter("bool", ParameterType.Required)
.Description("Sets a command's permission at the user level.") .Description("Sets a command's permission at the user level.")