Skip to content

Commit

Permalink
Refactor InteractionServiceHandler to user IHostedService
Browse files Browse the repository at this point in the history
InteractionServiceHandler now uses the IHostedService interface instead of a custom interface. This allows Bot.cs to not need to take care of the InteractionServiceHandler, as it can do it itself.
  • Loading branch information
HEJOK254 committed Oct 29, 2024
1 parent a38e6bc commit 89e6580
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
19 changes: 1 addition & 18 deletions Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@

namespace QuickEdit;

internal sealed class Bot(DiscordSocketClient client, Config.DiscordConfig discordConfig, IInteractionServiceHandler interactionServiceHandler, IHostApplicationLifetime appLifetime) : IHostedService
internal sealed class Bot(DiscordSocketClient client, Config.DiscordConfig discordConfig, IHostApplicationLifetime appLifetime) : IHostedService
{
private readonly DiscordSocketClient _client = client;
private readonly Config.DiscordConfig _discordConfig = discordConfig;
private readonly IInteractionServiceHandler _interactionServiceHandler = interactionServiceHandler;
private readonly IHostApplicationLifetime _appLifetime = appLifetime;

public async Task StartAsync(CancellationToken cancellationToken)
{
_client.Log += AutoLog.LogMessage;
_client.Ready += OnReadyAsync;

// Try-catch ValidateToken since LoginAsync doesn't throw exceptions, they just catch them
// So there's no way to know if the token is invalid without checking it first (or is there?)
Expand Down Expand Up @@ -66,19 +64,4 @@ public async Task StopAsync(CancellationToken cancellationToken)
await _client.LogoutAsync();
await _client.StopAsync();
}

private async Task OnReadyAsync()
{
try
{
await _interactionServiceHandler.InitAsync();
}
catch
{
Log.Fatal("Program is exiting due to an error in InteractionServiceHandler.");
// The program cannot continue without the InteractionService, so terminate it. Nothing important should be running at this point.
Environment.ExitCode = 1;
_appLifetime.StopApplication();
}
}
}
29 changes: 19 additions & 10 deletions Commands/InteractionServiceHandler.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
using Discord;
using Discord.Interactions;
using Discord.WebSocket;
using Microsoft.Extensions.Hosting;
using Serilog;

namespace QuickEdit.Commands;

internal interface IInteractionServiceHandler
{
Task InitAsync();
}

internal sealed class DefaultInteractionServiceHandler(DiscordSocketClient client, InteractionService interactionService, InteractionServiceConfig interactionServiceConfig) : IInteractionServiceHandler
internal sealed class InteractionServiceHandler(DiscordSocketClient client, InteractionService interactionService, InteractionServiceConfig interactionServiceConfig, IHostApplicationLifetime appLifetime) : IHostedService
{
private readonly DiscordSocketClient _client = client;
private InteractionService _interactionService = interactionService;
private readonly InteractionServiceConfig _interactionServiceConfig = interactionServiceConfig;
private readonly IHostApplicationLifetime _appLifetime = appLifetime;
private static readonly SemaphoreSlim _initSemaphore = new(1);
private static bool isReady = false;

public Task StartAsync(CancellationToken cancellationToken)
{
_client.Ready += InitAsync;
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
_interactionService?.Dispose();
return Task.CompletedTask;
}

/// <summary>
/// Initialize the InteractionService
/// </summary>
public async Task InitAsync()
private async Task InitAsync()
{
await _initSemaphore.WaitAsync();

// Prevent reinitialization
// Prevent re-initialization
if (isReady)
return;

Expand All @@ -42,7 +50,8 @@ public async Task InitAsync()
catch (Exception e)
{
Log.Fatal("Error initializing InteractionService: {e}", e);
throw;
Environment.ExitCode = 1; // TODO: Maybe implement different exit codes in the future
_appLifetime.StopApplication();
}
finally
{
Expand Down
2 changes: 1 addition & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private static bool ConfigureServices(IServiceCollection services)
services.AddSingleton(interactionServiceConfig);
services.AddSingleton<DiscordSocketClient>();
services.AddSingleton(x => new InteractionService(x.GetRequiredService<DiscordSocketClient>(), interactionServiceConfig));
services.AddSingleton<IInteractionServiceHandler, DefaultInteractionServiceHandler>();
services.AddHostedService<InteractionServiceHandler>();
services.AddHostedService<Bot>();
return true;
}
Expand Down

0 comments on commit 89e6580

Please sign in to comment.