fix(CommandProcessor): missing guild check and if command exists before execution

This commit is contained in:
anthorob committed 2024-10-05 10:13:21 -04:00
1 parent 9ee4797aaf
commit 5a8e606a89
14 files changed
+93 -49

No files matched your search

@@ -8,7 +8,6 @@ package me.bobert.netneutrality.command.admin;
import me.bobert.netneutrality.command.AbstractAdminCommand;
import me.bobert.netneutrality.context.MessageCommandContext;
import me.bobert.netneutrality.context.SlashCommandContext;
import me.bobert.netneutrality.core.LogHandler;
import me.bobert.netneutrality.core.command.CommandExecutionResult;
import me.bobert.netneutrality.db.entities.OBlacklist;
import me.bobert.netneutrality.db.repository.BlacklistRepository;
@@ -17,7 +16,6 @@ import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import java.util.ArrayList;
import java.util.List;
public class BlacklistCommand extends AbstractAdminCommand {
@@ -67,43 +67,44 @@ public class CryptoPriceCommand extends AbstractCommand {
String[] args = context.getArgs();
String currency = "USD";
if (args.length > 0) {
if (args.length == 2) {
try {
currency = Currency.getInstance(args[1].toUpperCase()).getCurrencyCode();
} catch (IllegalArgumentException exception) {
return CommandExecutionResult.warn("Please enter a valid currency");
}
}
String crypto = args[0].toUpperCase();
String finalCurrency = currency;
context.async("Retreiving price, please wait...", asyncMessage -> {
String price;
Locale usa = new Locale("en", "US");
NumberFormat dollarFormat = NumberFormat.getCurrencyInstance(usa);
dollarFormat.setMaximumFractionDigits(24);
try {
WebModule http = new WebModule(String.format("%s?fsym=%s&tsyms=%s", baseURL, crypto, finalCurrency.toUpperCase())).request();
JSONObject json = http.getAsObject();
if (json.isEmpty() || (json.has("Response") && json.getString("Response").equalsIgnoreCase("Error"))) {
asyncMessage.edit("Crypto doesn't exist.. yet");
return;
}
price = dollarFormat.format(json.getDouble(finalCurrency));
asyncMessage.edit("The current price of %s in %s is %s", crypto, finalCurrency, price);
} catch (JSONException | IllegalArgumentException | ArithmeticException e) {
asyncMessage.edit("Error getting price of %s", crypto);
LogHandler.log(e);
}
});
if (args.length == 0) {
return CommandExecutionResult.warn("Usage: crypto <symbol> <currency>");
}
if (args.length == 2) {
try {
currency = Currency.getInstance(args[1].toUpperCase()).getCurrencyCode();
} catch (IllegalArgumentException exception) {
return CommandExecutionResult.warn("Please enter a valid currency");
}
}
String crypto = args[0].toUpperCase();
String finalCurrency = currency;
context.async("Retreiving price, please wait...", asyncMessage -> {
String price;
Locale usa = new Locale("en", "US");
NumberFormat dollarFormat = NumberFormat.getCurrencyInstance(usa);
dollarFormat.setMaximumFractionDigits(24);
try {
WebModule http = new WebModule(String.format("%s?fsym=%s&tsyms=%s", baseURL, crypto, finalCurrency.toUpperCase())).request();
JSONObject json = http.getAsObject();
if (json.isEmpty() || (json.has("Response") && json.getString("Response").equalsIgnoreCase("Error"))) {
asyncMessage.edit("Crypto doesn't exist.. yet");
return;
}
price = dollarFormat.format(json.getDouble(finalCurrency));
asyncMessage.edit("The current price of %s in %s is %s", crypto, finalCurrency, price);
} catch (JSONException | IllegalArgumentException | ArithmeticException e) {
asyncMessage.edit("Error getting price of %s", crypto);
LogHandler.log(e);
}
});
return CommandExecutionResult.none();
}
@@ -66,6 +66,11 @@ public class MessageCommandContext extends CommandContext {
return true;
}
@Override
public boolean isFromGuild() {
return event.isFromGuild();
}
@Override
public InteractionHook getInteractionHook() {
return null;
@@ -37,6 +37,11 @@ public class MusicChannelContext extends AbstractContext {
return true;
}
@Override
public boolean isFromGuild() {
return true;
}
@Override
public void deleteMessage() {
@@ -33,6 +33,11 @@ public class SlashCommandContext extends CommandContext {
return event.isAcknowledged();
}
@Override
public boolean isFromGuild() {
return event.isFromGuild();
}
@Override
public InteractionHook getInteractionHook() {
if (!event.isAcknowledged())
@@ -47,6 +47,11 @@ public class ButtonTaskContext extends TaskContext {
return event.isAcknowledged();
}
@Override
public boolean isFromGuild() {
return event.isFromGuild();
}
@Override
public void reply(MessageCreateData message, boolean ephemeral, Consumer<AsyncMessage> consumer, LayoutComponent... components) {
if (isAcknowledged()) {
@@ -59,6 +59,11 @@ public class EmojiTaskContext extends TaskContext {
return true;
}
@Override
public boolean isFromGuild() {
return event.isFromGuild();
}
@Override
public void reply(MessageCreateData message, boolean ephemeral, Consumer<AsyncMessage> consumer, LayoutComponent... components) {
if (getChannel() != null && getChannel().canTalk()) {
@@ -47,6 +47,11 @@ public class ModalTaskContext extends TaskContext {
return event.isAcknowledged();
}
@Override
public boolean isFromGuild() {
return event.isFromGuild();
}
@Override
public void reply(MessageCreateData message, boolean ephemeral, Consumer<AsyncMessage> consumer, LayoutComponent... components) {
if (isAcknowledged()) {
@@ -47,6 +47,11 @@ public class AsyncMessage extends AbstractContext {
return true;
}
@Override
public boolean isFromGuild() {
return false;
}
@Override
public void deleteMessage() {
if (hook != null) {
@@ -40,7 +40,14 @@ public class GuildManager {
}
public static GuildManager getManager(Guild guild) {
return getManager(guild.getId());
if (managers.containsKey(guild.getId())) {
return managers.get(guild.getId());
}
GuildManager manager = new GuildManager(guild);
managers.put(guild.getId(), manager);
return manager;
}
public static GuildManager getManager(String guildId) {
@@ -53,10 +60,7 @@ public class GuildManager {
throw new IllegalArgumentException("Guild does not exist in current cache");
}
GuildManager manager = new GuildManager(temp);
managers.put(guildId, manager);
return manager;
return getManager(temp);
}
// Getter
@@ -42,7 +42,7 @@ public class CommandManager {
return commands.get(commandString);
}
private static void initCommands() {
public static void initCommands() {
Reflections reflections = new Reflections("me.bobert.netneutrality.command");
Set<Class<? extends Command>> classes = reflections.getSubTypesOf(Command.class);
@@ -29,6 +29,10 @@ public class CommandProcessor {
}
public void dispatch() {
if (!CommandManager.commands.containsKey(context.parseCommandName())) {
return;
}
Command command = CommandManager.getCommand(context.parseCommandName());
context.setCommand(command);
@@ -36,12 +40,16 @@ public class CommandProcessor {
throw new IllegalArgumentException("Context has no command");
}
logger.info("Executing command {} on guild {}", command.getCommandName(), context.getGuild() == null ? "PrivateMessage": context.getGuild().getId());
if (command.isGuildOnly() && !context.isFromGuild()) {
return;
}
if (context instanceof SlashCommandContext slashCommandContext)
if (context instanceof SlashCommandContext slashCommandContext) {
logger.debug("SlashCommandInteractionId: {}", slashCommandContext.getEvent().getInteraction().getId());
}
try {
logger.info("Executing command {} on guild {}", command.getCommandName(), context.getGuild() == null ? "PrivateMessage": context.getGuild().getId());
configureSentry(command);
if (context instanceof MessageCommandContext && command.needAdmin() && !UserFunction.isAdmin(context.getGuildUser())) {
@@ -73,7 +81,6 @@ public class CommandProcessor {
if (result == ERROR) {
Exception exception = commandExecutionResult.getException();
logger.error("Error from command {}: {}", command.getCommandName(), message, exception);
}
@@ -41,6 +41,8 @@ public interface Context {
boolean isAcknowledged();
boolean isFromGuild();
void deleteMessage();
void reply(Modal modal);
@@ -19,9 +19,6 @@ public class MessageReceivedHook extends ListenerAdapter {
if (event.getAuthor().isBot()) return;
if (event.isWebhookMessage()) return;
if (!event.isFromGuild())
return;
String rawMessage = event.getMessage().getContentDisplay().trim();
String prefix = Settings.getInstance().get("NETNEUTRALITY_BOT_PREFIX", "$");