2022-11-20 18:56:57 +01:00
|
|
|
package wtf.beatrice.hidekobot.commands.slash;
|
2022-11-20 06:04:00 +01:00
|
|
|
|
2022-11-21 11:14:07 +01:00
|
|
|
import net.dv8tion.jda.api.entities.Message;
|
|
|
|
import net.dv8tion.jda.api.entities.User;
|
2022-11-20 22:09:58 +01:00
|
|
|
import net.dv8tion.jda.api.entities.emoji.Emoji;
|
2022-11-20 06:04:00 +01:00
|
|
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
2022-11-20 22:09:58 +01:00
|
|
|
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;
|
2022-11-20 06:04:00 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2022-11-21 11:14:07 +01:00
|
|
|
import wtf.beatrice.hidekobot.Configuration;
|
2022-11-20 06:04:00 +01:00
|
|
|
import wtf.beatrice.hidekobot.utils.RandomUtil;
|
|
|
|
|
2022-11-20 22:09:58 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2022-11-20 06:04:00 +01:00
|
|
|
public class CoinFlipCommand
|
|
|
|
{
|
|
|
|
|
2022-11-20 22:23:26 +01:00
|
|
|
private final Button reflipButton = Button.primary("coinflip_reflip", "Flip again")
|
|
|
|
.withEmoji(Emoji.fromFormatted("\uD83E\uDE99"));
|
|
|
|
|
2022-11-20 22:09:58 +01:00
|
|
|
public void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
|
|
|
|
{
|
2022-11-20 22:23:26 +01:00
|
|
|
// perform coin flip
|
|
|
|
event.reply(genRandom())
|
|
|
|
.addActionRow(reflipButton)
|
2022-11-21 11:14:07 +01:00
|
|
|
.queue((interaction) ->
|
|
|
|
{
|
|
|
|
// set the command as expiring and restrict it to the user who ran it
|
|
|
|
interaction.retrieveOriginal().queue((message) ->
|
|
|
|
{
|
|
|
|
trackAndRestrict(message, event.getUser());
|
|
|
|
}, (error) -> {});
|
|
|
|
}, (error) -> {});
|
|
|
|
|
2022-11-20 22:09:58 +01:00
|
|
|
}
|
|
|
|
|
2022-11-20 22:23:26 +01:00
|
|
|
public void buttonReFlip(ButtonInteractionEvent event)
|
2022-11-20 06:04:00 +01:00
|
|
|
{
|
2022-11-21 11:14:07 +01:00
|
|
|
// check if the user interacting is the same one who ran the command
|
|
|
|
if(!(Configuration.getDatabaseManager().isUserTrackedFor(event.getUser().getId(), event.getMessageId())))
|
|
|
|
{
|
|
|
|
event.reply("❌ You did not run this command!").setEphemeral(true).queue();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-20 22:23:26 +01:00
|
|
|
// set old message's button as disabled
|
2022-11-20 22:09:58 +01:00
|
|
|
List<ActionRow> actionRows = event.getMessage().getActionRows();
|
|
|
|
actionRows.set(0, actionRows.get(0).asDisabled());
|
|
|
|
event.editComponents(actionRows).queue();
|
|
|
|
|
2022-11-20 22:23:26 +01:00
|
|
|
// perform coin flip
|
|
|
|
event.getHook().sendMessage(genRandom())
|
|
|
|
.addActionRow(reflipButton)
|
2022-11-21 11:14:07 +01:00
|
|
|
.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();
|
|
|
|
String replyChannelId = replyMessage.getId();
|
|
|
|
String replyGuildId = replyMessage.getGuild().getId();
|
|
|
|
String userId = user.getId();
|
|
|
|
|
|
|
|
Configuration.getDatabaseManager().queueDisabling(replyGuildId, replyChannelId, replyMessageId);
|
|
|
|
Configuration.getDatabaseManager().trackRanCommandReply(replyGuildId, replyChannelId, replyMessageId, userId);
|
2022-11-20 22:23:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private String genRandom()
|
|
|
|
{
|
2022-11-20 06:04:00 +01:00
|
|
|
int rand = RandomUtil.getRandomNumber(0, 1);
|
|
|
|
String msg;
|
|
|
|
|
|
|
|
if(rand == 1)
|
|
|
|
{
|
|
|
|
msg = ":coin: It's **Heads**!";
|
|
|
|
} else {
|
|
|
|
msg = "It's **Tails**! :coin:";
|
|
|
|
}
|
|
|
|
|
2022-11-20 22:23:26 +01:00
|
|
|
return msg;
|
2022-11-20 06:04:00 +01:00
|
|
|
}
|
2022-11-20 22:09:58 +01:00
|
|
|
|
2022-11-20 06:04:00 +01:00
|
|
|
}
|