From 0ce0b3eb4fd7b312df159a532bb4de04ea78c83e Mon Sep 17 00:00:00 2001 From: tueem Date: Tue, 5 Nov 2024 15:54:49 +0100 Subject: [PATCH] Add Button Interaction with specific method parameter support. --- .../interaction/annotation/Button.java | 5 +++ .../methods/ButtonInteractionMethod.java | 40 +++++++++++++++++++ .../marinara/wrapper/LibraryWrapper.java | 6 ++- 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 lib/src/main/java/net/tomatentum/marinara/interaction/annotation/Button.java create mode 100644 lib/src/main/java/net/tomatentum/marinara/interaction/methods/ButtonInteractionMethod.java diff --git a/lib/src/main/java/net/tomatentum/marinara/interaction/annotation/Button.java b/lib/src/main/java/net/tomatentum/marinara/interaction/annotation/Button.java new file mode 100644 index 0000000..0032484 --- /dev/null +++ b/lib/src/main/java/net/tomatentum/marinara/interaction/annotation/Button.java @@ -0,0 +1,5 @@ +package net.tomatentum.marinara.interaction.annotation; + +public @interface Button { + public String customId(); +} diff --git a/lib/src/main/java/net/tomatentum/marinara/interaction/methods/ButtonInteractionMethod.java b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/ButtonInteractionMethod.java new file mode 100644 index 0000000..7e1014a --- /dev/null +++ b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/ButtonInteractionMethod.java @@ -0,0 +1,40 @@ +package net.tomatentum.marinara.interaction.methods; + +import java.lang.reflect.Method; + +import net.tomatentum.marinara.interaction.InteractionHandler; +import net.tomatentum.marinara.interaction.InteractionType; +import net.tomatentum.marinara.interaction.annotation.Button; +import net.tomatentum.marinara.wrapper.LibraryWrapper; + +public class ButtonInteractionMethod extends InteractionMethod { + + private String customId; + + ButtonInteractionMethod(Method method, InteractionHandler handler, LibraryWrapper wrapper) { + super(method, handler, wrapper); + parseMethod(); + } + + @Override + public Object getParameter(Object parameter, int index) { + Class type = getMethod().getParameterTypes()[index]; + return wrapper.getComponentContextObject(parameter, type); + } + + @Override + public boolean canRun(Object context) { + return wrapper.getButtonId(context).equals(customId); + } + + @Override + public InteractionType getType() { + return InteractionType.BUTTON; + } + + private void parseMethod() { + Button button = getMethod().getAnnotation(Button.class); + this.customId = button.customId(); + } + +} diff --git a/lib/src/main/java/net/tomatentum/marinara/wrapper/LibraryWrapper.java b/lib/src/main/java/net/tomatentum/marinara/wrapper/LibraryWrapper.java index ccbb6c9..4fe8ef9 100644 --- a/lib/src/main/java/net/tomatentum/marinara/wrapper/LibraryWrapper.java +++ b/lib/src/main/java/net/tomatentum/marinara/wrapper/LibraryWrapper.java @@ -17,7 +17,6 @@ public abstract class LibraryWrapper { interactionSubscriber = new ArrayList<>(); } - public abstract void registerSlashCommands(SlashCommandDefinition[] defs); public void handleInteraction(Object context) { interactionSubscriber.forEach((o) -> o.accept(context)); @@ -31,6 +30,11 @@ public abstract class LibraryWrapper { } public abstract InteractionType getInteractionType(Class clazz); + + public abstract void registerSlashCommands(SlashCommandDefinition[] defs); public abstract Object convertCommandOption(Object context, SlashCommandOptionType type, String optionName); public abstract ExecutableSlashCommandDefinition getCommandDefinition(Object context); + + public abstract String getButtonId(Object context); + public abstract Object getComponentContextObject(Object context, Class type); } \ No newline at end of file