From ca52c29559b96ab61ed09ce5e8455f120d293820 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Sat, 13 Feb 2016 17:01:24 +0100 Subject: [PATCH] fixes, ;pr , donators fixed --- .../Classes/Permissions/PermissionChecker.cs | 12 ++++-- .../Classes/Permissions/PermissionsHandler.cs | 40 +++++++++++-------- NadekoBot/Modules/Administration.cs | 4 +- NadekoBot/Modules/NSFW.cs | 6 +-- NadekoBot/Modules/Permissions.cs | 40 +++++++++++++++---- 5 files changed, 69 insertions(+), 33 deletions(-) diff --git a/NadekoBot/Classes/Permissions/PermissionChecker.cs b/NadekoBot/Classes/Permissions/PermissionChecker.cs index 7544cafe..bba332e3 100644 --- a/NadekoBot/Classes/Permissions/PermissionChecker.cs +++ b/NadekoBot/Classes/Permissions/PermissionChecker.cs @@ -25,11 +25,15 @@ namespace NadekoBot.Classes.Permissions { //is it a permission command? // if it is, check if the user has the correct role // if yes return true, if no return false - if (command.Category == "Permissions") - if (user.Server.IsOwner || user.HasRole(PermissionHelper.ValidateRole(user.Server, PermissionsHandler.GetServerPermissionsRoleName(user.Server)))) + if (command.Category == "Permissions") { + Discord.Role role = null; + try { + role = PermissionHelper.ValidateRole(user.Server, PermissionsHandler.GetServerPermissionsRoleName(user.Server)); + } catch { } + if (user.Server.Owner.Id == user.Id || (role != null && user.HasRole(role))) return true; - else - throw new Exception($"You don't have the necessary role (**{PermissionsHandler._permissionsDict[user.Server].PermissionsControllerRole}**) to change permissions."); + throw new Exception($"You don't have the necessary role (**{PermissionsHandler._permissionsDict[user.Server].PermissionsControllerRole}**) to change permissions."); + } var permissionType = PermissionsHandler.GetPermissionBanType(command, user, channel); diff --git a/NadekoBot/Classes/Permissions/PermissionsHandler.cs b/NadekoBot/Classes/Permissions/PermissionsHandler.cs index 27739d39..81e2edf6 100644 --- a/NadekoBot/Classes/Permissions/PermissionsHandler.cs +++ b/NadekoBot/Classes/Permissions/PermissionsHandler.cs @@ -14,7 +14,6 @@ namespace NadekoBot.Classes.Permissions { public static ConcurrentDictionary _permissionsDict = new ConcurrentDictionary(); - public enum PermissionBanType { None, ServerBanCommand, ServerBanModule, ChannelBanCommand, ChannelBanModule, RoleBanCommand, @@ -42,13 +41,6 @@ namespace NadekoBot.Classes.Permissions { Console.WriteLine("Permission initialization complete."); } - internal static void SetVerbosity(Server server, bool val) { - if (!_permissionsDict.ContainsKey(server)) { - _permissionsDict.TryAdd(server, new ServerPermissions(server.Id, server.Name)); - } - _permissionsDict[server].Verbose = val; - } - internal static Permissions GetRolePermissionsById(Server server, ulong id) { if (!_permissionsDict.ContainsKey(server)) return null; @@ -172,6 +164,22 @@ namespace NadekoBot.Classes.Permissions { return _permissionsDict[server].PermissionsControllerRole; } + internal static void SetPermissionsRole(Server server, string roleName) { + if (!_permissionsDict.ContainsKey(server)) { + _permissionsDict.TryAdd(server, new ServerPermissions(server.Id, server.Name)); + } + _permissionsDict[server].PermissionsControllerRole = roleName; + Task.Run(() => WriteServerToJson(server)); + } + + internal static void SetVerbosity(Server server, bool val) { + if (!_permissionsDict.ContainsKey(server)) { + _permissionsDict.TryAdd(server, new ServerPermissions(server.Id, server.Name)); + } + _permissionsDict[server].Verbose = val; + Task.Run(() => WriteServerToJson(server)); + } + public static void SetServerModulePermission(Server server, string moduleName, bool value) { if (!_permissionsDict.ContainsKey(server)) { _permissionsDict.TryAdd(server, new ServerPermissions(server.Id, server.Name)); @@ -181,7 +189,7 @@ namespace NadekoBot.Classes.Permissions { modules[moduleName] = value; else modules.Add(moduleName, value); - WriteServerToJson(server); + Task.Run(() => WriteServerToJson(server)); } public static void SetServerCommandPermission(Server server, string commandName, bool value) { @@ -193,7 +201,7 @@ namespace NadekoBot.Classes.Permissions { commands[commandName] = value; else commands.Add(commandName, value); - WriteServerToJson(server); + Task.Run(() => WriteServerToJson(server)); } public static void SetChannelModulePermission(Channel channel, string moduleName, bool value) { @@ -210,7 +218,7 @@ namespace NadekoBot.Classes.Permissions { modules[moduleName] = value; else modules.Add(moduleName, value); - WriteServerToJson(server); + Task.Run(() => WriteServerToJson(server)); } public static void SetChannelCommandPermission(Channel channel, string commandName, bool value) { @@ -227,7 +235,7 @@ namespace NadekoBot.Classes.Permissions { commands[commandName] = value; else commands.Add(commandName, value); - WriteServerToJson(server); + Task.Run(() => WriteServerToJson(server)); } public static void SetRoleModulePermission(Role role, string moduleName, bool value) { @@ -244,7 +252,7 @@ namespace NadekoBot.Classes.Permissions { modules[moduleName] = value; else modules.Add(moduleName, value); - WriteServerToJson(server); + Task.Run(() => WriteServerToJson(server)); } public static void SetRoleCommandPermission(Role role, string commandName, bool value) { @@ -261,7 +269,7 @@ namespace NadekoBot.Classes.Permissions { commands[commandName] = value; else commands.Add(commandName, value); - WriteServerToJson(server); + Task.Run(() => WriteServerToJson(server)); } public static void SetUserModulePermission(User user, string moduleName, bool value) { @@ -278,7 +286,7 @@ namespace NadekoBot.Classes.Permissions { modules[moduleName] = value; else modules.Add(moduleName, value); - WriteServerToJson(server); + Task.Run(() => WriteServerToJson(server)); } public static void SetUserCommandPermission(User user, string commandName, bool value) { @@ -295,7 +303,7 @@ namespace NadekoBot.Classes.Permissions { commands[commandName] = value; else commands.Add(commandName, value); - WriteServerToJson(server); + Task.Run(() => WriteServerToJson(server)); } } /// diff --git a/NadekoBot/Modules/Administration.cs b/NadekoBot/Modules/Administration.cs index 58920e68..ce88f19c 100644 --- a/NadekoBot/Modules/Administration.cs +++ b/NadekoBot/Modules/Administration.cs @@ -580,8 +580,8 @@ namespace NadekoBot.Modules { var rows = Classes.DBHandler.Instance.GetAllRows(); var donatorsOrdered = rows.OrderBy(d => d.Amount); string str = $"`Total number of people who donated is {donatorsOrdered.Count()}`\n"; - - await e.Channel.SendMessage(string.Join(", ", donatorsOrdered)); + + await e.Channel.SendMessage(string.Join(", ", donatorsOrdered.Select(d => d.UserName))); }); }); diff --git a/NadekoBot/Modules/NSFW.cs b/NadekoBot/Modules/NSFW.cs index 6b4eda39..92a936e2 100644 --- a/NadekoBot/Modules/NSFW.cs +++ b/NadekoBot/Modules/NSFW.cs @@ -20,7 +20,7 @@ namespace NadekoBot.Modules { cgb.AddCheck(Classes.Permissions.PermissionChecker.Instance); cgb.CreateCommand("~hentai") - .Description("Shows a random NSFW hentai image from gelbooru and danbooru with a given tag. Tag is optional but preffered.\n**Usage**: ~hentai yuri") + .Description("Shows a random NSFW hentai image from gelbooru and danbooru with a given tag. Tag is optional but preffered. (multiple tags are appended with +)\n**Usage**: ~hentai yuri") .Parameter("tag", ParameterType.Unparsed) .Do(async e => { string tag = e.GetArg("tag"); @@ -30,7 +30,7 @@ namespace NadekoBot.Modules { await e.Send(":heart: Danbooru: " + await SearchHelper.GetDanbooruImageLink(tag)); }); cgb.CreateCommand("~danbooru") - .Description("Shows a random hentai image from danbooru with a given tag. Tag is optional but preffered.\n**Usage**: ~danbooru yuri") + .Description("Shows a random hentai image from danbooru with a given tag. Tag is optional but preffered. (multiple tags are appended with +)\n**Usage**: ~danbooru yuri+kissing") .Parameter("tag", ParameterType.Unparsed) .Do(async e => { string tag = e.GetArg("tag"); @@ -39,7 +39,7 @@ namespace NadekoBot.Modules { await e.Send(await SearchHelper.GetDanbooruImageLink(tag)); }); cgb.CreateCommand("~gelbooru") - .Description("Shows a random hentai image from gelbooru with a given tag. Tag is optional but preffered.\n**Usage**: ~gelbooru yuri") + .Description("Shows a random hentai image from gelbooru with a given tag. Tag is optional but preffered. (multiple tags are appended with +)\n**Usage**: ~gelbooru yuri+kissing") .Parameter("tag", ParameterType.Unparsed) .Do(async e => { string tag = e.GetArg("tag"); diff --git a/NadekoBot/Modules/Permissions.cs b/NadekoBot/Modules/Permissions.cs index 5bee84a2..b5f87d98 100644 --- a/NadekoBot/Modules/Permissions.cs +++ b/NadekoBot/Modules/Permissions.cs @@ -21,15 +21,39 @@ namespace NadekoBot.Modules { commands.ForEach(cmd => cmd.Init(cgb)); + cgb.CreateCommand(prefix + "permrole") + .Alias(prefix + "pr") + .Description("Sets a role which can change permissions. Or supply no parameters to find out the current one. Default one is 'Nadeko'.") + .Parameter("role", ParameterType.Unparsed) + .Do(async e => { + if (string.IsNullOrWhiteSpace(e.GetArg("role"))) { + await e.Send($"Current permissions role is `{PermsHandler.GetServerPermissionsRoleName(e.Server)}`"); + return; + } + + var arg = e.GetArg("role"); + Discord.Role role = null; + try { + role = PermissionHelper.ValidateRole(e.Server, arg); + } catch (Exception ex) { + Console.WriteLine(ex.Message); + await e.Send($"Role `{arg}` probably doesn't exist. Create the role with that name first."); + return; + } + PermsHandler.SetPermissionsRole(e.Server, role.Name); + await e.Send($"Role `{role.Name}` is now required in order to change permissions."); + }); + cgb.CreateCommand(prefix + "verbose") - .Description("Sets whether to show when a command/module is blocked.\n**Usage**: ;verbose true") - .Parameter("arg", ParameterType.Required) - .Do(async e => { - var arg = e.GetArg("arg"); - bool val = PermissionHelper.ValidateBool(arg); - PermsHandler.SetVerbosity(e.Server, val); - await e.Send($"Verbosity set to {val}."); - }); + .Alias(prefix + "v") + .Description("Sets whether to show when a command/module is blocked.\n**Usage**: ;verbose true") + .Parameter("arg", ParameterType.Required) + .Do(async e => { + var arg = e.GetArg("arg"); + bool val = PermissionHelper.ValidateBool(arg); + PermsHandler.SetVerbosity(e.Server, val); + await e.Send($"Verbosity set to {val}."); + }); cgb.CreateCommand(prefix + "serverperms") .Alias(prefix + "sp")