Rework all config controllers again

This commit is contained in:
Daniel_I_Am 2020-10-16 21:01:04 +02:00
parent 21514746ce
commit 951692e5fb
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
19 changed files with 201 additions and 186 deletions

View File

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
namespace ChaosBot.ConfigHelpers
@ -10,141 +11,153 @@ namespace ChaosBot.ConfigHelpers
{
private readonly IConfiguration _appSettingsWrapper;
private static readonly Dictionary<string, (Type type, object defaultValue)> ConfigurationFlags = new Dictionary<string, (Type type, object defaultValue)>
private static readonly Dictionary<string, IConfigurationDetails> ConfigurationFlags = new Dictionary<string, IConfigurationDetails>
{
{"Bot:Name", (typeof(string), "ChaosBot")},
{"Bot:Version", (typeof(string), "1.0.0")},
{"Bot:Name", new ConfigurationDetails<string>("Bot:Name", "ChaosBot", true)},
{"Bot:Version", new ConfigurationDetails<string>("Bot:Version", "1.0.0", true)},
{"WebServer:Port", (typeof(int), 8080)},
{"WebServer:Debug", (typeof(bool), false)},
{"WebServer:Port", new ConfigurationDetails<int>("WebServer:Port", 8080, true)},
{"WebServer:Debug", new ConfigurationDetails<bool>("WebServer:Debug", false, true)},
{"Discord:Prefix", (typeof(string), "!")},
{"Discord:Token", (typeof(string), "SECRET_TOKEN")},
{"Discord:BaseUri", (typeof(string), "http://localhost:8080/")},
{"Discord:ClientId", (typeof(string), "1234567890")},
{"Discord:ClientSecret", (typeof(string), "1234567890_SECRET_TOKEN")},
{"Discord:Prefix", new ConfigurationDetails<string>("Discord:Prefix", "!", true)},
{"Discord:Token", new ConfigurationDetails<string>("Discord:Token", "SECRET_TOKEN", true)},
{"Discord:BaseUri", new ConfigurationDetails<string>("Discord:BaseUri", "http://localhost:8080/", true)},
{"Discord:ClientId", new ConfigurationDetails<string>("Discord:ClientId", "1234567890", true)},
{"Discord:ClientSecret", new ConfigurationDetails<string>("Discord:ClientSecret", "1234567890_SECRET_TOKEN", true)},
{"Lodestone:ChaosBotApi:ApiToken", (typeof(string), "SECRET_TOKEN")},
{"Lodestone:ChaosBotApi:Url", (typeof(string), "http://locahost:8000")},
{"Lodestone:ChaosBotApi:ApiToken", new ConfigurationDetails<string>("Lodestone:ChaosBotApi:ApiToken", "SECRET_TOKEN", true)},
{"Lodestone:ChaosBotApi:Url", new ConfigurationDetails<string>("Lodestone:ChaosBotApi:Url", "http://locahost:8000", true)},
{"Database:Host", (typeof(string), "localhost")},
{"Database:Port", (typeof(int), 3306)},
{"Database:User", (typeof(string), "root")},
{"Database:Pass", (typeof(string), "password")},
{"Database:Name", (typeof(string), "chaosbot")},
{"Database:Host", new ConfigurationDetails<string>("Database:Host", "localhost", true)},
{"Database:Port", new ConfigurationDetails<int>("Database:Port", 3306, true)},
{"Database:User", new ConfigurationDetails<string>("Database:User", "root", true)},
{"Database:Pass", new ConfigurationDetails<string>("Database:Pass", "password", true)},
{"Database:Name", new ConfigurationDetails<string>("Database:Name", "chaosbot", true)},
{"Module:Experience:Enabled", (typeof(bool), true)},
{"LevelUp:Channel", (typeof(string), null)},
{"LevelUp:MentionUser", (typeof(bool), true)},
{"Module:Experience:Enabled", new ConfigurationDetails<bool>("Module:Experience:Enabled", true, false)},
{"LevelUp:Channel", new ConfigurationDetails<string>("LevelUp:Channel", null, false)},
{"LevelUp:MentionUser", new ConfigurationDetails<bool>("LevelUp:MentionUser", true, false)},
};
public Configuration()
{
_appSettingsWrapper = Program.AppSettingsHandler;
_appSettingsWrapper = GetAppSettingsWrapper();
}
if (_appSettingsWrapper == null)
public IConfigurationDetails GetByKey(string key)
{
if (ConfigurationFlags.TryGetValue(key, out IConfigurationDetails details))
{
return details;
}
throw new ArgumentException($"Configuration '{key}' is not available");
}
public IConfigurationDetails<T> GetByKey<T>(string key)
{
IConfigurationDetails configurationDetails = GetByKey(key);
if (configurationDetails.Type != typeof(T))
throw new ArgumentException($"Configuration flag '{key}' is not of type '{typeof(T)}'");
return (IConfigurationDetails<T>) configurationDetails;
}
internal static IConfiguration GetAppSettingsWrapper()
{
if (Program.AppSettingsHandler == null)
throw new NullReferenceException("Program.AppSettingsHandler is unset");
return Program.AppSettingsHandler;
}
/**
* Gets the configuration value associated with a key in an optional guild
* <exception cref="ArgumentException">Configuration key does not exist</exception>
* <exception cref="ArgumentException">Configuration key does not have the provided type</exception>
* <returns>Configuration value</returns>
*/
public T GetValue<T>(string key, T defaultValue, ulong? guildId = null)
public ImmutableDictionary<string, IConfigurationDetails> GetConfigurationFlags()
{
string realKey = EnsureKeyExists(key);
EnsureTypeCorrect<T>(realKey);
if (guildId.HasValue)
return ConfigurationRepository.GetValue(realKey, guildId.Value, defaultValue);
return _appSettingsWrapper.GetValue(realKey, defaultValue);
}
public T GetValue<T>(string key)
{
return GetValue<T>(key, default);
}
public T GetValueGlobalDefault<T>(string key, ulong? guildId = null)
{
bool keyExists = TryGetKeyFromRegexMatch(key, out string realKey);
object defaultObject = keyExists ? ConfigurationFlags[realKey].defaultValue : null;
T defaultValue = default;
if (defaultObject != null)
defaultValue = (T)Convert.ChangeType(defaultObject, typeof(T));
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);
return ConfigurationFlags.ToImmutableDictionary();
}
public IConfigurationSection GetSection(string key)
{
return _appSettingsWrapper.GetSection(key);
}
}
public void DeleteValue(string key, ulong guildId)
public interface IConfigurationDetails
{
ConfigurationRepository.DeleteValue(key, guildId);
string Key { get; }
bool Restricted { get; }
Type Type { get; }
object DefaultValue { get; }
string GetStringValue(ulong guildId);
void SetValueFromString(string value, ulong guildId);
void DeleteValue(ulong guildId);
}
/**
* Get the available configuration flags
* <returns>Immutable dictionary of config-key/type pairs</returns>
*/
public ImmutableDictionary<string, (Type type, object defaultvalue)> GetConfigurationFlags()
public interface IConfigurationDetails<T> : IConfigurationDetails
{
return ConfigurationFlags.ToImmutableDictionary();
new T DefaultValue { get; }
T GetValue(ulong? guildId = null, bool readRestricted = false);
T GetValue(T defaultValue, ulong? guildId = null, bool readRestricted = false);
void SetValue(T value, ulong guildId);
}
private string EnsureKeyExists(string key)
public class ConfigurationDetails<T> : IConfigurationDetails<T>
{
bool keyExists = TryGetKeyFromRegexMatch(key, out string realKey);
public string Key { get; }
public bool Restricted { get; }
public T DefaultValue { get; }
if (!keyExists)
throw new ArgumentException($"Configuration does not contain key '{key}'");
object IConfigurationDetails.DefaultValue => DefaultValue;
public Type Type => typeof(T);
return realKey;
}
private void EnsureTypeCorrect<T>(string realKey)
public ConfigurationDetails(string key, T defaultValue, bool restricted)
{
if (!(ConfigurationFlags[realKey].type == typeof(T)))
throw new ArgumentException($"Configuration flag '{realKey}<{ConfigurationFlags[realKey]}>' does not have type '{typeof(T)}'");
Key = key;
DefaultValue = defaultValue;
Restricted = restricted;
}
private bool TryGetKeyFromRegexMatch(string key, out string realKey)
public T GetValue(ulong? guildId = null, bool readRestricted = false)
{
if (ConfigurationFlags.ContainsKey(key))
{
realKey = key;
return true;
return GetValue(DefaultValue, guildId, readRestricted);
}
foreach (string configurationFlagsKey in ConfigurationFlags.Keys)
public T GetValue(T defaultValue, ulong? guildId = null, bool readRestricted = false)
{
if (new Regex(configurationFlagsKey).IsMatch(key))
if (!readRestricted && Restricted)
throw new UnauthorizedAccessException($"Configuration key '{Key}' is restricted");
if (guildId.HasValue)
{
realKey = key;
return true;
}
return ConfigurationRepository.GetValue(Key, guildId.Value, defaultValue);
}
realKey = null;
return false;
return Configuration.GetAppSettingsWrapper().GetValue(Key, defaultValue);
}
public string GetStringValue(ulong guildId)
{
return GetValue(guildId: guildId).ToString();
}
public void SetValue(T value, ulong guildId)
{
if (Restricted)
throw new UnauthorizedAccessException($"Configuration key '{Key}' is restricted");
ConfigurationRepository.SetValue(Key, value, guildId);
}
public void SetValueFromString(string value, ulong guildId)
{
SetValue((T)Convert.ChangeType(value, typeof(T)), guildId);
}
public void DeleteValue(ulong guildId)
{
if (Restricted)
throw new UnauthorizedAccessException($"Configuration key '{Key}' is restricted");
ConfigurationRepository.DeleteValue(Key, guildId);
}
}
}

