Move command handling out of constructor, add coin reflip command
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Having heavy code run in a constructor is bad practice. We made separate methods for command handling.
This commit is contained in:
@@ -12,7 +12,7 @@ import java.awt.*;
|
||||
public class AvatarCommand
|
||||
{
|
||||
|
||||
public AvatarCommand(@NotNull SlashCommandInteractionEvent event)
|
||||
public void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
|
||||
{
|
||||
event.deferReply().queue();
|
||||
|
||||
|
@@ -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.
|
||||
|
@@ -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<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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -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())
|
||||
{
|
||||
|
@@ -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());
|
||||
|
||||
|
@@ -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();
|
||||
}
|
||||
|
Reference in New Issue
Block a user