From 1521df23fdbd36ed38c9e387b378e29f401bec0a Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Thu, 13 Aug 2020 20:43:00 +0200 Subject: [PATCH 1/3] Fix indenting and add comment for future --- ChaosBot/Services/RestrictedConfig.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ChaosBot/Services/RestrictedConfig.cs b/ChaosBot/Services/RestrictedConfig.cs index 8c19c8b..d3a2c91 100644 --- a/ChaosBot/Services/RestrictedConfig.cs +++ b/ChaosBot/Services/RestrictedConfig.cs @@ -16,12 +16,13 @@ namespace ChaosBot.Services public static async Task IsAllowed(string key) { - List restrictedCfg = new List {"Database:Host", "Database:Port", "Database:Name", "Database:User", "Database:Pass", "Bot:Version", "NLog", "WebServer", "Discord:Token"}; + // TODO: List populated from DB + List restrictedCfg = new List {"Database:Host", "Database:Port", "Database:Name", "Database:User", "Database:Pass", "Bot:Version", "NLog", "WebServer", "Discord:Token"}; - if (restrictedCfg.Contains(key)) - return false; + if (restrictedCfg.Contains(key)) + return false; - return true; + return true; } } } \ No newline at end of file From ff854df99de9161b905a7d033b17468d7eb73dca Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Thu, 13 Aug 2020 21:11:06 +0200 Subject: [PATCH 2/3] Enable the possibility to disable a module --- .../PreConditions/CheckModuleEnabled.cs | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 ChaosBot/Discord/PreConditions/CheckModuleEnabled.cs diff --git a/ChaosBot/Discord/PreConditions/CheckModuleEnabled.cs b/ChaosBot/Discord/PreConditions/CheckModuleEnabled.cs new file mode 100644 index 0000000..4eae395 --- /dev/null +++ b/ChaosBot/Discord/PreConditions/CheckModuleEnabled.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using ChaosBot.Models; +using ChaosBot.Repositories; +using Discord.Commands; +using Discord.WebSocket; +using NLog; + +namespace ChaosBot.Discord.PreConditions +{ + public class CheckModuleEnabled : PreconditionAttribute + { + private static ILogger _logger = Program.Logger; + private readonly string _moduleName; + public CheckModuleEnabled(string moduleName) => _moduleName = moduleName; + + public override Task CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) + { + // Debug information + _logger.Trace($"CheckModuleEnabled.CheckPermissionsAsync|Checking module enabled for module: {_moduleName}"); + + if (context.Guild == null) return Task.FromResult(PreconditionResult.FromError("This must be run in a guild.")); + + // Check if module enabled in database + bool isModuleEnabled = GetResult(context, _moduleName); + + if (isModuleEnabled) + return Task.FromResult(PreconditionResult.FromSuccess()); + + // Permission denied + return Task.FromResult(PreconditionResult.FromError($"You do not have access to this command.")); + } + + public static bool GetResult(ICommandContext context, string moduleName) + { + if (context.Guild == null) throw new Exception("This must be run in a guild"); + + // Check if module enabled in database + return Convert.ToBoolean(ConfigurationRepository.GetValue($"Module:{moduleName}:Enabled", + context.Guild.Id, "true")); + } + } +} \ No newline at end of file From d71f4ac3c60181aa75e0e7991de0be4dcde3d1fe Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Thu, 13 Aug 2020 21:11:15 +0200 Subject: [PATCH 3/3] Implement disabling the exp module --- ChaosBot/Discord/Modules/User/Level.cs | 1 + ChaosBot/Discord/Services/ExperienceHandler.cs | 3 +++ 2 files changed, 4 insertions(+) diff --git a/ChaosBot/Discord/Modules/User/Level.cs b/ChaosBot/Discord/Modules/User/Level.cs index 76c7a02..e29688c 100644 --- a/ChaosBot/Discord/Modules/User/Level.cs +++ b/ChaosBot/Discord/Modules/User/Level.cs @@ -20,6 +20,7 @@ namespace ChaosBot.Discord.Modules.User [Command("level")] [Alias("xp", "exp", "experience", "lvl")] [CheckCommandPerm("User")] + [CheckModuleEnabled("Experience")] public async Task XpShowInfo() { try diff --git a/ChaosBot/Discord/Services/ExperienceHandler.cs b/ChaosBot/Discord/Services/ExperienceHandler.cs index 4e50d81..8e9b890 100644 --- a/ChaosBot/Discord/Services/ExperienceHandler.cs +++ b/ChaosBot/Discord/Services/ExperienceHandler.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using System.Reflection; +using ChaosBot.Discord.PreConditions; using ChaosBot.Models; using ChaosBot.Repositories; using Discord; @@ -19,6 +20,8 @@ namespace ChaosBot.Discord.Services { try { + if (!CheckModuleEnabled.GetResult(context, "Experience")) return; + using (ChaosbotContext dbContext = new ChaosbotContext()) { IQueryable ctxUser = dbContext.ExperiencePoints;