diff --git a/ChaosBot/Database/Controller.cs b/ChaosBot/Database/Controller.cs index 50d4c6e..2117ba2 100644 --- a/ChaosBot/Database/Controller.cs +++ b/ChaosBot/Database/Controller.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Data; using System.Text; +using ChaosBot.Database.Repository; using Microsoft.Data.Sqlite; using Microsoft.Extensions.Configuration; using Microsoft.VisualBasic; @@ -11,7 +12,7 @@ namespace ChaosBot.Database { public static class Controller { - static SqliteConnection _conn = new SqliteConnection($"Data Source={System.IO.Directory.GetCurrentDirectory()}/{Program.Cfg.GetValue("Bot:Database")}"); + static SqliteConnection _conn = new SqliteConnection($"Data Source={System.IO.Directory.GetCurrentDirectory()}/{ConfigurationRepository.GetValue("Bot:Database")}"); private static Logger _logger = Program._logger; /// diff --git a/ChaosBot/Database/Repository/ConfigurationRepository.cs b/ChaosBot/Database/Repository/ConfigurationRepository.cs new file mode 100644 index 0000000..c763b1b --- /dev/null +++ b/ChaosBot/Database/Repository/ConfigurationRepository.cs @@ -0,0 +1,21 @@ +using Microsoft.Extensions.Configuration; +using NLog; + +namespace ChaosBot.Database.Repository +{ + public static class ConfigurationRepository + { + private static Logger _logger = Program._logger; + public static IConfiguration AppSettingsHandler { get; set; } + + public static T GetValue(string configurationFlag) + { + return AppSettingsHandler.GetValue(configurationFlag); + } + + public static IConfigurationSection GetSection(string configurationFlag) + { + return AppSettingsHandler.GetSection(configurationFlag); + } + } +} \ No newline at end of file diff --git a/ChaosBot/Discord/DiscordConnect.cs b/ChaosBot/Discord/DiscordConnect.cs index 2d38d95..d82cd7b 100644 --- a/ChaosBot/Discord/DiscordConnect.cs +++ b/ChaosBot/Discord/DiscordConnect.cs @@ -4,6 +4,7 @@ using Discord; using Discord.Commands; using Discord.WebSocket; using System.Threading.Tasks; +using ChaosBot.Database.Repository; using ChaosBot.Discord.Services; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -33,7 +34,7 @@ namespace ChaosBot.Discord services.GetRequiredService().Log += Log; // this is where we get the Token value from the configuration file, and start the bot - await client.LoginAsync(TokenType.Bot, Program.Cfg.GetValue("Discord:Token")); + await client.LoginAsync(TokenType.Bot, ConfigurationRepository.GetValue("Discord:Token")); await client.StartAsync(); // we get the CommandHandler class here and call the InitializeAsync method to start things up for the CommandHandler service @@ -62,7 +63,6 @@ namespace ChaosBot.Discord try { csInfo = new ServiceCollection() - .AddSingleton(Program.Cfg) .AddSingleton() .AddSingleton() .AddSingleton() diff --git a/ChaosBot/Discord/Modules/InfoCommands.cs b/ChaosBot/Discord/Modules/InfoCommands.cs index 62c5fa8..46b15ff 100644 --- a/ChaosBot/Discord/Modules/InfoCommands.cs +++ b/ChaosBot/Discord/Modules/InfoCommands.cs @@ -4,6 +4,7 @@ using System.Text; using Discord.Commands; using System.Threading.Tasks; using ChaosBot.Database; +using ChaosBot.Database.Repository; using Microsoft.Extensions.Configuration; using NLog; @@ -25,10 +26,10 @@ namespace ChaosBot.Discord.Modules embed.WithColor(new Color(255, 255,0)); embed.Title = $"General Information"; - sb.AppendLine($"{Context.User.Mention} has requested information from {Program.Cfg.GetValue("Bot:Name")}."); + sb.AppendLine($"{Context.User.Mention} has requested information from {ConfigurationRepository.GetValue("Bot:Name")}."); sb.AppendLine(); - sb.AppendLine($"Bot Version: {Program.Cfg.GetValue("Bot:Version")}"); - sb.AppendLine($"Bot Prefix: {Program.Cfg.GetValue("Discord:Prefix")}"); + sb.AppendLine($"Bot Version: {ConfigurationRepository.GetValue("Bot:Version")}"); + sb.AppendLine($"Bot Prefix: {ConfigurationRepository.GetValue("Discord:Prefix")}"); sb.AppendLine($"{Controller.RawQuery("select * from RaffleTable where Id = (abs(random()) % (select (select max(Id) from RaffleTable)+1)) or rowid = (select min(Id) from RaffleTable) order by Id DESC limit 1;").Rows[0]["userId"]}"); /* diff --git a/ChaosBot/Discord/Modules/PointsCommands.cs b/ChaosBot/Discord/Modules/PointsCommands.cs index 5a4bbdd..6c5beb7 100644 --- a/ChaosBot/Discord/Modules/PointsCommands.cs +++ b/ChaosBot/Discord/Modules/PointsCommands.cs @@ -24,7 +24,7 @@ namespace ChaosBot.Discord.Modules { var sb = new StringBuilder(); var embed = new EmbedBuilder(); - string prefix = Program.Cfg.GetValue("Discord:Prefix"); + string prefix = ConfigurationRepository.GetValue("Discord:Prefix"); embed.WithColor(new Color(255, 255, 0)); embed.Title = $"Points system"; diff --git a/ChaosBot/Discord/Modules/RaffleSystem.cs b/ChaosBot/Discord/Modules/RaffleSystem.cs index f286bec..c38930c 100644 --- a/ChaosBot/Discord/Modules/RaffleSystem.cs +++ b/ChaosBot/Discord/Modules/RaffleSystem.cs @@ -24,7 +24,7 @@ namespace ChaosBot.Discord.Modules { var sb = new StringBuilder(); var embed = new EmbedBuilder(); - string prefix = Program.Cfg.GetValue("Discord:Prefix"); + string prefix = ConfigurationRepository.GetValue("Discord:Prefix"); embed.WithColor(new Color(255, 255,0)); embed.Title = $"Raffle system"; @@ -59,12 +59,12 @@ namespace ChaosBot.Discord.Modules [RequireUserPermission(GuildPermission.ManageGuild)] public async Task RaffleCommandAdd(string user, int amount = 1) { - if (Program.Cfg.GetValue($"Servers:{Context.Guild.Id}:Raffle:Max") >= amount) + if (ConfigurationRepository.GetValue($"Servers:{Context.Guild.Id}:Raffle:Max") >= amount) await RaffleCommandHelper("add", user, amount); else { await ReplyAsync( - $"You cannot give more then {Program.Cfg.GetValue($"Servers:{Context.Guild.Id}:Raffle:Max").ToString()} tickets at a time", false); + $"You cannot give more then {ConfigurationRepository.GetValue($"Servers:{Context.Guild.Id}:Raffle:Max").ToString()} tickets at a time", false); _logger.Warn($"{Context.User.Username} attempted to give {amount} tickets to {user}!"); } } @@ -94,7 +94,7 @@ namespace ChaosBot.Discord.Modules { StringBuilder sb = new StringBuilder(); var embed = new EmbedBuilder(); - string prefix = Program.Cfg.GetValue("Discord:Prefix"); + string prefix = ConfigurationRepository.GetValue("Discord:Prefix"); embed.WithColor(new Color(255, 255,0)); embed.Title = $"Raffle system"; diff --git a/ChaosBot/Discord/Services/TimerHandler.cs b/ChaosBot/Discord/Services/TimerHandler.cs index 5836b7e..9cbdc1c 100644 --- a/ChaosBot/Discord/Services/TimerHandler.cs +++ b/ChaosBot/Discord/Services/TimerHandler.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using ChaosBot.Database.Repository; using ChaosBot.Services; using Discord; using Discord.WebSocket; @@ -18,7 +19,7 @@ namespace ChaosBot.Discord.Services { _client = services.GetRequiredService(); - foreach (IConfigurationSection serverConfig in Program.Cfg.GetSection("Servers").GetChildren()) + foreach (IConfigurationSection serverConfig in ConfigurationRepository.GetSection("Servers").GetChildren()) { long? lodestoneChannelSloganDescriptionId = serverConfig.GetValue("Lodestone:SloganDescription:Channel", null); int refreshMinutes = serverConfig.GetValue("Lodestone:SloganDescription:RefreshMinutes", 60); diff --git a/ChaosBot/Logging.cs b/ChaosBot/Logging.cs index 90a3c7c..b6557e4 100644 --- a/ChaosBot/Logging.cs +++ b/ChaosBot/Logging.cs @@ -1,4 +1,5 @@ using System; +using ChaosBot.Database.Repository; using NLog; using NLog.Extensions.Logging; @@ -10,7 +11,7 @@ namespace ChaosBot { try { - LogManager.Configuration = new NLogLoggingConfiguration(Program.Cfg.GetSection("NLog")); + LogManager.Configuration = new NLogLoggingConfiguration(ConfigurationRepository.GetSection("NLog")); return LogManager.GetCurrentClassLogger(); } diff --git a/ChaosBot/Program.cs b/ChaosBot/Program.cs index 11d40b7..6a1d659 100644 --- a/ChaosBot/Program.cs +++ b/ChaosBot/Program.cs @@ -3,6 +3,7 @@ using System; using ChaosBot.Discord; using System.Threading.Tasks; using ChaosBot.Attribute; +using ChaosBot.Database.Repository; using Microsoft.Extensions.Configuration; @@ -10,7 +11,6 @@ namespace ChaosBot { internal class Program { - public static IConfiguration Cfg { get; set; } public static Logger _logger; private static string appsettingsPath; @@ -27,7 +27,7 @@ namespace ChaosBot /* * Load configuration from AppSettings.Json and save as Cfg */ - Cfg = new ConfigurationBuilder() + ConfigurationRepository.AppSettingsHandler = new ConfigurationBuilder() .SetBasePath(System.IO.Directory.GetCurrentDirectory()) .AddJsonFile(appsettingsPath, optional: false, reloadOnChange: true).Build(); @@ -45,7 +45,7 @@ namespace ChaosBot /* * Initialize the Discord Client and Login */ - _logger.Info($"Starting Up {Cfg.GetValue("Bot:Name")} v{Cfg.GetValue("Bot:Version")}"); + _logger.Info($"Starting Up {ConfigurationRepository.GetValue("Bot:Name")} v{ConfigurationRepository.GetValue("Bot:Version")}"); var discordBot = DiscordConnect.StartUp(); await discordBot;