Fix config get command
This commit is contained in:
parent
7f1bb9668f
commit
dd00a6edeb
@ -54,7 +54,8 @@ namespace ChaosBot.ConfigHelpers
|
|||||||
*/
|
*/
|
||||||
public T GetValue<T>(string key, T defaultValue, ulong? guildId = null)
|
public T GetValue<T>(string key, T defaultValue, ulong? guildId = null)
|
||||||
{
|
{
|
||||||
string realKey = EnsureKeyExistsAndTypeCorrect<T>(key);
|
string realKey = EnsureKeyExists(key);
|
||||||
|
EnsureTypeCorrect<T>(realKey);
|
||||||
|
|
||||||
if (guildId.HasValue)
|
if (guildId.HasValue)
|
||||||
return ConfigurationRepository.GetValue(realKey, guildId.Value, defaultValue);
|
return ConfigurationRepository.GetValue(realKey, guildId.Value, defaultValue);
|
||||||
@ -79,6 +80,16 @@ namespace ChaosBot.ConfigHelpers
|
|||||||
return GetValue(key, defaultValue);
|
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)
|
public IConfigurationSection GetSection(string key)
|
||||||
{
|
{
|
||||||
return _appSettingsWrapper.GetSection(key);
|
return _appSettingsWrapper.GetSection(key);
|
||||||
@ -99,19 +110,22 @@ namespace ChaosBot.ConfigHelpers
|
|||||||
return ConfigurationFlags.ToImmutableDictionary();
|
return ConfigurationFlags.ToImmutableDictionary();
|
||||||
}
|
}
|
||||||
|
|
||||||
private string EnsureKeyExistsAndTypeCorrect<T>(string key)
|
private string EnsureKeyExists(string key)
|
||||||
{
|
{
|
||||||
bool keyExists = TryGetKeyFromRegexMatch(key, out string realKey);
|
bool keyExists = TryGetKeyFromRegexMatch(key, out string realKey);
|
||||||
|
|
||||||
if (!keyExists)
|
if (!keyExists)
|
||||||
throw new ArgumentException($"Configuration does not contain key '{key}'");
|
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;
|
return realKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void EnsureTypeCorrect<T>(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)
|
private bool TryGetKeyFromRegexMatch(string key, out string realKey)
|
||||||
{
|
{
|
||||||
if (ConfigurationFlags.ContainsKey(key))
|
if (ConfigurationFlags.ContainsKey(key))
|
||||||
|
|||||||
@ -134,9 +134,13 @@ namespace ChaosBot.Discord.Modules.Admin
|
|||||||
|
|
||||||
dynamic configValue;
|
dynamic configValue;
|
||||||
if (configFlags.TryGetValue(key, out (Type type, object defaultvalue) configFlagValue))
|
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
|
else
|
||||||
|
{
|
||||||
configValue = "Not a valid key";
|
configValue = "Not a valid key";
|
||||||
|
}
|
||||||
|
|
||||||
sb.AppendLine($"Value: {configValue}");
|
sb.AppendLine($"Value: {configValue}");
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user