From 61ef1e0ed915c335706fcd5835387b6a7ff16967 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Thu, 13 Aug 2020 22:37:45 +0200 Subject: [PATCH 1/2] Add delete configuration flag --- ChaosBot/Repositories/ConfigurationRepository.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ChaosBot/Repositories/ConfigurationRepository.cs b/ChaosBot/Repositories/ConfigurationRepository.cs index ace0be9..e85c9de 100644 --- a/ChaosBot/Repositories/ConfigurationRepository.cs +++ b/ChaosBot/Repositories/ConfigurationRepository.cs @@ -25,6 +25,18 @@ namespace ChaosBot.Repositories } } + public static void DeleteValue(string key, ulong guildId) + { + using (ChaosbotContext dbContext = new ChaosbotContext()) + { + Configuration config = dbContext.Configuration + .SingleOrDefault(c => c.DiscordGuildId == guildId && c.Key == key); + if (config == null) return; + dbContext.Remove(config); + dbContext.SaveChanges(); + } + } + private static T GetValueFromAppSettings(string key, ulong guildId, T defaultValue) { return Program.AppSettingsHandler.GetValue($"Servers:{guildId}:{key}", Program.AppSettingsHandler.GetValue(key, defaultValue)); From e8fd7a47a4f99e7cceecea14e1a7979a62615899 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Thu, 13 Aug 2020 22:38:06 +0200 Subject: [PATCH 2/2] Register reset command --- ChaosBot/Discord/Modules/Admin/Config.cs | 44 ++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/ChaosBot/Discord/Modules/Admin/Config.cs b/ChaosBot/Discord/Modules/Admin/Config.cs index d95fd4a..bdd4dc6 100644 --- a/ChaosBot/Discord/Modules/Admin/Config.cs +++ b/ChaosBot/Discord/Modules/Admin/Config.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using Discord.Commands; using System.Threading.Tasks; using System.Reflection; @@ -30,6 +31,8 @@ namespace ChaosBot.Discord.Modules.Admin await ConfigSet(key, value); else if (cmd == "get") await ConfigGet(key); + else if (cmd == "reset" || cmd == "unset") + await ConfigReset(key); else await ReplyAsync($"{Context.User.Mention}, The Sub-Command of Config {cmd} does not exist."); } @@ -54,6 +57,8 @@ namespace ChaosBot.Discord.Modules.Admin sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}config set "); sb.AppendLine("To get a configuration value:"); sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}config get "); + sb.AppendLine("To reset a configuration value to default:"); + sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}config reset "); sb.AppendLine(); sb.AppendLine("To view this help:"); sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}config help"); @@ -151,5 +156,44 @@ namespace ChaosBot.Discord.Modules.Admin $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); } } + + public async Task ConfigReset(string key) + { + try + { + if ((key != null) && (await RestrictedConfig.IsAllowed(key))) + { + StringBuilder sb = new StringBuilder(); + EmbedBuilder embed = new EmbedBuilder(); + + ConfigurationRepository.DeleteValue(key, Context.Guild.Id); + + embed.WithColor(new Color(255, 255, 0)); + embed.Title = $"Configuration Reset"; + sb.AppendLine(); + sb.AppendLine($" Flag: {key}"); + + /* + * Add the string to the Embed + */ + embed.Description = sb.ToString(); + + /* + * Reply with the Embed created above + */ + await ReplyAsync(null, false, embed.Build()); + } + else + { + await ConfigHelp(); + } + + } + catch (Exception ex) + { + _logger.Error( + $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } } } \ No newline at end of file