Rework configuration setup to allow database flags
This commit is contained in:
parent
6ecf7d7cd7
commit
9a98960717
@ -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<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;
|
||||
|
||||
/// <summary>
|
||||
|
||||
21
ChaosBot/Database/Repository/ConfigurationRepository.cs
Normal file
21
ChaosBot/Database/Repository/ConfigurationRepository.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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<CommandService>().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<string>("Discord:Token"));
|
||||
await client.LoginAsync(TokenType.Bot, ConfigurationRepository.GetValue<string>("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<DiscordSocketClient>()
|
||||
.AddSingleton<CommandService>()
|
||||
.AddSingleton<CommandHandler>()
|
||||
|
||||
@ -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<string>("Bot:Name")}.");
|
||||
sb.AppendLine($"{Context.User.Mention} has requested information from {ConfigurationRepository.GetValue<string>("Bot:Name")}.");
|
||||
sb.AppendLine();
|
||||
sb.AppendLine($"Bot Version: {Program.Cfg.GetValue<string>("Bot:Version")}");
|
||||
sb.AppendLine($"Bot Prefix: {Program.Cfg.GetValue<string>("Discord:Prefix")}");
|
||||
sb.AppendLine($"Bot Version: {ConfigurationRepository.GetValue<string>("Bot:Version")}");
|
||||
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"]}");
|
||||
|
||||
/*
|
||||
|
||||
@ -24,7 +24,7 @@ namespace ChaosBot.Discord.Modules
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
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.Title = $"Points system";
|
||||
|
||||
@ -24,7 +24,7 @@ namespace ChaosBot.Discord.Modules
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
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.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<int>($"Servers:{Context.Guild.Id}:Raffle:Max") >= amount)
|
||||
if (ConfigurationRepository.GetValue<int>($"Servers:{Context.Guild.Id}:Raffle:Max") >= amount)
|
||||
await RaffleCommandHelper("add", user, amount);
|
||||
else
|
||||
{
|
||||
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}!");
|
||||
}
|
||||
}
|
||||
@ -94,7 +94,7 @@ namespace ChaosBot.Discord.Modules
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
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.Title = $"Raffle system";
|
||||
|
||||
@ -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<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);
|
||||
int refreshMinutes = serverConfig.GetValue<int>("Lodestone:SloganDescription:RefreshMinutes", 60);
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
@ -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<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();
|
||||
await discordBot;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user