diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index 8a65b49..bb617d1 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -116,6 +116,7 @@ public class HidekoBot MessageCommandListener messageCommandListener = new MessageCommandListener(); messageCommandListener.registerCommand(new HelloCommand()); messageCommandListener.registerCommand(new CommandsCommand()); + messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.CoinFlipCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.ClearCommand()); Cache.setMessageCommandListener(messageCommandListener); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/base/CoinFlip.java b/src/main/java/wtf/beatrice/hidekobot/commands/base/CoinFlip.java new file mode 100644 index 0000000..b39f1df --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/commands/base/CoinFlip.java @@ -0,0 +1,69 @@ +package wtf.beatrice.hidekobot.commands.base; + +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.entities.emoji.Emoji; +import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; +import net.dv8tion.jda.api.interactions.components.ActionRow; +import net.dv8tion.jda.api.interactions.components.buttons.Button; +import wtf.beatrice.hidekobot.Cache; +import wtf.beatrice.hidekobot.util.RandomUtil; + +import java.util.List; + +public class CoinFlip +{ + + + public static Button getReflipButton() { + return Button.primary("coinflip_reflip", "Flip again") + .withEmoji(Emoji.fromFormatted("\uD83E\uDE99")); + } + + public static String genRandom() + { + int rand = RandomUtil.getRandomNumber(0, 1); + String msg; + + if(rand == 1) + { + msg = ":coin: It's **Heads**!"; + } else { + msg = "It's **Tails**! :coin:"; + } + + return msg; + } + + public static void buttonReFlip(ButtonInteractionEvent event) + { + // check if the user interacting is the same one who ran the command + if(!(Cache.getDatabaseSource().isUserTrackedFor(event.getUser().getId(), event.getMessageId()))) + { + event.reply("❌ You did not run this command!").setEphemeral(true).queue(); + return; + } + + // set old message's button as disabled + List actionRows = event.getMessage().getActionRows(); + actionRows.set(0, actionRows.get(0).asDisabled()); + event.editComponents(actionRows).queue(); + + // perform coin flip + event.getHook().sendMessage(genRandom()) + .addActionRow(getReflipButton()) + .queue((message) -> + { + // set the command as expiring and restrict it to the user who ran it + trackAndRestrict(message, event.getUser()); + }, (error) -> {}); + } + + public static void trackAndRestrict(Message replyMessage, User user) + { + String replyMessageId = replyMessage.getId(); + + Cache.getDatabaseSource().queueDisabling(replyMessage); + Cache.getDatabaseSource().trackRanCommandReply(replyMessage, user); + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/CoinFlipCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/CoinFlipCommand.java new file mode 100644 index 0000000..6cda1a2 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/CoinFlipCommand.java @@ -0,0 +1,44 @@ +package wtf.beatrice.hidekobot.commands.message; + +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import org.jetbrains.annotations.Nullable; +import wtf.beatrice.hidekobot.commands.base.CoinFlip; +import wtf.beatrice.hidekobot.objects.commands.MessageCommand; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +public class CoinFlipCommand implements MessageCommand +{ + + @Override + public LinkedList getCommandLabels() { + return new LinkedList<>(Arrays.asList("coinflip", "flip", "flipcoin")); + } + + @Nullable + @Override + public List getPermissions() { + return null; // null because it can be used anywhere + } + + @Override + public boolean passRawArgs() { + return false; + } + + @Override + public void runCommand(MessageReceivedEvent event, String label, String[] args) { + + // perform coin flip + event.getMessage().reply(CoinFlip.genRandom()) + .addActionRow(CoinFlip.getReflipButton()) + .queue((message) -> + { + // set the command as expiring and restrict it to the user who ran it + CoinFlip.trackAndRestrict(message, event.getAuthor()); + }, (error) -> {}); + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/CoinFlipCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/CoinFlipCommand.java index b25f7bd..eb3257a 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/CoinFlipCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/CoinFlipCommand.java @@ -1,20 +1,11 @@ package wtf.beatrice.hidekobot.commands.slash; -import net.dv8tion.jda.api.entities.Message; -import net.dv8tion.jda.api.entities.User; -import net.dv8tion.jda.api.entities.emoji.Emoji; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; -import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.Commands; -import net.dv8tion.jda.api.interactions.components.ActionRow; -import net.dv8tion.jda.api.interactions.components.buttons.Button; import org.jetbrains.annotations.NotNull; -import wtf.beatrice.hidekobot.Cache; +import wtf.beatrice.hidekobot.commands.base.CoinFlip; import wtf.beatrice.hidekobot.objects.commands.SlashCommandImpl; -import wtf.beatrice.hidekobot.util.RandomUtil; - -import java.util.List; public class CoinFlipCommand extends SlashCommandImpl { @@ -26,71 +17,24 @@ public class CoinFlipCommand extends SlashCommandImpl "Flip a coin and get head or tails."); } - private final Button reflipButton = Button.primary("coinflip_reflip", "Flip again") - .withEmoji(Emoji.fromFormatted("\uD83E\uDE99")); - @Override public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) { // perform coin flip - event.reply(genRandom()) - .addActionRow(reflipButton) + event.reply(CoinFlip.genRandom()) + .addActionRow(CoinFlip.getReflipButton()) .queue((interaction) -> { // set the command as expiring and restrict it to the user who ran it interaction.retrieveOriginal().queue((message) -> { - trackAndRestrict(message, event.getUser()); + CoinFlip.trackAndRestrict(message, event.getUser()); }, (error) -> {}); }, (error) -> {}); - } - public void buttonReFlip(ButtonInteractionEvent event) - { - // check if the user interacting is the same one who ran the command - if(!(Cache.getDatabaseSource().isUserTrackedFor(event.getUser().getId(), event.getMessageId()))) - { - event.reply("❌ You did not run this command!").setEphemeral(true).queue(); - return; - } - // set old message's button as disabled - List actionRows = event.getMessage().getActionRows(); - actionRows.set(0, actionRows.get(0).asDisabled()); - event.editComponents(actionRows).queue(); - // perform coin flip - event.getHook().sendMessage(genRandom()) - .addActionRow(reflipButton) - .queue((message) -> - { - // set the command as expiring and restrict it to the user who ran it - trackAndRestrict(message, event.getUser()); - }, (error) -> {}); - } - private void trackAndRestrict(Message replyMessage, User user) - { - String replyMessageId = replyMessage.getId(); - - Cache.getDatabaseSource().queueDisabling(replyMessage); - Cache.getDatabaseSource().trackRanCommandReply(replyMessage, user); - } - - private String genRandom() - { - int rand = RandomUtil.getRandomNumber(0, 1); - String msg; - - if(rand == 1) - { - msg = ":coin: It's **Heads**!"; - } else { - msg = "It's **Tails**! :coin:"; - } - - return msg; - } } diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/ButtonInteractionListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/ButtonInteractionListener.java index 17606e6..bdf5269 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/ButtonInteractionListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/ButtonInteractionListener.java @@ -3,7 +3,7 @@ package wtf.beatrice.hidekobot.listeners; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import wtf.beatrice.hidekobot.commands.base.ClearChat; -import wtf.beatrice.hidekobot.commands.slash.CoinFlipCommand; +import wtf.beatrice.hidekobot.commands.base.CoinFlip; public class ButtonInteractionListener extends ListenerAdapter { @@ -15,7 +15,7 @@ public class ButtonInteractionListener extends ListenerAdapter switch (event.getComponentId().toLowerCase()) { // coinflip - case "coinflip_reflip" -> new CoinFlipCommand().buttonReFlip(event); + case "coinflip_reflip" -> CoinFlip.buttonReFlip(event); // clearchat command case "clear_dismiss" -> ClearChat.dismissMessage(event);