diff --git a/README.md b/README.md index 19abfa82..aea0b9c6 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,7 @@ As you can see, this was shown using the command `/help order`. It shows each pa ##### 3. **FUN** * `avatar` - Get yours or someone else's avatar image! * `banner` - Get yours or someone else's profile banner! +* `choose-random` - Choose a random variable. * `birthday` - Add, change, or remove your birthday! Even get someone else's birthday! * `coffee-meme` - Get a coffee meme! * `counting-statistics` - Get counting information for your server! diff --git a/src/main/java/com/beanbeanjuice/command/fun/ChooseRandom.java b/src/main/java/com/beanbeanjuice/command/fun/ChooseRandom.java new file mode 100644 index 00000000..ab0a8822 --- /dev/null +++ b/src/main/java/com/beanbeanjuice/command/fun/ChooseRandom.java @@ -0,0 +1,79 @@ +package com.beanbeanjuice.command.fun; + +import com.beanbeanjuice.utility.command.CommandCategory; +import com.beanbeanjuice.utility.command.ICommand; +import com.beanbeanjuice.utility.helper.Helper; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; + +/** + * An {@link ICommand} used to choose a random variable. + * + * @author beanbeanjuice + */ +public class ChooseRandom implements ICommand { + + @Override + public void handle(@NotNull SlashCommandInteractionEvent event) { + String[] variables = Helper.removeCommaSpace(event.getOption("choices").getAsString()); // Will not be null. + + int randomNum = Helper.getRandomNumber(0, variables.length); + String chosenVariable = variables[randomNum]; + + StringBuilder messageBuilder = new StringBuilder(); + messageBuilder.append("Options: ```"); + + for (int i = 0; i < variables.length; i++) { + messageBuilder.append(variables[i]); + + if (i != variables.length - 1) messageBuilder.append(", "); + } + + messageBuilder.append("```\n\n🎲 (").append(randomNum + 1).append("/").append(variables.length).append("): ") + .append("**").append(chosenVariable).append("**"); + + event.getHook().sendMessageEmbeds( + Helper.successEmbed( + "Random Variable Chooser", + messageBuilder.toString() + ) + ).queue(); + } + + @NotNull + @Override + public String getDescription() { + return "Choose a random variable!"; + } + + @NotNull + @Override + public String exampleUsage() { + return "`/choose-random bacon,lettuce,pineapple`"; + } + + @NotNull + @Override + public ArrayList getOptions() { + ArrayList options = new ArrayList<>(); + options.add(new OptionData(OptionType.STRING, "choices", "The random variables to choose from, separated by commas.", true)); + return options; + } + + @NotNull + @Override + public CommandCategory getCategoryType() { + return CommandCategory.FUN; + } + + @NotNull + @Override + public Boolean allowDM() { + return true; + } + +} diff --git a/src/main/java/com/beanbeanjuice/utility/command/CommandHandler.java b/src/main/java/com/beanbeanjuice/utility/command/CommandHandler.java index 15112d4a..b1e2dfe2 100644 --- a/src/main/java/com/beanbeanjuice/utility/command/CommandHandler.java +++ b/src/main/java/com/beanbeanjuice/utility/command/CommandHandler.java @@ -67,6 +67,7 @@ public CommandHandler(@NotNull JDA jda) { commands.put("birthday", new BirthdayCommand()); commands.put("avatar", new AvatarCommand()); commands.put("banner", new BannerCommand()); + commands.put("choose-random", new ChooseRandom()); commands.put("coffee-meme", new CoffeeMemeCommand()); commands.put("counting-statistics", new CountingStatisticsCommand()); commands.put("joke", new JokeCommand()); diff --git a/src/main/java/com/beanbeanjuice/utility/helper/Helper.java b/src/main/java/com/beanbeanjuice/utility/helper/Helper.java index da8afd03..48570cf1 100644 --- a/src/main/java/com/beanbeanjuice/utility/helper/Helper.java +++ b/src/main/java/com/beanbeanjuice/utility/helper/Helper.java @@ -452,4 +452,14 @@ public static JsonNode parseJson(String filePath) throws IOException { return new ObjectMapper().readTree(file); } + /** + * Return a {@link String[]} from a given {@link String input}. + * The input wil be a comma separated {@link String}. + * @param input The comma-separated {@link String}. + * @return The {@link String[]} of values, now separated. + */ + public static String[] removeCommaSpace(String input) { + return input.split(",\\s*"); + } + }