Start dependency injection
This commit is contained in:
parent
d6d8aa7e8a
commit
33e8b74813
@ -10,7 +10,7 @@ namespace ChaosBot.Attribute
|
|||||||
{
|
{
|
||||||
public static class AssemblyController
|
public static class AssemblyController
|
||||||
{
|
{
|
||||||
private static Logger _logger = Program._logger;
|
private static ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||||
public static void RegisterAll()
|
public static void RegisterAll()
|
||||||
{
|
{
|
||||||
Assembly dbEntityAssembly = Assembly.GetAssembly(typeof(DBEntity));
|
Assembly dbEntityAssembly = Assembly.GetAssembly(typeof(DBEntity));
|
||||||
|
|||||||
@ -13,7 +13,7 @@ namespace ChaosBot.Database
|
|||||||
public static class Controller
|
public static class Controller
|
||||||
{
|
{
|
||||||
static SqliteConnection _conn = new SqliteConnection($"Data Source={System.IO.Directory.GetCurrentDirectory()}/{ConfigurationRepository.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 ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Run a raw query on the database
|
/// Run a raw query on the database
|
||||||
|
|||||||
@ -3,11 +3,13 @@ using System.Collections.Generic;
|
|||||||
using System.Data;
|
using System.Data;
|
||||||
using ChaosBot.Database.Entity;
|
using ChaosBot.Database.Entity;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using NLog;
|
||||||
|
|
||||||
namespace ChaosBot.Database.Repository
|
namespace ChaosBot.Database.Repository
|
||||||
{
|
{
|
||||||
public static class ConfigurationRepository
|
public static class ConfigurationRepository
|
||||||
{
|
{
|
||||||
|
private static ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||||
public static IConfiguration AppSettingsHandler { get; set; }
|
public static IConfiguration AppSettingsHandler { get; set; }
|
||||||
|
|
||||||
private static readonly string ServersTable = "ServerConfigurationFlags";
|
private static readonly string ServersTable = "ServerConfigurationFlags";
|
||||||
@ -32,7 +34,7 @@ namespace ChaosBot.Database.Repository
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static T GetValue<T>(string configurationFlag, ulong guildId)
|
public static T GetValue<T>(string configurationFlag, ulong guildId)
|
||||||
{
|
{
|
||||||
if (Program._logger == null) return AppSettingsHandler.GetValue<T>($"Servers:{guildId}:{configurationFlag}");
|
if (_logger == null) return AppSettingsHandler.GetValue<T>($"Servers:{guildId}:{configurationFlag}");
|
||||||
|
|
||||||
string valueSerialized = GetValueRaw(configurationFlag, Convert.ToInt64(guildId));
|
string valueSerialized = GetValueRaw(configurationFlag, Convert.ToInt64(guildId));
|
||||||
if (valueSerialized != null)
|
if (valueSerialized != null)
|
||||||
@ -54,16 +56,6 @@ namespace ChaosBot.Database.Repository
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Grab configuration from appsettings.json, method does not look at the database
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="configurationFlag"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static IConfigurationSection GetSection(string configurationFlag)
|
|
||||||
{
|
|
||||||
return AppSettingsHandler.GetSection(configurationFlag);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get guild specific configuration option
|
/// Get guild specific configuration option
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -99,7 +91,7 @@ namespace ChaosBot.Database.Repository
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Program._logger.Fatal($"ConfigurationRepository.SetValueRaw: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
_logger.Fatal($"ConfigurationRepository.SetValueRaw: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
29
ChaosBot/Dependency.cs
Normal file
29
ChaosBot/Dependency.cs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using NLog;
|
||||||
|
|
||||||
|
namespace ChaosBot
|
||||||
|
{
|
||||||
|
public static class Dependency
|
||||||
|
{
|
||||||
|
private static Dictionary<Type, object> InstanceList;
|
||||||
|
|
||||||
|
public static void Initialize(IConfiguration appSettingsHandler)
|
||||||
|
{
|
||||||
|
InstanceList = new Dictionary<Type, object>
|
||||||
|
{
|
||||||
|
{typeof(ILogger), Logging.GenLog(appSettingsHandler)}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static T GetInstance<T>()
|
||||||
|
{
|
||||||
|
if (InstanceList.TryGetValue(typeof(T), out object obj))
|
||||||
|
{
|
||||||
|
return (T) obj;
|
||||||
|
}
|
||||||
|
throw new KeyNotFoundException($"Could not find dependency of type {typeof(T)}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -15,7 +15,7 @@ namespace ChaosBot.Discord
|
|||||||
public class DiscordConnect
|
public class DiscordConnect
|
||||||
{
|
{
|
||||||
private static DiscordSocketClient _client;
|
private static DiscordSocketClient _client;
|
||||||
private static Logger _logger = Program._logger;
|
private static ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||||
|
|
||||||
public static async Task StartUp()
|
public static async Task StartUp()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -11,7 +11,7 @@ namespace ChaosBot.Discord.Modules
|
|||||||
{
|
{
|
||||||
public class AdminCommands : ModuleBase
|
public class AdminCommands : ModuleBase
|
||||||
{
|
{
|
||||||
private static readonly Logger _logger = Program._logger;
|
private static readonly ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||||
|
|
||||||
|
|
||||||
[Command("clear")]
|
[Command("clear")]
|
||||||
|
|||||||
@ -11,7 +11,7 @@ namespace ChaosBot.Discord.Modules
|
|||||||
{
|
{
|
||||||
public class ConfigCommands : ModuleBase
|
public class ConfigCommands : ModuleBase
|
||||||
{
|
{
|
||||||
private static readonly Logger _logger = Program._logger;
|
private static readonly ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||||
private static readonly string _prefix = ConfigurationRepository.GetValue<string>("Discord:Prefix");
|
private static readonly string _prefix = ConfigurationRepository.GetValue<string>("Discord:Prefix");
|
||||||
|
|
||||||
[Command("config")]
|
[Command("config")]
|
||||||
|
|||||||
@ -12,7 +12,7 @@ namespace ChaosBot.Discord.Modules
|
|||||||
{
|
{
|
||||||
public class DiceCommands : ModuleBase
|
public class DiceCommands : ModuleBase
|
||||||
{
|
{
|
||||||
private static Logger _logger = Program._logger;
|
private static ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||||
|
|
||||||
[Command("roll")]
|
[Command("roll")]
|
||||||
public async Task Roll(params string[] args)
|
public async Task Roll(params string[] args)
|
||||||
|
|||||||
@ -12,7 +12,7 @@ namespace ChaosBot.Discord.Modules
|
|||||||
{
|
{
|
||||||
public class InfoCommands : ModuleBase
|
public class InfoCommands : ModuleBase
|
||||||
{
|
{
|
||||||
private static readonly Logger _logger = Program._logger;
|
private static readonly ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||||
|
|
||||||
|
|
||||||
[Command("info")]
|
[Command("info")]
|
||||||
|
|||||||
@ -17,7 +17,7 @@ namespace ChaosBot.Discord.Modules
|
|||||||
{
|
{
|
||||||
public class LodestoneCommands : ModuleBase
|
public class LodestoneCommands : ModuleBase
|
||||||
{
|
{
|
||||||
private static readonly Logger _logger = Program._logger;
|
private static readonly ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||||
|
|
||||||
|
|
||||||
[Command("lodestone character")]
|
[Command("lodestone character")]
|
||||||
|
|||||||
@ -15,7 +15,7 @@ namespace ChaosBot.Discord.Modules
|
|||||||
{
|
{
|
||||||
public class PointsCommands : ModuleBase
|
public class PointsCommands : ModuleBase
|
||||||
{
|
{
|
||||||
private static Logger _logger = Program._logger;
|
private static ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||||
|
|
||||||
[Command("points help")]
|
[Command("points help")]
|
||||||
public async Task PointsCommandInfo()
|
public async Task PointsCommandInfo()
|
||||||
|
|||||||
@ -13,7 +13,7 @@ namespace ChaosBot.Discord.Modules
|
|||||||
|
|
||||||
public class RaffleSystem : ModuleBase
|
public class RaffleSystem : ModuleBase
|
||||||
{
|
{
|
||||||
private static Logger _logger = Program._logger;
|
private static ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||||
|
|
||||||
[Command("raffle")]
|
[Command("raffle")]
|
||||||
public async Task RaffleStatus()
|
public async Task RaffleStatus()
|
||||||
|
|||||||
@ -4,15 +4,18 @@ using System.Threading.Tasks;
|
|||||||
using ChaosBot.Database.Repository;
|
using ChaosBot.Database.Repository;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
|
using NLog;
|
||||||
|
|
||||||
namespace ChaosBot.Discord.PreConditions
|
namespace ChaosBot.Discord.PreConditions
|
||||||
{
|
{
|
||||||
public class CheckCommandPerm : PreconditionAttribute
|
public class CheckCommandPerm : PreconditionAttribute
|
||||||
{
|
{
|
||||||
|
private static ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||||
|
|
||||||
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
|
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
|
||||||
{
|
{
|
||||||
// Debug information
|
// Debug information
|
||||||
Program._logger.Trace($"CheckCommandPerm.CheckPermissionsAsync|Checking permissions for command: {command.Name}");
|
_logger.Trace($"CheckCommandPerm.CheckPermissionsAsync|Checking permissions for command: {command.Name}");
|
||||||
|
|
||||||
// If user is not SocketGuildUser, then return error
|
// If user is not SocketGuildUser, then return error
|
||||||
if (!(context.User is SocketGuildUser gUser)) return Task.FromResult(PreconditionResult.FromError("You must be in a guild to run this command."));
|
if (!(context.User is SocketGuildUser gUser)) return Task.FromResult(PreconditionResult.FromError("You must be in a guild to run this command."));
|
||||||
@ -46,7 +49,7 @@ namespace ChaosBot.Discord.PreConditions
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Program._logger.Info("CheckCommandperm.CheckPermissionsAsync|commandPermissions: Null Value");
|
_logger.Info("CheckCommandperm.CheckPermissionsAsync|commandPermissions: Null Value");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Permission denied
|
// Permission denied
|
||||||
|
|||||||
@ -16,7 +16,7 @@ namespace ChaosBot.Discord.Services
|
|||||||
private readonly CommandService _commands;
|
private readonly CommandService _commands;
|
||||||
private readonly DiscordSocketClient _client;
|
private readonly DiscordSocketClient _client;
|
||||||
private readonly IServiceProvider _services;
|
private readonly IServiceProvider _services;
|
||||||
private readonly Logger _logger = Program._logger;
|
private readonly ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||||
|
|
||||||
public CommandHandler(IServiceProvider services)
|
public CommandHandler(IServiceProvider services)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -12,14 +12,14 @@ namespace ChaosBot.Discord.Services
|
|||||||
{
|
{
|
||||||
public static class TimerHandler
|
public static class TimerHandler
|
||||||
{
|
{
|
||||||
private static readonly Logger _logger = Program._logger;
|
private static readonly ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||||
private static DiscordSocketClient _client;
|
private static DiscordSocketClient _client;
|
||||||
|
|
||||||
public static void Initialize(IServiceProvider services)
|
public static void Initialize(IServiceProvider services)
|
||||||
{
|
{
|
||||||
_client = services.GetRequiredService<DiscordSocketClient>();
|
_client = services.GetRequiredService<DiscordSocketClient>();
|
||||||
|
|
||||||
foreach (IConfigurationSection serverConfig in ConfigurationRepository.GetSection("Servers").GetChildren())
|
foreach (IConfigurationSection serverConfig in ConfigurationRepository.AppSettingsHandler.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);
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using ChaosBot.Database.Repository;
|
using ChaosBot.Database.Repository;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using NLog;
|
using NLog;
|
||||||
using NLog.Extensions.Logging;
|
using NLog.Extensions.Logging;
|
||||||
|
|
||||||
@ -7,11 +8,11 @@ namespace ChaosBot
|
|||||||
{
|
{
|
||||||
public class Logging
|
public class Logging
|
||||||
{
|
{
|
||||||
public static Logger GenLog()
|
public static Logger GenLog(IConfiguration appSettingsHandler)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
LogManager.Configuration = new NLogLoggingConfiguration(ConfigurationRepository.GetSection("NLog"));
|
LogManager.Configuration = new NLogLoggingConfiguration(appSettingsHandler.GetSection("NLog"));
|
||||||
|
|
||||||
return LogManager.GetCurrentClassLogger();
|
return LogManager.GetCurrentClassLogger();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,12 +12,12 @@ namespace ChaosBot
|
|||||||
{
|
{
|
||||||
internal class Program
|
internal class Program
|
||||||
{
|
{
|
||||||
public static Logger _logger;
|
private ILogger _logger;
|
||||||
private static string appsettingsPath;
|
private static string _appsettingsPath;
|
||||||
|
|
||||||
private static void Main(string[] args)
|
private static void Main(string[] args)
|
||||||
{
|
{
|
||||||
appsettingsPath = args.Length > 0 ? args[0] : "./appsettings.json";
|
_appsettingsPath = args.Length > 0 ? args[0] : "./appsettings.json";
|
||||||
new Program().MainFunction().GetAwaiter().GetResult();
|
new Program().MainFunction().GetAwaiter().GetResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,12 +28,22 @@ namespace ChaosBot
|
|||||||
/*
|
/*
|
||||||
* Load configuration from AppSettings.Json and save as Cfg
|
* Load configuration from AppSettings.Json and save as Cfg
|
||||||
*/
|
*/
|
||||||
LoadConfiguration(appsettingsPath);
|
IConfiguration configurationHandler = LoadConfiguration(_appsettingsPath);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Initialize dependency injector
|
||||||
|
*/
|
||||||
|
Dependency.Initialize(configurationHandler);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Set AppSettingsHandler on ConfigurationRepository
|
||||||
|
*/
|
||||||
|
ConfigurationRepository.AppSettingsHandler = configurationHandler;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Initialize the _logger for logging purposes
|
* Initialize the _logger for logging purposes
|
||||||
*/
|
*/
|
||||||
LoadLogger();
|
_logger = Dependency.GetInstance<ILogger>();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Attempt to load our custom assemblies
|
* Attempt to load our custom assemblies
|
||||||
@ -56,18 +66,13 @@ namespace ChaosBot
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void LoadConfiguration(string appsettingsPath)
|
public static IConfiguration LoadConfiguration(string appsettingsPath)
|
||||||
{
|
{
|
||||||
ConfigurationRepository.AppSettingsHandler = new ConfigurationBuilder()
|
return 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void LoadLogger()
|
|
||||||
{
|
|
||||||
_logger = Logging.GenLog();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void RegisterAssemblyController()
|
public static void RegisterAssemblyController()
|
||||||
{
|
{
|
||||||
AssemblyController.RegisterAll();
|
AssemblyController.RegisterAll();
|
||||||
|
|||||||
@ -29,7 +29,7 @@ namespace ChaosBot.Services
|
|||||||
|
|
||||||
static class LodestoneHttpConnection
|
static class LodestoneHttpConnection
|
||||||
{
|
{
|
||||||
private static readonly Logger _logger = Program._logger;
|
private static readonly ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||||
static HttpClient client = new HttpClient();
|
static HttpClient client = new HttpClient();
|
||||||
private static bool _firstRun = true;
|
private static bool _firstRun = true;
|
||||||
|
|
||||||
|
|||||||
@ -9,7 +9,7 @@ namespace ChaosBot.WebServer.App.Controller
|
|||||||
[Route("/config/{guildId}/{flag}")]
|
[Route("/config/{guildId}/{flag}")]
|
||||||
public class ConfigurationController
|
public class ConfigurationController
|
||||||
{
|
{
|
||||||
private readonly Logger _logger = Program._logger;
|
private readonly ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public string GetConfigurationFlag(long guildId, string flag)
|
public string GetConfigurationFlag(long guildId, string flag)
|
||||||
|
|||||||
@ -10,7 +10,7 @@ namespace ChaosBot.WebServer
|
|||||||
{
|
{
|
||||||
class Startup
|
class Startup
|
||||||
{
|
{
|
||||||
private readonly Logger _logger = Program._logger;
|
private readonly ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||||
|
|
||||||
public IConfiguration Configuration { get; }
|
public IConfiguration Configuration { get; }
|
||||||
|
|
||||||
|
|||||||
@ -31,7 +31,7 @@ namespace ChaosBot.WebServer
|
|||||||
{
|
{
|
||||||
logging.ClearProviders();
|
logging.ClearProviders();
|
||||||
logging.SetMinimumLevel(LogLevel.Trace);
|
logging.SetMinimumLevel(LogLevel.Trace);
|
||||||
logging.AddNLog(new NLogLoggingConfiguration(ConfigurationRepository.GetSection("NLog")));
|
logging.AddNLog(new NLogLoggingConfiguration(ConfigurationRepository.AppSettingsHandler.GetSection("NLog")));
|
||||||
})
|
})
|
||||||
.UseStartup<Startup>();
|
.UseStartup<Startup>();
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user