From eb90bb5bd136a7b6ceb359f25ffd8d540d8b1964 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Thu, 14 Apr 2016 23:39:10 +0200 Subject: [PATCH] Help, Conversations, Permissions refactored --- .../{Commands => Classes}/DiscordCommand.cs | 0 NadekoBot/Commands/Bomberman.cs | 56 - .../Conversations}/Commands/CopyCommand.cs | 36 +- .../Commands/RequestsCommand.cs | 90 +- .../{ => Conversations}/Conversations.cs | 4 +- .../Help}/Commands/HelpCommand.cs | 2 +- NadekoBot/Modules/{ => Help}/Help.cs | 4 +- .../Commands/FilterInvitesCommand.cs | 6 +- .../Commands/FilterWordsCommand.cs | 6 +- .../PermissionsModule.cs} | 1352 ++++++++--------- NadekoBot/NadekoBot.cs | 4 + NadekoBot/NadekoBot.csproj | 28 +- 12 files changed, 788 insertions(+), 800 deletions(-) rename NadekoBot/{Commands => Classes}/DiscordCommand.cs (100%) delete mode 100644 NadekoBot/Commands/Bomberman.cs rename NadekoBot/{ => Modules/Conversations}/Commands/CopyCommand.cs (74%) rename NadekoBot/{ => Modules/Conversations}/Commands/RequestsCommand.cs (69%) rename NadekoBot/Modules/{ => Conversations}/Conversations.cs (99%) rename NadekoBot/{ => Modules/Help}/Commands/HelpCommand.cs (99%) rename NadekoBot/Modules/{ => Help}/Help.cs (96%) rename NadekoBot/{ => Modules/Permissions}/Commands/FilterInvitesCommand.cs (97%) rename NadekoBot/{ => Modules/Permissions}/Commands/FilterWordsCommand.cs (98%) rename NadekoBot/Modules/{Permissions.cs => Permissions/PermissionsModule.cs} (98%) diff --git a/NadekoBot/Commands/DiscordCommand.cs b/NadekoBot/Classes/DiscordCommand.cs similarity index 100% rename from NadekoBot/Commands/DiscordCommand.cs rename to NadekoBot/Classes/DiscordCommand.cs diff --git a/NadekoBot/Commands/Bomberman.cs b/NadekoBot/Commands/Bomberman.cs deleted file mode 100644 index 44d460e5..00000000 --- a/NadekoBot/Commands/Bomberman.cs +++ /dev/null @@ -1,56 +0,0 @@ -using Discord; -using Discord.Commands; -using NadekoBot.Classes; -using NadekoBot.Modules; -using System; - -namespace NadekoBot.Commands -{ - class Bomberman : DiscordCommand - { - public Bomberman(DiscordModule module) : base(module) - { - NadekoBot.Client.MessageReceived += async (s, e) => - { - if (e.Channel.Id != bombGame.ChannelId) return; - - var text = e.Message.Text; - await e.Message.Delete(); - HandleBombermanCommand(e.User, text); - }; - } - - private void HandleBombermanCommand(User user, string text) - { - throw new NotImplementedException(); - } - - //only one bomberman game can run at any one time - public static BombermanGame bombGame = null; - private readonly object locker = new object(); - internal override void Init(CommandGroupBuilder cgb) - { - cgb.CreateCommand($"{Module.Prefix}bmb") - .Description("Creates a bomberman game for this channel or join existing one." + - " If you are 4th player - Game will start. After game starts " + - " everything written in the channel will be autodeleted and treated as a bomberman command." + - " only one bomberman game can run at any one time per bot. Game will run at 1FPS." + - " You must have manage messages permissions in order to create the game.") - .Do(e => - { - lock (locker) - { - if (bombGame == null || bombGame.Ended) - { - if (!e.User.ServerPermissions.ManageMessages || - !e.Server.GetUser(NadekoBot.Client.CurrentUser.Id).ServerPermissions.ManageMessages) - { - e.Channel.SendMessage("Both you and Nadeko need manage messages permissions to start a new bomberman game.").Wait(); - } - - } - } - }); - } - } -} diff --git a/NadekoBot/Commands/CopyCommand.cs b/NadekoBot/Modules/Conversations/Commands/CopyCommand.cs similarity index 74% rename from NadekoBot/Commands/CopyCommand.cs rename to NadekoBot/Modules/Conversations/Commands/CopyCommand.cs index 6c9b0225..6bba9c45 100644 --- a/NadekoBot/Commands/CopyCommand.cs +++ b/NadekoBot/Modules/Conversations/Commands/CopyCommand.cs @@ -1,36 +1,45 @@ -using System; +using Discord.Commands; +using NadekoBot.Modules; +using System; using System.Collections.Generic; using System.Threading.Tasks; -using Discord.Commands; -using NadekoBot.Modules; -namespace NadekoBot.Commands { - internal class CopyCommand : DiscordCommand { +namespace NadekoBot.Commands.Conversations.Commands +{ + internal class CopyCommand : DiscordCommand + { private readonly HashSet CopiedUsers = new HashSet(); - public CopyCommand(DiscordModule module) : base(module) { + public CopyCommand(DiscordModule module) : base(module) + { NadekoBot.Client.MessageReceived += Client_MessageReceived; } - private async void Client_MessageReceived(object sender, Discord.MessageEventArgs e) { + private async void Client_MessageReceived(object sender, Discord.MessageEventArgs e) + { if (e.User.Id == NadekoBot.Client.CurrentUser.Id) return; - try { + try + { if (string.IsNullOrWhiteSpace(e.Message.Text)) return; - if (CopiedUsers.Contains(e.User.Id)) { + if (CopiedUsers.Contains(e.User.Id)) + { await e.Channel.SendMessage(e.Message.Text); } - } catch { } + } + catch { } } - public Func DoFunc() => async e => { + public Func DoFunc() => async e => + { if (CopiedUsers.Contains(e.User.Id)) return; CopiedUsers.Add(e.User.Id); await e.Channel.SendMessage(" I'll start copying you now."); }; - internal override void Init(CommandGroupBuilder cgb) { + internal override void Init(CommandGroupBuilder cgb) + { cgb.CreateCommand("copyme") .Alias("cm") .Description("Nadeko starts copying everything you say. Disable with cs") @@ -42,7 +51,8 @@ namespace NadekoBot.Commands { .Do(StopCopy()); } - private Func StopCopy() => async e => { + private Func StopCopy() => async e => + { if (!CopiedUsers.Contains(e.User.Id)) return; CopiedUsers.Remove(e.User.Id); diff --git a/NadekoBot/Commands/RequestsCommand.cs b/NadekoBot/Modules/Conversations/Commands/RequestsCommand.cs similarity index 69% rename from NadekoBot/Commands/RequestsCommand.cs rename to NadekoBot/Modules/Conversations/Commands/RequestsCommand.cs index c26a6cee..57d7b2b8 100644 --- a/NadekoBot/Commands/RequestsCommand.cs +++ b/NadekoBot/Modules/Conversations/Commands/RequestsCommand.cs @@ -1,13 +1,17 @@ -using System; -using System.Threading.Tasks; -using Discord.Commands; +using Discord.Commands; using NadekoBot.Extensions; using NadekoBot.Modules; +using System; +using System.Threading.Tasks; -namespace NadekoBot.Commands { - internal class RequestsCommand : DiscordCommand { - public void SaveRequest(CommandEventArgs e, string text) { - Classes.DbHandler.Instance.InsertData(new Classes._DataModels.Request { +namespace NadekoBot.Commands.Conversations.Commands +{ + internal class RequestsCommand : DiscordCommand + { + public void SaveRequest(CommandEventArgs e, string text) + { + Classes.DbHandler.Instance.InsertData(new Classes._DataModels.Request + { RequestText = text, UserName = e.User.Name, UserId = (long)e.User.Id, @@ -17,18 +21,20 @@ namespace NadekoBot.Commands { }); } // todo what if it's too long? - public string GetRequests() { + public string GetRequests() + { var task = Classes.DbHandler.Instance.GetAllRows(); var str = "Here are all current requests for NadekoBot:\n\n"; - foreach (var reqObj in task) { + foreach (var reqObj in task) + { str += $"{reqObj.Id}. by **{reqObj.UserName}** from **{reqObj.ServerName}** at {reqObj.DateAdded.ToLocalTime()}\n" + $"**{reqObj.RequestText}**\n----------\n"; } return str + "\n__Type [@NadekoBot clr] to clear all of my messages.__"; } - public bool DeleteRequest(int requestNumber) => + public bool DeleteRequest(int requestNumber) => Classes.DbHandler.Instance.Delete(requestNumber) != null; /// @@ -38,18 +44,23 @@ namespace NadekoBot.Commands { public Classes._DataModels.Request ResolveRequest(int requestNumber) => Classes.DbHandler.Instance.Delete(requestNumber); - internal override void Init(CommandGroupBuilder cgb) { + internal override void Init(CommandGroupBuilder cgb) + { cgb.CreateCommand("req") .Alias("request") .Description("Requests a feature for nadeko.\n**Usage**: @NadekoBot req new_feature") .Parameter("all", ParameterType.Unparsed) - .Do(async e => { + .Do(async e => + { var str = e.Args[0]; - try { + try + { SaveRequest(e, str); - } catch { + } + catch + { await e.Channel.SendMessage("Something went wrong."); return; } @@ -58,7 +69,8 @@ namespace NadekoBot.Commands { cgb.CreateCommand("lr") .Description("PMs the user all current nadeko requests.") - .Do(async e => { + .Do(async e => + { var str = await Task.Run(() => GetRequests()); if (str.Trim().Length > 110) await e.User.Send(str); @@ -69,40 +81,58 @@ namespace NadekoBot.Commands { cgb.CreateCommand("dr") .Description("Deletes a request. Only owner is able to do this.") .Parameter("reqNumber", ParameterType.Required) - .Do(async e => { - if (NadekoBot.IsOwner(e.User.Id)) { - try { - if (DeleteRequest(int.Parse(e.Args[0]))) { + .Do(async e => + { + if (NadekoBot.IsOwner(e.User.Id)) + { + try + { + if (DeleteRequest(int.Parse(e.Args[0]))) + { await e.Channel.SendMessage(e.User.Mention + " Request deleted."); - } else { + } + else + { await e.Channel.SendMessage("No request on that number."); } - } catch { + } + catch + { await e.Channel.SendMessage("Error deleting request, probably NaN error."); } - } else await e.Channel.SendMessage("You don't have permission to do that."); + } + else await e.Channel.SendMessage("You don't have permission to do that."); }); cgb.CreateCommand("rr") .Description("Resolves a request. Only owner is able to do this.") .Parameter("reqNumber", ParameterType.Required) - .Do(async e => { - if (NadekoBot.IsOwner(e.User.Id)) { - try { + .Do(async e => + { + if (NadekoBot.IsOwner(e.User.Id)) + { + try + { var sc = ResolveRequest(int.Parse(e.Args[0])); - if (sc != null) { + if (sc != null) + { await e.Channel.SendMessage(e.User.Mention + " Request resolved, notice sent."); await NadekoBot.Client.GetServer((ulong)sc.ServerId).GetUser((ulong)sc.UserId).Send("**This request of yours has been resolved:**\n" + sc.RequestText); - } else { + } + else + { await e.Channel.SendMessage("No request on that number."); } - } catch { + } + catch + { await e.Channel.SendMessage("Error resolving request, probably NaN error."); } - } else await e.Channel.SendMessage("You don't have permission to do that."); + } + else await e.Channel.SendMessage("You don't have permission to do that."); }); } - public RequestsCommand(DiscordModule module) : base(module) {} + public RequestsCommand(DiscordModule module) : base(module) { } } } diff --git a/NadekoBot/Modules/Conversations.cs b/NadekoBot/Modules/Conversations/Conversations.cs similarity index 99% rename from NadekoBot/Modules/Conversations.cs rename to NadekoBot/Modules/Conversations/Conversations.cs index 58b61f26..62f28a00 100644 --- a/NadekoBot/Modules/Conversations.cs +++ b/NadekoBot/Modules/Conversations/Conversations.cs @@ -2,7 +2,7 @@ using Discord; using Discord.Commands; using Discord.Modules; using NadekoBot.Classes; -using NadekoBot.Commands; +using NadekoBot.Commands.Conversations.Commands; using NadekoBot.Extensions; using NadekoBot.Properties; using System; @@ -13,7 +13,7 @@ using System.IO; using System.Linq; using System.Threading.Tasks; -namespace NadekoBot.Modules +namespace NadekoBot.Modules.Conversations { internal class Conversations : DiscordModule { diff --git a/NadekoBot/Commands/HelpCommand.cs b/NadekoBot/Modules/Help/Commands/HelpCommand.cs similarity index 99% rename from NadekoBot/Commands/HelpCommand.cs rename to NadekoBot/Modules/Help/Commands/HelpCommand.cs index 666f5148..9c8aedac 100644 --- a/NadekoBot/Commands/HelpCommand.cs +++ b/NadekoBot/Modules/Help/Commands/HelpCommand.cs @@ -6,7 +6,7 @@ using System.IO; using System.Linq; using System.Threading.Tasks; -namespace NadekoBot.Commands +namespace NadekoBot.Commands.Help.Commands { internal class HelpCommand : DiscordCommand { diff --git a/NadekoBot/Modules/Help.cs b/NadekoBot/Modules/Help/Help.cs similarity index 96% rename from NadekoBot/Modules/Help.cs rename to NadekoBot/Modules/Help/Help.cs index 78bea65f..05f2e206 100644 --- a/NadekoBot/Modules/Help.cs +++ b/NadekoBot/Modules/Help/Help.cs @@ -1,10 +1,10 @@ using Discord.Commands; using Discord.Modules; -using NadekoBot.Commands; +using NadekoBot.Commands.Help.Commands; using NadekoBot.Extensions; using System.Linq; -namespace NadekoBot.Modules +namespace NadekoBot.Modules.Help { internal class Help : DiscordModule { diff --git a/NadekoBot/Commands/FilterInvitesCommand.cs b/NadekoBot/Modules/Permissions/Commands/FilterInvitesCommand.cs similarity index 97% rename from NadekoBot/Commands/FilterInvitesCommand.cs rename to NadekoBot/Modules/Permissions/Commands/FilterInvitesCommand.cs index 519f6b43..a6981e06 100644 --- a/NadekoBot/Commands/FilterInvitesCommand.cs +++ b/NadekoBot/Modules/Permissions/Commands/FilterInvitesCommand.cs @@ -2,12 +2,12 @@ using Discord.Commands; using NadekoBot.Classes; using NadekoBot.Classes.Permissions; -using NadekoBot.Modules; +using NadekoBot.Commands; using System; using System.Text.RegularExpressions; using ServerPermissions = NadekoBot.Classes.Permissions.ServerPermissions; -namespace NadekoBot.Commands +namespace NadekoBot.Modules.Permissions.Commands { internal class FilterInvitesCommand : DiscordCommand { @@ -46,7 +46,7 @@ namespace NadekoBot.Commands if (serverPerms.Permissions.FilterInvites) return true; - Permissions perms; + Classes.Permissions.Permissions perms; return serverPerms.ChannelPermissions.TryGetValue(channel.Id, out perms) && perms.FilterInvites; } diff --git a/NadekoBot/Commands/FilterWordsCommand.cs b/NadekoBot/Modules/Permissions/Commands/FilterWordsCommand.cs similarity index 98% rename from NadekoBot/Commands/FilterWordsCommand.cs rename to NadekoBot/Modules/Permissions/Commands/FilterWordsCommand.cs index 5c140238..83e1b2af 100644 --- a/NadekoBot/Commands/FilterWordsCommand.cs +++ b/NadekoBot/Modules/Permissions/Commands/FilterWordsCommand.cs @@ -2,12 +2,12 @@ using Discord.Commands; using NadekoBot.Classes; using NadekoBot.Classes.Permissions; -using NadekoBot.Modules; +using NadekoBot.Commands; using System; using System.Linq; using ServerPermissions = NadekoBot.Classes.Permissions.ServerPermissions; -namespace NadekoBot.Commands +namespace NadekoBot.Modules.Permissions.Commands { internal class FilterWords : DiscordCommand { @@ -44,7 +44,7 @@ namespace NadekoBot.Commands if (serverPerms.Permissions.FilterWords) return true; - Permissions perms; + Classes.Permissions.Permissions perms; return serverPerms.ChannelPermissions.TryGetValue(channel.Id, out perms) && perms.FilterWords; } diff --git a/NadekoBot/Modules/Permissions.cs b/NadekoBot/Modules/Permissions/PermissionsModule.cs similarity index 98% rename from NadekoBot/Modules/Permissions.cs rename to NadekoBot/Modules/Permissions/PermissionsModule.cs index 60c29f37..998906b7 100644 --- a/NadekoBot/Modules/Permissions.cs +++ b/NadekoBot/Modules/Permissions/PermissionsModule.cs @@ -1,676 +1,676 @@ -using Discord.Commands; -using Discord.Modules; -using NadekoBot.Classes.JSONModels; -using NadekoBot.Classes.Permissions; -using NadekoBot.Commands; -using NadekoBot.Extensions; -using NadekoBot.Modules.Games.Commands; -using System; -using System.Linq; -using System.Threading.Tasks; -using PermsHandler = NadekoBot.Classes.Permissions.PermissionsHandler; - -namespace NadekoBot.Modules -{ - internal class PermissionModule : DiscordModule - { - public override string Prefix { get; } = NadekoBot.Config.CommandPrefixes.Permissions; - - public PermissionModule() - { - commands.Add(new FilterInvitesCommand(this)); - commands.Add(new FilterWords(this)); - } - - public override void Install(ModuleManager manager) - { - manager.CreateCommands("", cgb => - { - - cgb.AddCheck(PermissionChecker.Instance); - - 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.Channel.SendMessage($"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.Channel.SendMessage($"Role `{arg}` probably doesn't exist. Create the role with that name first."); - return; - } - PermsHandler.SetPermissionsRole(e.Server, role.Name); - await e.Channel.SendMessage($"Role `{role.Name}` is now required in order to change permissions."); - }); - - cgb.CreateCommand(Prefix + "verbose") - .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"); - var val = PermissionHelper.ValidateBool(arg); - PermsHandler.SetVerbosity(e.Server, val); - await e.Channel.SendMessage($"Verbosity set to {val}."); - }); - - cgb.CreateCommand(Prefix + "serverperms") - .Alias(Prefix + "sp") - .Description("Shows banned permissions for this server.") - .Do(async e => - { - var perms = PermsHandler.GetServerPermissions(e.Server); - if (string.IsNullOrWhiteSpace(perms?.ToString())) - await e.Channel.SendMessage("No permissions set for this server."); - await e.Channel.SendMessage(perms.ToString()); - }); - - cgb.CreateCommand(Prefix + "roleperms") - .Alias(Prefix + "rp") - .Description("Shows banned permissions for a certain role. No argument means for everyone.\n**Usage**: ;rp AwesomeRole") - .Parameter("role", ParameterType.Unparsed) - .Do(async e => - { - var arg = e.GetArg("role"); - var role = e.Server.EveryoneRole; - if (!string.IsNullOrWhiteSpace(arg)) - try - { - role = PermissionHelper.ValidateRole(e.Server, arg); - } - catch (Exception ex) - { - await e.Channel.SendMessage("💢 Error: " + ex.Message); - return; - } - - var perms = PermsHandler.GetRolePermissionsById(e.Server, role.Id); - - if (string.IsNullOrWhiteSpace(perms?.ToString())) - await e.Channel.SendMessage($"No permissions set for **{role.Name}** role."); - await e.Channel.SendMessage(perms.ToString()); - }); - - cgb.CreateCommand(Prefix + "channelperms") - .Alias(Prefix + "cp") - .Description("Shows banned permissions for a certain channel. No argument means for this channel.\n**Usage**: ;cp #dev") - .Parameter("channel", ParameterType.Unparsed) - .Do(async e => - { - var arg = e.GetArg("channel"); - var channel = e.Channel; - if (!string.IsNullOrWhiteSpace(arg)) - try - { - channel = PermissionHelper.ValidateChannel(e.Server, arg); - } - catch (Exception ex) - { - await e.Channel.SendMessage("💢 Error: " + ex.Message); - return; - } - - var perms = PermsHandler.GetChannelPermissionsById(e.Server, channel.Id); - if (string.IsNullOrWhiteSpace(perms?.ToString())) - await e.Channel.SendMessage($"No permissions set for **{channel.Name}** channel."); - await e.Channel.SendMessage(perms.ToString()); - }); - - cgb.CreateCommand(Prefix + "userperms") - .Alias(Prefix + "up") - .Description("Shows banned permissions for a certain user. No argument means for yourself.\n**Usage**: ;up Kwoth") - .Parameter("user", ParameterType.Unparsed) - .Do(async e => - { - var user = e.User; - if (!string.IsNullOrWhiteSpace(e.GetArg("user"))) - try - { - user = PermissionHelper.ValidateUser(e.Server, e.GetArg("user")); - } - catch (Exception ex) - { - await e.Channel.SendMessage("💢 Error: " + ex.Message); - return; - } - - var perms = PermsHandler.GetUserPermissionsById(e.Server, user.Id); - if (string.IsNullOrWhiteSpace(perms?.ToString())) - await e.Channel.SendMessage($"No permissions set for user **{user.Name}**."); - await e.Channel.SendMessage(perms.ToString()); - }); - - cgb.CreateCommand(Prefix + "sm").Alias(Prefix + "servermodule") - .Parameter("module", ParameterType.Required) - .Parameter("bool", ParameterType.Required) - .Description("Sets a module's permission at the server level.\n**Usage**: ;sm [module_name] enable") - .Do(async e => - { - try - { - var module = PermissionHelper.ValidateModule(e.GetArg("module")); - var state = PermissionHelper.ValidateBool(e.GetArg("bool")); - - PermsHandler.SetServerModulePermission(e.Server, module, state); - await e.Channel.SendMessage($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** on this server."); - } - catch (ArgumentException exArg) - { - await e.Channel.SendMessage(exArg.Message); - } - catch (Exception ex) - { - await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); - } - }); - - cgb.CreateCommand(Prefix + "sc").Alias(Prefix + "servercommand") - .Parameter("command", ParameterType.Required) - .Parameter("bool", ParameterType.Required) - .Description("Sets a command's permission at the server level.\n**Usage**: ;sc [command_name] disable") - .Do(async e => - { - try - { - var command = PermissionHelper.ValidateCommand(e.GetArg("command")); - var state = PermissionHelper.ValidateBool(e.GetArg("bool")); - - PermsHandler.SetServerCommandPermission(e.Server, command, state); - await e.Channel.SendMessage($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** on this server."); - } - catch (ArgumentException exArg) - { - await e.Channel.SendMessage(exArg.Message); - } - catch (Exception ex) - { - await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); - } - }); - - cgb.CreateCommand(Prefix + "rm").Alias(Prefix + "rolemodule") - .Parameter("module", ParameterType.Required) - .Parameter("bool", ParameterType.Required) - .Parameter("role", ParameterType.Unparsed) - .Description("Sets a module's permission at the role level.\n**Usage**: ;rm [module_name] enable [role_name]") - .Do(async e => - { - try - { - var module = PermissionHelper.ValidateModule(e.GetArg("module")); - var state = PermissionHelper.ValidateBool(e.GetArg("bool")); - - if (e.GetArg("role")?.ToLower() == "all") - { - foreach (var role in e.Server.Roles) - { - PermsHandler.SetRoleModulePermission(role, module, state); - } - await e.Channel.SendMessage($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** for **ALL** roles."); - } - else - { - var role = PermissionHelper.ValidateRole(e.Server, e.GetArg("role")); - - PermsHandler.SetRoleModulePermission(role, module, state); - await e.Channel.SendMessage($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** for **{role.Name}** role."); - } - } - catch (ArgumentException exArg) - { - await e.Channel.SendMessage(exArg.Message); - } - catch (Exception ex) - { - await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); - } - }); - - cgb.CreateCommand(Prefix + "rc").Alias(Prefix + "rolecommand") - .Parameter("command", ParameterType.Required) - .Parameter("bool", ParameterType.Required) - .Parameter("role", ParameterType.Unparsed) - .Description("Sets a command's permission at the role level.\n**Usage**: ;rc [command_name] disable [role_name]") - .Do(async e => - { - try - { - var command = PermissionHelper.ValidateCommand(e.GetArg("command")); - var state = PermissionHelper.ValidateBool(e.GetArg("bool")); - - if (e.GetArg("role")?.ToLower() == "all") - { - foreach (var role in e.Server.Roles) - { - PermsHandler.SetRoleCommandPermission(role, command, state); - } - await e.Channel.SendMessage($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** for **ALL** roles."); - } - else - { - var role = PermissionHelper.ValidateRole(e.Server, e.GetArg("role")); - - PermsHandler.SetRoleCommandPermission(role, command, state); - await e.Channel.SendMessage($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** for **{role.Name}** role."); - } - } - catch (ArgumentException exArg) - { - await e.Channel.SendMessage(exArg.Message); - } - catch (Exception ex) - { - await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); - } - }); - - cgb.CreateCommand(Prefix + "cm").Alias(Prefix + "channelmodule") - .Parameter("module", ParameterType.Required) - .Parameter("bool", ParameterType.Required) - .Parameter("channel", ParameterType.Unparsed) - .Description("Sets a module's permission at the channel level.\n**Usage**: ;cm [module_name] enable [channel_name]") - .Do(async e => - { - try - { - var module = PermissionHelper.ValidateModule(e.GetArg("module")); - var state = PermissionHelper.ValidateBool(e.GetArg("bool")); - - if (e.GetArg("channel")?.ToLower() == "all") - { - foreach (var channel in e.Server.TextChannels) - { - PermsHandler.SetChannelModulePermission(channel, module, state); - } - await e.Channel.SendMessage($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** on **ALL** channels."); - } - else - { - var channel = PermissionHelper.ValidateChannel(e.Server, e.GetArg("channel")); - - PermsHandler.SetChannelModulePermission(channel, module, state); - await e.Channel.SendMessage($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** for **{channel.Name}** channel."); - } - } - catch (ArgumentException exArg) - { - await e.Channel.SendMessage(exArg.Message); - } - catch (Exception ex) - { - await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); - } - }); - - cgb.CreateCommand(Prefix + "cc").Alias(Prefix + "channelcommand") - .Parameter("command", ParameterType.Required) - .Parameter("bool", ParameterType.Required) - .Parameter("channel", ParameterType.Unparsed) - .Description("Sets a command's permission at the channel level.\n**Usage**: ;cc [command_name] enable [channel_name]") - .Do(async e => - { - try - { - var command = PermissionHelper.ValidateCommand(e.GetArg("command")); - var state = PermissionHelper.ValidateBool(e.GetArg("bool")); - - if (e.GetArg("channel")?.ToLower() == "all") - { - foreach (var channel in e.Server.TextChannels) - { - PermsHandler.SetChannelCommandPermission(channel, command, state); - } - await e.Channel.SendMessage($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** on **ALL** channels."); - } - else - { - var channel = PermissionHelper.ValidateChannel(e.Server, e.GetArg("channel")); - - PermsHandler.SetChannelCommandPermission(channel, command, state); - await e.Channel.SendMessage($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** for **{channel.Name}** channel."); - } - } - catch (ArgumentException exArg) - { - await e.Channel.SendMessage(exArg.Message); - } - catch (Exception ex) - { - await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); - } - }); - - cgb.CreateCommand(Prefix + "um").Alias(Prefix + "usermodule") - .Parameter("module", ParameterType.Required) - .Parameter("bool", ParameterType.Required) - .Parameter("user", ParameterType.Unparsed) - .Description("Sets a module's permission at the user level.\n**Usage**: ;um [module_name] enable [user_name]") - .Do(async e => - { - try - { - var module = PermissionHelper.ValidateModule(e.GetArg("module")); - var state = PermissionHelper.ValidateBool(e.GetArg("bool")); - var user = PermissionHelper.ValidateUser(e.Server, e.GetArg("user")); - - PermsHandler.SetUserModulePermission(user, module, state); - await e.Channel.SendMessage($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** for user **{user.Name}**."); - } - catch (ArgumentException exArg) - { - await e.Channel.SendMessage(exArg.Message); - } - catch (Exception ex) - { - await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); - } - }); - - cgb.CreateCommand(Prefix + "uc").Alias(Prefix + "usercommand") - .Parameter("command", ParameterType.Required) - .Parameter("bool", ParameterType.Required) - .Parameter("user", ParameterType.Unparsed) - .Description("Sets a command's permission at the user level.\n**Usage**: ;uc [command_name] enable [user_name]") - .Do(async e => - { - try - { - var command = PermissionHelper.ValidateCommand(e.GetArg("command")); - var state = PermissionHelper.ValidateBool(e.GetArg("bool")); - var user = PermissionHelper.ValidateUser(e.Server, e.GetArg("user")); - - PermsHandler.SetUserCommandPermission(user, command, state); - await e.Channel.SendMessage($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** for user **{user.Name}**."); - } - catch (ArgumentException exArg) - { - await e.Channel.SendMessage(exArg.Message); - } - catch (Exception ex) - { - await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); - } - }); - - cgb.CreateCommand(Prefix + "asm").Alias(Prefix + "allservermodules") - .Parameter("bool", ParameterType.Required) - .Description("Sets permissions for all modules at the server level.\n**Usage**: ;asm [enable/disable]") - .Do(async e => - { - try - { - var state = PermissionHelper.ValidateBool(e.GetArg("bool")); - - foreach (var module in NadekoBot.Client.GetService().Modules) - { - PermsHandler.SetServerModulePermission(e.Server, module.Name, state); - } - await e.Channel.SendMessage($"All modules have been **{(state ? "enabled" : "disabled")}** on this server."); - } - catch (ArgumentException exArg) - { - await e.Channel.SendMessage(exArg.Message); - } - catch (Exception ex) - { - await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); - } - }); - - cgb.CreateCommand(Prefix + "asc").Alias(Prefix + "allservercommands") - .Parameter("module", ParameterType.Required) - .Parameter("bool", ParameterType.Required) - .Description("Sets permissions for all commands from a certain module at the server level.\n**Usage**: ;asc [module_name] [enable/disable]") - .Do(async e => - { - try - { - var state = PermissionHelper.ValidateBool(e.GetArg("bool")); - var module = PermissionHelper.ValidateModule(e.GetArg("module")); - - foreach (var command in NadekoBot.Client.GetService().AllCommands.Where(c => c.Category == module)) - { - PermsHandler.SetServerCommandPermission(e.Server, command.Text, state); - } - await e.Channel.SendMessage($"All commands from the **{module}** module have been **{(state ? "enabled" : "disabled")}** on this server."); - } - catch (ArgumentException exArg) - { - await e.Channel.SendMessage(exArg.Message); - } - catch (Exception ex) - { - await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); - } - }); - - cgb.CreateCommand(Prefix + "acm").Alias(Prefix + "allchannelmodules") - .Parameter("bool", ParameterType.Required) - .Parameter("channel", ParameterType.Unparsed) - .Description("Sets permissions for all modules at the channel level.\n**Usage**: ;acm [enable/disable] [channel_name]") - .Do(async e => - { - try - { - var state = PermissionHelper.ValidateBool(e.GetArg("bool")); - var channel = PermissionHelper.ValidateChannel(e.Server, e.GetArg("channel")); - foreach (var module in NadekoBot.Client.GetService().Modules) - { - PermsHandler.SetChannelModulePermission(channel, module.Name, state); - } - - await e.Channel.SendMessage($"All modules have been **{(state ? "enabled" : "disabled")}** for **{channel.Name}** channel."); - } - catch (ArgumentException exArg) - { - await e.Channel.SendMessage(exArg.Message); - } - catch (Exception ex) - { - await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); - } - }); - - cgb.CreateCommand(Prefix + "acc").Alias(Prefix + "allchannelcommands") - .Parameter("module", ParameterType.Required) - .Parameter("bool", ParameterType.Required) - .Parameter("channel", ParameterType.Unparsed) - .Description("Sets permissions for all commands from a certain module at the channel level.\n**Usage**: ;acc [module_name] [enable/disable] [channel_name]") - .Do(async e => - { - try - { - var state = PermissionHelper.ValidateBool(e.GetArg("bool")); - var module = PermissionHelper.ValidateModule(e.GetArg("module")); - var channel = PermissionHelper.ValidateChannel(e.Server, e.GetArg("channel")); - foreach (var command in NadekoBot.Client.GetService().AllCommands.Where(c => c.Category == module)) - { - PermsHandler.SetChannelCommandPermission(channel, command.Text, state); - } - await e.Channel.SendMessage($"All commands from the **{module}** module have been **{(state ? "enabled" : "disabled")}** for **{channel.Name}** channel."); - } - catch (ArgumentException exArg) - { - await e.Channel.SendMessage(exArg.Message); - } - catch (Exception ex) - { - await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); - } - }); - - cgb.CreateCommand(Prefix + "arm").Alias(Prefix + "allrolemodules") - .Parameter("bool", ParameterType.Required) - .Parameter("role", ParameterType.Unparsed) - .Description("Sets permissions for all modules at the role level.\n**Usage**: ;arm [enable/disable] [role_name]") - .Do(async e => - { - try - { - var state = PermissionHelper.ValidateBool(e.GetArg("bool")); - var role = PermissionHelper.ValidateRole(e.Server, e.GetArg("role")); - foreach (var module in NadekoBot.Client.GetService().Modules) - { - PermsHandler.SetRoleModulePermission(role, module.Name, state); - } - - await e.Channel.SendMessage($"All modules have been **{(state ? "enabled" : "disabled")}** for **{role.Name}** role."); - } - catch (ArgumentException exArg) - { - await e.Channel.SendMessage(exArg.Message); - } - catch (Exception ex) - { - await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); - } - }); - - cgb.CreateCommand(Prefix + "arc").Alias(Prefix + "allrolecommands") - .Parameter("module", ParameterType.Required) - .Parameter("bool", ParameterType.Required) - .Parameter("channel", ParameterType.Unparsed) - .Description("Sets permissions for all commands from a certain module at the role level.\n**Usage**: ;arc [module_name] [enable/disable] [role_name]") - .Do(async e => - { - try - { - var state = PermissionHelper.ValidateBool(e.GetArg("bool")); - var module = PermissionHelper.ValidateModule(e.GetArg("module")); - var role = PermissionHelper.ValidateRole(e.Server, e.GetArg("channel")); - foreach (var command in NadekoBot.Client.GetService().AllCommands.Where(c => c.Category == module)) - { - PermsHandler.SetRoleCommandPermission(role, command.Text, state); - } - await e.Channel.SendMessage($"All commands from the **{module}** module have been **{(state ? "enabled" : "disabled")}** for **{role.Name}** role."); - } - catch (ArgumentException exArg) - { - await e.Channel.SendMessage(exArg.Message); - } - catch (Exception ex) - { - await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); - } - }); - - cgb.CreateCommand(Prefix + "ubl") - .Description("Blacklists a mentioned user.\n**Usage**: ;ubl [user_mention]") - .Parameter("user", ParameterType.Unparsed) - .AddCheck(SimpleCheckers.OwnerOnly()) - .Do(async e => - { - await Task.Run(async () => - { - if (!e.Message.MentionedUsers.Any()) return; - var usr = e.Message.MentionedUsers.First(); - NadekoBot.Config.UserBlacklist.Add(usr.Id); - ConfigHandler.SaveConfig(); - await e.Channel.SendMessage($"`Sucessfully blacklisted user {usr.Name}`"); - }); - }); - - cgb.CreateCommand(Prefix + "uubl") - .Description($"Unblacklists a mentioned user.\n**Usage**: {Prefix}uubl [user_mention]") - .Parameter("user", ParameterType.Unparsed) - .AddCheck(SimpleCheckers.OwnerOnly()) - .Do(async e => - { - await Task.Run(async () => - { - if (!e.Message.MentionedUsers.Any()) return; - var usr = e.Message.MentionedUsers.First(); - if (NadekoBot.Config.UserBlacklist.Contains(usr.Id)) - { - NadekoBot.Config.UserBlacklist.Remove(usr.Id); - ConfigHandler.SaveConfig(); - await e.Channel.SendMessage($"`Sucessfully unblacklisted user {usr.Name}`"); - } - else - { - await e.Channel.SendMessage($"`{usr.Name} was not in blacklist`"); - } - }); - }); - - cgb.CreateCommand(Prefix + "cbl") - .Description("Blacklists a mentioned channel (#general for example).\n**Usage**: ;ubl [channel_mention]") - .Parameter("channel", ParameterType.Unparsed) - .Do(async e => - { - await Task.Run(async () => - { - if (!e.Message.MentionedChannels.Any()) return; - var ch = e.Message.MentionedChannels.First(); - NadekoBot.Config.UserBlacklist.Add(ch.Id); - ConfigHandler.SaveConfig(); - await e.Channel.SendMessage($"`Sucessfully blacklisted channel {ch.Name}`"); - }); - }); - - cgb.CreateCommand(Prefix + "cubl") - .Description("Unblacklists a mentioned channel (#general for example).\n**Usage**: ;cubl [channel_mention]") - .Parameter("channel", ParameterType.Unparsed) - .Do(async e => - { - await Task.Run(async () => - { - if (!e.Message.MentionedChannels.Any()) return; - var ch = e.Message.MentionedChannels.First(); - NadekoBot.Config.UserBlacklist.Remove(ch.Id); - ConfigHandler.SaveConfig(); - await e.Channel.SendMessage($"`Sucessfully blacklisted channel {ch.Name}`"); - }); - }); - - cgb.CreateCommand(Prefix + "sbl") - .Description("Blacklists a server by a name or id (#general for example). **BOT OWNER ONLY**\n**Usage**: ;usl [servername/serverid]") - .Parameter("server", ParameterType.Unparsed) - .AddCheck(SimpleCheckers.OwnerOnly()) - .Do(async e => - { - await Task.Run(async () => - { - var arg = e.GetArg("server")?.Trim(); - if (string.IsNullOrWhiteSpace(arg)) - return; - var server = NadekoBot.Client.Servers.FirstOrDefault(s => s.Id.ToString() == arg) ?? - NadekoBot.Client.FindServers(arg.Trim()).FirstOrDefault(); - if (server == null) - { - await e.Channel.SendMessage("Cannot find that server"); - return; - } - var serverId = server.Id; - NadekoBot.Config.ServerBlacklist.Add(serverId); - ConfigHandler.SaveConfig(); - //cleanup trivias and typeracing - Classes.Trivia.TriviaGame trivia; - Trivia.RunningTrivias.TryRemove(serverId, out trivia); - TypingGame typeracer; - SpeedTyping.RunningContests.TryRemove(serverId, out typeracer); - - await e.Channel.SendMessage($"`Sucessfully blacklisted server {server.Name}`"); - }); - }); - }); - } - } -} +using Discord.Commands; +using Discord.Modules; +using NadekoBot.Classes.JSONModels; +using NadekoBot.Classes.Permissions; +using NadekoBot.Extensions; +using NadekoBot.Modules.Games.Commands; +using NadekoBot.Modules.Permissions.Commands; +using System; +using System.Linq; +using System.Threading.Tasks; +using PermsHandler = NadekoBot.Classes.Permissions.PermissionsHandler; + +namespace NadekoBot.Modules.Permissions +{ + internal class PermissionModule : DiscordModule + { + public override string Prefix { get; } = NadekoBot.Config.CommandPrefixes.Permissions; + + public PermissionModule() + { + commands.Add(new FilterInvitesCommand(this)); + commands.Add(new FilterWords(this)); + } + + public override void Install(ModuleManager manager) + { + manager.CreateCommands("", cgb => + { + + cgb.AddCheck(PermissionChecker.Instance); + + 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.Channel.SendMessage($"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.Channel.SendMessage($"Role `{arg}` probably doesn't exist. Create the role with that name first."); + return; + } + PermsHandler.SetPermissionsRole(e.Server, role.Name); + await e.Channel.SendMessage($"Role `{role.Name}` is now required in order to change permissions."); + }); + + cgb.CreateCommand(Prefix + "verbose") + .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"); + var val = PermissionHelper.ValidateBool(arg); + PermsHandler.SetVerbosity(e.Server, val); + await e.Channel.SendMessage($"Verbosity set to {val}."); + }); + + cgb.CreateCommand(Prefix + "serverperms") + .Alias(Prefix + "sp") + .Description("Shows banned permissions for this server.") + .Do(async e => + { + var perms = PermsHandler.GetServerPermissions(e.Server); + if (string.IsNullOrWhiteSpace(perms?.ToString())) + await e.Channel.SendMessage("No permissions set for this server."); + await e.Channel.SendMessage(perms.ToString()); + }); + + cgb.CreateCommand(Prefix + "roleperms") + .Alias(Prefix + "rp") + .Description("Shows banned permissions for a certain role. No argument means for everyone.\n**Usage**: ;rp AwesomeRole") + .Parameter("role", ParameterType.Unparsed) + .Do(async e => + { + var arg = e.GetArg("role"); + var role = e.Server.EveryoneRole; + if (!string.IsNullOrWhiteSpace(arg)) + try + { + role = PermissionHelper.ValidateRole(e.Server, arg); + } + catch (Exception ex) + { + await e.Channel.SendMessage("💢 Error: " + ex.Message); + return; + } + + var perms = PermsHandler.GetRolePermissionsById(e.Server, role.Id); + + if (string.IsNullOrWhiteSpace(perms?.ToString())) + await e.Channel.SendMessage($"No permissions set for **{role.Name}** role."); + await e.Channel.SendMessage(perms.ToString()); + }); + + cgb.CreateCommand(Prefix + "channelperms") + .Alias(Prefix + "cp") + .Description("Shows banned permissions for a certain channel. No argument means for this channel.\n**Usage**: ;cp #dev") + .Parameter("channel", ParameterType.Unparsed) + .Do(async e => + { + var arg = e.GetArg("channel"); + var channel = e.Channel; + if (!string.IsNullOrWhiteSpace(arg)) + try + { + channel = PermissionHelper.ValidateChannel(e.Server, arg); + } + catch (Exception ex) + { + await e.Channel.SendMessage("💢 Error: " + ex.Message); + return; + } + + var perms = PermsHandler.GetChannelPermissionsById(e.Server, channel.Id); + if (string.IsNullOrWhiteSpace(perms?.ToString())) + await e.Channel.SendMessage($"No permissions set for **{channel.Name}** channel."); + await e.Channel.SendMessage(perms.ToString()); + }); + + cgb.CreateCommand(Prefix + "userperms") + .Alias(Prefix + "up") + .Description("Shows banned permissions for a certain user. No argument means for yourself.\n**Usage**: ;up Kwoth") + .Parameter("user", ParameterType.Unparsed) + .Do(async e => + { + var user = e.User; + if (!string.IsNullOrWhiteSpace(e.GetArg("user"))) + try + { + user = PermissionHelper.ValidateUser(e.Server, e.GetArg("user")); + } + catch (Exception ex) + { + await e.Channel.SendMessage("💢 Error: " + ex.Message); + return; + } + + var perms = PermsHandler.GetUserPermissionsById(e.Server, user.Id); + if (string.IsNullOrWhiteSpace(perms?.ToString())) + await e.Channel.SendMessage($"No permissions set for user **{user.Name}**."); + await e.Channel.SendMessage(perms.ToString()); + }); + + cgb.CreateCommand(Prefix + "sm").Alias(Prefix + "servermodule") + .Parameter("module", ParameterType.Required) + .Parameter("bool", ParameterType.Required) + .Description("Sets a module's permission at the server level.\n**Usage**: ;sm [module_name] enable") + .Do(async e => + { + try + { + var module = PermissionHelper.ValidateModule(e.GetArg("module")); + var state = PermissionHelper.ValidateBool(e.GetArg("bool")); + + PermsHandler.SetServerModulePermission(e.Server, module, state); + await e.Channel.SendMessage($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** on this server."); + } + catch (ArgumentException exArg) + { + await e.Channel.SendMessage(exArg.Message); + } + catch (Exception ex) + { + await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(Prefix + "sc").Alias(Prefix + "servercommand") + .Parameter("command", ParameterType.Required) + .Parameter("bool", ParameterType.Required) + .Description("Sets a command's permission at the server level.\n**Usage**: ;sc [command_name] disable") + .Do(async e => + { + try + { + var command = PermissionHelper.ValidateCommand(e.GetArg("command")); + var state = PermissionHelper.ValidateBool(e.GetArg("bool")); + + PermsHandler.SetServerCommandPermission(e.Server, command, state); + await e.Channel.SendMessage($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** on this server."); + } + catch (ArgumentException exArg) + { + await e.Channel.SendMessage(exArg.Message); + } + catch (Exception ex) + { + await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(Prefix + "rm").Alias(Prefix + "rolemodule") + .Parameter("module", ParameterType.Required) + .Parameter("bool", ParameterType.Required) + .Parameter("role", ParameterType.Unparsed) + .Description("Sets a module's permission at the role level.\n**Usage**: ;rm [module_name] enable [role_name]") + .Do(async e => + { + try + { + var module = PermissionHelper.ValidateModule(e.GetArg("module")); + var state = PermissionHelper.ValidateBool(e.GetArg("bool")); + + if (e.GetArg("role")?.ToLower() == "all") + { + foreach (var role in e.Server.Roles) + { + PermsHandler.SetRoleModulePermission(role, module, state); + } + await e.Channel.SendMessage($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** for **ALL** roles."); + } + else + { + var role = PermissionHelper.ValidateRole(e.Server, e.GetArg("role")); + + PermsHandler.SetRoleModulePermission(role, module, state); + await e.Channel.SendMessage($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** for **{role.Name}** role."); + } + } + catch (ArgumentException exArg) + { + await e.Channel.SendMessage(exArg.Message); + } + catch (Exception ex) + { + await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(Prefix + "rc").Alias(Prefix + "rolecommand") + .Parameter("command", ParameterType.Required) + .Parameter("bool", ParameterType.Required) + .Parameter("role", ParameterType.Unparsed) + .Description("Sets a command's permission at the role level.\n**Usage**: ;rc [command_name] disable [role_name]") + .Do(async e => + { + try + { + var command = PermissionHelper.ValidateCommand(e.GetArg("command")); + var state = PermissionHelper.ValidateBool(e.GetArg("bool")); + + if (e.GetArg("role")?.ToLower() == "all") + { + foreach (var role in e.Server.Roles) + { + PermsHandler.SetRoleCommandPermission(role, command, state); + } + await e.Channel.SendMessage($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** for **ALL** roles."); + } + else + { + var role = PermissionHelper.ValidateRole(e.Server, e.GetArg("role")); + + PermsHandler.SetRoleCommandPermission(role, command, state); + await e.Channel.SendMessage($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** for **{role.Name}** role."); + } + } + catch (ArgumentException exArg) + { + await e.Channel.SendMessage(exArg.Message); + } + catch (Exception ex) + { + await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(Prefix + "cm").Alias(Prefix + "channelmodule") + .Parameter("module", ParameterType.Required) + .Parameter("bool", ParameterType.Required) + .Parameter("channel", ParameterType.Unparsed) + .Description("Sets a module's permission at the channel level.\n**Usage**: ;cm [module_name] enable [channel_name]") + .Do(async e => + { + try + { + var module = PermissionHelper.ValidateModule(e.GetArg("module")); + var state = PermissionHelper.ValidateBool(e.GetArg("bool")); + + if (e.GetArg("channel")?.ToLower() == "all") + { + foreach (var channel in e.Server.TextChannels) + { + PermsHandler.SetChannelModulePermission(channel, module, state); + } + await e.Channel.SendMessage($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** on **ALL** channels."); + } + else + { + var channel = PermissionHelper.ValidateChannel(e.Server, e.GetArg("channel")); + + PermsHandler.SetChannelModulePermission(channel, module, state); + await e.Channel.SendMessage($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** for **{channel.Name}** channel."); + } + } + catch (ArgumentException exArg) + { + await e.Channel.SendMessage(exArg.Message); + } + catch (Exception ex) + { + await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(Prefix + "cc").Alias(Prefix + "channelcommand") + .Parameter("command", ParameterType.Required) + .Parameter("bool", ParameterType.Required) + .Parameter("channel", ParameterType.Unparsed) + .Description("Sets a command's permission at the channel level.\n**Usage**: ;cc [command_name] enable [channel_name]") + .Do(async e => + { + try + { + var command = PermissionHelper.ValidateCommand(e.GetArg("command")); + var state = PermissionHelper.ValidateBool(e.GetArg("bool")); + + if (e.GetArg("channel")?.ToLower() == "all") + { + foreach (var channel in e.Server.TextChannels) + { + PermsHandler.SetChannelCommandPermission(channel, command, state); + } + await e.Channel.SendMessage($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** on **ALL** channels."); + } + else + { + var channel = PermissionHelper.ValidateChannel(e.Server, e.GetArg("channel")); + + PermsHandler.SetChannelCommandPermission(channel, command, state); + await e.Channel.SendMessage($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** for **{channel.Name}** channel."); + } + } + catch (ArgumentException exArg) + { + await e.Channel.SendMessage(exArg.Message); + } + catch (Exception ex) + { + await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(Prefix + "um").Alias(Prefix + "usermodule") + .Parameter("module", ParameterType.Required) + .Parameter("bool", ParameterType.Required) + .Parameter("user", ParameterType.Unparsed) + .Description("Sets a module's permission at the user level.\n**Usage**: ;um [module_name] enable [user_name]") + .Do(async e => + { + try + { + var module = PermissionHelper.ValidateModule(e.GetArg("module")); + var state = PermissionHelper.ValidateBool(e.GetArg("bool")); + var user = PermissionHelper.ValidateUser(e.Server, e.GetArg("user")); + + PermsHandler.SetUserModulePermission(user, module, state); + await e.Channel.SendMessage($"Module **{module}** has been **{(state ? "enabled" : "disabled")}** for user **{user.Name}**."); + } + catch (ArgumentException exArg) + { + await e.Channel.SendMessage(exArg.Message); + } + catch (Exception ex) + { + await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(Prefix + "uc").Alias(Prefix + "usercommand") + .Parameter("command", ParameterType.Required) + .Parameter("bool", ParameterType.Required) + .Parameter("user", ParameterType.Unparsed) + .Description("Sets a command's permission at the user level.\n**Usage**: ;uc [command_name] enable [user_name]") + .Do(async e => + { + try + { + var command = PermissionHelper.ValidateCommand(e.GetArg("command")); + var state = PermissionHelper.ValidateBool(e.GetArg("bool")); + var user = PermissionHelper.ValidateUser(e.Server, e.GetArg("user")); + + PermsHandler.SetUserCommandPermission(user, command, state); + await e.Channel.SendMessage($"Command **{command}** has been **{(state ? "enabled" : "disabled")}** for user **{user.Name}**."); + } + catch (ArgumentException exArg) + { + await e.Channel.SendMessage(exArg.Message); + } + catch (Exception ex) + { + await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(Prefix + "asm").Alias(Prefix + "allservermodules") + .Parameter("bool", ParameterType.Required) + .Description("Sets permissions for all modules at the server level.\n**Usage**: ;asm [enable/disable]") + .Do(async e => + { + try + { + var state = PermissionHelper.ValidateBool(e.GetArg("bool")); + + foreach (var module in NadekoBot.Client.GetService().Modules) + { + PermsHandler.SetServerModulePermission(e.Server, module.Name, state); + } + await e.Channel.SendMessage($"All modules have been **{(state ? "enabled" : "disabled")}** on this server."); + } + catch (ArgumentException exArg) + { + await e.Channel.SendMessage(exArg.Message); + } + catch (Exception ex) + { + await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(Prefix + "asc").Alias(Prefix + "allservercommands") + .Parameter("module", ParameterType.Required) + .Parameter("bool", ParameterType.Required) + .Description("Sets permissions for all commands from a certain module at the server level.\n**Usage**: ;asc [module_name] [enable/disable]") + .Do(async e => + { + try + { + var state = PermissionHelper.ValidateBool(e.GetArg("bool")); + var module = PermissionHelper.ValidateModule(e.GetArg("module")); + + foreach (var command in NadekoBot.Client.GetService().AllCommands.Where(c => c.Category == module)) + { + PermsHandler.SetServerCommandPermission(e.Server, command.Text, state); + } + await e.Channel.SendMessage($"All commands from the **{module}** module have been **{(state ? "enabled" : "disabled")}** on this server."); + } + catch (ArgumentException exArg) + { + await e.Channel.SendMessage(exArg.Message); + } + catch (Exception ex) + { + await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(Prefix + "acm").Alias(Prefix + "allchannelmodules") + .Parameter("bool", ParameterType.Required) + .Parameter("channel", ParameterType.Unparsed) + .Description("Sets permissions for all modules at the channel level.\n**Usage**: ;acm [enable/disable] [channel_name]") + .Do(async e => + { + try + { + var state = PermissionHelper.ValidateBool(e.GetArg("bool")); + var channel = PermissionHelper.ValidateChannel(e.Server, e.GetArg("channel")); + foreach (var module in NadekoBot.Client.GetService().Modules) + { + PermsHandler.SetChannelModulePermission(channel, module.Name, state); + } + + await e.Channel.SendMessage($"All modules have been **{(state ? "enabled" : "disabled")}** for **{channel.Name}** channel."); + } + catch (ArgumentException exArg) + { + await e.Channel.SendMessage(exArg.Message); + } + catch (Exception ex) + { + await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(Prefix + "acc").Alias(Prefix + "allchannelcommands") + .Parameter("module", ParameterType.Required) + .Parameter("bool", ParameterType.Required) + .Parameter("channel", ParameterType.Unparsed) + .Description("Sets permissions for all commands from a certain module at the channel level.\n**Usage**: ;acc [module_name] [enable/disable] [channel_name]") + .Do(async e => + { + try + { + var state = PermissionHelper.ValidateBool(e.GetArg("bool")); + var module = PermissionHelper.ValidateModule(e.GetArg("module")); + var channel = PermissionHelper.ValidateChannel(e.Server, e.GetArg("channel")); + foreach (var command in NadekoBot.Client.GetService().AllCommands.Where(c => c.Category == module)) + { + PermsHandler.SetChannelCommandPermission(channel, command.Text, state); + } + await e.Channel.SendMessage($"All commands from the **{module}** module have been **{(state ? "enabled" : "disabled")}** for **{channel.Name}** channel."); + } + catch (ArgumentException exArg) + { + await e.Channel.SendMessage(exArg.Message); + } + catch (Exception ex) + { + await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(Prefix + "arm").Alias(Prefix + "allrolemodules") + .Parameter("bool", ParameterType.Required) + .Parameter("role", ParameterType.Unparsed) + .Description("Sets permissions for all modules at the role level.\n**Usage**: ;arm [enable/disable] [role_name]") + .Do(async e => + { + try + { + var state = PermissionHelper.ValidateBool(e.GetArg("bool")); + var role = PermissionHelper.ValidateRole(e.Server, e.GetArg("role")); + foreach (var module in NadekoBot.Client.GetService().Modules) + { + PermsHandler.SetRoleModulePermission(role, module.Name, state); + } + + await e.Channel.SendMessage($"All modules have been **{(state ? "enabled" : "disabled")}** for **{role.Name}** role."); + } + catch (ArgumentException exArg) + { + await e.Channel.SendMessage(exArg.Message); + } + catch (Exception ex) + { + await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(Prefix + "arc").Alias(Prefix + "allrolecommands") + .Parameter("module", ParameterType.Required) + .Parameter("bool", ParameterType.Required) + .Parameter("channel", ParameterType.Unparsed) + .Description("Sets permissions for all commands from a certain module at the role level.\n**Usage**: ;arc [module_name] [enable/disable] [role_name]") + .Do(async e => + { + try + { + var state = PermissionHelper.ValidateBool(e.GetArg("bool")); + var module = PermissionHelper.ValidateModule(e.GetArg("module")); + var role = PermissionHelper.ValidateRole(e.Server, e.GetArg("channel")); + foreach (var command in NadekoBot.Client.GetService().AllCommands.Where(c => c.Category == module)) + { + PermsHandler.SetRoleCommandPermission(role, command.Text, state); + } + await e.Channel.SendMessage($"All commands from the **{module}** module have been **{(state ? "enabled" : "disabled")}** for **{role.Name}** role."); + } + catch (ArgumentException exArg) + { + await e.Channel.SendMessage(exArg.Message); + } + catch (Exception ex) + { + await e.Channel.SendMessage("Something went terribly wrong - " + ex.Message); + } + }); + + cgb.CreateCommand(Prefix + "ubl") + .Description("Blacklists a mentioned user.\n**Usage**: ;ubl [user_mention]") + .Parameter("user", ParameterType.Unparsed) + .AddCheck(SimpleCheckers.OwnerOnly()) + .Do(async e => + { + await Task.Run(async () => + { + if (!e.Message.MentionedUsers.Any()) return; + var usr = e.Message.MentionedUsers.First(); + NadekoBot.Config.UserBlacklist.Add(usr.Id); + ConfigHandler.SaveConfig(); + await e.Channel.SendMessage($"`Sucessfully blacklisted user {usr.Name}`"); + }); + }); + + cgb.CreateCommand(Prefix + "uubl") + .Description($"Unblacklists a mentioned user.\n**Usage**: {Prefix}uubl [user_mention]") + .Parameter("user", ParameterType.Unparsed) + .AddCheck(SimpleCheckers.OwnerOnly()) + .Do(async e => + { + await Task.Run(async () => + { + if (!e.Message.MentionedUsers.Any()) return; + var usr = e.Message.MentionedUsers.First(); + if (NadekoBot.Config.UserBlacklist.Contains(usr.Id)) + { + NadekoBot.Config.UserBlacklist.Remove(usr.Id); + ConfigHandler.SaveConfig(); + await e.Channel.SendMessage($"`Sucessfully unblacklisted user {usr.Name}`"); + } + else + { + await e.Channel.SendMessage($"`{usr.Name} was not in blacklist`"); + } + }); + }); + + cgb.CreateCommand(Prefix + "cbl") + .Description("Blacklists a mentioned channel (#general for example).\n**Usage**: ;ubl [channel_mention]") + .Parameter("channel", ParameterType.Unparsed) + .Do(async e => + { + await Task.Run(async () => + { + if (!e.Message.MentionedChannels.Any()) return; + var ch = e.Message.MentionedChannels.First(); + NadekoBot.Config.UserBlacklist.Add(ch.Id); + ConfigHandler.SaveConfig(); + await e.Channel.SendMessage($"`Sucessfully blacklisted channel {ch.Name}`"); + }); + }); + + cgb.CreateCommand(Prefix + "cubl") + .Description("Unblacklists a mentioned channel (#general for example).\n**Usage**: ;cubl [channel_mention]") + .Parameter("channel", ParameterType.Unparsed) + .Do(async e => + { + await Task.Run(async () => + { + if (!e.Message.MentionedChannels.Any()) return; + var ch = e.Message.MentionedChannels.First(); + NadekoBot.Config.UserBlacklist.Remove(ch.Id); + ConfigHandler.SaveConfig(); + await e.Channel.SendMessage($"`Sucessfully blacklisted channel {ch.Name}`"); + }); + }); + + cgb.CreateCommand(Prefix + "sbl") + .Description("Blacklists a server by a name or id (#general for example). **BOT OWNER ONLY**\n**Usage**: ;usl [servername/serverid]") + .Parameter("server", ParameterType.Unparsed) + .AddCheck(SimpleCheckers.OwnerOnly()) + .Do(async e => + { + await Task.Run(async () => + { + var arg = e.GetArg("server")?.Trim(); + if (string.IsNullOrWhiteSpace(arg)) + return; + var server = NadekoBot.Client.Servers.FirstOrDefault(s => s.Id.ToString() == arg) ?? + NadekoBot.Client.FindServers(arg.Trim()).FirstOrDefault(); + if (server == null) + { + await e.Channel.SendMessage("Cannot find that server"); + return; + } + var serverId = server.Id; + NadekoBot.Config.ServerBlacklist.Add(serverId); + ConfigHandler.SaveConfig(); + //cleanup trivias and typeracing + Classes.Trivia.TriviaGame trivia; + Trivia.RunningTrivias.TryRemove(serverId, out trivia); + TypingGame typeracer; + SpeedTyping.RunningContests.TryRemove(serverId, out typeracer); + + await e.Channel.SendMessage($"`Sucessfully blacklisted server {server.Name}`"); + }); + }); + }); + } + } +} diff --git a/NadekoBot/NadekoBot.cs b/NadekoBot/NadekoBot.cs index adacf289..3b77114a 100644 --- a/NadekoBot/NadekoBot.cs +++ b/NadekoBot/NadekoBot.cs @@ -4,11 +4,15 @@ using Discord.Commands; using Discord.Modules; using NadekoBot.Classes.JSONModels; using NadekoBot.Commands; +using NadekoBot.Commands.Help.Commands; using NadekoBot.Modules; using NadekoBot.Modules.Administration; +using NadekoBot.Modules.Conversations; using NadekoBot.Modules.Gambling; using NadekoBot.Modules.Games; using NadekoBot.Modules.Games.Commands; +using NadekoBot.Modules.Help; +using NadekoBot.Modules.Permissions; using NadekoBot.Modules.Pokemon; using NadekoBot.Modules.Searches; using NadekoBot.Modules.Translator; diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index c1b36bd3..85904a90 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -160,48 +160,48 @@ - - - + + + - - + + - + - + - + - + - + - + - + - + - +