diff --git a/ChaosBot/Configuration.cs b/ChaosBot/Configuration.cs index 2246f2e..b79031a 100644 --- a/ChaosBot/Configuration.cs +++ b/ChaosBot/Configuration.cs @@ -11,28 +11,32 @@ namespace ChaosBot { private readonly IConfiguration _appSettingsWrapper; - private static readonly Dictionary ConfigurationFlags = new Dictionary + private static readonly Dictionary ConfigurationFlags = new Dictionary { - {"Bot:Name", typeof(string)}, - {"Bot:Version", typeof(string)}, + {"Bot:Name", (typeof(string), "ChaosBot")}, + {"Bot:Version", (typeof(string), "1.0.0")}, - {"WebServer:Port", typeof(int)}, - {"WebServer:Debug", typeof(bool)}, + {"WebServer:Port", (typeof(int), 8080)}, + {"WebServer:Debug", (typeof(bool), false)}, - {"Discord:Prefix", typeof(string)}, - {"Discord:Token", typeof(string)}, - {"Discord:BaseUri", typeof(string)}, - {"Discord:ClientId", typeof(string)}, - {"Discord:ClientSecret", typeof(string)}, + {"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")}, - {"Lodestone:ChaosBotApi:ApiToken", typeof(string)}, - {"Lodestone:ChaosBotApi:Url", typeof(string)}, + {"Lodestone:ChaosBotApi:ApiToken", (typeof(string), "SECRET_TOKEN")}, + {"Lodestone:ChaosBotApi:Url", (typeof(string), "http://locahost:8000")}, - {"Database:Host", typeof(string)}, - {"Database:Port", typeof(int)}, - {"Database:User", typeof(string)}, - {"Database:Pass", typeof(string)}, - {"Database:Name", typeof(string)}, + {"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")}, + + {"Module:Experience:Enabled", (typeof(bool), true)}, + {"LevelUp:Channel", (typeof(string), null)}, + {"LevelUp:MentionUser", (typeof(bool), true)}, }; public Configuration() @@ -56,7 +60,7 @@ namespace ChaosBot if (!keyExists) throw new ArgumentException($"Configuration does not contain key '{key}'"); - if (!(ConfigurationFlags[realKey] == typeof(T))) + if (!(ConfigurationFlags[realKey].type == typeof(T))) throw new ArgumentException($"Configuration flag '{realKey}<{ConfigurationFlags[realKey]}>' does not have type '{typeof(T)}'"); if (guildId.HasValue) @@ -65,6 +69,18 @@ namespace ChaosBot return _appSettingsWrapper.GetValue(realKey, defaultValue); } + 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 T GetValue(string key) { return GetValue(key, default); @@ -79,7 +95,7 @@ namespace ChaosBot * Get the available configuration flags * Immutable dictionary of config-key/type pairs */ - public ImmutableDictionary GetConfigurationFlags() + public ImmutableDictionary GetConfigurationFlags() { return ConfigurationFlags.ToImmutableDictionary(); } diff --git a/ChaosBot/Discord/Modules/Admin/Config.cs b/ChaosBot/Discord/Modules/Admin/Config.cs index f305aa4..4b4ad9c 100644 --- a/ChaosBot/Discord/Modules/Admin/Config.cs +++ b/ChaosBot/Discord/Modules/Admin/Config.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Immutable; using Discord.Commands; using System.Threading.Tasks; using System.Text; @@ -43,19 +44,20 @@ namespace ChaosBot.Discord.Modules.Admin { var sb = new StringBuilder(); var embed = new EmbedBuilder(); + Configuration config = new Configuration(); embed.WithColor(new Color(255, 255, 0)); embed.Title = $"Configuration Management Help"; sb.AppendLine(); sb.AppendLine("To set a configuration value:"); - sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}config set "); + sb.AppendLine($"{config.GetValueGlobalDefault("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($"{config.GetValueGlobalDefault("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($"{config.GetValueGlobalDefault("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"); + sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}config help"); /* * Add the string to the Embed @@ -124,7 +126,18 @@ namespace ChaosBot.Discord.Modules.Admin embed.Title = $"Configuration Retrieval"; sb.AppendLine(); sb.AppendLine($" Flag: {key}"); - sb.AppendLine($"Value: {ConfigurationRepository.GetValue(key, Context.Guild.Id, "NotSet")}"); + + Configuration config = new Configuration(); + ImmutableDictionary configFlags = + config.GetConfigurationFlags(); + + dynamic configValue; + if (configFlags.TryGetValue(key, out (Type type, object defaultvalue) configFlagValue)) + configValue = new Configuration().GetValue(key, configFlagValue.defaultvalue, Context.Guild.Id); + else + configValue = "Not a valid key"; + + sb.AppendLine($"Value: {configValue}"); /* * Add the string to the Embed diff --git a/ChaosBot/Discord/Modules/Admin/Role.cs b/ChaosBot/Discord/Modules/Admin/Role.cs index 95bd044..98f7b43 100644 --- a/ChaosBot/Discord/Modules/Admin/Role.cs +++ b/ChaosBot/Discord/Modules/Admin/Role.cs @@ -23,19 +23,20 @@ namespace ChaosBot.Discord.Modules.Admin { var sb = new StringBuilder(); var embed = new EmbedBuilder(); + Configuration config = new Configuration(); embed.WithColor(new Color(255, 255, 0)); embed.Title = $"Role Management Help"; sb.AppendLine(); sb.AppendLine("To add a role-reaction to a message:"); - sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}role add "); + sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}role add "); sb.AppendLine("To add many role-reactions to a message add more sets of emote and role:"); - sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}role add "); + sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}role add "); sb.AppendLine("To remove a role-reaction from a message:"); - sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}role remove "); + sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}role remove "); sb.AppendLine(); sb.AppendLine("To view this help:"); - sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}role help"); + sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", 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 60af2c0..e4a4d57 100644 --- a/ChaosBot/Discord/Modules/User/Info.cs +++ b/ChaosBot/Discord/Modules/User/Info.cs @@ -24,7 +24,7 @@ namespace ChaosBot.Discord.Modules.User embed.WithColor(new Color(255, 255, 0)); embed.Title = $"Information {config.GetValue("Bot:Name")} v{config.GetValue("Bot:Version")}"; - sb.AppendLine($"Prefix: {config.GetValue("Discord:Prefix", default(string), Context.Guild.Id)}"); + sb.AppendLine($"Prefix: {config.GetValueGlobalDefault("Discord:Prefix", 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 f44c4db..c9f5cde 100644 --- a/ChaosBot/Discord/Modules/User/Lodestone.cs +++ b/ChaosBot/Discord/Modules/User/Lodestone.cs @@ -73,28 +73,29 @@ namespace ChaosBot.Discord.Modules.User { var sb = new StringBuilder(); var embed = new EmbedBuilder(); + Configuration config = new Configuration(); embed.WithColor(new Color(255, 255, 0)); embed.Title = "Lodestone API Help"; sb.AppendLine(); sb.AppendLine(); sb.AppendLine("To get FreeCompany Info:"); - sb.AppendLine($"By Id: {ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}lodestone freecompany 9231394073691143535"); - sb.AppendLine($"By Name: {ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}lodestone freecompany Siren Helix"); + 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(); sb.AppendLine("To get Character Info:"); - sb.AppendLine($"By Id: {ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}lodestone character 9231394073691143535"); - sb.AppendLine($"By Name: {ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}lodestone character Siren Luna Kaisar"); + 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"); if (CheckPermissions.CheckPerms(Context, "lodestone.link")) { sb.AppendLine("To Link your Character:"); - sb.AppendLine($"By Id: {ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}lodestone link 9231394073691143535"); - sb.AppendLine($"By Name: {ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}lodestone link Siren Luna Kaisar"); + 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(); sb.AppendLine(); sb.AppendLine("To view this help:"); - sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}config help"); + sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", 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 e3bc9a7..8ecfa3f 100644 --- a/ChaosBot/Discord/Modules/User/Points.cs +++ b/ChaosBot/Discord/Modules/User/Points.cs @@ -76,19 +76,20 @@ namespace ChaosBot.Discord.Modules.User { var sb = new StringBuilder(); var embed = new EmbedBuilder(); + Configuration config = new Configuration(); embed.WithColor(new Color(255, 255, 0)); embed.Title = "Points system"; sb.AppendLine($"{Context.User.Mention} has requested points information."); sb.AppendLine(); sb.AppendLine("Usage:"); - sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}points info"); - sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}points help"); + sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}points info"); + sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}points help"); sb.AppendLine(); sb.AppendLine("Moderation commands:"); - sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}points add "); - sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}point remove "); - sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}point delete "); + 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 "); /* * Add the string to the Embed diff --git a/ChaosBot/Discord/Modules/User/RaffleCmd.cs b/ChaosBot/Discord/Modules/User/RaffleCmd.cs index 0da4467..7ced268 100644 --- a/ChaosBot/Discord/Modules/User/RaffleCmd.cs +++ b/ChaosBot/Discord/Modules/User/RaffleCmd.cs @@ -85,23 +85,24 @@ namespace ChaosBot.Discord.Modules.User { var sb = new StringBuilder(); var embed = new EmbedBuilder(); + Configuration config = new Configuration(); embed.WithColor(new Color(255, 255, 0)); embed.Title = "Raffle system"; sb.AppendLine($"{Context.User.Mention} has requested Raffle information."); sb.AppendLine(); sb.AppendLine("Usage:"); - sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}raffle info"); - sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}raffle help"); - sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}raffle buy "); + 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(); sb.AppendLine("Moderation commands:"); if(CheckPermissions.CheckPerms(Context, "raffle.add", "Admin")) - sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}raffle add "); + sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}raffle add "); if(CheckPermissions.CheckPerms(Context, "raffle.remove", "Admin")) { - sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}raffle remove "); - sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}raffle delete "); + sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}raffle remove "); + sb.AppendLine($"{config.GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}raffle delete "); } /* * Add the string to the Embed @@ -279,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.```{ConfigurationRepository.GetValue("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().GetValueGlobalDefault("Discord:Prefix", Context.Guild.Id)}raffle clear confirm```"); } } catch (Exception ex) diff --git a/ChaosBot/Discord/PreConditions/CheckModuleEnabled.cs b/ChaosBot/Discord/PreConditions/CheckModuleEnabled.cs index 502c7c5..dbd1d24 100644 --- a/ChaosBot/Discord/PreConditions/CheckModuleEnabled.cs +++ b/ChaosBot/Discord/PreConditions/CheckModuleEnabled.cs @@ -32,8 +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 Convert.ToBoolean(ConfigurationRepository.GetValue($"Module:{moduleName}:Enabled", - context.Guild.Id, "true")); + return new Configuration().GetValue($"Module:{moduleName}:Enabled", true, context.Guild.Id); } } } diff --git a/ChaosBot/Discord/Services/CommandHandler.cs b/ChaosBot/Discord/Services/CommandHandler.cs index 1c370ea..25593e3 100644 --- a/ChaosBot/Discord/Services/CommandHandler.cs +++ b/ChaosBot/Discord/Services/CommandHandler.cs @@ -66,13 +66,14 @@ namespace ChaosBot.Discord.Services SocketCommandContext context = new SocketCommandContext(_client, message); + Configuration config = new Configuration(); int argPos = 0; - string prefix = ConfigurationRepository.GetValue("Discord:Prefix", context.Guild.Id, "!"); + string prefix = config.GetValueGlobalDefault("Discord:Prefix", context.Guild.Id); if (!(message.HasMentionPrefix(_client.CurrentUser, ref argPos) || message.HasStringPrefix(prefix, ref argPos))) { - if(Convert.ToBoolean(ConfigurationRepository.GetValue("Experience:Commands", context.Guild.Id, "false"))) + if(config.GetValue("Module:Experience:Enabled", false, context.Guild.Id)) ExperienceHandler.AddXp(context); return; } diff --git a/ChaosBot/Discord/Services/ExperienceHandler.cs b/ChaosBot/Discord/Services/ExperienceHandler.cs index ade1678..cc59850 100644 --- a/ChaosBot/Discord/Services/ExperienceHandler.cs +++ b/ChaosBot/Discord/Services/ExperienceHandler.cs @@ -66,12 +66,13 @@ namespace ChaosBot.Discord.Services { // The user has leveled up, we can send a message LoggingFacade.Info($"User leveled up [{context.User.Username}#{context.User.Discriminator} -> Level {newLevel}]"); + Configuration config = new Configuration(); string channelToSendIn = - ConfigurationRepository.GetValue("LevelUp:Channel", context.Guild.Id, "false"); + config.GetValue("LevelUp:Channel", null, context.Guild.Id); string mentionString = $"<@{context.User.Id}>"; - if (!Convert.ToBoolean(ConfigurationRepository.GetValue("LevelUp:MentionUser", context.Guild.Id, "true"))) + if (!config.GetValueGlobalDefault("LevelUp:MentionUser", context.Guild.Id)) { mentionString = context.User.Username; if (context.User is IGuildUser guildUser) diff --git a/ChaosBot/WebServer/Startup.cs b/ChaosBot/WebServer/Startup.cs index a35b965..1914204 100644 --- a/ChaosBot/WebServer/Startup.cs +++ b/ChaosBot/WebServer/Startup.cs @@ -42,7 +42,7 @@ namespace ChaosBot.WebServer { LoggingFacade.Info("Initializing Kestrel Startup and Configuration"); - if (new Configuration().GetValue("WebServer:Debug", false)) + if (new Configuration().GetValueGlobalDefault("WebServer:Debug")) app.UseDeveloperExceptionPage(); app.UseForwardedHeaders(); diff --git a/ChaosBot/WebServer/WebServer.cs b/ChaosBot/WebServer/WebServer.cs index 71a3a13..22d24f7 100644 --- a/ChaosBot/WebServer/WebServer.cs +++ b/ChaosBot/WebServer/WebServer.cs @@ -28,7 +28,7 @@ namespace ChaosBot.WebServer webBuilder.UseWebRoot(webRoot); webBuilder.ConfigureKestrel(serverOptions => { - serverOptions.Listen(IPAddress.Any, config.GetValue("WebServer:Port", 80), + serverOptions.Listen(IPAddress.Any, config.GetValueGlobalDefault("WebServer:Port"), listenOptions => { listenOptions.UseConnectionLogging();