diff --git a/Bot.cs b/Bot.cs index c346853..e19e5ba 100644 --- a/Bot.cs +++ b/Bot.cs @@ -1,5 +1,3 @@ -using System; -using System.Diagnostics; using Discord; using Discord.WebSocket; using Microsoft.Extensions.Hosting; @@ -21,8 +19,36 @@ public async Task StartAsync(CancellationToken cancellationToken) _client.Log += AutoLog.LogMessage; _client.Ready += OnReadyAsync; - await _client.LoginAsync(TokenType.Bot, _discordConfig.Token); - await _client.StartAsync(); + // 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?) + // Also this is separate from the other try-catch to make sure it's the token that's invalid + try + { + TokenUtils.ValidateToken(TokenType.Bot, _discordConfig.Token); + } + catch (ArgumentException e) + { + Log.Fatal("{e}", e.Message); + Environment.ExitCode = 1; + _appLifetime.StopApplication(); + return; // The app would normally continue for a short amount of time before stopping + } + + // Most of the exceptions are caught by the library :( [maybe not idk i'm writing this at 2am] + // This means that the program will log stuff in an ugly way and NOT stop the program on fatal errors + try + { + // The token is already validated, so there's no need to validate it again + await _client.LoginAsync(TokenType.Bot, _discordConfig.Token, validateToken: false); + await _client.StartAsync(); + } + catch (Exception e) + { + Log.Fatal("Failed to start the bot: {e}", e); + Environment.ExitCode = 1; + _appLifetime.StopApplication(); + return; + } // Custom activities use a different method if (_discordConfig.StatusType == ActivityType.CustomStatus)