-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Update YumeCore Dependency Injection
- Updated the `Program.cs` file to use the new `AddYumeCoreServices` extension method for configuring services. - Added a new file `YumeCoreDependencyInjectionExtensions.cs` which contains the implementation of the `AddYumeCoreServices` extension method. - Added a new file `DiscordBotTokenProvider.cs` which provides the Discord bot token from either the `ICoreProperties` or environment variables. - Updated the project file to enable nullable reference types. - Updated the `YumeCore.cs` class to use dependency injection for configuring services and initializing core properties.
- Loading branch information
1 parent
8e24a23
commit e0e9edf
Showing
8 changed files
with
188 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/YumeChan.Core/Infrastructure/YumeCoreDependencyInjectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using DSharpPlus; | ||
using Microsoft.Extensions.Logging; | ||
using YumeChan.Core; | ||
using YumeChan.Core.Config; | ||
using YumeChan.Core.Services; | ||
using YumeChan.Core.Services.Config; | ||
using YumeChan.Core.Services.Plugins; | ||
using YumeChan.PluginBase.Database.MongoDB; | ||
using YumeChan.PluginBase.Database.Postgres; | ||
using YumeChan.PluginBase.Tools; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// Extension methods for the YumeCore Dependency Injection. | ||
/// </summary> | ||
public static class YumeCoreDependencyInjectionExtensions | ||
{ | ||
public static IServiceCollection AddYumeCoreServices(this IServiceCollection services) | ||
{ | ||
services.AddSingleton<DiscordClient>(static services => new(new() | ||
{ | ||
Intents = DiscordIntents.All, | ||
TokenType = TokenType.Bot, | ||
Token = services.GetRequiredService<DiscordBotTokenProvider>().GetBotToken(), // You should find a way to get this in DI context | ||
LoggerFactory = services.GetService<ILoggerFactory>(), | ||
MinimumLogLevel = LogLevel.Information | ||
})); | ||
|
||
services.AddSingleton<PluginsLoader>(serviceProvider => | ||
new(serviceProvider.GetRequiredService<ICoreProperties>().Path_Plugins)); | ||
|
||
services.AddSingleton(PluginLifetimeListener.Instance); | ||
services.AddSingleton<CommandHandler>(); | ||
services.AddSingleton<LavalinkHandler>(); | ||
services.AddSingleton<NugetPluginsFetcher>(); | ||
services.AddSingleton<PluginsDependenciesManager>(); | ||
Check failure on line 38 in src/YumeChan.Core/Infrastructure/YumeCoreDependencyInjectionExtensions.cs GitHub Actions / build-debug
Check failure on line 38 in src/YumeChan.Core/Infrastructure/YumeCoreDependencyInjectionExtensions.cs GitHub Actions / build-debug
Check failure on line 38 in src/YumeChan.Core/Infrastructure/YumeCoreDependencyInjectionExtensions.cs GitHub Actions / build-release
Check failure on line 38 in src/YumeChan.Core/Infrastructure/YumeCoreDependencyInjectionExtensions.cs GitHub Actions / build-release
|
||
services.AddSingleton<DiscordBotTokenProvider>(); | ||
|
||
services.AddSingleton(typeof(InterfaceConfigProvider<>)); | ||
|
||
services.AddSingleton(typeof(IMongoDatabaseProvider<>), typeof(UnifiedDatabaseProvider<>)); | ||
services.AddSingleton(typeof(IPostgresDatabaseProvider<>), typeof(UnifiedDatabaseProvider<>)); | ||
services.AddSingleton(typeof(IInterfaceConfigProvider<>), typeof(InterfaceConfigProvider<>)); | ||
services.AddSingleton(typeof(IJsonConfigProvider<>), typeof(JsonConfigProvider<>)); | ||
|
||
services.AddSingleton<ICoreProperties>(sp => | ||
sp.GetRequiredService<InterfaceConfigProvider<ICoreProperties>>() | ||
.InitConfig("coreconfig.json", true) | ||
.InitDefaults()); | ||
|
||
services.AddSingleton<IPluginLoaderProperties>(sp => | ||
sp.GetRequiredService<InterfaceConfigProvider<IPluginLoaderProperties>>() | ||
.InitConfig("plugins.json", true) | ||
.InitDefaults()); | ||
|
||
|
||
services.AddHttpClient() | ||
.AddNuGetPluginsFetcher(); // You should implement this in NuGetPluginsFetcher class as an extension method | ||
|
||
return services; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.InteropServices; | ||
using Microsoft.Extensions.Logging; | ||
using YumeChan.Core.Config; | ||
|
||
namespace YumeChan.Core.Services; | ||
|
||
/// <summary> | ||
/// Provides the Discord bot token. | ||
/// </summary> | ||
internal sealed class DiscordBotTokenProvider | ||
{ | ||
private readonly ICoreProperties _coreProperties; | ||
private readonly ILogger<DiscordBotTokenProvider> _logger; | ||
|
||
public DiscordBotTokenProvider(ICoreProperties coreProperties, ILogger<DiscordBotTokenProvider> logger) | ||
{ | ||
_coreProperties = coreProperties; | ||
_logger = logger; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the Discord bot token from either the <see cref="ICoreProperties"/>, or from the environment variables. | ||
/// </summary> | ||
/// <returns>The Discord bot token.</returns> | ||
/// <exception cref="ApplicationException">Thrown if no bot token was supplied.</exception> | ||
public string GetBotToken() | ||
{ | ||
string? token = _coreProperties.BotToken; | ||
|
||
if (!string.IsNullOrWhiteSpace(token)) | ||
{ | ||
return token; | ||
} | ||
|
||
string envVarName = $"{_coreProperties.AppInternalName}.Token"; | ||
|
||
if (TryBotTokenFromEnvironment(envVarName, out token, out EnvironmentVariableTarget target)) | ||
{ | ||
_logger.LogInformation("Bot Token was read from {target} Environment Variable \"{envVar}\", instead of \"coreproperties.json\" Config File", target, envVarName); | ||
return token; | ||
} | ||
|
||
ApplicationException e = new("No Bot Token supplied."); | ||
_logger.LogCritical(e, "No Bot Token was found in \"coreconfig.json\" Config File, and Environment Variables \"{envVar}\" from relevant targets are empty. Please set a Bot Token before launching the Bot", envVarName); | ||
|
||
throw e; | ||
} | ||
|
||
private static bool TryBotTokenFromEnvironment(string envVarName, [NotNullWhen(true)] out string? token, out EnvironmentVariableTarget foundFromTarget) | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
foreach (EnvironmentVariableTarget target in typeof(EnvironmentVariableTarget).GetEnumValues()) | ||
{ | ||
token = Environment.GetEnvironmentVariable(envVarName, target); | ||
|
||
if (token is not null) | ||
{ | ||
foundFromTarget = target; | ||
|
||
return true; | ||
} | ||
} | ||
|
||
token = null; | ||
foundFromTarget = default; | ||
|
||
return false; | ||
} | ||
|
||
token = Environment.GetEnvironmentVariable(envVarName); | ||
foundFromTarget = EnvironmentVariableTarget.Process; | ||
|
||
return token is not null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.