feat: added ShutdownHook

This commit is contained in:
limited_dev 2023-01-24 21:14:01 +01:00
parent 096778c0c7
commit bda16fbdb7
2 changed files with 29 additions and 12 deletions

View file

@ -20,6 +20,10 @@ import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.GuildVoiceState;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.managers.AudioManager;
import net.dv8tion.jda.api.requests.GatewayIntent;
public class Main {
@ -55,6 +59,13 @@ public class Main {
throw new RuntimeException(e);
}
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run() {
shutdown();
}
});
MapManager.registerMaps();
WeaponManager.registerWeapons();
TimePlanner.registerMessageThread();
@ -65,6 +76,22 @@ public class Main {
ConsoleCommandManager.registerListener();
}
public static void shutdown(){
lgr.consoleOut("Shutting down...");
for(Guild g : jda.getSelfUser().getMutualGuilds()){
Member self = g.getSelfMember();
GuildVoiceState selfState = self.getVoiceState();
if(selfState.inAudioChannel()){
AudioManager audioManager = g.getAudioManager();
lgr.consoleOut("Leaving VC " + audioManager.getConnectedChannel().asVoiceChannel().getName() + " in " + g.getName());
audioManager.closeAudioConnection();
}
}
TimePlanner.schedulerService.shutdown();
jda.shutdown();
lgr.consoleOut("Done.. Exiting..");
}
public static JDA getJda() {
return jda;
}

View file

@ -1,5 +1,6 @@
package de.limited_dev.lil_judd.commands.consolecommand.system;
import de.limited_dev.lil_judd.Main;
import de.limited_dev.lil_judd.commands.consolecommand.component.ConsoleCommand;
import de.limited_dev.lil_judd.features.planner.TimePlanner;
import net.dv8tion.jda.api.entities.Guild;
@ -14,17 +15,6 @@ public class ShutdownCommand extends ConsoleCommand {
@Override
public void onCommand(String[] args) {
lgr.consoleOut("Shutting down...");
for(Guild g : jda.getSelfUser().getMutualGuilds()){
Member self = g.getSelfMember();
GuildVoiceState selfState = self.getVoiceState();
if(selfState.inAudioChannel()){
AudioManager audioManager = g.getAudioManager();
lgr.consoleOut("Leaving VC " + audioManager.getConnectedChannel().asVoiceChannel().getName() + " in " + g.getName());
audioManager.closeAudioConnection();
}
}
TimePlanner.schedulerService.shutdown();
jda.shutdown();
Main.shutdown();
}
}