Add isSlashCommandOnly to command interface

This commit is contained in:
anthorob committed 2024-12-14 00:59:50 -05:00
1 parent 6b453bb7e2
commit 50430589e6
6 files changed
+23 -11

No files matched your search

@@ -17,6 +17,11 @@ public abstract class AbstractCommand implements Command {
return true; return true;
} }
@Override
public boolean isSlashCommandOnly() {
return false;
}
@Override @Override
public DefaultMemberPermissions getSlashCommandPermission() { public DefaultMemberPermissions getSlashCommandPermission() {
return DefaultMemberPermissions.ENABLED; return DefaultMemberPermissions.ENABLED;
@@ -22,6 +22,8 @@ public interface Command {
boolean isSlashCommand(); boolean isSlashCommand();
boolean isSlashCommandOnly();
String getCommandName(); String getCommandName();
String getDescription(); String getDescription();
@@ -31,6 +31,11 @@ public class ReportIssueCommand extends AbstractCommand {
return false; return false;
} }
@Override
public boolean isSlashCommandOnly() {
return true;
}
@Override @Override
public String getCommandName() { public String getCommandName() {
return "report-bug"; return "report-bug";
@@ -62,11 +62,11 @@ public class SlashCommandContext extends CommandContext implements InteractionCo
@Override @Override
public String getSubcommandGroup() { public String getSubcommandGroup() {
return getFullCommand().split(" ")[2]; return event.getSubcommandGroup();
} }
@Override @Override
public String getSubcommand() { public String getSubcommand() {
return getFullCommand().split(" ")[1]; return event.getSubcommandName();
} }
} }
@@ -8,7 +8,6 @@ package me.bobert.netneutrality.core.command;
import io.sentry.Sentry; import io.sentry.Sentry;
import me.bobert.netneutrality.command.Command; import me.bobert.netneutrality.command.Command;
import me.bobert.netneutrality.command.CommandOptions; import me.bobert.netneutrality.command.CommandOptions;
import me.bobert.netneutrality.context.MessageCommandContext;
import me.bobert.netneutrality.context.SlashCommandContext; import me.bobert.netneutrality.context.SlashCommandContext;
import me.bobert.netneutrality.core.LogHandler; import me.bobert.netneutrality.core.LogHandler;
import me.bobert.netneutrality.core.context.CommandContext; import me.bobert.netneutrality.core.context.CommandContext;
@@ -57,8 +56,9 @@ public class CommandProcessor {
return; return;
} }
if (context instanceof SlashCommandContext slashCommandContext) { if (command.isSlashCommandOnly() && !(context instanceof SlashCommandContext)) {
logger.debug("SlashCommandInteractionId: {}", slashCommandContext.getEvent().getInteraction().getId()); context.replyEphemeral("This command is only available as a slash command");
return;
} }
try { try {
@@ -24,7 +24,7 @@ public interface InteractionContext extends Context {
throw new RuntimeException("Interaction isn't acknowledged yet"); throw new RuntimeException("Interaction isn't acknowledged yet");
} }
return getReplyCallback(getEvent()).getHook(); return getReplyCallback().getHook();
} }
@Override @Override
@@ -38,7 +38,7 @@ public interface InteractionContext extends Context {
throw new RuntimeException("Interaction is already acknowledged, please use getInteractionHook() instead."); throw new RuntimeException("Interaction is already acknowledged, please use getInteractionHook() instead.");
} }
getReplyCallback(getEvent()).deferReply(ephemeral).queue(hook -> consumer.accept(new AsyncContext(hook))); getReplyCallback().deferReply(ephemeral).queue(hook -> consumer.accept(new AsyncContext(hook)));
} }
@Override @Override
@@ -70,14 +70,14 @@ public interface InteractionContext extends Context {
return; return;
} }
getReplyCallback(getEvent()).reply(messageCreateData).setEphemeral(ephemeral).setComponents(components).queue(message -> consumer.accept(new AsyncContext(message))); getReplyCallback().reply(messageCreateData).setEphemeral(ephemeral).setComponents(components).queue(message -> consumer.accept(new AsyncContext(message)));
} }
static IReplyCallback getReplyCallback(Event event) { private IReplyCallback getReplyCallback() {
try { try {
return (IReplyCallback) event; return (IReplyCallback) getEvent();
} catch (ClassCastException e) { } catch (ClassCastException e) {
throw new IllegalArgumentException(event.getClass().getSimpleName() + " does not implement class 'IReplyCallback'", e); throw new IllegalArgumentException(getEvent().getClass().getSimpleName() + " does not implement class 'IReplyCallback'", e);
} }
} }
} }