feat: stop/restart reason

This commit is contained in:
ChomeNS
2024-12-25 08:08:01 +07:00
parent 8e84565090
commit eba4f92e45
4 changed files with 20 additions and 12 deletions

View File

@@ -139,11 +139,19 @@ public class Main {
}
// most of these are stolen from HBot
public static void stop (int exitCode) {
public static void stop (int exitCode) { stop(exitCode, null, null); }
public static void stop (int exitCode, String reason) { stop(exitCode, reason, null); }
public static void stop (int exitCode, String reason, String type) {
if (stopping) return;
stopping = true;
final String stoppingMessage = String.format(
"%s (%s)",
type != null ? type : "Stopping..",
reason != null ? reason : "No reason given"
);
executor.shutdown();
executorService.shutdown();
@@ -171,7 +179,7 @@ public class Main {
if (discordEnabled) {
final String channelId = bot.discord.servers.get(bot.host + ":" + bot.port);
final MessageCreateAction messageAction = bot.discord.sendMessageInstantly("Stopping..", channelId, false);
final MessageCreateAction messageAction = bot.discord.sendMessageInstantly(stoppingMessage, channelId, false);
final int finalBotIndex = botIndex;
messageAction.queue(
@@ -180,7 +188,7 @@ public class Main {
);
}
if (ircEnabled) bot.irc.quit("Stopping..");
if (ircEnabled) bot.irc.quit(stoppingMessage);
bot.stop();
} catch (Exception ignored) {}

View File

@@ -14,7 +14,7 @@ public class RestartCommand extends Command {
super(
"restart",
"Gracefully restarts the bot",
new String[] { "" },
new String[] { "[reason]" },
new String[] {},
TrustLevel.OWNER,
false
@@ -23,11 +23,11 @@ public class RestartCommand extends Command {
@Override
public Component execute(CommandContext context) throws CommandException {
context.checkOverloadArgs(0);
final Bot bot = context.bot;
Main.stop(1);
final String reason = context.getString(true, false);
Main.stop(1, reason.isEmpty() ? null : reason, "Restarting..");
return Component.text("Restarting").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
}

View File

@@ -14,7 +14,7 @@ public class StopCommand extends Command {
super(
"stop",
"Gracefully stops the bot",
new String[] { "" },
new String[] { "[reason]" },
new String[] {},
TrustLevel.OWNER,
false
@@ -23,11 +23,11 @@ public class StopCommand extends Command {
@Override
public Component execute(CommandContext context) throws CommandException {
context.checkOverloadArgs(0);
final Bot bot = context.bot;
Main.stop(0);
final String reason = context.getString(true, false);
Main.stop(0, reason.isEmpty() ? null : reason);
return Component.text("Stopping").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
}