diff --git a/ChaosBot/ConfigHelpers/Configuration.cs b/ChaosBot/ConfigHelpers/Configuration.cs index f1f7caa..de6cd76 100644 --- a/ChaosBot/ConfigHelpers/Configuration.cs +++ b/ChaosBot/ConfigHelpers/Configuration.cs @@ -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 ConfigurationFlags = new Dictionary + private static readonly Dictionary ConfigurationFlags = new Dictionary { - {"Bot:Name", (typeof(string), "ChaosBot")}, - {"Bot:Version", (typeof(string), "1.0.0")}, + {"Bot:Name", new ConfigurationDetails("Bot:Name", "ChaosBot", true)}, + {"Bot:Version", new ConfigurationDetails("Bot:Version", "1.0.0", true)}, - {"WebServer:Port", (typeof(int), 8080)}, - {"WebServer:Debug", (typeof(bool), false)}, + {"WebServer:Port", new ConfigurationDetails("WebServer:Port", 8080, true)}, + {"WebServer:Debug", new ConfigurationDetails("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("Discord:Prefix", "!", true)}, + {"Discord:Token", new ConfigurationDetails("Discord:Token", "SECRET_TOKEN", true)}, + {"Discord:BaseUri", new ConfigurationDetails("Discord:BaseUri", "http://localhost:8080/", true)}, + {"Discord:ClientId", new ConfigurationDetails("Discord:ClientId", "1234567890", true)}, + {"Discord:ClientSecret", new ConfigurationDetails("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("Lodestone:ChaosBotApi:ApiToken", "SECRET_TOKEN", true)}, + {"Lodestone:ChaosBotApi:Url", new ConfigurationDetails("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("Database:Host", "localhost", true)}, + {"Database:Port", new ConfigurationDetails("Database:Port", 3306, true)}, + {"Database:User", new ConfigurationDetails("Database:User", "root", true)}, + {"Database:Pass", new ConfigurationDetails("Database:Pass", "password", true)}, + {"Database:Name", new ConfigurationDetails("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("Module:Experience:Enabled", true, false)}, + {"LevelUp:Channel", new ConfigurationDetails("LevelUp:Channel", null, false)}, + {"LevelUp:MentionUser", new ConfigurationDetails("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 GetByKey(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) configurationDetails; + } + + internal static IConfiguration GetAppSettingsWrapper() + { + if (Program.AppSettingsHandler == null) throw new NullReferenceException("Program.AppSettingsHandler is unset"); - } - /** - * Gets the configuration value associated with a key in an optional guild - * Configuration key does not exist - * Configuration key does not have the provided type - * Configuration value - */ - public T GetValue(string key, T defaultValue, ulong? guildId = null) + return Program.AppSettingsHandler; + } + + public ImmutableDictionary GetConfigurationFlags() { - string realKey = EnsureKeyExists(key); - EnsureTypeCorrect(realKey); - - if (guildId.HasValue) - return ConfigurationRepository.GetValue(realKey, guildId.Value, defaultValue); - - return _appSettingsWrapper.GetValue(realKey, defaultValue); + return ConfigurationFlags.ToImmutableDictionary(); } - - public T GetValue(string key) - { - return GetValue(key, default); - } - - public T GetValueGlobalDefault(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); - } - + public IConfigurationSection GetSection(string key) { return _appSettingsWrapper.GetSection(key); } + } - public void DeleteValue(string key, ulong guildId) + public interface IConfigurationDetails + { + 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); + } + + public interface IConfigurationDetails : IConfigurationDetails + { + 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); + } + + public class ConfigurationDetails : IConfigurationDetails + { + public string Key { get; } + public bool Restricted { get; } + public T DefaultValue { get; } + + object IConfigurationDetails.DefaultValue => DefaultValue; + public Type Type => typeof(T); + + public ConfigurationDetails(string key, T defaultValue, bool restricted) { - ConfigurationRepository.DeleteValue(key, guildId); + Key = key; + DefaultValue = defaultValue; + Restricted = restricted; } - - /** - * Get the available configuration flags - * Immutable dictionary of config-key/type pairs - */ - public ImmutableDictionary GetConfigurationFlags() + public T GetValue(ulong? guildId = null, bool readRestricted = false) { - return ConfigurationFlags.ToImmutableDictionary(); + return GetValue(DefaultValue, guildId, readRestricted); } - private string EnsureKeyExists(string key) + public T GetValue(T defaultValue, ulong? guildId = null, bool readRestricted = false) { - bool keyExists = TryGetKeyFromRegexMatch(key, out string realKey); + if (!readRestricted && Restricted) + throw new UnauthorizedAccessException($"Configuration key '{Key}' is restricted"); - if (!keyExists) - throw new ArgumentException($"Configuration does not contain key '{key}'"); - - 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)) + if (guildId.HasValue) { - realKey = key; - return true; + return ConfigurationRepository.GetValue(Key, guildId.Value, defaultValue); } - foreach (string configurationFlagsKey in ConfigurationFlags.Keys) - { - if (new Regex(configurationFlagsKey).IsMatch(key)) - { - realKey = key; - return true; - } - } + return Configuration.GetAppSettingsWrapper().GetValue(Key, defaultValue); + } - realKey = null; - return false; + 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); } } } diff --git a/ChaosBot/ConfigHelpers/ConfigurationRepository.cs b/ChaosBot/ConfigHelpers/ConfigurationRepository.cs index 93f0b8b..f5d1d1b 100644 --- a/ChaosBot/ConfigHelpers/ConfigurationRepository.cs +++ b/ChaosBot/ConfigHelpers/ConfigurationRepository.cs @@ -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,26 +17,36 @@ namespace ChaosBot.ConfigHelpers public static T GetValue(string key, ulong guildId, T defaultValue) { - 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(config.SerializedValue); - } + 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(config.SerializedValue); + } + + public static void SetValue(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()) - { - Models.Configuration config = dbContext.Configuration - .SingleOrDefault(c => c.DiscordGuildId == guildId && c.Key == key); - if (config == null) return; - dbContext.Remove(config); - dbContext.SaveChanges(); - } + 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(string key, ulong guildId, T defaultValue) diff --git a/ChaosBot/Discord/DiscordConnect.cs b/ChaosBot/Discord/DiscordConnect.cs index 8d73ba5..86914d5 100644 --- a/ChaosBot/Discord/DiscordConnect.cs +++ b/ChaosBot/Discord/DiscordConnect.cs @@ -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("Discord:Token")); + await client.LoginAsync(TokenType.Bot, config.GetByKey("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 diff --git a/ChaosBot/Discord/Modules/Admin/Config.cs b/ChaosBot/Discord/Modules/Admin/Config.cs index e19224a..e5d2089 100644 --- a/ChaosBot/Discord/Modules/Admin/Config.cs +++ b/ChaosBot/Discord/Modules/Admin/Config.cs @@ -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("Discord:Prefix", Context.Guild.Id)}config set "); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}config set "); sb.AppendLine("To get a configuration value:"); - sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}config get "); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}config get "); sb.AppendLine("To reset a configuration value to default:"); - sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}config reset "); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}config reset "); sb.AppendLine(); sb.AppendLine("To view this help:"); - sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}config help"); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}config help"); /* * Add the string to the Embed @@ -84,19 +84,8 @@ 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(); - - await ConfigGet(key, true); - } + new Configuration().GetByKey(key).SetValueFromString(value, Context.Guild.Id); + await ConfigGet(key, true); } } else @@ -129,13 +118,13 @@ namespace ChaosBot.Discord.Modules.Admin sb.AppendLine($" Flag: {key}"); Configuration config = new Configuration(); - ImmutableDictionary configFlags = + ImmutableDictionary 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"; diff --git a/ChaosBot/Discord/Modules/Admin/RankCheck.cs b/ChaosBot/Discord/Modules/Admin/RankCheck.cs index ec233a5..8c9a402 100644 --- a/ChaosBot/Discord/Modules/Admin/RankCheck.cs +++ b/ChaosBot/Discord/Modules/Admin/RankCheck.cs @@ -110,8 +110,8 @@ namespace ChaosBot.Discord.Modules.Admin using HttpClient client = new HttpClient(); Configuration config = new Configuration(); - string endpoint =config.GetValue("Lodestone:ChaosBotApi:Url"); - string apiToken = config.GetValue("Lodestone:ChaosBotApi:ApiToken"); + string endpoint =config.GetByKey("Lodestone:ChaosBotApi:Url").GetValue(); + string apiToken = config.GetByKey("Lodestone:ChaosBotApi:ApiToken").GetValue(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiToken); HttpResponseMessage result = diff --git a/ChaosBot/Discord/Modules/Admin/Role.cs b/ChaosBot/Discord/Modules/Admin/Role.cs index 73f848d..3e91f7f 100644 --- a/ChaosBot/Discord/Modules/Admin/Role.cs +++ b/ChaosBot/Discord/Modules/Admin/Role.cs @@ -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("Discord:Prefix", Context.Guild.Id)}role add "); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}role add "); sb.AppendLine("To add many role-reactions to a message add more sets of emote and role:"); - sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}role add "); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}role add "); sb.AppendLine("To remove a role-reaction from a message:"); - sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}role remove "); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}role remove "); sb.AppendLine(); sb.AppendLine("To view this help:"); - sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}role help"); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}role help"); /* * Add the string to the Embed diff --git a/ChaosBot/Discord/Modules/User/Info.cs b/ChaosBot/Discord/Modules/User/Info.cs index 0a639ea..0f760b2 100644 --- a/ChaosBot/Discord/Modules/User/Info.cs +++ b/ChaosBot/Discord/Modules/User/Info.cs @@ -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("Bot:Name")} v{config.GetValue("Bot:Version")}"; - sb.AppendLine($"Prefix: {config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}"); + embed.Title = $"Information {config.GetByKey("Bot:Name").GetValue()} v{config.GetByKey("Bot:Version").GetValue()}"; + sb.AppendLine($"Prefix: {config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}"); /* * Add the string to the Embed diff --git a/ChaosBot/Discord/Modules/User/Lodestone.cs b/ChaosBot/Discord/Modules/User/Lodestone.cs index 5b3ffed..c1ca9c3 100644 --- a/ChaosBot/Discord/Modules/User/Lodestone.cs +++ b/ChaosBot/Discord/Modules/User/Lodestone.cs @@ -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("Discord:Prefix", Context.Guild.Id)}lodestone freecompany 9231394073691143535"); - sb.AppendLine($"By Name: {config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}lodestone freecompany Siren Helix"); + sb.AppendLine($"By Id: {config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}lodestone freecompany 9231394073691143535"); + sb.AppendLine($"By Name: {config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}lodestone freecompany Siren Helix"); sb.AppendLine(); sb.AppendLine("To get Character Info:"); - sb.AppendLine($"By Id: {config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}lodestone character 9231394073691143535"); - sb.AppendLine($"By Name: {config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}lodestone character Siren Luna Kaisar"); + sb.AppendLine($"By Id: {config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}lodestone character 9231394073691143535"); + sb.AppendLine($"By Name: {config.GetByKey("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("Discord:Prefix", Context.Guild.Id)}lodestone link 9231394073691143535"); - sb.AppendLine($"By Name: {config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}lodestone link Siren Luna Kaisar"); + sb.AppendLine($"By Id: {config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}lodestone link 9231394073691143535"); + sb.AppendLine($"By Name: {config.GetByKey("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("Discord:Prefix", Context.Guild.Id)}config help"); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}config help"); /* * Add the string to the Embed diff --git a/ChaosBot/Discord/Modules/User/Points.cs b/ChaosBot/Discord/Modules/User/Points.cs index 0b2e7fd..264e958 100644 --- a/ChaosBot/Discord/Modules/User/Points.cs +++ b/ChaosBot/Discord/Modules/User/Points.cs @@ -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("Discord:Prefix", Context.Guild.Id)}points info"); - sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}points help"); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}points info"); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}points help"); sb.AppendLine(); sb.AppendLine("Moderation commands:"); - sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}points add "); - sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}point remove "); - sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}point delete "); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}points add "); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}point remove "); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}point delete "); /* * Add the string to the Embed diff --git a/ChaosBot/Discord/Modules/User/RaffleCmd.cs b/ChaosBot/Discord/Modules/User/RaffleCmd.cs index fefaa55..a8b398c 100644 --- a/ChaosBot/Discord/Modules/User/RaffleCmd.cs +++ b/ChaosBot/Discord/Modules/User/RaffleCmd.cs @@ -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("Discord:Prefix", Context.Guild.Id)}raffle info"); - sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}raffle help"); - sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}raffle buy "); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}raffle info"); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}raffle help"); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}raffle buy "); sb.AppendLine(); sb.AppendLine("Moderation commands:"); if(CheckPermissions.CheckPerms(Context, "raffle.add", "Admin")) - sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}raffle add "); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}raffle add "); if(CheckPermissions.CheckPerms(Context, "raffle.remove", "Admin")) { - sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}raffle remove "); - sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}raffle delete "); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}raffle remove "); + sb.AppendLine($"{config.GetByKey("Discord:Prefix").GetValue(Context.Guild.Id)}raffle delete "); } /* * 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("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("Discord:Prefix").GetValue(Context.Guild.Id)}raffle clear confirm```"); } } catch (Exception ex) diff --git a/ChaosBot/Discord/PreConditions/CheckModuleEnabled.cs b/ChaosBot/Discord/PreConditions/CheckModuleEnabled.cs index 6452326..1ae3d64 100644 --- a/ChaosBot/Discord/PreConditions/CheckModuleEnabled.cs +++ b/ChaosBot/Discord/PreConditions/CheckModuleEnabled.cs @@ -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($"Module:{moduleName}:Enabled", true, context.Guild.Id); + return new Configuration().GetByKey($"Module:{moduleName}:Enabled").GetValue(true, context.Guild.Id); } } } diff --git a/ChaosBot/Discord/Services/CommandHandler.cs b/ChaosBot/Discord/Services/CommandHandler.cs index 1e89cc6..893677c 100644 --- a/ChaosBot/Discord/Services/CommandHandler.cs +++ b/ChaosBot/Discord/Services/CommandHandler.cs @@ -69,11 +69,11 @@ namespace ChaosBot.Discord.Services Configuration config = new Configuration(); int argPos = 0; - string prefix = config.GetValueGlobalDefault("Discord:Prefix", context.Guild.Id); + string prefix = config.GetByKey("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("Module:Experience:Enabled").GetValue(false, context.Guild.Id)) ExperienceHandler.AddXp(context); return; } diff --git a/ChaosBot/Discord/Services/ExperienceHandler.cs b/ChaosBot/Discord/Services/ExperienceHandler.cs index 7da9f06..dd85c52 100644 --- a/ChaosBot/Discord/Services/ExperienceHandler.cs +++ b/ChaosBot/Discord/Services/ExperienceHandler.cs @@ -69,10 +69,10 @@ namespace ChaosBot.Discord.Services Configuration config = new Configuration(); string channelToSendIn = - config.GetValue("LevelUp:Channel", null, context.Guild.Id); + config.GetByKey("LevelUp:Channel").GetValue(null, context.Guild.Id); string mentionString = $"<@{context.User.Id}>"; - if (!config.GetValueGlobalDefault("LevelUp:MentionUser", context.Guild.Id)) + if (!config.GetByKey("LevelUp:MentionUser").GetValue(context.Guild.Id)) { mentionString = context.User.Username; if (context.User is IGuildUser guildUser) diff --git a/ChaosBot/Models/ChaosbotContext.cs b/ChaosBot/Models/ChaosbotContext.cs index d0d3f7b..64ee36b 100644 --- a/ChaosBot/Models/ChaosbotContext.cs +++ b/ChaosBot/Models/ChaosbotContext.cs @@ -38,11 +38,11 @@ namespace ChaosBot.Models { ConfigHelpers.Configuration config = new ConfigHelpers.Configuration(); - server = config.GetValue("Database:Host"); - port = config.GetValue("Database:Port"); - user = config.GetValue("Database:User"); - pass = config.GetValue("Database:Pass"); - name = config.GetValue("Database:Name"); + server = config.GetByKey("Database:Host").GetValue(); + port = config.GetByKey("Database:Port").GetValue(); + user = config.GetByKey("Database:User").GetValue(); + pass = config.GetByKey("Database:Pass").GetValue(); + name = config.GetByKey("Database:Name").GetValue(); } optionsBuilder.UseMySql( diff --git a/ChaosBot/Program.cs b/ChaosBot/Program.cs index 1a0e23c..f735594 100644 --- a/ChaosBot/Program.cs +++ b/ChaosBot/Program.cs @@ -44,7 +44,7 @@ namespace ChaosBot * Initialize the Discord Client and Login */ Configuration config = new Configuration(); - _logger.Info($"Starting Up {config.GetValue("Bot:Name")} v{config.GetValue("Bot:Version")}"); + _logger.Info($"Starting Up {config.GetByKey("Bot:Name").GetValue()} v{config.GetByKey("Bot:Version").GetValue()}"); try { diff --git a/ChaosBot/WebServer/App/DiscordController.cs b/ChaosBot/WebServer/App/DiscordController.cs index 2e6939b..97fa6cf 100644 --- a/ChaosBot/WebServer/App/DiscordController.cs +++ b/ChaosBot/WebServer/App/DiscordController.cs @@ -38,9 +38,9 @@ namespace ChaosBot.WebServer.App { Configuration config = new Configuration(); - string redirectUri = $"{config.GetValue("Discord:BaseUri")}/api/discord"; - string clientId = config.GetValue("Discord:ClientId"); - string clientSecret = config.GetValue("Discord:ClientSecret"); + string redirectUri = $"{config.GetByKey("Discord:BaseUri").GetValue()}/api/discord"; + string clientId = config.GetByKey("Discord:ClientId").GetValue(); + string clientSecret = config.GetByKey("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"); diff --git a/ChaosBot/WebServer/Services/DiscordInviteGenerator.cs b/ChaosBot/WebServer/Services/DiscordInviteGenerator.cs index b689851..2c7bb8d 100644 --- a/ChaosBot/WebServer/Services/DiscordInviteGenerator.cs +++ b/ChaosBot/WebServer/Services/DiscordInviteGenerator.cs @@ -7,7 +7,7 @@ namespace ChaosBot.WebServer.Services { public string Generate() { - string clientId = new Configuration().GetValue("Discord:ClientId"); + string clientId = new Configuration().GetByKey("Discord:ClientId").GetValue(); const ulong permissions = 0x00000020 + // MANAGE_CHANNELS 0x04000000 + // CHANGE_NICKNAME diff --git a/ChaosBot/WebServer/Startup.cs b/ChaosBot/WebServer/Startup.cs index 8c6ec5a..b71f352 100644 --- a/ChaosBot/WebServer/Startup.cs +++ b/ChaosBot/WebServer/Startup.cs @@ -43,7 +43,7 @@ namespace ChaosBot.WebServer { LoggingFacade.Info("Initializing Kestrel Startup and Configuration"); - if (new Configuration().GetValueGlobalDefault("WebServer:Debug")) + if (new Configuration().GetByKey("WebServer:Debug").GetValue()) app.UseDeveloperExceptionPage(); app.UseForwardedHeaders(); diff --git a/ChaosBot/WebServer/WebServer.cs b/ChaosBot/WebServer/WebServer.cs index 7e37f66..503e97c 100644 --- a/ChaosBot/WebServer/WebServer.cs +++ b/ChaosBot/WebServer/WebServer.cs @@ -29,7 +29,7 @@ namespace ChaosBot.WebServer webBuilder.UseWebRoot(webRoot); webBuilder.ConfigureKestrel(serverOptions => { - serverOptions.Listen(IPAddress.Any, config.GetValueGlobalDefault("WebServer:Port"), + serverOptions.Listen(IPAddress.Any, config.GetByKey("WebServer:Port").GetValue(), listenOptions => { listenOptions.UseConnectionLogging();