From dd4ffe252e4d03d84aaa0c3ddd0d8453e554f40e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Sun, 20 Nov 2022 05:57:42 +0100 Subject: [PATCH] Add command to stop bot process Previously, anyone could send the keywords in chat and kill the bot. Now, only the set bot owner can run the command. --- .../wtf/beatrice/hidekobot/Configuration.java | 5 +++++ .../hidekobot/listeners/MessageListener.java | 11 ----------- .../hidekobot/listeners/SlashCommandListener.java | 15 +++++++++++++++ .../hidekobot/utils/SlashCommandsUtil.java | 6 ++++-- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/main/java/wtf/beatrice/hidekobot/Configuration.java b/src/main/java/wtf/beatrice/hidekobot/Configuration.java index c5297d2..704fbd3 100644 --- a/src/main/java/wtf/beatrice/hidekobot/Configuration.java +++ b/src/main/java/wtf/beatrice/hidekobot/Configuration.java @@ -11,6 +11,9 @@ public class Configuration private static boolean paused = false; private static String prefix = "."; + // todo: allow people to set their own user id + private static final long botOwnerId = 979809420714332260L; + /** * Checks if the bot has been started with the verbose argument. @@ -61,4 +64,6 @@ public class Configuration */ public static void setPaused(boolean p) { paused = p; } + public static long getBotOwnerId() { return botOwnerId; } + } diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java index 416c748..2c723e4 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java @@ -53,17 +53,6 @@ public class MessageListener extends ListenerAdapter return; } - if(eventMessage.equalsIgnoreCase("hideko die")) - { - MessageChannel channel = event.getChannel(); - - channel.sendMessage("Going to sleep! Cya :sparkles:").queue(); - - Executors.newSingleThreadScheduledExecutor().schedule(HidekoBot::shutdown, 3, TimeUnit.SECONDS); - - return; - } - if(eventMessage.equalsIgnoreCase("hideko verbose")) { MessageChannel channel = event.getChannel(); diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java index 664c3f8..dfc6118 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java @@ -10,10 +10,14 @@ import net.dv8tion.jda.api.interactions.InteractionHook; import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; import org.jetbrains.annotations.NotNull; +import wtf.beatrice.hidekobot.Configuration; +import wtf.beatrice.hidekobot.HidekoBot; import wtf.beatrice.hidekobot.utils.RandomUtil; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; public class SlashCommandListener extends ListenerAdapter { @@ -26,6 +30,17 @@ public class SlashCommandListener extends ListenerAdapter event.reply("Pong!").queue(); } + else if (event.getName().equals("die")) + { + if(Configuration.getBotOwnerId() != event.getMember().getIdLong()) + { + event.reply("Sorry, only the bot owner can run this command!").setEphemeral(true).queue(); + } else { + event.reply("Going to sleep! Cya :sparkles:").queue(); + Executors.newSingleThreadScheduledExecutor().schedule(HidekoBot::shutdown, 3, TimeUnit.SECONDS); + } + } + else if (event.getName().equals("coinflip")) { int rand = RandomUtil.getRandomNumber(0, 1); diff --git a/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java b/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java index d666898..e6988d8 100644 --- a/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java +++ b/src/main/java/wtf/beatrice/hidekobot/utils/SlashCommandsUtil.java @@ -20,11 +20,13 @@ public class SlashCommandsUtil static List allCommands = new ArrayList<>() {{ - add(Commands.slash("ping", "Test if the bot is responsive.")); - add(Commands.slash("coinflip", "Flip a coin and get head or tails.")); + add(Commands.slash("die", "Stop the bot's process") + .setDefaultPermissions(DefaultMemberPermissions.DISABLED)); add(Commands.slash("clear", "Clear the current channel's chat.") .addOption(OptionType.INTEGER, "amount", "The amount of messages to delete.") .setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.MESSAGE_MANAGE))); + add(Commands.slash("coinflip", "Flip a coin and get head or tails.")); + add(Commands.slash("ping", "Test if the bot is responsive.")); }}; public static void updateSlashCommands()