Compare commits

..

2 Commits

Author SHA1 Message Date
fddabae3c3 Make commands util class grab API instance
All checks were successful
continuous-integration/drone/push Build is passing
Instead of passing it as an argument, let the class grab the instance itself.
2022-11-20 03:18:14 +01:00
8b9ce25684 Add shutdown interrupt signal listener
This way, we can nicely close the API connection and perform general cleanup.
2022-11-20 03:17:37 +01:00
3 changed files with 20 additions and 10 deletions

View File

@ -5,8 +5,8 @@ import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus; import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent; import net.dv8tion.jda.api.requests.GatewayIntent;
import sun.misc.Signal;
import wtf.beatrice.hidekobot.listeners.MessageListener; import wtf.beatrice.hidekobot.listeners.MessageListener;
import wtf.beatrice.hidekobot.listeners.MessageLogger;
import wtf.beatrice.hidekobot.listeners.SlashCommandListener; import wtf.beatrice.hidekobot.listeners.SlashCommandListener;
import wtf.beatrice.hidekobot.utils.Logger; import wtf.beatrice.hidekobot.utils.Logger;
import wtf.beatrice.hidekobot.utils.SlashCommandsUtil; import wtf.beatrice.hidekobot.utils.SlashCommandsUtil;
@ -15,6 +15,8 @@ import javax.security.auth.login.LoginException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class HidekoBot public class HidekoBot
{ {
@ -70,8 +72,8 @@ public class HidekoBot
jda.addEventListener(new MessageListener()); jda.addEventListener(new MessageListener());
jda.addEventListener(new SlashCommandListener()); jda.addEventListener(new SlashCommandListener());
// update slash commands // update slash commands (delayed)
SlashCommandsUtil.updateSlashCommands(jda); Executors.newSingleThreadScheduledExecutor().schedule(SlashCommandsUtil::updateSlashCommands, 5, TimeUnit.SECONDS);
// set the bot's status // set the bot's status
jda.getPresence().setStatus(OnlineStatus.ONLINE); jda.getPresence().setStatus(OnlineStatus.ONLINE);
@ -88,6 +90,9 @@ public class HidekoBot
// print the bot logo. // print the bot logo.
logger.log("\n\n" + logger.getLogo() + "\nv" + version + " - bot is ready!\n", 2); logger.log("\n\n" + logger.getLogo() + "\nv" + version + " - bot is ready!\n", 2);
// register shutdown interrupt signal listener for proper shutdown.
Signal.handle(new Signal("INT"), signal -> shutdown());
// log the invite-link to console so noob users can just click on it. // log the invite-link to console so noob users can just click on it.
logger.log("Bot User ID: " + botUserId, 3); logger.log("Bot User ID: " + botUserId, 3);
logger.log("Invite Link: " + standardInviteLink, 4); logger.log("Invite Link: " + standardInviteLink, 4);
@ -98,4 +103,11 @@ public class HidekoBot
return jda; return jda;
} }
public static void shutdown()
{
logger.log("WARNING! Shutting down!");
jda.shutdown();
System.exit(0);
}
} }

View File

@ -111,12 +111,7 @@ public class MessageListener extends ListenerAdapter
channel.sendMessage("Going to sleep! Cya :sparkles:").queue(); channel.sendMessage("Going to sleep! Cya :sparkles:").queue();
Executors.newSingleThreadScheduledExecutor().schedule(() -> Executors.newSingleThreadScheduledExecutor().schedule(HidekoBot::shutdown, 3, TimeUnit.SECONDS);
{
logger.log("WARNING! Shutting down!");
HidekoBot.getAPI().shutdown();
System.exit(0);
}, 5, TimeUnit.SECONDS);
return; return;
} }

View File

@ -4,6 +4,7 @@ import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.interactions.commands.Command; import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands; import net.dv8tion.jda.api.interactions.commands.build.Commands;
import wtf.beatrice.hidekobot.HidekoBot;
import wtf.beatrice.hidekobot.listeners.MessageListener; import wtf.beatrice.hidekobot.listeners.MessageListener;
import java.util.ArrayList; import java.util.ArrayList;
@ -20,8 +21,10 @@ public class SlashCommandsUtil
add(Commands.slash("coinflip", "Flip a coin and get head or tails.")); add(Commands.slash("coinflip", "Flip a coin and get head or tails."));
}}; }};
public static void updateSlashCommands(JDA jdaInstance) public static void updateSlashCommands()
{ {
JDA jdaInstance = HidekoBot.getAPI();
List<CommandData> toAdd = new ArrayList<>(); List<CommandData> toAdd = new ArrayList<>();
List<Command> toDelete = new ArrayList<>(); List<Command> toDelete = new ArrayList<>();