From 1dc63ef0af8401cfe776af1824f0a8f945caeabb Mon Sep 17 00:00:00 2001 From: bivashy <85439143+bivashy@users.noreply.github.com> Date: Mon, 11 Mar 2024 22:51:07 +0500 Subject: [PATCH] Replace @RenameTo annotation with built-in @Named (#161) * Replace @RenameTo annotation with internal @Named * Log out exceptions at discord command registration * Validate Interaction and InteractionHook before deferReply --- .../me/mastercapexd/auth/BaseAuthPlugin.java | 4 ++++ .../command/DiscordCommandRegistry.java | 10 ++-------- .../discord/command/annotation/RenameTo.java | 18 ------------------ .../discord/listener/JDACommandListener.java | 11 +++++++---- .../commands/AccountsListCommand.java | 4 ++-- 5 files changed, 15 insertions(+), 32 deletions(-) delete mode 100644 core/src/main/java/me/mastercapexd/auth/discord/command/annotation/RenameTo.java diff --git a/core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java b/core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java index 815810e3..d22dc5bf 100644 --- a/core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java +++ b/core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java @@ -214,6 +214,10 @@ private void initializeDiscord() { eventBus.register(new DiscordLinkRoleModifierListener()); new DiscordCommandRegistry(); + }).exceptionally(throwable -> { + throwable.printStackTrace(); + // TODO: Replace with proper logging + return null; }); } diff --git a/core/src/main/java/me/mastercapexd/auth/discord/command/DiscordCommandRegistry.java b/core/src/main/java/me/mastercapexd/auth/discord/command/DiscordCommandRegistry.java index fc6288f7..2d9fd6b9 100644 --- a/core/src/main/java/me/mastercapexd/auth/discord/command/DiscordCommandRegistry.java +++ b/core/src/main/java/me/mastercapexd/auth/discord/command/DiscordCommandRegistry.java @@ -1,10 +1,7 @@ package me.mastercapexd.auth.discord.command; -import java.util.Collections; - import com.bivashy.auth.api.AuthPlugin; -import me.mastercapexd.auth.discord.command.annotation.RenameTo; import me.mastercapexd.auth.discord.listener.JDACommandListener; import me.mastercapexd.auth.hooks.DiscordHook; import me.mastercapexd.auth.link.LinkCommandActorWrapper; @@ -13,15 +10,13 @@ import me.mastercapexd.auth.messenger.commands.MessengerCommandRegistry; import me.mastercapexd.auth.shared.commands.DiscordLinkCommand; import me.mastercapexd.auth.shared.commands.MessengerLinkCommandTemplate; -import net.dv8tion.jda.api.interactions.commands.OptionType; -import revxrsal.commands.annotation.dynamic.Annotations; import revxrsal.commands.command.CommandActor; import revxrsal.commands.exception.CommandErrorException; import revxrsal.commands.jda.JDAActor; import revxrsal.commands.jda.JDACommandHandler; -import revxrsal.commands.jda.annotation.OptionData; public class DiscordCommandRegistry extends MessengerCommandRegistry { + private static final AuthPlugin PLUGIN = AuthPlugin.instance(); private static final DiscordHook DISCORD_HOOK = PLUGIN.getHook(DiscordHook.class); private static final JDACommandHandler COMMAND_HANDLER = JDACommandHandler.create(DISCORD_HOOK.getJDA(), ""); @@ -30,8 +25,6 @@ public DiscordCommandRegistry() { super(COMMAND_HANDLER, DiscordLinkType.getInstance()); COMMAND_HANDLER.disableStackTraceSanitizing(); COMMAND_HANDLER.registerContextResolver(LinkCommandActorWrapper.class, context -> new DiscordCommandActorWrapper(context.actor())); - COMMAND_HANDLER.registerAnnotationReplacer(RenameTo.class, (element, parameter) -> Collections.singletonList( - Annotations.create(OptionData.class, "value", OptionType.valueOf(parameter.type()), "name", parameter.value()))); COMMAND_HANDLER.registerCondition((actor, command, arguments) -> { DiscordCommandActorWrapper actorWrapper = actor.as(DiscordCommandActorWrapper.class); @@ -67,4 +60,5 @@ protected LinkCommandActorWrapper wrapActor(CommandActor actor) { protected MessengerLinkCommandTemplate createLinkCommand() { return new DiscordLinkCommand(DiscordLinkType.getInstance().getLinkMessages()); } + } diff --git a/core/src/main/java/me/mastercapexd/auth/discord/command/annotation/RenameTo.java b/core/src/main/java/me/mastercapexd/auth/discord/command/annotation/RenameTo.java deleted file mode 100644 index 5314a3c5..00000000 --- a/core/src/main/java/me/mastercapexd/auth/discord/command/annotation/RenameTo.java +++ /dev/null @@ -1,18 +0,0 @@ -package me.mastercapexd.auth.discord.command.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Required and used in discord module. Used when we need to change {@link revxrsal.commands.annotation.Flag} or {@link revxrsal.commands.annotation.Named} - * name to another. - */ -@Target(ElementType.PARAMETER) -@Retention(RetentionPolicy.RUNTIME) -public @interface RenameTo { - String value(); - - String type(); -} diff --git a/core/src/main/java/me/mastercapexd/auth/discord/listener/JDACommandListener.java b/core/src/main/java/me/mastercapexd/auth/discord/listener/JDACommandListener.java index 86dbb402..3c4a5ab2 100644 --- a/core/src/main/java/me/mastercapexd/auth/discord/listener/JDACommandListener.java +++ b/core/src/main/java/me/mastercapexd/auth/discord/listener/JDACommandListener.java @@ -37,6 +37,7 @@ import revxrsal.commands.jda.core.actor.BaseJDASlashCommandActor; public class JDACommandListener implements EventListener { + private static final DiscordLinkType DISCORD_LINK_TYPE = DiscordLinkType.getInstance(); private final JDACommandHandler handler; private final Function wrapper; @@ -50,7 +51,7 @@ private void dispatch(JDAActor actor, ArgumentStack arguments) { LinkCommandActorWrapper actorWrapper = wrapper.apply(actor); try { handler.dispatch(actorWrapper, arguments); - } catch(Throwable t) { + } catch (Throwable t) { handler.getExceptionHandler().handleException(t, actorWrapper); } } @@ -90,7 +91,7 @@ private void onAutocompleteEvent(CommandAutoCompleteInteractionEvent event) { return new Choice(suggestion, Long.parseLong(suggestion)); return new Choice(suggestion, suggestion); }).collect(Collectors.toList())).queue(); - } catch(Throwable e) { + } catch (Throwable e) { e.printStackTrace(); } } @@ -105,7 +106,7 @@ private void onButtonEvent(ButtonInteractionEvent event) { try { ArgumentStack arguments = ArgumentStack.parse(content); dispatch(actor, arguments); - } catch(Throwable t) { + } catch (Throwable t) { handler.getExceptionHandler().handleException(t, actor); } } @@ -113,7 +114,8 @@ private void onButtonEvent(ButtonInteractionEvent event) { private void onSlashCommandEvent(SlashCommandInteractionEvent event) { parseSlashCommandEvent(event).ifPresent(arguments -> { JDAActor actor = new BaseJDASlashCommandActor(event, handler); - event.deferReply(true).queue(); + if (!event.getInteraction().isAcknowledged() && !event.getHook().isExpired()) + event.deferReply(true).queue(); dispatch(actor, arguments); }); } @@ -190,5 +192,6 @@ private void appendOptionMapping(Collection arguments, OptionMapping opt arguments.add(optionMapping.getAsString()); } } + } diff --git a/core/src/main/java/me/mastercapexd/auth/messenger/commands/AccountsListCommand.java b/core/src/main/java/me/mastercapexd/auth/messenger/commands/AccountsListCommand.java index 2e65572b..4faede2e 100644 --- a/core/src/main/java/me/mastercapexd/auth/messenger/commands/AccountsListCommand.java +++ b/core/src/main/java/me/mastercapexd/auth/messenger/commands/AccountsListCommand.java @@ -13,7 +13,6 @@ import com.bivashy.messenger.common.button.ButtonColor; import com.bivashy.messenger.common.keyboard.Keyboard; -import me.mastercapexd.auth.discord.command.annotation.RenameTo; import me.mastercapexd.auth.link.LinkCommandActorWrapper; import me.mastercapexd.auth.messenger.commands.annotation.CommandKey; import me.mastercapexd.auth.shared.commands.annotation.CommandCooldown; @@ -21,6 +20,7 @@ import revxrsal.commands.annotation.DefaultFor; import revxrsal.commands.annotation.Dependency; import revxrsal.commands.annotation.Flag; +import revxrsal.commands.annotation.Named; import revxrsal.commands.orphan.OrphanCommand; @CommandKey(AccountsListCommand.CONFIGURATION_KEY) @@ -35,7 +35,7 @@ public class AccountsListCommand implements OrphanCommand { @DefaultFor("~") @CommandCooldown(CommandCooldown.DEFAULT_VALUE) public void onAccountsMenu(LinkCommandActorWrapper actorWrapper, LinkType linkType, @Flag("page") @Default("1") Integer page, - @RenameTo(value = "size", type = "NUMBER") @Flag("pageSize") @Default("5") Integer accountsPerPage, + @Flag("pageSize") @Named("size") @Default("5") Integer accountsPerPage, @Flag("type") @Default("my") AccountListType type) { if (!linkType.getSettings().isAdministrator(actorWrapper.userId()) && type.isAdministratorOnly) { actorWrapper.reply(linkType.getLinkMessages().getMessage("not-enough-permission"));