View File

@ -1,7 +1,10 @@
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using ChaosBot.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace ChaosBot.ConfigHelpers
{
@ -14,27 +17,37 @@ namespace ChaosBot.ConfigHelpers
public static T GetValue<T>(string key, ulong guildId, T defaultValue)
{
using (ChaosbotContext dbContext = new ChaosbotContext())
{
using ChaosbotContext dbContext = new ChaosbotContext();
Models.Configuration config = dbContext.Configuration
.SingleOrDefault(c => c.DiscordGuildId == guildId && c.Key == key);
if (config == null || string.IsNullOrEmpty(config.SerializedValue))
return GetValueFromAppSettings(key, guildId, defaultValue);
return JsonSerializer.Deserialize<T>(config.SerializedValue);
}
public static void SetValue<T>(string key, T value, ulong guildId)
{
using ChaosbotContext dbContext = new ChaosbotContext();
Models.Configuration cnfSet = new Models.Configuration();
cnfSet.Key = key;
cnfSet.DiscordGuildId = guildId;
cnfSet.SerializedValue = JsonConvert.SerializeObject(value);
dbContext.Configuration.Upsert(cnfSet)
.On(x => new {x.Key, x.DiscordGuildId})
.Run();
}
public static void DeleteValue(string key, ulong guildId)
{
using (ChaosbotContext dbContext = new ChaosbotContext())
{
using ChaosbotContext dbContext = new ChaosbotContext();
Models.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<T>(string key, ulong guildId, T defaultValue)
{

View File

@ -34,7 +34,7 @@ namespace ChaosBot.Discord
Configuration config = new Configuration();
// this is where we get the Token value from the configuration file, and start the bot
await client.LoginAsync(TokenType.Bot, config.GetValue<string>("Discord:Token"));
await client.LoginAsync(TokenType.Bot, config.GetByKey<string>("Discord:Token").GetValue());
await client.StartAsync();
// we get the CommandHandler class here and call the InitializeAsync method to start things up for the CommandHandler service

View File

@ -51,14 +51,14 @@ namespace ChaosBot.Discord.Modules.Admin
embed.Title = $"Configuration Management Help";
sb.AppendLine();
sb.AppendLine("To set a configuration value:");
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}config set <configFlag> <value>");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}config set <configFlag> <value>");
sb.AppendLine("To get a configuration value:");
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}config get <configFlag>");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}config get <configFlag>");
sb.AppendLine("To reset a configuration value to default:");
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}config reset <configFlag>");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}config reset <configFlag>");
sb.AppendLine();
sb.AppendLine("To view this help:");
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}config help");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}config help");
/*
* Add the string to the Embed
@ -84,21 +84,10 @@ namespace ChaosBot.Discord.Modules.Admin
{
if(RestrictedConfig.IsAllowed(key))
{
using (ChaosbotContext dbContext = new ChaosbotContext())
{
Models.Configuration cnfSet = new Models.Configuration();
cnfSet.Key = key;
cnfSet.DiscordGuildId = Context.Guild.Id;
cnfSet.SerializedValue = JsonConvert.SerializeObject(value);
await dbContext.Configuration.Upsert(cnfSet)
.On(x => new {x.Key, x.DiscordGuildId}).RunAsync();
new Configuration().GetByKey(key).SetValueFromString(value, Context.Guild.Id);
await ConfigGet(key, true);
}
}
}
else
{
await ConfigHelp();
@ -129,13 +118,13 @@ namespace ChaosBot.Discord.Modules.Admin
sb.AppendLine($" Flag: {key}");
Configuration config = new Configuration();
ImmutableDictionary<string, (Type type, object defaultvalue)> configFlags =
ImmutableDictionary<string, IConfigurationDetails> configFlags =
config.GetConfigurationFlags();
dynamic configValue;
if (configFlags.TryGetValue(key, out (Type type, object defaultvalue) configFlagValue))
if (configFlags.TryGetValue(key, out IConfigurationDetails configFlagValue))
{
configValue = new Configuration().GetStringValue(key, Context.Guild.Id);
configValue = new Configuration().GetByKey(key).GetStringValue(Context.Guild.Id);
}
else
{
@ -176,7 +165,7 @@ namespace ChaosBot.Discord.Modules.Admin
EmbedBuilder embed = new EmbedBuilder();
Configuration config = new Configuration();
config.DeleteValue(key, Context.Guild.Id);
config.GetByKey(key).DeleteValue(Context.Guild.Id);
embed.WithColor(new Color(255, 255, 0));
embed.Title = $"Configuration Reset";

View File

@ -110,8 +110,8 @@ namespace ChaosBot.Discord.Modules.Admin
using HttpClient client = new HttpClient();
Configuration config = new Configuration();
string endpoint =config.GetValue<string>("Lodestone:ChaosBotApi:Url");
string apiToken = config.GetValue<string>("Lodestone:ChaosBotApi:ApiToken");
string endpoint =config.GetByKey<string>("Lodestone:ChaosBotApi:Url").GetValue();
string apiToken = config.GetByKey<string>("Lodestone:ChaosBotApi:ApiToken").GetValue();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiToken);
HttpResponseMessage result =

View File

@ -29,14 +29,14 @@ namespace ChaosBot.Discord.Modules.Admin
embed.Title = $"Role Management Help";
sb.AppendLine();
sb.AppendLine("To add a role-reaction to a message:");
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}role add <emote> <role>");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}role add <emote> <role>");
sb.AppendLine("To add many role-reactions to a message add more sets of emote and role:");
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}role add <emote> <role> <emote> <role> <emote> <role>");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}role add <emote> <role> <emote> <role> <emote> <role>");
sb.AppendLine("To remove a role-reaction from a message:");
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}role remove <emote>");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}role remove <emote>");
sb.AppendLine();
sb.AppendLine("To view this help:");
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}role help");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}role help");
/*
* Add the string to the Embed

View File

@ -23,8 +23,8 @@ namespace ChaosBot.Discord.Modules.User
Configuration config = new Configuration();
embed.WithColor(new Color(255, 255, 0));
embed.Title = $"Information {config.GetValue<string>("Bot:Name")} v{config.GetValue<string>("Bot:Version")}";
sb.AppendLine($"Prefix: {config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}");
embed.Title = $"Information {config.GetByKey<string>("Bot:Name").GetValue()} v{config.GetByKey<string>("Bot:Version").GetValue()}";
sb.AppendLine($"Prefix: {config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}");
/*
* Add the string to the Embed

View File

@ -80,22 +80,22 @@ namespace ChaosBot.Discord.Modules.User
sb.AppendLine();
sb.AppendLine();
sb.AppendLine("To get FreeCompany Info:");
sb.AppendLine($"By Id: {config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}lodestone freecompany 9231394073691143535");
sb.AppendLine($"By Name: {config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}lodestone freecompany Siren Helix");
sb.AppendLine($"By Id: {config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}lodestone freecompany 9231394073691143535");
sb.AppendLine($"By Name: {config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}lodestone freecompany Siren Helix");
sb.AppendLine();
sb.AppendLine("To get Character Info:");
sb.AppendLine($"By Id: {config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}lodestone character 9231394073691143535");
sb.AppendLine($"By Name: {config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}lodestone character Siren Luna Kaisar");
sb.AppendLine($"By Id: {config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}lodestone character 9231394073691143535");
sb.AppendLine($"By Name: {config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}lodestone character Siren Luna Kaisar");
if (CheckPermissions.CheckPerms(Context, "lodestone.link"))
{
sb.AppendLine("To Link your Character:");
sb.AppendLine($"By Id: {config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}lodestone link 9231394073691143535");
sb.AppendLine($"By Name: {config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}lodestone link Siren Luna Kaisar");
sb.AppendLine($"By Id: {config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}lodestone link 9231394073691143535");
sb.AppendLine($"By Name: {config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}lodestone link Siren Luna Kaisar");
}
sb.AppendLine();
sb.AppendLine();
sb.AppendLine("To view this help:");
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}config help");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}config help");
/*
* Add the string to the Embed

View File

@ -83,13 +83,13 @@ namespace ChaosBot.Discord.Modules.User
sb.AppendLine($"{Context.User.Mention} has requested points information.");
sb.AppendLine();
sb.AppendLine("Usage:");
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}points info");
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}points help");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}points info");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}points help");
sb.AppendLine();
sb.AppendLine("Moderation commands:");
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}points add <discord mention> <amount>");
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}point remove <discord mention> <amount>");
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}point delete <discord mention>");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}points add <discord mention> <amount>");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}point remove <discord mention> <amount>");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}point delete <discord mention>");
/*
* Add the string to the Embed

View File

@ -92,17 +92,17 @@ namespace ChaosBot.Discord.Modules.User
sb.AppendLine($"{Context.User.Mention} has requested Raffle information.");
sb.AppendLine();
sb.AppendLine("Usage:");
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}raffle info");
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}raffle help");
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}raffle buy <amount>");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}raffle info");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}raffle help");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}raffle buy <amount>");
sb.AppendLine();
sb.AppendLine("Moderation commands:");
if(CheckPermissions.CheckPerms(Context, "raffle.add", "Admin"))
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}raffle add <discord mention> <amount>");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}raffle add <discord mention> <amount>");
if(CheckPermissions.CheckPerms(Context, "raffle.remove", "Admin"))
{
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}raffle remove <discord mention> <amount>");
sb.AppendLine($"{config.GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}raffle delete <discord mention>");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}raffle remove <discord mention> <amount>");
sb.AppendLine($"{config.GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}raffle delete <discord mention>");
}
/*
* Add the string to the Embed
@ -280,7 +280,7 @@ namespace ChaosBot.Discord.Modules.User
else
{
await ReplyAsync(
$"{Context.User.Mention}, if you wish to clear ALL tickets, please send the below command.```{new Configuration().GetValueGlobalDefault<string>("Discord:Prefix", Context.Guild.Id)}raffle clear confirm```");
$"{Context.User.Mention}, if you wish to clear ALL tickets, please send the below command.```{new Configuration().GetByKey<string>("Discord:Prefix").GetValue(Context.Guild.Id)}raffle clear confirm```");
}
}
catch (Exception ex)

View File

@ -32,7 +32,7 @@ namespace ChaosBot.Discord.PreConditions
if (context.Guild == null) throw new Exception("This must be run in a guild");
// Check if module enabled in database
return new Configuration().GetValue<bool>($"Module:{moduleName}:Enabled", true, context.Guild.Id);
return new Configuration().GetByKey<bool>($"Module:{moduleName}:Enabled").GetValue(true, context.Guild.Id);
}
}
}

View File

@ -69,11 +69,11 @@ namespace ChaosBot.Discord.Services
Configuration config = new Configuration();
int argPos = 0;
string prefix = config.GetValueGlobalDefault<string>("Discord:Prefix", context.Guild.Id);
string prefix = config.GetByKey<string>("Discord:Prefix").GetValue(context.Guild.Id);
if (!(message.HasMentionPrefix(_client.CurrentUser, ref argPos) ||
message.HasStringPrefix(prefix, ref argPos)))
{
if(config.GetValue("Module:Experience:Enabled", false, context.Guild.Id))
if(config.GetByKey<bool>("Module:Experience:Enabled").GetValue(false, context.Guild.Id))
ExperienceHandler.AddXp(context);
return;
}

View File

@ -69,10 +69,10 @@ namespace ChaosBot.Discord.Services
Configuration config = new Configuration();
string channelToSendIn =
config.GetValue<string>("LevelUp:Channel", null, context.Guild.Id);
config.GetByKey<string>("LevelUp:Channel").GetValue(null, context.Guild.Id);
string mentionString = $"<@{context.User.Id}>";
if (!config.GetValueGlobalDefault<bool>("LevelUp:MentionUser", context.Guild.Id))
if (!config.GetByKey<bool>("LevelUp:MentionUser").GetValue(context.Guild.Id))
{
mentionString = context.User.Username;
if (context.User is IGuildUser guildUser)

View File

@ -38,11 +38,11 @@ namespace ChaosBot.Models
{
ConfigHelpers.Configuration config = new ConfigHelpers.Configuration();
server = config.GetValue<string>("Database:Host");
port = config.GetValue<int>("Database:Port");
user = config.GetValue<string>("Database:User");
pass = config.GetValue<string>("Database:Pass");
name = config.GetValue<string>("Database:Name");
server = config.GetByKey<string>("Database:Host").GetValue();
port = config.GetByKey<int>("Database:Port").GetValue();
user = config.GetByKey<string>("Database:User").GetValue();
pass = config.GetByKey<string>("Database:Pass").GetValue();
name = config.GetByKey<string>("Database:Name").GetValue();
}
optionsBuilder.UseMySql(

View File

@ -44,7 +44,7 @@ namespace ChaosBot
* Initialize the Discord Client and Login
*/
Configuration config = new Configuration();
_logger.Info($"Starting Up {config.GetValue<string>("Bot:Name")} v{config.GetValue<string>("Bot:Version")}");
_logger.Info($"Starting Up {config.GetByKey<string>("Bot:Name").GetValue()} v{config.GetByKey<string>("Bot:Version").GetValue()}");
try
{

View File

@ -38,9 +38,9 @@ namespace ChaosBot.WebServer.App
{
Configuration config = new Configuration();
string redirectUri = $"{config.GetValue<string>("Discord:BaseUri")}/api/discord";
string clientId = config.GetValue<string>("Discord:ClientId");
string clientSecret = config.GetValue<string>("Discord:ClientSecret");
string redirectUri = $"{config.GetByKey<string>("Discord:BaseUri").GetValue()}/api/discord";
string clientId = config.GetByKey<string>("Discord:ClientId").GetValue();
string clientSecret = config.GetByKey<string>("Discord:ClientSecret").GetValue();
if (code == null)
return Redirect($"https://discord.com/api/oauth2/authorize?client_id={clientId}&redirect_uri={redirectUri}&response_type=code&scope=identify%20guilds");

View File

@ -7,7 +7,7 @@ namespace ChaosBot.WebServer.Services
{
public string Generate()
{
string clientId = new Configuration().GetValue<string>("Discord:ClientId");
string clientId = new Configuration().GetByKey<string>("Discord:ClientId").GetValue();
const ulong permissions =
0x00000020 + // MANAGE_CHANNELS
0x04000000 + // CHANGE_NICKNAME

View File

@ -43,7 +43,7 @@ namespace ChaosBot.WebServer
{
LoggingFacade.Info("Initializing Kestrel Startup and Configuration");
if (new Configuration().GetValueGlobalDefault<bool>("WebServer:Debug"))
if (new Configuration().GetByKey<bool>("WebServer:Debug").GetValue())
app.UseDeveloperExceptionPage();
app.UseForwardedHeaders();

View File

@ -29,7 +29,7 @@ namespace ChaosBot.WebServer
webBuilder.UseWebRoot(webRoot);
webBuilder.ConfigureKestrel(serverOptions =>
{
serverOptions.Listen(IPAddress.Any, config.GetValueGlobalDefault<int>("WebServer:Port"),
serverOptions.Listen(IPAddress.Any, config.GetByKey<int>("WebServer:Port").GetValue(),
listenOptions =>
{
listenOptions.UseConnectionLogging();