Made reload command no longer restart the discord client
This commit is contained in:
parent
3442ec317d
commit
e7377cdece
3 changed files with 51 additions and 26 deletions
|
@ -130,6 +130,6 @@ public class AdminCommands
|
||||||
await LogChannel.Success(command.User.Mention + " reloaded the bot.");
|
await LogChannel.Success(command.User.Mention + " reloaded the bot.");
|
||||||
|
|
||||||
Logger.Log("Reloading bot...");
|
Logger.Log("Reloading bot...");
|
||||||
await SupportChild.Reload();
|
SupportChild.Reload();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -4,6 +4,7 @@ using System.IO;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
using DSharpPlus;
|
using DSharpPlus;
|
||||||
using DSharpPlus.Interactivity;
|
using DSharpPlus.Interactivity;
|
||||||
using DSharpPlus.Interactivity.Enums;
|
using DSharpPlus.Interactivity.Enums;
|
||||||
|
@ -85,9 +86,15 @@ internal static class SupportChild
|
||||||
Logger.Log("Starting " + Assembly.GetEntryAssembly()?.GetName().Name + " version " + GetVersion() + "...");
|
Logger.Log("Starting " + Assembly.GetEntryAssembly()?.GetName().Name + " version " + GetVersion() + "...");
|
||||||
try
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,7 +104,6 @@ internal static class SupportChild
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Logger.Fatal("Fatal error:\n" + e);
|
Logger.Fatal("Fatal error:\n" + e);
|
||||||
Console.ReadLine();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,38 +117,46 @@ internal static class SupportChild
|
||||||
+ " (" + ThisAssembly.Git.Commit + ")";
|
+ " (" + ThisAssembly.Git.Commit + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<bool> Reload()
|
public static bool Reload()
|
||||||
{
|
{
|
||||||
if (client != null)
|
try
|
||||||
{
|
{
|
||||||
await client.DisconnectAsync();
|
Config.LoadConfig();
|
||||||
client.Dispose();
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Logger.Fatal("Unable to read the config file: \"" + commandLineArgs.configPath + "\"", e);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Config.LoadConfig();
|
|
||||||
|
|
||||||
// Check if token is unset
|
// Check if token is unset
|
||||||
if (Config.token is "<add-token-here>" or "")
|
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.");
|
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
|
// Database connection and setup
|
||||||
try
|
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.SetConnectionString(Config.hostName, Config.port, Config.database, Config.username, Config.password);
|
||||||
Database.SetupTables();
|
Database.SetupTables();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.Log("Setting up Discord client...");
|
return true;
|
||||||
DiscordClientBuilder clientBuilder = DiscordClientBuilder.CreateDefault(Config.token, DiscordIntents.All).SetReconnectOnFatalGatewayErrors();
|
}
|
||||||
|
|
||||||
|
private static async Task<bool> Connect()
|
||||||
|
{
|
||||||
|
Logger.Log("Setting up Discord client.");
|
||||||
|
DiscordClientBuilder clientBuilder = DiscordClientBuilder.CreateDefault(Config.token, DiscordIntents.All)
|
||||||
|
.SetReconnectOnFatalGatewayErrors();
|
||||||
|
|
||||||
clientBuilder.ConfigureServices(configure =>
|
clientBuilder.ConfigureServices(configure =>
|
||||||
{
|
{
|
||||||
|
@ -204,7 +218,7 @@ internal static class SupportChild
|
||||||
]);
|
]);
|
||||||
extension.AddProcessor(new SlashCommandProcessor());
|
extension.AddProcessor(new SlashCommandProcessor());
|
||||||
extension.CommandErrored += EventHandler.OnCommandError;
|
extension.CommandErrored += EventHandler.OnCommandError;
|
||||||
}, new CommandsConfiguration()
|
}, new CommandsConfiguration
|
||||||
{
|
{
|
||||||
RegisterDefaultCommandProcessors = false,
|
RegisterDefaultCommandProcessors = false,
|
||||||
UseDefaultCommandErrorHandler = false
|
UseDefaultCommandErrorHandler = false
|
||||||
|
@ -223,26 +237,37 @@ internal static class SupportChild
|
||||||
|
|
||||||
client = clientBuilder.Build();
|
client = clientBuilder.Build();
|
||||||
|
|
||||||
Logger.Log("Connecting to Discord...");
|
Logger.Log("Connecting to Discord.");
|
||||||
EventHandler.hasLoggedGuilds = false;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class ErrorHandler : IClientErrorHandler
|
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);
|
Logger.Error("Client exception occured:\n" + exception);
|
||||||
switch (exception)
|
if (exception is BadRequestException ex)
|
||||||
{
|
{
|
||||||
case BadRequestException ex:
|
Logger.Error("JSON Message: " + ex.JsonMessage);
|
||||||
Logger.Error("JSON Message: " + ex.JsonMessage);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ValueTask.FromException(exception);
|
return ValueTask.FromException(exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
bot:
|
bot:
|
||||||
# Bot token.
|
# Bot token. Changing this requires a bot restart, it will not update when reloading.
|
||||||
token: "<add-token-here>"
|
token: "<add-token-here>"
|
||||||
|
|
||||||
# Channel where ticket logs are posted (recommended).
|
# Channel where ticket logs are posted (recommended).
|
||||||
|
|
Loading…
Add table
Reference in a new issue