From a5a8bb34c6afccd7b77a29f77ab910be52cc790d Mon Sep 17 00:00:00 2001 From: sam Date: Fri, 25 Oct 2024 03:35:37 +0200 Subject: [PATCH] hopefully fix random disconnects that don't try to restart? --- .../Bot/ShardedDiscordService.cs | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Catalogger.Backend/Bot/ShardedDiscordService.cs b/Catalogger.Backend/Bot/ShardedDiscordService.cs index 38ad54e..06453ce 100644 --- a/Catalogger.Backend/Bot/ShardedDiscordService.cs +++ b/Catalogger.Backend/Bot/ShardedDiscordService.cs @@ -17,13 +17,26 @@ using Remora.Discord.Gateway.Results; namespace Catalogger.Backend.Bot; -public class ShardedDiscordService(ShardedGatewayClient client, IHostApplicationLifetime lifetime) - : BackgroundService +public class ShardedDiscordService( + ILogger logger, + ShardedGatewayClient client, + IHostApplicationLifetime lifetime +) : BackgroundService { + private readonly ILogger _logger = logger.ForContext(); + protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - var result = await client.RunAsync(stoppingToken); - if (result.Error is GatewayError { IsCritical: true }) - lifetime.StopApplication(); + while (!stoppingToken.IsCancellationRequested) + { + _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; + } + } } }