Add command to stop bot process
All checks were successful
continuous-integration/drone/push Build is passing

Previously, anyone could send the keywords in chat and kill the bot. Now, only the set bot owner can run the command.
This commit is contained in:
Bea 2022-11-20 05:57:42 +01:00
parent bd76562bcc
commit dd4ffe252e
4 changed files with 24 additions and 13 deletions

View File

@ -11,6 +11,9 @@ public class Configuration
private static boolean paused = false; private static boolean paused = false;
private static String prefix = "."; 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. * 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 void setPaused(boolean p) { paused = p; }
public static long getBotOwnerId() { return botOwnerId; }
} }

View File

@ -53,17 +53,6 @@ public class MessageListener extends ListenerAdapter
return; 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")) if(eventMessage.equalsIgnoreCase("hideko verbose"))
{ {
MessageChannel channel = event.getChannel(); MessageChannel channel = event.getChannel();

View File

@ -10,10 +10,14 @@ import net.dv8tion.jda.api.interactions.InteractionHook;
import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import wtf.beatrice.hidekobot.Configuration;
import wtf.beatrice.hidekobot.HidekoBot;
import wtf.beatrice.hidekobot.utils.RandomUtil; import wtf.beatrice.hidekobot.utils.RandomUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class SlashCommandListener extends ListenerAdapter public class SlashCommandListener extends ListenerAdapter
{ {
@ -26,6 +30,17 @@ public class SlashCommandListener extends ListenerAdapter
event.reply("Pong!").queue(); 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")) else if (event.getName().equals("coinflip"))
{ {
int rand = RandomUtil.getRandomNumber(0, 1); int rand = RandomUtil.getRandomNumber(0, 1);

View File

@ -20,11 +20,13 @@ public class SlashCommandsUtil
static List<CommandData> allCommands = new ArrayList<>() static List<CommandData> allCommands = new ArrayList<>()
{{ {{
add(Commands.slash("ping", "Test if the bot is responsive.")); add(Commands.slash("die", "Stop the bot's process")
add(Commands.slash("coinflip", "Flip a coin and get head or tails.")); .setDefaultPermissions(DefaultMemberPermissions.DISABLED));
add(Commands.slash("clear", "Clear the current channel's chat.") add(Commands.slash("clear", "Clear the current channel's chat.")
.addOption(OptionType.INTEGER, "amount", "The amount of messages to delete.") .addOption(OptionType.INTEGER, "amount", "The amount of messages to delete.")
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.MESSAGE_MANAGE))); .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() public static void updateSlashCommands()