refactor: optimize stopping discord
This commit is contained in:
parent
6c507219f2
commit
d3845f7c8a
3 changed files with 50 additions and 40 deletions
|
@ -5,10 +5,8 @@ import me.chayapak1.chomens_bot.plugins.ConsolePlugin;
|
|||
import me.chayapak1.chomens_bot.plugins.DiscordPlugin;
|
||||
import me.chayapak1.chomens_bot.plugins.IRCPlugin;
|
||||
import me.chayapak1.chomens_bot.plugins.LoggerPlugin;
|
||||
import me.chayapak1.chomens_bot.util.ComponentUtilities;
|
||||
import me.chayapak1.chomens_bot.util.HttpUtilities;
|
||||
import me.chayapak1.chomens_bot.util.LoggerUtilities;
|
||||
import me.chayapak1.chomens_bot.util.PersistentDataUtilities;
|
||||
import me.chayapak1.chomens_bot.util.*;
|
||||
import net.dv8tion.jda.api.requests.restaction.MessageCreateAction;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.yaml.snakeyaml.LoaderOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
@ -22,6 +20,7 @@ import java.nio.file.Files;
|
|||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
@ -198,11 +197,9 @@ public class Main {
|
|||
executorService.shutdown();
|
||||
|
||||
try {
|
||||
final boolean executorDone = executor.awaitTermination(5, TimeUnit.SECONDS);
|
||||
final boolean executorServiceDone = executorService.awaitTermination(5, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
final boolean ignoredExecutorDone = executor.awaitTermination(5, TimeUnit.SECONDS);
|
||||
final boolean ignoredExecutorServiceDone = executorService.awaitTermination(5, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException ignored) {}
|
||||
|
||||
ArrayList<Bot> copiedList;
|
||||
synchronized (bots) {
|
||||
|
@ -212,46 +209,42 @@ public class Main {
|
|||
final boolean ircEnabled = config.irc.enabled;
|
||||
final boolean discordEnabled = config.discord.enabled;
|
||||
|
||||
final boolean[] stoppedDiscord = new boolean[copiedList.size()];
|
||||
|
||||
int botIndex = 0;
|
||||
for (Bot bot : copiedList) {
|
||||
try {
|
||||
if (discordEnabled) {
|
||||
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..");
|
||||
|
||||
bot.stop();
|
||||
} catch (Exception ignored) {}
|
||||
|
||||
botIndex++;
|
||||
}
|
||||
|
||||
if (discord.jda != null) discord.jda.shutdown();
|
||||
|
||||
if (discordEnabled) {
|
||||
for (int i = 0; i < 150; i++) {
|
||||
boolean stoppedDiscord = true;
|
||||
|
||||
for (Bot bot : bots) {
|
||||
if (!bot.discord.shuttedDown) {
|
||||
stoppedDiscord = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!stoppedDiscord) {
|
||||
try {
|
||||
Thread.sleep(50);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
try {
|
||||
if (!ArrayUtilities.isAllTrue(stoppedDiscord)) Thread.sleep(50);
|
||||
else break;
|
||||
} catch (InterruptedException ignored) {}
|
||||
}
|
||||
}
|
||||
|
||||
System.exit(69); // nice
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
|
@ -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.hooks.ListenerAdapter;
|
||||
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.JoinConfiguration;
|
||||
import net.kyori.adventure.text.event.ClickEvent;
|
||||
|
@ -360,24 +361,31 @@ public class DiscordPlugin {
|
|||
}
|
||||
}
|
||||
|
||||
public void sendMessageInstantly (String message, String channelId) {
|
||||
if (jda == null) return;
|
||||
public void sendMessageInstantly (String message, String channelId) { sendMessageInstantly(message, channelId, true); }
|
||||
public MessageCreateAction sendMessageInstantly (String message, String channelId, boolean queue) {
|
||||
if (jda == null) return null;
|
||||
|
||||
final TextChannel logChannel = jda.getTextChannelById(channelId);
|
||||
|
||||
if (logChannel == null) {
|
||||
LoggerUtilities.error("Log channel for " + channelId + " is null");
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
logChannel.sendMessage(message).queue(
|
||||
(msg) -> doneSendingInLogs.put(channelId, true),
|
||||
(err) -> {
|
||||
err.printStackTrace();
|
||||
if (queue) {
|
||||
logChannel.sendMessage(message).queue(
|
||||
(msg) -> doneSendingInLogs.put(channelId, true),
|
||||
(err) -> {
|
||||
err.printStackTrace();
|
||||
|
||||
doneSendingInLogs.put(channelId, false);
|
||||
}
|
||||
);
|
||||
doneSendingInLogs.put(channelId, false);
|
||||
}
|
||||
);
|
||||
|
||||
return null;
|
||||
} else {
|
||||
return logChannel.sendMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
public void onDiscordTick(String channelId) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue