diff --git a/Commands/AdminCommands.cs b/Commands/AdminCommands.cs index 9c79f82..f395aaf 100644 --- a/Commands/AdminCommands.cs +++ b/Commands/AdminCommands.cs @@ -130,6 +130,6 @@ public class AdminCommands await LogChannel.Success(command.User.Mention + " reloaded the bot."); Logger.Log("Reloading bot..."); - await SupportChild.Reload(); + SupportChild.Reload(); } } \ No newline at end of file diff --git a/SupportChild.cs b/SupportChild.cs index 183ae85..8893e7c 100644 --- a/SupportChild.cs +++ b/SupportChild.cs @@ -4,6 +4,7 @@ using System.IO; using System.Reflection; using System.Threading.Tasks; using System.Linq; +using System.Threading; using DSharpPlus; using DSharpPlus.Interactivity; using DSharpPlus.Interactivity.Enums; @@ -85,9 +86,15 @@ internal static class SupportChild Logger.Log("Starting " + Assembly.GetEntryAssembly()?.GetName().Name + " version " + GetVersion() + "..."); try { - if (!await Reload()) + if (!Reload()) { - Logger.Fatal("Aborting startup due to a fatal error..."); + Logger.Fatal("Aborting startup due to a fatal error."); + return; + } + + if (!await Connect()) + { + Logger.Fatal("Aborting startup due to a fatal error when trying to connect to Discord."); return; } @@ -97,7 +104,6 @@ internal static class SupportChild catch (Exception e) { Logger.Fatal("Fatal error:\n" + e); - Console.ReadLine(); } } @@ -111,38 +117,46 @@ internal static class SupportChild + " (" + ThisAssembly.Git.Commit + ")"; } - public static async Task<bool> Reload() + public static bool Reload() { - if (client != null) + try { - await client.DisconnectAsync(); - client.Dispose(); + Config.LoadConfig(); + } + catch (Exception e) + { + Logger.Fatal("Unable to read the config file: \"" + commandLineArgs.configPath + "\"", e); + return false; } - - Config.LoadConfig(); // Check if token is unset if (Config.token is "<add-token-here>" or "") { Logger.Fatal("You need to set your bot token in the config and start the bot again."); - throw new ArgumentException("Invalid Discord bot token"); + return false; } // Database connection and setup try { - Logger.Log("Connecting to database... (" + Config.hostName + ":" + Config.port + ")"); + Logger.Log("Connecting to database. (" + Config.hostName + ":" + Config.port + ")"); Database.SetConnectionString(Config.hostName, Config.port, Config.database, Config.username, Config.password); Database.SetupTables(); } catch (Exception e) { - Logger.Fatal("Could not set up database tables, please confirm connection settings, status of the server and permissions of MySQL user. Error: ", e); + Logger.Fatal("Could not set up database tables, please confirm connection settings, status of the server and permissions of MySQL user.", e); return false; } - Logger.Log("Setting up Discord client..."); - DiscordClientBuilder clientBuilder = DiscordClientBuilder.CreateDefault(Config.token, DiscordIntents.All).SetReconnectOnFatalGatewayErrors(); + return true; + } + + private static async Task<bool> Connect() + { + Logger.Log("Setting up Discord client."); + DiscordClientBuilder clientBuilder = DiscordClientBuilder.CreateDefault(Config.token, DiscordIntents.All) + .SetReconnectOnFatalGatewayErrors(); clientBuilder.ConfigureServices(configure => { @@ -204,7 +218,7 @@ internal static class SupportChild ]); extension.AddProcessor(new SlashCommandProcessor()); extension.CommandErrored += EventHandler.OnCommandError; - }, new CommandsConfiguration() + }, new CommandsConfiguration { RegisterDefaultCommandProcessors = false, UseDefaultCommandErrorHandler = false @@ -223,26 +237,37 @@ internal static class SupportChild client = clientBuilder.Build(); - Logger.Log("Connecting to Discord..."); + Logger.Log("Connecting to Discord."); EventHandler.hasLoggedGuilds = false; - await client.ConnectAsync(); + + try + { + await client.ConnectAsync(); + } + catch (Exception e) + { + Logger.Fatal("Error occured while connecting to Discord.", e); + return false; + } + return true; } } internal class ErrorHandler : IClientErrorHandler { - public ValueTask HandleEventHandlerError(string name, Exception exception, Delegate invokedDelegate, object sender, object args) + public ValueTask HandleEventHandlerError(string name, + Exception exception, + Delegate invokedDelegate, + object sender, + object args) { Logger.Error("Client exception occured:\n" + exception); - switch (exception) + if (exception is BadRequestException ex) { - case BadRequestException ex: - Logger.Error("JSON Message: " + ex.JsonMessage); - break; - default: - break; + Logger.Error("JSON Message: " + ex.JsonMessage); } + return ValueTask.FromException(exception); } diff --git a/default_config.yml b/default_config.yml index e0ece3b..b05cced 100644 --- a/default_config.yml +++ b/default_config.yml @@ -1,5 +1,5 @@ bot: - # Bot token. + # Bot token. Changing this requires a bot restart, it will not update when reloading. token: "<add-token-here>" # Channel where ticket logs are posted (recommended).