Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the Random Variable Chooser #599

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/main/java/com/beanbeanjuice/command/fun/ChooseRandom.java
Original file line number Diff line number Diff line change
@@ -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<OptionData> getOptions() {
ArrayList<OptionData> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/beanbeanjuice/utility/helper/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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*");
}

}