diff --git a/src/AzzyBot.Bot/Commands/AdminCommands.cs b/src/AzzyBot.Bot/Commands/AdminCommands.cs index 41635196..9a0136c8 100644 --- a/src/AzzyBot.Bot/Commands/AdminCommands.cs +++ b/src/AzzyBot.Bot/Commands/AdminCommands.cs @@ -24,6 +24,7 @@ using DSharpPlus.Commands.Processors.SlashCommands.ArgumentModifiers; using DSharpPlus.Entities; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; namespace AzzyBot.Bot.Commands; @@ -31,10 +32,10 @@ namespace AzzyBot.Bot.Commands; public sealed class AdminCommands { [Command("admin"), RequireGuild, RequireApplicationOwner, RequirePermissions(BotPermissions = [], UserPermissions = [DiscordPermission.Administrator])] - public sealed class AdminGroup(ILogger logger, AzzyBotSettings settings, DbActions dbActions, DiscordBotService botService) + public sealed class AdminGroup(ILogger logger, IOptions settings, DbActions dbActions, DiscordBotService botService) { private readonly ILogger _logger = logger; - private readonly AzzyBotSettings _settings = settings; + private readonly AzzyBotSettings _settings = settings.Value; private readonly DbActions _dbActions = dbActions; private readonly DiscordBotService _botService = botService; diff --git a/src/AzzyBot.Bot/Commands/Autocompletes/AzzyHelpAutocomplete.cs b/src/AzzyBot.Bot/Commands/Autocompletes/AzzyHelpAutocomplete.cs index 162653b6..642d9866 100644 --- a/src/AzzyBot.Bot/Commands/Autocompletes/AzzyHelpAutocomplete.cs +++ b/src/AzzyBot.Bot/Commands/Autocompletes/AzzyHelpAutocomplete.cs @@ -8,12 +8,13 @@ using DSharpPlus.Commands.Processors.SlashCommands; using DSharpPlus.Commands.Processors.SlashCommands.ArgumentModifiers; using DSharpPlus.Entities; +using Microsoft.Extensions.Options; namespace AzzyBot.Bot.Commands.Autocompletes; -public sealed class AzzyHelpAutocomplete(AzzyBotSettings settings) : IAutoCompleteProvider +public sealed class AzzyHelpAutocomplete(IOptions settings) : IAutoCompleteProvider { - private readonly AzzyBotSettings _settings = settings; + private readonly AzzyBotSettings _settings = settings.Value; public ValueTask> AutoCompleteAsync(AutoCompleteContext context) { diff --git a/src/AzzyBot.Bot/Commands/Autocompletes/GuildsAutocomplete.cs b/src/AzzyBot.Bot/Commands/Autocompletes/GuildsAutocomplete.cs index 657d1a83..8e33a3be 100644 --- a/src/AzzyBot.Bot/Commands/Autocompletes/GuildsAutocomplete.cs +++ b/src/AzzyBot.Bot/Commands/Autocompletes/GuildsAutocomplete.cs @@ -8,12 +8,13 @@ using DSharpPlus.Commands.Processors.SlashCommands; using DSharpPlus.Commands.Processors.SlashCommands.ArgumentModifiers; using DSharpPlus.Entities; +using Microsoft.Extensions.Options; namespace AzzyBot.Bot.Commands.Autocompletes; -public sealed class GuildsAutocomplete(AzzyBotSettings settings, DiscordBotService botService) : IAutoCompleteProvider +public sealed class GuildsAutocomplete(IOptions settings, DiscordBotService botService) : IAutoCompleteProvider { - private readonly AzzyBotSettings _settings = settings; + private readonly AzzyBotSettings _settings = settings.Value; private readonly DiscordBotService _botService = botService; public ValueTask> AutoCompleteAsync(AutoCompleteContext context) diff --git a/src/AzzyBot.Bot/Commands/CoreCommands.cs b/src/AzzyBot.Bot/Commands/CoreCommands.cs index 10684fec..93b63c5a 100644 --- a/src/AzzyBot.Bot/Commands/CoreCommands.cs +++ b/src/AzzyBot.Bot/Commands/CoreCommands.cs @@ -20,6 +20,7 @@ using DSharpPlus.Commands.Processors.SlashCommands.ArgumentModifiers; using DSharpPlus.Entities; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; namespace AzzyBot.Bot.Commands; @@ -27,10 +28,10 @@ namespace AzzyBot.Bot.Commands; public sealed class CoreCommands { [Command("core"), RequireGuild] - public sealed class CoreGroup(ILogger logger, AzzyBotSettings settings, DbActions dbActions, DiscordBotService botService) + public sealed class CoreGroup(ILogger logger, IOptions settings, DbActions dbActions, DiscordBotService botService) { private readonly ILogger _logger = logger; - private readonly AzzyBotSettings _settings = settings; + private readonly AzzyBotSettings _settings = settings.Value; private readonly DbActions _dbActions = dbActions; private readonly DiscordBotService _botService = botService; @@ -110,9 +111,9 @@ public async ValueTask HelpAsync } [Command("stats")] - public sealed class CoreStats(AppStats stats, ILogger logger) + public sealed class CoreStats(IOptions stats, ILogger logger) { - private readonly AppStats _stats = stats; + private readonly AppStats _stats = stats.Value; private readonly ILogger _logger = logger; [Command("hardware"), Description("Shows information about the hardware side of the bot.")] diff --git a/src/AzzyBot.Bot/Extensions/IConfigurationManagerExtensions.cs b/src/AzzyBot.Bot/Extensions/IConfigurationManagerExtensions.cs index 98665930..56dc9f86 100644 --- a/src/AzzyBot.Bot/Extensions/IConfigurationManagerExtensions.cs +++ b/src/AzzyBot.Bot/Extensions/IConfigurationManagerExtensions.cs @@ -5,27 +5,15 @@ namespace AzzyBot.Bot.Extensions; public static class IConfigurationManagerExtensions { - public static void AddAppConfiguration(this IConfigurationManager configurationManager, bool isDev, bool isDocker) + public static void AddAppConfiguration(this IConfigurationManager configurationManager, bool isDev, string settingsFile) { - string settingsFile = "AzzyBotSettings.json"; - if (isDev) - { - settingsFile = "AzzyBotSettings-Dev.json"; - } - else if (isDocker) - { - settingsFile = "AzzyBotSettings-Docker.json"; - } - - string path = Path.Combine("Settings", settingsFile); - - configurationManager.AddJsonFile(path); + configurationManager.AddJsonFile(settingsFile); if (isDev) return; - path = Path.Combine("Modules", "Core", "Files", "AppStats.json"); + settingsFile = Path.Combine("Modules", "Core", "Files", "AppStats.json"); - configurationManager.AddJsonFile(path); + configurationManager.AddJsonFile(settingsFile); } } diff --git a/src/AzzyBot.Bot/Extensions/IServiceCollectionExtensions.cs b/src/AzzyBot.Bot/Extensions/IServiceCollectionExtensions.cs index 2bd7f8a8..71ffe4e5 100644 --- a/src/AzzyBot.Bot/Extensions/IServiceCollectionExtensions.cs +++ b/src/AzzyBot.Bot/Extensions/IServiceCollectionExtensions.cs @@ -99,7 +99,7 @@ public static void AzzyBotServices(this IServiceCollection services, bool isDev, services.AddSingleton(); } - public static void AddAppSettings(this IServiceCollection services) + public static void AddAppSettings(this IServiceCollection services, string settingsFile) { services.AddSingleton, AzzyBotSettingsValidator>().AddOptionsWithValidateOnStart(); services.AddSingleton, DatabaseSettingsValidator>().AddOptionsWithValidateOnStart(); @@ -108,7 +108,7 @@ public static void AddAppSettings(this IServiceCollection services) services.AddSingleton, CoreUpdaterValidator>().AddOptionsWithValidateOnStart(); services.AddSingleton, AppStatsValidator>().AddOptionsWithValidateOnStart(); - services.AddOptions().BindConfiguration(nameof(AzzyBotSettings)); + services.AddOptions().BindConfiguration(nameof(AzzyBotSettings)).Configure(c => c.SettingsFile = settingsFile); services.AddOptions().BindConfiguration(nameof(DatabaseSettings)); services.AddOptions().BindConfiguration(nameof(DiscordStatusSettings)); services.AddOptions().BindConfiguration(nameof(MusicStreamingSettings)); diff --git a/src/AzzyBot.Bot/Startup.cs b/src/AzzyBot.Bot/Startup.cs index ca849dbd..efdd8c80 100644 --- a/src/AzzyBot.Bot/Startup.cs +++ b/src/AzzyBot.Bot/Startup.cs @@ -45,7 +45,9 @@ public static async Task Main(string[] args) #region Add configuration - appBuilder.Configuration.AddAppConfiguration(isDev, isDocker); + string settingsFilePath = Path.Combine("Settings", GetAppSettingsPath(isDev, isDocker)); + + appBuilder.Configuration.AddAppConfiguration(isDev, settingsFilePath); #endregion Add configuration @@ -53,7 +55,7 @@ public static async Task Main(string[] args) try { - appBuilder.Services.AddAppSettings(); + appBuilder.Services.AddAppSettings(settingsFilePath); appBuilder.Services.AzzyBotServices(isDev, isDocker, logDays); } catch (OptionsValidationException ex) @@ -72,4 +74,18 @@ public static async Task Main(string[] args) await app.StartAsync(); await app.WaitForShutdownAsync(); } + + private static string GetAppSettingsPath(bool isDev, bool isDocker) + { + if (isDev) + { + return "AzzyBotSettings-Dev.json"; + } + else if (isDocker) + { + return "AzzyBotSettings-Docker.json"; + } + + return "AzzyBotSettings.json"; + } }