From 2d72f6f4980b7c2bf7e914d02f5e2d1a7233b302 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Mon, 2 Dec 2024 02:20:33 +0000 Subject: [PATCH] add: Added .dmmod and .dmcmd to disable modules and commands in bot DMs --- .../GlobalPermissionCommands.cs | 33 +++++++++++ .../GlobalPermissionService.cs | 55 +++++++++++++++++-- src/NadekoBot/_common/Configs/BotConfig.cs | 5 +- .../_common/Settings/BotConfigService.cs | 4 +- src/NadekoBot/data/aliases.yml | 6 ++ src/NadekoBot/data/bot.yml | 6 +- .../strings/responses/responses.en-US.json | 4 ++ 7 files changed, 103 insertions(+), 10 deletions(-) diff --git a/src/NadekoBot/Modules/Permissions/GlobalPermissions/GlobalPermissionCommands.cs b/src/NadekoBot/Modules/Permissions/GlobalPermissions/GlobalPermissionCommands.cs index fd7691535..3e7a81e61 100644 --- a/src/NadekoBot/Modules/Permissions/GlobalPermissions/GlobalPermissionCommands.cs +++ b/src/NadekoBot/Modules/Permissions/GlobalPermissions/GlobalPermissionCommands.cs @@ -73,5 +73,38 @@ public partial class Permissions await Response().Confirm(strs.gcmd_remove(Format.Bold(cmd.Name))).SendAsync(); } + + [Cmd] + [OwnerOnly] + public async Task DmModule(ModuleOrExpr module) + { + var moduleName = module.Name.ToLowerInvariant(); + + var added = _service.ToggleModule(moduleName, true); + + if (added) + { + await Response().Confirm(strs.dmmod_add(Format.Bold(module.Name))).SendAsync(); + return; + } + + await Response().Confirm(strs.dmmod_remove(Format.Bold(module.Name))).SendAsync(); + } + + [Cmd] + [OwnerOnly] + public async Task DmCommand(CommandOrExprInfo cmd) + { + var commandName = cmd.Name.ToLowerInvariant(); + var added = _service.ToggleCommand(commandName, true); + + if (added) + { + await Response().Confirm(strs.dmcmd_add(Format.Bold(cmd.Name))).SendAsync(); + return; + } + + await Response().Confirm(strs.dmcmd_remove(Format.Bold(cmd.Name))).SendAsync(); + } } } \ No newline at end of file diff --git a/src/NadekoBot/Modules/Permissions/GlobalPermissions/GlobalPermissionService.cs b/src/NadekoBot/Modules/Permissions/GlobalPermissions/GlobalPermissionService.cs index d5313bac2..43cafc176 100644 --- a/src/NadekoBot/Modules/Permissions/GlobalPermissions/GlobalPermissionService.cs +++ b/src/NadekoBot/Modules/Permissions/GlobalPermissions/GlobalPermissionService.cs @@ -24,10 +24,19 @@ public class GlobalPermissionService : IExecPreCommand, INService var settings = _bss.Data; var commandName = command.Name.ToLowerInvariant(); - if (commandName != "resetglobalperms" - && (settings.Blocked.Commands.Contains(commandName) - || settings.Blocked.Modules.Contains(moduleName.ToLowerInvariant()))) - return Task.FromResult(true); + if (commandName != "resetglobalperms") + { + if (settings.Blocked.Commands.Contains(commandName) + || settings.Blocked.Modules.Contains(moduleName.ToLowerInvariant())) + return Task.FromResult(true); + + if (ctx.Guild is null) + { + if (settings.DmBlocked.Commands.Contains(commandName) + || settings.DmBlocked.Modules.Contains(moduleName.ToLowerInvariant())) + return Task.FromResult(true); + } + } return Task.FromResult(false); } @@ -37,13 +46,30 @@ public class GlobalPermissionService : IExecPreCommand, INService /// /// Lowercase module name /// Whether the module is added - public bool ToggleModule(string moduleName) + public bool ToggleModule(string moduleName, bool priv = false) { var added = false; _bss.ModifyConfig(bs => { + if (priv) + { + if (bs.DmBlocked.Modules.Add(moduleName)) + { + added = true; + } + else + { + bs.DmBlocked.Modules.Remove(moduleName); + added = false; + } + + return; + } + if (bs.Blocked.Modules.Add(moduleName)) + { added = true; + } else { bs.Blocked.Modules.Remove(moduleName); @@ -59,13 +85,30 @@ public class GlobalPermissionService : IExecPreCommand, INService /// /// Lowercase command name /// Whether the command is added - public bool ToggleCommand(string commandName) + public bool ToggleCommand(string commandName, bool priv = false) { var added = false; _bss.ModifyConfig(bs => { + if (priv) + { + if (bs.Blocked.Commands.Add(commandName)) + { + added = true; + } + else + { + bs.Blocked.Commands.Remove(commandName); + added = false; + } + + return; + } + if (bs.Blocked.Commands.Add(commandName)) + { added = true; + } else { bs.Blocked.Commands.Remove(commandName); diff --git a/src/NadekoBot/_common/Configs/BotConfig.cs b/src/NadekoBot/_common/Configs/BotConfig.cs index ce747548d..1d435cdaf 100644 --- a/src/NadekoBot/_common/Configs/BotConfig.cs +++ b/src/NadekoBot/_common/Configs/BotConfig.cs @@ -13,7 +13,7 @@ namespace NadekoBot.Common.Configs; public sealed partial class BotConfig : ICloneable { [Comment("""DO NOT CHANGE""")] - public int Version { get; set; } = 8; + public int Version { get; set; } = 9; [Comment(""" Most commands, when executed, have a small colored line @@ -82,6 +82,9 @@ public sealed partial class BotConfig : ICloneable [Comment("""List of modules and commands completely blocked on the bot""")] public BlockedConfig Blocked { get; set; } + [Comment("""List of modules and commands blocked from usage in DMs on the bot""")] + public BlockedConfig DmBlocked { get; set; } = new(); + [Comment("""Which string will be used to recognize the commands""")] public string Prefix { get; set; } diff --git a/src/NadekoBot/_common/Settings/BotConfigService.cs b/src/NadekoBot/_common/Settings/BotConfigService.cs index bdaae1510..2d1af7c67 100644 --- a/src/NadekoBot/_common/Settings/BotConfigService.cs +++ b/src/NadekoBot/_common/Settings/BotConfigService.cs @@ -70,10 +70,10 @@ public sealed class BotConfigService : ConfigServiceBase c.IgnoreOtherBots = true; }); - if(data.Version < 8) + if(data.Version < 9) ModifyConfig(c => { - c.Version = 8; + c.Version = 9; }); } } \ No newline at end of file diff --git a/src/NadekoBot/data/aliases.yml b/src/NadekoBot/data/aliases.yml index 5c97900a8..7bb9f6f2b 100644 --- a/src/NadekoBot/data/aliases.yml +++ b/src/NadekoBot/data/aliases.yml @@ -1039,6 +1039,12 @@ gamevoicechannel: - gvc shoplistadd: - shoplistadd +dmcommand: + - dmcommand + - dmcmd +dmmodule: + - dmmodule + - dmmod globalcommand: - globalcommand - gcmd diff --git a/src/NadekoBot/data/bot.yml b/src/NadekoBot/data/bot.yml index bd4e5303b..cf96d2d62 100644 --- a/src/NadekoBot/data/bot.yml +++ b/src/NadekoBot/data/bot.yml @@ -1,5 +1,5 @@ # DO NOT CHANGE -version: 8 +version: 9 # Most commands, when executed, have a small colored line # next to the response. The color depends whether the command # is completed, errored or in progress (pending) @@ -78,6 +78,10 @@ helpText: |- blocked: commands: [] modules: [] +# List of modules and commands blocked from usage in DMs on the bot +dmBlocked: + commands: [] + modules: [] # Which string will be used to recognize the commands prefix: . # Whether the bot will rotate through all specified statuses. diff --git a/src/NadekoBot/data/strings/responses/responses.en-US.json b/src/NadekoBot/data/strings/responses/responses.en-US.json index ffcafa129..c5c2e7cb3 100644 --- a/src/NadekoBot/data/strings/responses/responses.en-US.json +++ b/src/NadekoBot/data/strings/responses/responses.en-US.json @@ -758,6 +758,10 @@ "gcmd_remove": "Command {0} has been enabled on all servers.", "gmod_add": "Module {0} has been disabled on all servers.", "gmod_remove": "Module {0} has been enabled on all servers.", + "dmmod_add": "Module {0} has been disabled in bot DMs.", + "dmmod_remove": "Module {0} has been enabled in bot DMs.", + "dmcmd_add": "Command {0} has been disabled in bot DMs.", + "dmcmd_remove": "Command {0} has been enabled in bot DMs.", "lgp_none": "No blocked commands or modules.", "cant_read_or_send": "You can't read from or send messages to that channel.", "quotes_notfound": "No quotes found matching the quote ID specified.",