public async Task Restart() { _logger.LogInformation("Shutdown triggered by 'restart' command. Application will shut down in 10 seconds ..."); await RespondAsync("Shutting down in 10 seconds. Restart will be performed automatically."); _ = Task.Run(async() => { await Task.Delay(TimeSpan.FromSeconds(10)); ApplicationLifecycle.End(); }).ConfigureAwait(false); }
private async Task DisconnectedHandler() { const int delayInSeconds = 30; _logger.LogWarning("Bot disconnected. Checking for connection status in {DelayInSeconds} seconds.", delayInSeconds); // The Discord connection might get lost, but should reconnect automatically. // In rare cases, the reconnect doesn't work and the bot will just idle. // Therefore, after a disconnect, we grant the connection 30 seconds to recover. await Task.Delay(TimeSpan.FromSeconds(delayInSeconds)); if (_discordAccess.IsConnected) { _logger.LogInformation("Bot re-connected successfully."); return; } // If it doesn't recover during this period of time, we'll kill the process. _logger.LogWarning("Failed to recover connection to Discord. Killing the process."); // Give the logger some time to log the message await Task.Delay(TimeSpan.FromSeconds(5)); // Finally kill the process to start over ApplicationLifecycle.End(); }