hopefully fix random disconnects that don't try to restart?

This commit is contained in:
sam 2024-10-25 03:35:37 +02:00
parent 9302ba200f
commit a5a8bb34c6
Signed by: sam
GPG key ID: 5F3C3C1B3166639D

View file

@ -17,13 +17,26 @@ using Remora.Discord.Gateway.Results;
namespace Catalogger.Backend.Bot; namespace Catalogger.Backend.Bot;
public class ShardedDiscordService(ShardedGatewayClient client, IHostApplicationLifetime lifetime) public class ShardedDiscordService(
: BackgroundService ILogger logger,
ShardedGatewayClient client,
IHostApplicationLifetime lifetime
) : BackgroundService
{ {
private readonly ILogger _logger = logger.ForContext<ShardedDiscordService>();
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{ {
var result = await client.RunAsync(stoppingToken); while (!stoppingToken.IsCancellationRequested)
if (result.Error is GatewayError { IsCritical: true }) {
lifetime.StopApplication(); _logger.Information("Starting sharded Discord client");
var result = await client.RunAsync(stoppingToken);
_logger.Information("Discord client finished running");
if (result.Error is GatewayError { IsCritical: true })
{
lifetime.StopApplication();
break;
}
}
} }
} }