Make coinflip support both slash and message commands
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bea 2022-11-22 20:50:37 +01:00
parent ff80e754ff
commit 4015aecc99
5 changed files with 120 additions and 62 deletions

View File

@ -116,6 +116,7 @@ public class HidekoBot
MessageCommandListener messageCommandListener = new MessageCommandListener(); MessageCommandListener messageCommandListener = new MessageCommandListener();
messageCommandListener.registerCommand(new HelloCommand()); messageCommandListener.registerCommand(new HelloCommand());
messageCommandListener.registerCommand(new CommandsCommand()); messageCommandListener.registerCommand(new CommandsCommand());
messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.CoinFlipCommand());
messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.ClearCommand()); messageCommandListener.registerCommand(new wtf.beatrice.hidekobot.commands.message.ClearCommand());
Cache.setMessageCommandListener(messageCommandListener); Cache.setMessageCommandListener(messageCommandListener);

View File

@ -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<ActionRow> 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);
}
}

View File

@ -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<String> getCommandLabels() {
return new LinkedList<>(Arrays.asList("coinflip", "flip", "flipcoin"));
}
@Nullable
@Override
public List<Permission> 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) -> {});
}
}

View File

@ -1,20 +1,11 @@
package wtf.beatrice.hidekobot.commands.slash; 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.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.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands; 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 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.objects.commands.SlashCommandImpl;
import wtf.beatrice.hidekobot.util.RandomUtil;
import java.util.List;
public class CoinFlipCommand extends SlashCommandImpl public class CoinFlipCommand extends SlashCommandImpl
{ {
@ -26,71 +17,24 @@ public class CoinFlipCommand extends SlashCommandImpl
"Flip a coin and get head or tails."); "Flip a coin and get head or tails.");
} }
private final Button reflipButton = Button.primary("coinflip_reflip", "Flip again")
.withEmoji(Emoji.fromFormatted("\uD83E\uDE99"));
@Override @Override
public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) public void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
{ {
// perform coin flip // perform coin flip
event.reply(genRandom()) event.reply(CoinFlip.genRandom())
.addActionRow(reflipButton) .addActionRow(CoinFlip.getReflipButton())
.queue((interaction) -> .queue((interaction) ->
{ {
// set the command as expiring and restrict it to the user who ran it // set the command as expiring and restrict it to the user who ran it
interaction.retrieveOriginal().queue((message) -> interaction.retrieveOriginal().queue((message) ->
{ {
trackAndRestrict(message, event.getUser()); CoinFlip.trackAndRestrict(message, event.getUser());
}, (error) -> {}); }, (error) -> {});
}, (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<ActionRow> 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;
}
} }

View File

@ -3,7 +3,7 @@ package wtf.beatrice.hidekobot.listeners;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
import wtf.beatrice.hidekobot.commands.base.ClearChat; 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 public class ButtonInteractionListener extends ListenerAdapter
{ {
@ -15,7 +15,7 @@ public class ButtonInteractionListener extends ListenerAdapter
switch (event.getComponentId().toLowerCase()) { switch (event.getComponentId().toLowerCase()) {
// coinflip // coinflip
case "coinflip_reflip" -> new CoinFlipCommand().buttonReFlip(event); case "coinflip_reflip" -> CoinFlip.buttonReFlip(event);
// clearchat command // clearchat command
case "clear_dismiss" -> ClearChat.dismissMessage(event); case "clear_dismiss" -> ClearChat.dismissMessage(event);