Skip to content

Commit

Permalink
Replace @RenameTo annotation with built-in @nAmed (#161)
Browse files Browse the repository at this point in the history
* Replace @RenameTo annotation with internal @nAmed

* Log out exceptions at discord command registration

* Validate Interaction and InteractionHook before deferReply
  • Loading branch information
bivashy authored Mar 11, 2024
1 parent 223d96e commit 1dc63ef
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 32 deletions.
4 changes: 4 additions & 0 deletions core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ private void initializeDiscord() {

eventBus.register(new DiscordLinkRoleModifierListener());
new DiscordCommandRegistry();
}).exceptionally(throwable -> {
throwable.printStackTrace();
// TODO: Replace with proper logging
return null;
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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(), "");
Expand All @@ -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);
Expand Down Expand Up @@ -67,4 +60,5 @@ protected LinkCommandActorWrapper wrapActor(CommandActor actor) {
protected MessengerLinkCommandTemplate createLinkCommand() {
return new DiscordLinkCommand(DiscordLinkType.getInstance().getLinkMessages());
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<JDAActor, LinkCommandActorWrapper> wrapper;
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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();
}
}
Expand All @@ -105,15 +106,16 @@ 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);
}
}

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);
});
}
Expand Down Expand Up @@ -190,5 +192,6 @@ private void appendOptionMapping(Collection<String> arguments, OptionMapping opt
arguments.add(optionMapping.getAsString());
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
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;
import revxrsal.commands.annotation.Default;
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)
Expand All @@ -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"));
Expand Down

0 comments on commit 1dc63ef

Please sign in to comment.