Skip to content

Commit

Permalink
Stop coding when you're tired...
Browse files Browse the repository at this point in the history
  • Loading branch information
Sella-GH committed Dec 19, 2024
1 parent 3e204c3 commit 6e890bf
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 30 deletions.
5 changes: 3 additions & 2 deletions src/AzzyBot.Bot/Commands/AdminCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@
using DSharpPlus.Commands.Processors.SlashCommands.ArgumentModifiers;
using DSharpPlus.Entities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace AzzyBot.Bot.Commands;

[SuppressMessage("Design", "CA1034:Nested types should not be visible", Justification = "DSharpPlus best practice")]
public sealed class AdminCommands
{
[Command("admin"), RequireGuild, RequireApplicationOwner, RequirePermissions(BotPermissions = [], UserPermissions = [DiscordPermission.Administrator])]
public sealed class AdminGroup(ILogger<AdminGroup> logger, AzzyBotSettings settings, DbActions dbActions, DiscordBotService botService)
public sealed class AdminGroup(ILogger<AdminGroup> logger, IOptions<AzzyBotSettings> settings, DbActions dbActions, DiscordBotService botService)
{
private readonly ILogger<AdminGroup> _logger = logger;
private readonly AzzyBotSettings _settings = settings;
private readonly AzzyBotSettings _settings = settings.Value;
private readonly DbActions _dbActions = dbActions;
private readonly DiscordBotService _botService = botService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AzzyBotSettings> settings) : IAutoCompleteProvider
{
private readonly AzzyBotSettings _settings = settings;
private readonly AzzyBotSettings _settings = settings.Value;

public ValueTask<IEnumerable<DiscordAutoCompleteChoice>> AutoCompleteAsync(AutoCompleteContext context)
{
Expand Down
5 changes: 3 additions & 2 deletions src/AzzyBot.Bot/Commands/Autocompletes/GuildsAutocomplete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AzzyBotSettings> settings, DiscordBotService botService) : IAutoCompleteProvider
{
private readonly AzzyBotSettings _settings = settings;
private readonly AzzyBotSettings _settings = settings.Value;
private readonly DiscordBotService _botService = botService;

public ValueTask<IEnumerable<DiscordAutoCompleteChoice>> AutoCompleteAsync(AutoCompleteContext context)
Expand Down
9 changes: 5 additions & 4 deletions src/AzzyBot.Bot/Commands/CoreCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@
using DSharpPlus.Commands.Processors.SlashCommands.ArgumentModifiers;
using DSharpPlus.Entities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace AzzyBot.Bot.Commands;

[SuppressMessage("Design", "CA1034:Nested types should not be visible", Justification = "DSharpPlus best practice")]
public sealed class CoreCommands
{
[Command("core"), RequireGuild]
public sealed class CoreGroup(ILogger<CoreGroup> logger, AzzyBotSettings settings, DbActions dbActions, DiscordBotService botService)
public sealed class CoreGroup(ILogger<CoreGroup> logger, IOptions<AzzyBotSettings> settings, DbActions dbActions, DiscordBotService botService)
{
private readonly ILogger<CoreGroup> _logger = logger;
private readonly AzzyBotSettings _settings = settings;
private readonly AzzyBotSettings _settings = settings.Value;
private readonly DbActions _dbActions = dbActions;
private readonly DiscordBotService _botService = botService;

Expand Down Expand Up @@ -110,9 +111,9 @@ public async ValueTask HelpAsync
}

[Command("stats")]
public sealed class CoreStats(AppStats stats, ILogger<CoreStats> logger)
public sealed class CoreStats(IOptions<AppStats> stats, ILogger<CoreStats> logger)
{
private readonly AppStats _stats = stats;
private readonly AppStats _stats = stats.Value;
private readonly ILogger<CoreStats> _logger = logger;

[Command("hardware"), Description("Shows information about the hardware side of the bot.")]
Expand Down
20 changes: 4 additions & 16 deletions src/AzzyBot.Bot/Extensions/IConfigurationManagerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/AzzyBot.Bot/Extensions/IServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static void AzzyBotServices(this IServiceCollection services, bool isDev,
services.AddSingleton<MusicStreamingService>();
}

public static void AddAppSettings(this IServiceCollection services)
public static void AddAppSettings(this IServiceCollection services, string settingsFile)
{
services.AddSingleton<IValidateOptions<AzzyBotSettings>, AzzyBotSettingsValidator>().AddOptionsWithValidateOnStart<AzzyBotSettings>();
services.AddSingleton<IValidateOptions<DatabaseSettings>, DatabaseSettingsValidator>().AddOptionsWithValidateOnStart<DatabaseSettings>();
Expand All @@ -108,7 +108,7 @@ public static void AddAppSettings(this IServiceCollection services)
services.AddSingleton<IValidateOptions<CoreUpdaterSettings>, CoreUpdaterValidator>().AddOptionsWithValidateOnStart<CoreUpdaterSettings>();
services.AddSingleton<IValidateOptions<AppStats>, AppStatsValidator>().AddOptionsWithValidateOnStart<AppStats>();

services.AddOptions<AzzyBotSettings>().BindConfiguration(nameof(AzzyBotSettings));
services.AddOptions<AzzyBotSettings>().BindConfiguration(nameof(AzzyBotSettings)).Configure(c => c.SettingsFile = settingsFile);
services.AddOptions<DatabaseSettings>().BindConfiguration(nameof(DatabaseSettings));
services.AddOptions<DiscordStatusSettings>().BindConfiguration(nameof(DiscordStatusSettings));
services.AddOptions<MusicStreamingSettings>().BindConfiguration(nameof(MusicStreamingSettings));
Expand Down
20 changes: 18 additions & 2 deletions src/AzzyBot.Bot/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ 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

#region Add services

try
{
appBuilder.Services.AddAppSettings();
appBuilder.Services.AddAppSettings(settingsFilePath);
appBuilder.Services.AzzyBotServices(isDev, isDocker, logDays);
}
catch (OptionsValidationException ex)
Expand All @@ -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";
}
}

0 comments on commit 6e890bf

Please sign in to comment.