refactor: optimize stopping discord

This commit is contained in:
Chayapak 2024-11-19 17:42:11 +07:00
parent 6c507219f2
commit d3845f7c8a
Signed by: ChomeNS
SSH key fingerprint: SHA256:0YoxhdyXsgbc0nfeB2N6FYE60mxMU7DS4uCUMaw2mvA
3 changed files with 50 additions and 40 deletions

View file

@ -5,10 +5,8 @@ import me.chayapak1.chomens_bot.plugins.ConsolePlugin;
import me.chayapak1.chomens_bot.plugins.DiscordPlugin; import me.chayapak1.chomens_bot.plugins.DiscordPlugin;
import me.chayapak1.chomens_bot.plugins.IRCPlugin; import me.chayapak1.chomens_bot.plugins.IRCPlugin;
import me.chayapak1.chomens_bot.plugins.LoggerPlugin; import me.chayapak1.chomens_bot.plugins.LoggerPlugin;
import me.chayapak1.chomens_bot.util.ComponentUtilities; import me.chayapak1.chomens_bot.util.*;
import me.chayapak1.chomens_bot.util.HttpUtilities; import net.dv8tion.jda.api.requests.restaction.MessageCreateAction;
import me.chayapak1.chomens_bot.util.LoggerUtilities;
import me.chayapak1.chomens_bot.util.PersistentDataUtilities;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
@ -22,6 +20,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -198,11 +197,9 @@ public class Main {
executorService.shutdown(); executorService.shutdown();
try { try {
final boolean executorDone = executor.awaitTermination(5, TimeUnit.SECONDS); final boolean ignoredExecutorDone = executor.awaitTermination(5, TimeUnit.SECONDS);
final boolean executorServiceDone = executorService.awaitTermination(5, TimeUnit.SECONDS); final boolean ignoredExecutorServiceDone = executorService.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) { } catch (InterruptedException ignored) {}
e.printStackTrace();
}
ArrayList<Bot> copiedList; ArrayList<Bot> copiedList;
synchronized (bots) { synchronized (bots) {
@ -212,46 +209,42 @@ public class Main {
final boolean ircEnabled = config.irc.enabled; final boolean ircEnabled = config.irc.enabled;
final boolean discordEnabled = config.discord.enabled; final boolean discordEnabled = config.discord.enabled;
final boolean[] stoppedDiscord = new boolean[copiedList.size()];
int botIndex = 0;
for (Bot bot : copiedList) { for (Bot bot : copiedList) {
try { try {
if (discordEnabled) { if (discordEnabled) {
final String channelId = bot.discord.servers.get(bot.host + ":" + bot.port); final String channelId = bot.discord.servers.get(bot.host + ":" + bot.port);
bot.discord.sendMessageInstantly("Stopping..", channelId); final MessageCreateAction messageAction = bot.discord.sendMessageInstantly("Stopping..", channelId, false);
final int finalBotIndex = botIndex;
messageAction.queue(
(message) -> stoppedDiscord[finalBotIndex] = true,
(error) -> stoppedDiscord[finalBotIndex] = true // should i also set this to true on fail?
);
} }
if (ircEnabled) bot.irc.quit("Stopping.."); if (ircEnabled) bot.irc.quit("Stopping..");
bot.stop(); bot.stop();
} catch (Exception ignored) {} } catch (Exception ignored) {}
botIndex++;
} }
if (discord.jda != null) discord.jda.shutdown(); if (discord.jda != null) discord.jda.shutdown();
if (discordEnabled) { if (discordEnabled) {
for (int i = 0; i < 150; i++) { for (int i = 0; i < 150; i++) {
boolean stoppedDiscord = true; try {
if (!ArrayUtilities.isAllTrue(stoppedDiscord)) Thread.sleep(50);
for (Bot bot : bots) { else break;
if (!bot.discord.shuttedDown) { } catch (InterruptedException ignored) {}
stoppedDiscord = false;
break;
}
}
if (!stoppedDiscord) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
} else {
break;
}
} }
} }
System.exit(69); // nice System.exit(0);
} }
} }

View file

@ -16,6 +16,7 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.session.ShutdownEvent; import net.dv8tion.jda.api.events.session.ShutdownEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.requests.GatewayIntent; import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.requests.restaction.MessageCreateAction;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration; import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.ClickEvent;
@ -360,24 +361,31 @@ public class DiscordPlugin {
} }
} }
public void sendMessageInstantly (String message, String channelId) { public void sendMessageInstantly (String message, String channelId) { sendMessageInstantly(message, channelId, true); }
if (jda == null) return; public MessageCreateAction sendMessageInstantly (String message, String channelId, boolean queue) {
if (jda == null) return null;
final TextChannel logChannel = jda.getTextChannelById(channelId); final TextChannel logChannel = jda.getTextChannelById(channelId);
if (logChannel == null) { if (logChannel == null) {
LoggerUtilities.error("Log channel for " + channelId + " is null"); LoggerUtilities.error("Log channel for " + channelId + " is null");
return; return null;
} }
logChannel.sendMessage(message).queue( if (queue) {
(msg) -> doneSendingInLogs.put(channelId, true), logChannel.sendMessage(message).queue(
(err) -> { (msg) -> doneSendingInLogs.put(channelId, true),
err.printStackTrace(); (err) -> {
err.printStackTrace();
doneSendingInLogs.put(channelId, false); doneSendingInLogs.put(channelId, false);
} }
); );
return null;
} else {
return logChannel.sendMessage(message);
}
} }
public void onDiscordTick(String channelId) { public void onDiscordTick(String channelId) {

View file

@ -0,0 +1,9 @@
package me.chayapak1.chomens_bot.util;
public class ArrayUtilities {
public static boolean isAllTrue (boolean[] array) {
for (boolean value : array) if (!value) return false;
return true;
}
}