Make love calculator also support slash commands

This commit is contained in:
Bea 2022-12-20 22:32:40 +01:00
parent 174b78704f
commit 1c82d2b53b
4 changed files with 102 additions and 22 deletions

View File

@ -122,6 +122,7 @@ public class HidekoBot
slashCommandListener.registerCommand(new DieCommand());
slashCommandListener.registerCommand(new HelpCommand());
slashCommandListener.registerCommand(new InviteCommand());
slashCommandListener.registerCommand(new LoveCalculatorCommand());
slashCommandListener.registerCommand(new MagicBallCommand());
slashCommandListener.registerCommand(new PingCommand());
slashCommandListener.registerCommand(new SayCommand());

View File

@ -0,0 +1,36 @@
package wtf.beatrice.hidekobot.commands.base;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.User;
import wtf.beatrice.hidekobot.Cache;
import wtf.beatrice.hidekobot.util.RandomUtil;
public class LoveCalculator
{
public static MessageEmbed buildEmbed(User author, User user1, User user2)
{
int loveAmount = RandomUtil.getRandomNumber(0, 100);
String formattedAmount = loveAmount + "%";
if(loveAmount <= 30) formattedAmount += "... \uD83D\uDE22";
else if(loveAmount < 60) formattedAmount += "! \uD83E\uDDD0";
else if(loveAmount < 75) formattedAmount += "!!! \uD83E\uDD73";
else formattedAmount = "" + formattedAmount + "!!! \uD83D\uDE0D\uD83D\uDCA5";
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setColor(Cache.getBotColor());
embedBuilder.setAuthor(author.getAsTag(), null, author.getAvatarUrl());
embedBuilder.setTitle("Love Calculator");
embedBuilder.addField("\uD83D\uDC65 People",
user1.getAsMention() + " & " + user2.getAsMention(),
false);
embedBuilder.addField("❤️\u200D\uD83D\uDD25 Match",
formattedAmount,
false);
return embedBuilder.build();
}
}

View File

@ -4,12 +4,14 @@ import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.IMentionable;
import net.dv8tion.jda.api.entities.Mentions;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import wtf.beatrice.hidekobot.Cache;
import wtf.beatrice.hidekobot.HidekoBot;
import wtf.beatrice.hidekobot.commands.base.LoveCalculator;
import wtf.beatrice.hidekobot.objects.commands.CommandCategory;
import wtf.beatrice.hidekobot.objects.commands.MessageCommand;
import wtf.beatrice.hidekobot.util.RandomUtil;
@ -86,28 +88,8 @@ public class LoveCalculatorCommand implements MessageCommand
user2 = HidekoBot.getAPI().retrieveUserById(mentionedUserId).complete();
}
int loveAmount = RandomUtil.getRandomNumber(0, 100);
String formattedAmount = loveAmount + "%";
if(loveAmount <= 30) formattedAmount += "... \uD83D\uDE22";
else if(loveAmount < 60) formattedAmount += "! \uD83E\uDDD0";
else if(loveAmount < 75) formattedAmount += "!!! \uD83E\uDD73";
else formattedAmount = "" + formattedAmount + "!!! \uD83D\uDE0D\uD83D\uDCA5";
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setColor(Cache.getBotColor());
embedBuilder.setAuthor(event.getAuthor().getAsTag(), null, event.getAuthor().getAvatarUrl());
embedBuilder.setTitle("Love Calculator");
embedBuilder.addField("\uD83D\uDC65 People",
user1.getAsMention() + " & " + user2.getAsMention(),
false);
embedBuilder.addField("❤️\u200D\uD83D\uDD25 Match",
formattedAmount,
false);
event.getChannel().sendMessageEmbeds(embedBuilder.build()).queue();
MessageEmbed embed = LoveCalculator.buildEmbed(event.getAuthor(), user1, user2);
event.getChannel().sendMessageEmbeds(embed).queue();
}
}

View File

@ -0,0 +1,61 @@
package wtf.beatrice.hidekobot.commands.slash;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import org.jetbrains.annotations.NotNull;
import wtf.beatrice.hidekobot.commands.base.LoveCalculator;
import wtf.beatrice.hidekobot.objects.commands.SlashCommandImpl;
public class LoveCalculatorCommand extends SlashCommandImpl
{
@Override
public CommandData getSlashCommandData()
{
return Commands.slash("lovecalc",
"Calculate how much two people love each other.")
.addOption(OptionType.MENTIONABLE,
"first",
"The first person to account for",
true)
.addOption(OptionType.MENTIONABLE,
"second",
"The second person to account for",
false);
}
@Override
public void runSlashCommand(@NotNull SlashCommandInteractionEvent event)
{
User firstUser, secondUser;
OptionMapping firsUserArg = event.getOption("first");
if(firsUserArg != null)
{
firstUser = firsUserArg.getAsUser();
} else {
event.reply("\uD83D\uDE22 I need to know who to check! Please mention them.")
.setEphemeral(true)
.queue();
return;
}
OptionMapping secondUserArg = event.getOption("second");
if(secondUserArg != null)
{
secondUser = secondUserArg.getAsUser();
} else {
secondUser = event.getUser();
}
MessageEmbed embed = LoveCalculator.buildEmbed(event.getUser(), firstUser, secondUser);
event.replyEmbeds(embed).queue();
}
}