add option constraints for some commands

This commit is contained in:
anthorob committed 2024-12-24 08:46:32 -05:00
1 parent 29eecd42a9
commit 88fa8aac23
3 files changed
+39 -6

No files matched your search

@@ -44,21 +44,25 @@ public class ReactionCommand extends AbstractAdminCommand {
builder.addSubcommandGroups(
new SubcommandGroupData("emoji", "Give roles to users using emojis").addSubcommands(
new SubcommandData("setup", "Add a role to a message").addOptions(
new OptionData(OptionType.STRING, "messageid", "The ID of the message", true),
new OptionData(OptionType.STRING, "messageid", "The ID of the message", true)
.setRequiredLength(19, 19),
new OptionData(OptionType.STRING, "emoji", "Emoji associated with the role", true),
new OptionData(OptionType.ROLE, "role", "Role to add to the emoji", true)
),
new SubcommandData("add", "Add a role to an existing message").addOptions(
new OptionData(OptionType.STRING, "messageid", "The ID of the message", true),
new OptionData(OptionType.STRING, "messageid", "The ID of the message", true)
.setRequiredLength(19, 19),
new OptionData(OptionType.STRING, "emoji", "Emoji associated with the role", true),
new OptionData(OptionType.ROLE, "role", "Role to add to the emoji", true)
),
new SubcommandData("remove", "Remove a role from a message").addOptions(
new OptionData(OptionType.STRING, "messageid", "The ID of the message", true),
new OptionData(OptionType.STRING, "messageid", "The ID of the message", true)
.setRequiredLength(19, 19),
new OptionData(OptionType.STRING, "emoji", "Emoji to remove from the message", true)
),
new SubcommandData("delete", "Remove all roles from a message").addOptions(
new OptionData(OptionType.STRING, "messageid", "The ID of the message", true)
.setRequiredLength(19, 19)
)
)
);
@@ -49,7 +49,7 @@ public class ConfigureCommand extends AbstractAdminCommand {
public void registerCommandOptions(CommandOptionsBuilder builder) {
builder.addSubcommands(
new SubcommandData("music-volume", "Volume for the music player")
.addOptions(new OptionData(OptionType.INTEGER, "volume", "Integer between 0 and 100", true)
.addOptions(new OptionData(OptionType.INTEGER, "volume", "Playback volume for every session", true)
.setRequiredRange(0, 100)),
new SubcommandData("error-logging", "Send errors to #error and/or #log channel")
.addOption(OptionType.BOOLEAN, "debug", "Enable or disable the fonctionnality", true),
@@ -14,6 +14,7 @@ import me.bobert.netneutrality.core.command.options.CommandOptionsBuilder;
import me.bobert.netneutrality.core.context.CommandContext;
import me.bobert.netneutrality.http.WebModule;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.json.JSONObject;
import java.text.NumberFormat;
@@ -22,7 +23,9 @@ import java.util.Locale;
public class CryptoPriceCommand extends AbstractCommand {
private final String exchange = "Coinbase";
private final String baseURL = "https://min-api.cryptocompare.com/data/price";
private final String exchangeApi = String.format("https://min-api.cryptocompare.com/data/v4/all/exchanges?e=%s", exchange);
@Override
public boolean needAdmin() {
@@ -51,8 +54,11 @@ public class CryptoPriceCommand extends AbstractCommand {
@Override
public void registerCommandOptions(CommandOptionsBuilder builder) {
builder.addOption(OptionType.STRING, "symbol", "The crypto symbol to fetch price for", true);
builder.addOption(OptionType.STRING, "currency", "In which currency should the price be displayed");
builder.addOptions(
new OptionData(OptionType.STRING, "symbol", "The crypto symbol to fetch price for", true),
new OptionData(OptionType.STRING, "currency", "In which currency should the price be displayed")
.setRequiredLength(3, 3)
);
}
@Override
@@ -62,6 +68,7 @@ public class CryptoPriceCommand extends AbstractCommand {
@Override
public CommandExecutionResult execute(CommandContext context) {
//TODO: implement cache
Locale usa = new Locale("en", "US");
NumberFormat dollarFormat = NumberFormat.getCurrencyInstance(usa);
dollarFormat.setMaximumFractionDigits(24);
@@ -78,6 +85,10 @@ public class CryptoPriceCommand extends AbstractCommand {
String finalCurrency = currency.toUpperCase();
context.async(asyncMessage -> {
try {
if (!validateSymbol(symbol)) {
asyncMessage.edit("Crypto doesn't exist.. yet");
return;
}
WebModule http = new WebModule(String.format("%s?fsym=%s&tsyms=%s", baseURL, symbol, finalCurrency)).request();
JSONObject json = http.getAsObject();
@@ -100,4 +111,22 @@ public class CryptoPriceCommand extends AbstractCommand {
return CommandExecutionResult.none();
}
private boolean validateSymbol(String symbol) {
try {
WebModule http = new WebModule(exchangeApi).request();
JSONObject json = http.getAsObject();
JSONObject pairs = json.getJSONObject("Data")
.getJSONObject("exchanges")
.getJSONObject(exchange)
.getJSONObject("pairs");
return pairs.has(symbol);
} catch (Exception e) {
LogHandler.log(e);
return false;
}
}
}