From 3d626bb46f3a17fbf871a90efcbcb3f3e6406d98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Sun, 20 Nov 2022 22:09:58 +0100 Subject: [PATCH] Move command handling out of constructor, add coin reflip command Having heavy code run in a constructor is bad practice. We made separate methods for command handling. --- .../wtf/beatrice/hidekobot/HidekoBot.java | 2 + .../commands/slash/AvatarCommand.java | 2 +- .../commands/slash/ClearChatCommand.java | 6 +-- .../commands/slash/CoinFlipCommand.java | 37 ++++++++++++++++++- .../hidekobot/commands/slash/DieCommand.java | 2 +- .../commands/slash/InviteCommand.java | 2 +- .../hidekobot/commands/slash/PingCommand.java | 2 +- .../listeners/ButtonInteractionListener.java | 23 ++++++++++++ .../listeners/SlashCommandListener.java | 12 +++--- 9 files changed, 73 insertions(+), 15 deletions(-) create mode 100644 src/main/java/wtf/beatrice/hidekobot/listeners/ButtonInteractionListener.java diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index 9238532..d422208 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -6,6 +6,7 @@ import net.dv8tion.jda.api.OnlineStatus; import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.requests.GatewayIntent; import sun.misc.Signal; +import wtf.beatrice.hidekobot.listeners.ButtonInteractionListener; import wtf.beatrice.hidekobot.listeners.MessageListener; import wtf.beatrice.hidekobot.listeners.SlashCommandCompleter; import wtf.beatrice.hidekobot.listeners.SlashCommandListener; @@ -81,6 +82,7 @@ public class HidekoBot jda.addEventListener(new MessageListener()); jda.addEventListener(new SlashCommandListener()); jda.addEventListener(new SlashCommandCompleter()); + jda.addEventListener(new ButtonInteractionListener()); // update slash commands (delayed) final boolean finalForceUpdateCommands = forceUpdateCommands; diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/AvatarCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/AvatarCommand.java index 2ff2716..d986fbc 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/AvatarCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/AvatarCommand.java @@ -12,7 +12,7 @@ import java.awt.*; public class AvatarCommand { - public AvatarCommand(@NotNull SlashCommandInteractionEvent event) + public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) { event.deferReply().queue(); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearChatCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearChatCommand.java index b33a04e..1cea335 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearChatCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearChatCommand.java @@ -15,7 +15,7 @@ import java.util.List; public class ClearChatCommand { - public ClearChatCommand(@NotNull SlashCommandInteractionEvent event) + public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) { MessageChannel channel = event.getChannel(); @@ -39,10 +39,10 @@ public class ClearChatCommand // answer by saying that the operation has begun. InteractionHook replyInteraction = event.reply("\uD83D\uDEA7 Clearing...").complete(); - // int to keep track of how many messages we deleted. + // int to keep track of how many messages we actually deleted. int deleted = 0; - int limit = 95; //discord limits this method to range 2-100. we set it to 95 to be safe.. + int limit = 95; //discord limits this method to range 2-100. we set it to 95 to be safe. // increase the count by 1, because we technically aren't clearing the first ID ever // which is actually the slash command's ID and not a message. 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 a2c3edf..f2c52ef 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/CoinFlipCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/CoinFlipCommand.java @@ -1,13 +1,20 @@ package wtf.beatrice.hidekobot.commands.slash; +import net.dv8tion.jda.api.entities.emoji.Emoji; +import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +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 org.jetbrains.annotations.NotNull; import wtf.beatrice.hidekobot.utils.RandomUtil; +import java.util.List; + public class CoinFlipCommand { - public CoinFlipCommand(@NotNull SlashCommandInteractionEvent event) + public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) { int rand = RandomUtil.getRandomNumber(0, 1); String msg; @@ -19,6 +26,32 @@ public class CoinFlipCommand msg = "It's **Tails**! :coin:"; } - event.reply(msg).queue(); + event.reply(msg) + .addActionRow(Button.primary("reflip", "Flip again") + .withEmoji(Emoji.fromFormatted("\uD83E\uDE99")) // coin emoji + ).queue(); } + + public void buttonReflip(ButtonInteractionEvent event) + { + List actionRows = event.getMessage().getActionRows(); + actionRows.set(0, actionRows.get(0).asDisabled()); + event.editComponents(actionRows).queue(); + + int rand = RandomUtil.getRandomNumber(0, 1); + String msg; + + if(rand == 1) + { + msg = ":coin: It's **Heads**!"; + } else { + msg = "It's **Tails**! :coin:"; + } + + event.getHook().sendMessage(msg) + .addActionRow(Button.primary("reflip", "Flip again") + .withEmoji(Emoji.fromFormatted("\uD83E\uDE99")) // coin emoji + ).queue(); + } + } diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/DieCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/DieCommand.java index 6a394e4..6d000fd 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/DieCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/DieCommand.java @@ -11,7 +11,7 @@ import java.util.concurrent.TimeUnit; public class DieCommand { - public DieCommand(@NotNull SlashCommandInteractionEvent event) + public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) { if(Configuration.getBotOwnerId() != event.getMember().getIdLong()) { diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/InviteCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/InviteCommand.java index 3cd92e5..5667165 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/InviteCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/InviteCommand.java @@ -8,7 +8,7 @@ import wtf.beatrice.hidekobot.Configuration; public class InviteCommand { - public InviteCommand(@NotNull SlashCommandInteractionEvent event) + public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) { ReplyCallbackAction reply = event.reply("Here's your link ✨ " + Configuration.getInviteUrl()); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/PingCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/PingCommand.java index dc1933f..bc4a31e 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/PingCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/PingCommand.java @@ -5,7 +5,7 @@ import org.jetbrains.annotations.NotNull; public class PingCommand { - public PingCommand(@NotNull SlashCommandInteractionEvent event) + public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) { event.reply("Pong!").queue(); } diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/ButtonInteractionListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/ButtonInteractionListener.java new file mode 100644 index 0000000..bcc0e07 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/ButtonInteractionListener.java @@ -0,0 +1,23 @@ +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.slash.CoinFlipCommand; + +public class ButtonInteractionListener extends ListenerAdapter +{ + + @Override + public void onButtonInteraction(ButtonInteractionEvent event) + { + + switch (event.getComponentId().toLowerCase()) { + + // coinflip + case "reflip" -> new CoinFlipCommand().buttonReflip(event); + + } + + } + +} diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java index 8dfd996..18132c9 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java @@ -12,12 +12,12 @@ public class SlashCommandListener extends ListenerAdapter public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) { switch (event.getName().toLowerCase()) { - case "avatar" -> new AvatarCommand(event); - case "clear" -> new ClearChatCommand(event); - case "coinflip" -> new CoinFlipCommand(event); - case "die" -> new DieCommand(event); - case "invite" -> new InviteCommand(event); - case "ping" -> new PingCommand(event); + case "avatar" -> new AvatarCommand().runSlashCommand(event); + case "clear" -> new ClearChatCommand().runSlashCommand(event); + case "coinflip" -> new CoinFlipCommand().runSlashCommand(event); + case "die" -> new DieCommand().runSlashCommand(event); + case "invite" -> new InviteCommand().runSlashCommand(event); + case "ping" -> new PingCommand().runSlashCommand(event); } } }