diff --git a/NadekoBot/Classes/PermissionCheckers/NSWFPermissionChecker.cs b/NadekoBot/Classes/PermissionCheckers/NSWFPermissionChecker.cs deleted file mode 100644 index 6d22f09c..00000000 --- a/NadekoBot/Classes/PermissionCheckers/NSWFPermissionChecker.cs +++ /dev/null @@ -1,18 +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 { - class NSFWPermissionChecker : PermissionChecker { - public override bool CanRun(Command command, User user, Channel channel, out string error) { - error = string.Empty; - Console.WriteLine(command.Category); - return false; - } - } -} diff --git a/NadekoBot/Classes/PermissionCheckers/PermissionChecker.cs b/NadekoBot/Classes/PermissionCheckers/PermissionChecker.cs new file mode 100644 index 00000000..d33df5f3 --- /dev/null +++ b/NadekoBot/Classes/PermissionCheckers/PermissionChecker.cs @@ -0,0 +1,23 @@ +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.Permissions { + class PermissionChecker : IPermissionChecker { + public static readonly PermissionChecker _instance = new PermissionChecker(); + public static PermissionChecker Instance => _instance; + + static PermissionChecker() { } + public PermissionChecker() { } + + public bool CanRun(Command command, User user, Channel channel, out string error) { + error = string.Empty; + return false; + } + } +} diff --git a/NadekoBot/Classes/PermissionCheckers/PermissionsHandler.cs b/NadekoBot/Classes/PermissionCheckers/PermissionsHandler.cs index dace21b7..d8195524 100644 --- a/NadekoBot/Classes/PermissionCheckers/PermissionsHandler.cs +++ b/NadekoBot/Classes/PermissionCheckers/PermissionsHandler.cs @@ -25,6 +25,132 @@ namespace NadekoBot.Classes.Permissions { WriteServerToJson(kvp.Key); } } + + public static void SetServerModulePermission(Server server, string moduleName, bool value) { + if (!_permissionsDict.ContainsKey(server)) { + _permissionsDict.TryAdd(server, new ServerPermissions(server.Id.ToString())); + } + var modules = _permissionsDict[server].Permissions.modules; + if (modules.ContainsKey(moduleName)) + modules[moduleName] = value; + else + modules.Add(moduleName, value); + WriteServerToJson(server); + } + + public static void SetServerCommandPermission(Server server, string commandName, bool value) { + if (!_permissionsDict.ContainsKey(server)) { + _permissionsDict.TryAdd(server, new ServerPermissions(server.Id.ToString())); + } + var commands = _permissionsDict[server].Permissions.commands; + if (commands.ContainsKey(commandName)) + commands[commandName] = value; + else + commands.Add(commandName, value); + WriteServerToJson(server); + } + + public static void SetChannelModulePermission(Channel channel, string moduleName, bool value) { + var server = channel.Server; + if (!_permissionsDict.ContainsKey(server)) { + _permissionsDict.TryAdd(server, new ServerPermissions(server.Id.ToString())); + } + if(!_permissionsDict[server].ChannelPermissions.ContainsKey(channel.Id.ToString())) + _permissionsDict[server].ChannelPermissions.Add(channel.Id.ToString(), new Permissions()); + + var modules = _permissionsDict[server].ChannelPermissions[channel.Id.ToString()].modules; + + if (modules.ContainsKey(moduleName)) + modules[moduleName] = value; + else + modules.Add(moduleName, value); + WriteServerToJson(server); + } + + public static void SetChannelCommandPermission(Channel channel, string commandName, bool value) { + var server = channel.Server; + if (!_permissionsDict.ContainsKey(server)) { + _permissionsDict.TryAdd(server, new ServerPermissions(server.Id.ToString())); + } + if (!_permissionsDict[server].ChannelPermissions.ContainsKey(channel.Id.ToString())) + _permissionsDict[server].ChannelPermissions.Add(channel.Id.ToString(), new Permissions()); + + var commands = _permissionsDict[server].ChannelPermissions[channel.Id.ToString()].commands; + + if (commands.ContainsKey(commandName)) + commands[commandName] = value; + else + commands.Add(commandName, value); + WriteServerToJson(server); + } + + public static void SetRoleModulePermission(Role role, string roleName, bool value) { + var server = role.Server; + if (!_permissionsDict.ContainsKey(server)) { + _permissionsDict.TryAdd(server, new ServerPermissions(server.Id.ToString())); + } + if (!_permissionsDict[server].RolePermissions.ContainsKey(role.Id.ToString())) + _permissionsDict[server].RolePermissions.Add(role.Id.ToString(), new Permissions()); + + var modules = _permissionsDict[server].RolePermissions[role.Id.ToString()].modules; + + if (modules.ContainsKey(roleName)) + modules[roleName] = value; + else + modules.Add(roleName, value); + WriteServerToJson(server); + } + + public static void SetRoleCommandPermission(Role role, string roleName, bool value) { + var server = role.Server; + if (!_permissionsDict.ContainsKey(server)) { + _permissionsDict.TryAdd(server, new ServerPermissions(server.Id.ToString())); + } + if (!_permissionsDict[server].RolePermissions.ContainsKey(role.Id.ToString())) + _permissionsDict[server].RolePermissions.Add(role.Id.ToString(), new Permissions()); + + var commands = _permissionsDict[server].RolePermissions[role.Id.ToString()].commands; + + if (commands.ContainsKey(roleName)) + commands[roleName] = value; + else + commands.Add(roleName, value); + WriteServerToJson(server); + } + + public static void SetUserModulePermission(User user, string userName, bool value) { + var server = user.Server; + if (!_permissionsDict.ContainsKey(server)) { + _permissionsDict.TryAdd(server, new ServerPermissions(server.Id.ToString())); + } + if (!_permissionsDict[server].UserPermissions.ContainsKey(user.Id.ToString())) + _permissionsDict[server].UserPermissions.Add(user.Id.ToString(), new Permissions()); + + var modules = _permissionsDict[server].UserPermissions[user.Id.ToString()].modules; + + if (modules.ContainsKey(userName)) + modules[userName] = value; + else + modules.Add(userName, value); + WriteServerToJson(server); + } + + public static void SetUserCommandPermission(User user, string userName, bool value) { + var server = user.Server; + if (!_permissionsDict.ContainsKey(server)) { + _permissionsDict.TryAdd(server, new ServerPermissions(server.Id.ToString())); + } + if (!_permissionsDict[server].UserPermissions.ContainsKey(user.Id.ToString())) + _permissionsDict[server].UserPermissions.Add(user.Id.ToString(), new Permissions()); + + var commands = _permissionsDict[server].UserPermissions[user.Id.ToString()].commands; + + if (commands.ContainsKey(userName)) + commands[userName] = value; + else + commands.Add(userName, value); + WriteServerToJson(server); + } } /// /// Holds a permission list @@ -38,6 +164,11 @@ namespace NadekoBot.Classes.Permissions { /// Command name with allowed/disallowed /// public Dictionary commands { get; set; } + + public Permissions() { + modules = new Dictionary(); + commands = new Dictionary(); + } } public class PermissionsContainer { @@ -50,7 +181,10 @@ namespace NadekoBot.Classes.Permissions { /// public Permissions Permissions { get; set; } - public PermissionsContainer() { } + public PermissionsContainer(string id) { + Id = id; + Permissions = new Permissions(); + } } public class ServerPermissions : PermissionsContainer { @@ -62,9 +196,17 @@ namespace NadekoBot.Classes.Permissions { /// Does it print the error when a restriction occurs /// public bool Verbose { get; set; } - - public List UserPermissions { get; set; } - public List ChannelPermissions { get; set; } - public List RolePermissions { get; set; } + + public Dictionary UserPermissions { get; set; } + public Dictionary ChannelPermissions { get; set; } + public Dictionary RolePermissions { get; set; } + + public ServerPermissions(string id) : base(id) { + PermissionsControllerRoleName = "PermissionsKing"; + Verbose = true; + UserPermissions = new Dictionary(); + ChannelPermissions = new Dictionary(); + RolePermissions = new Dictionary(); + } } } \ No newline at end of file