Skip to content

Commit

Permalink
Merge pull request #599 from beanbeanjuice/595-random-variable-chooser
Browse files Browse the repository at this point in the history
Added the Random Variable Chooser
  • Loading branch information
beanbeanjuice authored Oct 30, 2023
2 parents 702dacf + 0309784 commit 9df173a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
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*");
}

}

0 comments on commit 9df173a

Please sign in to comment.