Rework configuration setup to allow database flags

This commit is contained in:
Daniel_I_Am 2020-06-06 13:01:43 +02:00
parent 6ecf7d7cd7
commit 9a98960717
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
9 changed files with 41 additions and 16 deletions

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Text; using System.Text;
using ChaosBot.Database.Repository;
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.VisualBasic; using Microsoft.VisualBasic;
@ -11,7 +12,7 @@ namespace ChaosBot.Database
{ {
public static class Controller public static class Controller
{ {
static SqliteConnection _conn = new SqliteConnection($"Data Source={System.IO.Directory.GetCurrentDirectory()}/{Program.Cfg.GetValue<string>("Bot:Database")}"); static SqliteConnection _conn = new SqliteConnection($"Data Source={System.IO.Directory.GetCurrentDirectory()}/{ConfigurationRepository.GetValue<string>("Bot:Database")}");
private static Logger _logger = Program._logger; private static Logger _logger = Program._logger;
/// <summary> /// <summary>

View File

@ -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<T>(string configurationFlag)
{
return AppSettingsHandler.GetValue<T>(configurationFlag);
}
public static IConfigurationSection GetSection(string configurationFlag)
{
return AppSettingsHandler.GetSection(configurationFlag);
}
}
}

View File

@ -4,6 +4,7 @@ using Discord;
using Discord.Commands; using Discord.Commands;
using Discord.WebSocket; using Discord.WebSocket;
using System.Threading.Tasks; using System.Threading.Tasks;
using ChaosBot.Database.Repository;
using ChaosBot.Discord.Services; using ChaosBot.Discord.Services;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -33,7 +34,7 @@ namespace ChaosBot.Discord
services.GetRequiredService<CommandService>().Log += Log; services.GetRequiredService<CommandService>().Log += Log;
// this is where we get the Token value from the configuration file, and start the bot // this is where we get the Token value from the configuration file, and start the bot
await client.LoginAsync(TokenType.Bot, Program.Cfg.GetValue<string>("Discord:Token")); await client.LoginAsync(TokenType.Bot, ConfigurationRepository.GetValue<string>("Discord:Token"));
await client.StartAsync(); await client.StartAsync();
// we get the CommandHandler class here and call the InitializeAsync method to start things up for the CommandHandler service // 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 try
{ {
csInfo = new ServiceCollection() csInfo = new ServiceCollection()
.AddSingleton(Program.Cfg)
.AddSingleton<DiscordSocketClient>() .AddSingleton<DiscordSocketClient>()
.AddSingleton<CommandService>() .AddSingleton<CommandService>()
.AddSingleton<CommandHandler>() .AddSingleton<CommandHandler>()

View File

@ -4,6 +4,7 @@ using System.Text;
using Discord.Commands; using Discord.Commands;
using System.Threading.Tasks; using System.Threading.Tasks;
using ChaosBot.Database; using ChaosBot.Database;
using ChaosBot.Database.Repository;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using NLog; using NLog;
@ -25,10 +26,10 @@ namespace ChaosBot.Discord.Modules
embed.WithColor(new Color(255, 255,0)); embed.WithColor(new Color(255, 255,0));
embed.Title = $"General Information"; embed.Title = $"General Information";
sb.AppendLine($"{Context.User.Mention} has requested information from {Program.Cfg.GetValue<string>("Bot:Name")}."); sb.AppendLine($"{Context.User.Mention} has requested information from {ConfigurationRepository.GetValue<string>("Bot:Name")}.");
sb.AppendLine(); sb.AppendLine();
sb.AppendLine($"Bot Version: {Program.Cfg.GetValue<string>("Bot:Version")}"); sb.AppendLine($"Bot Version: {ConfigurationRepository.GetValue<string>("Bot:Version")}");
sb.AppendLine($"Bot Prefix: {Program.Cfg.GetValue<string>("Discord:Prefix")}"); sb.AppendLine($"Bot Prefix: {ConfigurationRepository.GetValue<string>("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"]}"); 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"]}");
/* /*

View File

@ -24,7 +24,7 @@ namespace ChaosBot.Discord.Modules
{ {
var sb = new StringBuilder(); var sb = new StringBuilder();
var embed = new EmbedBuilder(); var embed = new EmbedBuilder();
string prefix = Program.Cfg.GetValue<string>("Discord:Prefix"); string prefix = ConfigurationRepository.GetValue<string>("Discord:Prefix");
embed.WithColor(new Color(255, 255, 0)); embed.WithColor(new Color(255, 255, 0));
embed.Title = $"Points system"; embed.Title = $"Points system";

View File

@ -24,7 +24,7 @@ namespace ChaosBot.Discord.Modules
{ {
var sb = new StringBuilder(); var sb = new StringBuilder();
var embed = new EmbedBuilder(); var embed = new EmbedBuilder();
string prefix = Program.Cfg.GetValue<string>("Discord:Prefix"); string prefix = ConfigurationRepository.GetValue<string>("Discord:Prefix");
embed.WithColor(new Color(255, 255,0)); embed.WithColor(new Color(255, 255,0));
embed.Title = $"Raffle system"; embed.Title = $"Raffle system";
@ -59,12 +59,12 @@ namespace ChaosBot.Discord.Modules
[RequireUserPermission(GuildPermission.ManageGuild)] [RequireUserPermission(GuildPermission.ManageGuild)]
public async Task RaffleCommandAdd(string user, int amount = 1) public async Task RaffleCommandAdd(string user, int amount = 1)
{ {
if (Program.Cfg.GetValue<int>($"Servers:{Context.Guild.Id}:Raffle:Max") >= amount) if (ConfigurationRepository.GetValue<int>($"Servers:{Context.Guild.Id}:Raffle:Max") >= amount)
await RaffleCommandHelper("add", user, amount); await RaffleCommandHelper("add", user, amount);
else else
{ {
await ReplyAsync( await ReplyAsync(
$"You cannot give more then {Program.Cfg.GetValue<int>($"Servers:{Context.Guild.Id}:Raffle:Max").ToString()} tickets at a time", false); $"You cannot give more then {ConfigurationRepository.GetValue<int>($"Servers:{Context.Guild.Id}:Raffle:Max").ToString()} tickets at a time", false);
_logger.Warn($"{Context.User.Username} attempted to give {amount} tickets to {user}!"); _logger.Warn($"{Context.User.Username} attempted to give {amount} tickets to {user}!");
} }
} }
@ -94,7 +94,7 @@ namespace ChaosBot.Discord.Modules
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
var embed = new EmbedBuilder(); var embed = new EmbedBuilder();
string prefix = Program.Cfg.GetValue<string>("Discord:Prefix"); string prefix = ConfigurationRepository.GetValue<string>("Discord:Prefix");
embed.WithColor(new Color(255, 255,0)); embed.WithColor(new Color(255, 255,0));
embed.Title = $"Raffle system"; embed.Title = $"Raffle system";

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using ChaosBot.Database.Repository;
using ChaosBot.Services; using ChaosBot.Services;
using Discord; using Discord;
using Discord.WebSocket; using Discord.WebSocket;
@ -18,7 +19,7 @@ namespace ChaosBot.Discord.Services
{ {
_client = services.GetRequiredService<DiscordSocketClient>(); _client = services.GetRequiredService<DiscordSocketClient>();
foreach (IConfigurationSection serverConfig in Program.Cfg.GetSection("Servers").GetChildren()) foreach (IConfigurationSection serverConfig in ConfigurationRepository.GetSection("Servers").GetChildren())
{ {
long? lodestoneChannelSloganDescriptionId = serverConfig.GetValue<long?>("Lodestone:SloganDescription:Channel", null); long? lodestoneChannelSloganDescriptionId = serverConfig.GetValue<long?>("Lodestone:SloganDescription:Channel", null);
int refreshMinutes = serverConfig.GetValue<int>("Lodestone:SloganDescription:RefreshMinutes", 60); int refreshMinutes = serverConfig.GetValue<int>("Lodestone:SloganDescription:RefreshMinutes", 60);

View File

@ -1,4 +1,5 @@
using System; using System;
using ChaosBot.Database.Repository;
using NLog; using NLog;
using NLog.Extensions.Logging; using NLog.Extensions.Logging;
@ -10,7 +11,7 @@ namespace ChaosBot
{ {
try try
{ {
LogManager.Configuration = new NLogLoggingConfiguration(Program.Cfg.GetSection("NLog")); LogManager.Configuration = new NLogLoggingConfiguration(ConfigurationRepository.GetSection("NLog"));
return LogManager.GetCurrentClassLogger(); return LogManager.GetCurrentClassLogger();
} }

View File

@ -3,6 +3,7 @@ using System;
using ChaosBot.Discord; using ChaosBot.Discord;
using System.Threading.Tasks; using System.Threading.Tasks;
using ChaosBot.Attribute; using ChaosBot.Attribute;
using ChaosBot.Database.Repository;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@ -10,7 +11,6 @@ namespace ChaosBot
{ {
internal class Program internal class Program
{ {
public static IConfiguration Cfg { get; set; }
public static Logger _logger; public static Logger _logger;
private static string appsettingsPath; private static string appsettingsPath;
@ -27,7 +27,7 @@ namespace ChaosBot
/* /*
* Load configuration from AppSettings.Json and save as Cfg * Load configuration from AppSettings.Json and save as Cfg
*/ */
Cfg = new ConfigurationBuilder() ConfigurationRepository.AppSettingsHandler = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory()) .SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile(appsettingsPath, optional: false, reloadOnChange: true).Build(); .AddJsonFile(appsettingsPath, optional: false, reloadOnChange: true).Build();
@ -45,7 +45,7 @@ namespace ChaosBot
/* /*
* Initialize the Discord Client and Login * Initialize the Discord Client and Login
*/ */
_logger.Info($"Starting Up {Cfg.GetValue<string>("Bot:Name")} v{Cfg.GetValue<string>("Bot:Version")}"); _logger.Info($"Starting Up {ConfigurationRepository.GetValue<string>("Bot:Name")} v{ConfigurationRepository.GetValue<string>("Bot:Version")}");
var discordBot = DiscordConnect.StartUp(); var discordBot = DiscordConnect.StartUp();
await discordBot; await discordBot;