fix(CommandProcessor): missing guild check and if command exists before execution
This commit is contained in:
1 parent
9ee4797aaf
commit
5a8e606a89
14 files changed
+61
-17
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,12 +67,14 @@ public class CryptoPriceCommand extends AbstractCommand {
|
||||
String[] args = context.getArgs();
|
||||
String currency = "USD";
|
||||
|
||||
if (args.length > 0) {
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -102,7 +104,6 @@ public class CryptoPriceCommand extends AbstractCommand {
|
||||
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", "$");
|
||||
|
||||
|
||||
Reference in new issue
Block a user