HidekoBot/src/main/java/wtf/beatrice/hidekobot/commands/slash/CoinFlipCommand.java
Beatrice DellacĂ  3d626bb46f
All checks were successful
continuous-integration/drone/push Build is passing
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.
2022-11-20 22:09:58 +01:00

58 lines
1.8 KiB
Java

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 void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
{
int rand = RandomUtil.getRandomNumber(0, 1);
String msg;
if(rand == 1)
{
msg = ":coin: It's **Heads**!";
} else {
msg = "It's **Tails**! :coin:";
}
event.reply(msg)
.addActionRow(Button.primary("reflip", "Flip again")
.withEmoji(Emoji.fromFormatted("\uD83E\uDE99")) // coin emoji
).queue();
}
public void buttonReflip(ButtonInteractionEvent event)
{
List<ActionRow> 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();
}
}