From c9ff329cbbd040637b3faf817bcf593c4aa0efa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Mon, 19 Dec 2022 18:37:17 +0100 Subject: [PATCH] Make magicball support slash commands too --- .../wtf/beatrice/hidekobot/HidekoBot.java | 1 + .../hidekobot/commands/base/MagicBall.java | 67 +++++++++++++++++++ .../commands/message/MagicBallCommand.java | 45 +------------ .../commands/slash/MagicBallCommand.java | 52 ++++++++++++++ 4 files changed, 123 insertions(+), 42 deletions(-) create mode 100644 src/main/java/wtf/beatrice/hidekobot/commands/base/MagicBall.java create mode 100644 src/main/java/wtf/beatrice/hidekobot/commands/slash/MagicBallCommand.java diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index ba4654a..e085906 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -121,6 +121,7 @@ public class HidekoBot slashCommandListener.registerCommand(new DieCommand()); slashCommandListener.registerCommand(new HelpCommand()); slashCommandListener.registerCommand(new InviteCommand()); + slashCommandListener.registerCommand(new MagicBallCommand()); slashCommandListener.registerCommand(new PingCommand()); slashCommandListener.registerCommand(new SayCommand()); Cache.setSlashCommandListener(slashCommandListener); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/base/MagicBall.java b/src/main/java/wtf/beatrice/hidekobot/commands/base/MagicBall.java new file mode 100644 index 0000000..151badc --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/commands/base/MagicBall.java @@ -0,0 +1,67 @@ +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; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +public class MagicBall +{ + + public static LinkedList getLabels() + { + return new LinkedList<>(Arrays.asList("8ball", "eightball", "magicball", "8b")); + } + + private final static List answers = new ArrayList<>( + Arrays.asList("It is certain.", + "It is decidedly so.", + "Without a doubt.", + "Yes, definitely.", + "You may rely on it.", + "As I see it, yes.", + "Most likely.", + "Looks like it.", + "Yes.", + "Signs point to yes.", + "Reply hazy, try again.", + "Ask again later.", + "Better not tell you now.", + "Seems uncertain.", + "Concentrate and ask again.", + "Don't count on it.", + "My answer is no.", + "My sources say no.", + "Outlook not so good.", + "Very doubtful.")); + + public static String getRandomAnswer() + { + int answerPos = RandomUtil.getRandomNumber(0, answers.size() - 1); + return answers.get(answerPos); + } + + public static MessageEmbed generateEmbed(String question, User author) + { + // add a question mark at the end, if missing. + // this might not always apply but it's fun + if(!question.endsWith("?")) question += "?"; + + String answer = getRandomAnswer(); + + EmbedBuilder embedBuilder = new EmbedBuilder(); + embedBuilder.setAuthor(author.getAsTag(), null, author.getAvatarUrl()); + embedBuilder.setTitle("Magic Ball"); + embedBuilder.setColor(Cache.getBotColor()); + embedBuilder.addField("❓ Question", question, false); + embedBuilder.addField("\uD83C\uDFB1 Answer", answer, false); + + return embedBuilder.build(); + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/MagicBallCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/MagicBallCommand.java index 78a2391..3581aa0 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/MagicBallCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/MagicBallCommand.java @@ -1,45 +1,19 @@ package wtf.beatrice.hidekobot.commands.message; -import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import org.jetbrains.annotations.Nullable; -import wtf.beatrice.hidekobot.Cache; +import wtf.beatrice.hidekobot.commands.base.MagicBall; import wtf.beatrice.hidekobot.objects.commands.MessageCommand; -import wtf.beatrice.hidekobot.util.RandomUtil; - -import java.util.ArrayList; -import java.util.Arrays; import java.util.LinkedList; import java.util.List; public class MagicBallCommand implements MessageCommand { - private final List answers = new ArrayList<>( - Arrays.asList("It is certain.", - "It is decidedly so.", - "Without a doubt.", - "Yes, definitely.", - "You may rely on it.", - "As I see it, yes.", - "Most likely.", - "Looks like it.", - "Yes.", - "Signs point to yes.", - "Reply hazy, try again.", - "Ask again later.", - "Better not tell you now.", - "Seems uncertain.", - "Concentrate and ask again.", - "Don't count on it.", - "My answer is no.", - "My sources say no.", - "Outlook not so good.", - "Very doubtful.")); @Override public LinkedList getCommandLabels() { - return new LinkedList<>(Arrays.asList("8ball", "eightball", "magicball", "8b")); + return MagicBall.getLabels(); } @Nullable @@ -73,20 +47,7 @@ public class MagicBallCommand implements MessageCommand String question = questionBuilder.toString(); - // add a question mark at the end, if missing. - // this might not always apply but it's fun - if(!question.endsWith("?")) question += "?"; - int answerPos = RandomUtil.getRandomNumber(0, answers.size() - 1); - String answer = answers.get(answerPos); - - EmbedBuilder embedBuilder = new EmbedBuilder(); - embedBuilder.setAuthor(event.getAuthor().getAsTag(), null, event.getAuthor().getAvatarUrl()); - embedBuilder.setTitle("Magic Ball"); - embedBuilder.setColor(Cache.getBotColor()); - embedBuilder.addField("❓ Question", question, false); - embedBuilder.addField("\uD83C\uDFB1 Answer", answer, false); - - event.getMessage().replyEmbeds(embedBuilder.build()).queue(); + event.getMessage().replyEmbeds(MagicBall.generateEmbed(question, event.getAuthor())).queue(); } } diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/MagicBallCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/MagicBallCommand.java new file mode 100644 index 0000000..f98b807 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/MagicBallCommand.java @@ -0,0 +1,52 @@ +package wtf.beatrice.hidekobot.commands.slash; + +import net.dv8tion.jda.api.entities.MessageEmbed; +import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +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.MagicBall; +import wtf.beatrice.hidekobot.objects.commands.SlashCommandImpl; + +public class MagicBallCommand extends SlashCommandImpl +{ + @Override + public CommandData getSlashCommandData() + { + + return Commands.slash(MagicBall.getLabels().get(0), + "Ask a question to the magic ball.") + .addOption(OptionType.STRING, "question", + "The question to ask.", + true, + false); + } + + @Override + public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) + { + MessageChannel channel = event.getChannel(); + + // get the asked question + OptionMapping textOption = event.getOption("question"); + String question = ""; + if(textOption != null) + { + question = textOption.getAsString(); + } + + if(textOption == null || question.isEmpty()) + { + event.reply("\uD83D\uDE20 Hey, you have to ask me a question!") + .setEphemeral(true) + .queue(); + return; + } + + MessageEmbed response = MagicBall.generateEmbed(question, event.getUser()); + event.replyEmbeds(response).queue(); + } +}