From dd00a6edeb6c386c9ad0335c0fbf97c28119b62c Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Fri, 16 Oct 2020 18:57:50 +0200 Subject: [PATCH] Fix config get command --- ChaosBot/ConfigHelpers/Configuration.cs | 24 +++++++++++++++++++----- ChaosBot/Discord/Modules/Admin/Config.cs | 6 +++++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/ChaosBot/ConfigHelpers/Configuration.cs b/ChaosBot/ConfigHelpers/Configuration.cs index bd298af..f1f7caa 100644 --- a/ChaosBot/ConfigHelpers/Configuration.cs +++ b/ChaosBot/ConfigHelpers/Configuration.cs @@ -54,7 +54,8 @@ namespace ChaosBot.ConfigHelpers */ public T GetValue(string key, T defaultValue, ulong? guildId = null) { - string realKey = EnsureKeyExistsAndTypeCorrect(key); + string realKey = EnsureKeyExists(key); + EnsureTypeCorrect(realKey); if (guildId.HasValue) return ConfigurationRepository.GetValue(realKey, guildId.Value, defaultValue); @@ -79,6 +80,16 @@ namespace ChaosBot.ConfigHelpers return GetValue(key, defaultValue); } + public object GetStringValue(string key, ulong? guildId = null) + { + string realKey = EnsureKeyExists(key); + + if (guildId.HasValue) + return ConfigurationRepository.GetValue(realKey, guildId.Value, ConfigurationFlags[realKey].defaultValue); + + return _appSettingsWrapper.GetValue(realKey, ConfigurationFlags[realKey].defaultValue); + } + public IConfigurationSection GetSection(string key) { return _appSettingsWrapper.GetSection(key); @@ -99,19 +110,22 @@ namespace ChaosBot.ConfigHelpers return ConfigurationFlags.ToImmutableDictionary(); } - private string EnsureKeyExistsAndTypeCorrect(string key) + private string EnsureKeyExists(string key) { bool keyExists = TryGetKeyFromRegexMatch(key, out string realKey); if (!keyExists) throw new ArgumentException($"Configuration does not contain key '{key}'"); - if (!(ConfigurationFlags[realKey].type == typeof(T))) - throw new ArgumentException( - $"Configuration flag '{realKey}<{ConfigurationFlags[realKey]}>' does not have type '{typeof(T)}'"); return realKey; } + private void EnsureTypeCorrect(string realKey) + { + if (!(ConfigurationFlags[realKey].type == typeof(T))) + throw new ArgumentException($"Configuration flag '{realKey}<{ConfigurationFlags[realKey]}>' does not have type '{typeof(T)}'"); + } + private bool TryGetKeyFromRegexMatch(string key, out string realKey) { if (ConfigurationFlags.ContainsKey(key)) diff --git a/ChaosBot/Discord/Modules/Admin/Config.cs b/ChaosBot/Discord/Modules/Admin/Config.cs index 4658b51..e19224a 100644 --- a/ChaosBot/Discord/Modules/Admin/Config.cs +++ b/ChaosBot/Discord/Modules/Admin/Config.cs @@ -134,9 +134,13 @@ namespace ChaosBot.Discord.Modules.Admin dynamic configValue; if (configFlags.TryGetValue(key, out (Type type, object defaultvalue) configFlagValue)) - configValue = new Configuration().GetValue(key, configFlagValue.defaultvalue, Context.Guild.Id); + { + configValue = new Configuration().GetStringValue(key, Context.Guild.Id); + } else + { configValue = "Not a valid key"; + } sb.AppendLine($"Value: {configValue}");