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 index f6b2a9e..fe039ac 100644 --- a/lib/src/main/java/net/tomatentum/marinara/interaction/methods/ButtonInteractionMethod.java +++ b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/ButtonInteractionMethod.java @@ -24,9 +24,9 @@ public class ButtonInteractionMethod extends InteractionMethod { } @Override - public Object getParameter(Object parameter, int index) { + public Object getParameter(Object context, int index) { Class type = getMethod().getParameterTypes()[index+1]; - return marinara.getWrapper().getComponentContextObject(parameter, type); + return marinara.getWrapper().getContextObjectProvider().getComponentContextObject(context, type); } @Override @@ -38,4 +38,5 @@ public class ButtonInteractionMethod extends InteractionMethod { public InteractionType getType() { return InteractionType.BUTTON; } + } diff --git a/lib/src/main/java/net/tomatentum/marinara/interaction/methods/SlashCommandInteractionMethod.java b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/SlashCommandInteractionMethod.java index 666dd3c..2f5faf4 100644 --- a/lib/src/main/java/net/tomatentum/marinara/interaction/methods/SlashCommandInteractionMethod.java +++ b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/SlashCommandInteractionMethod.java @@ -26,7 +26,7 @@ public class SlashCommandInteractionMethod extends InteractionMethod { @Override public Object getParameter(Object context, int index) { - return marinara.getWrapper().convertCommandOption(context, commandDefinition.options()[index].type(), commandDefinition.options()[index].name()); + return marinara.getWrapper().getContextObjectProvider().convertCommandOption(context, commandDefinition.options()[index].name()); } @Override diff --git a/lib/src/main/java/net/tomatentum/marinara/wrapper/ContextObjectProvider.java b/lib/src/main/java/net/tomatentum/marinara/wrapper/ContextObjectProvider.java new file mode 100644 index 0000000..27cbaa4 --- /dev/null +++ b/lib/src/main/java/net/tomatentum/marinara/wrapper/ContextObjectProvider.java @@ -0,0 +1,11 @@ +package net.tomatentum.marinara.wrapper; + +public interface ContextObjectProvider { + + public Object convertCommandOption(Object context, String optionName); + + public Object getComponentContextObject(Object context, Class type); + public Object getInteractionContextObject(Object context, Class type); + + public Object getAutocompleteFocusedOption(Object context); +} 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 4fe8ef9..06e1661 100644 --- a/lib/src/main/java/net/tomatentum/marinara/wrapper/LibraryWrapper.java +++ b/lib/src/main/java/net/tomatentum/marinara/wrapper/LibraryWrapper.java @@ -6,7 +6,6 @@ import java.util.function.Consumer; import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition; import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition; -import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType; import net.tomatentum.marinara.interaction.InteractionType; public abstract class LibraryWrapper { @@ -17,7 +16,6 @@ public abstract class LibraryWrapper { interactionSubscriber = new ArrayList<>(); } - public void handleInteraction(Object context) { interactionSubscriber.forEach((o) -> o.accept(context)); } @@ -32,9 +30,10 @@ 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); + + public abstract ContextObjectProvider getContextObjectProvider(); + } \ No newline at end of file diff --git a/wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordContextObjectProvider.java b/wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordContextObjectProvider.java new file mode 100644 index 0000000..f1b8f49 --- /dev/null +++ b/wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordContextObjectProvider.java @@ -0,0 +1,108 @@ +package net.tomatentum.marinara.wrapper.javacord; + +import org.javacord.api.interaction.AutocompleteInteraction; +import org.javacord.api.interaction.ButtonInteraction; +import org.javacord.api.interaction.SlashCommandInteraction; +import org.javacord.api.interaction.SlashCommandInteractionOption; + +import net.tomatentum.marinara.wrapper.ContextObjectProvider; + +public class JavacordContextObjectProvider implements ContextObjectProvider { + + @Override + public Object convertCommandOption(Object context, String optionName) { + if (!(context instanceof SlashCommandInteraction)) + return null; + SlashCommandInteraction interaction = (SlashCommandInteraction) context; + if (!interaction.getArguments().isEmpty()) + return getOptionValue(interaction.getOptionByName(optionName).get()); + + SlashCommandInteractionOption subCommandOption = interaction.getOptions().getFirst(); + + if (!subCommandOption.getOptions().isEmpty()) + subCommandOption = subCommandOption.getOptions().getFirst(); + + return getOptionValue(subCommandOption.getOptionByName(optionName).get()); + } + + private Object getOptionValue(SlashCommandInteractionOption option) { + switch (getOptionType(option)) { + case ATTACHMENT: + return option.getAttachmentValue().get(); + case BOOLEAN: + return option.getBooleanValue().get(); + case CHANNEL: + return option.getChannelValue().get(); + case DECIMAL: + return option.getDecimalValue().get(); + case LONG: + return option.getLongValue().get(); + case MENTIONABLE: + return option.getMentionableValue().get(); + case ROLE: + return option.getRoleValue().get(); + case STRING: + return option.getStringValue().get(); + case USER: + return option.getUserValue().get(); + default: + return null; + } + } + + + private org.javacord.api.interaction.SlashCommandOptionType getOptionType(SlashCommandInteractionOption option) { + if (option.getAttachmentValue().isPresent()) + return org.javacord.api.interaction.SlashCommandOptionType.ATTACHMENT; + if (option.getBooleanValue().isPresent()) + return org.javacord.api.interaction.SlashCommandOptionType.BOOLEAN; + if (option.getChannelValue().isPresent()) + return org.javacord.api.interaction.SlashCommandOptionType.CHANNEL; + if (option.getDecimalValue().isPresent()) + return org.javacord.api.interaction.SlashCommandOptionType.DECIMAL; + if (option.getLongValue().isPresent()) + return org.javacord.api.interaction.SlashCommandOptionType.LONG; + if (option.getMentionableValue().isPresent()) + return org.javacord.api.interaction.SlashCommandOptionType.MENTIONABLE; + if (option.getRoleValue().isPresent()) + return org.javacord.api.interaction.SlashCommandOptionType.ROLE; + if (option.getStringValue().isPresent()) + return org.javacord.api.interaction.SlashCommandOptionType.ATTACHMENT; + if (option.getUserValue().isPresent()) + return org.javacord.api.interaction.SlashCommandOptionType.USER; + + return org.javacord.api.interaction.SlashCommandOptionType.UNKNOWN; + } + + @Override + public Object getComponentContextObject(Object context, Class type) { + ButtonInteraction button = (ButtonInteraction) context; + switch (type.getName()) { + case "org.javacord.api.entity.message.Message": + return button.getMessage(); + default: + return getInteractionContextObject(context, type); + } + } + + @Override + public Object getInteractionContextObject(Object context, Class type) { + ButtonInteraction button = (ButtonInteraction) context; + switch (type.getName()) { + case "org.javacord.api.entity.channel.TextChannel": + return button.getChannel().orElse(null); + case "org.javacord.api.entity.server.Server": + return button.getServer().orElse(null); + case "org.javacord.api.entity.user.User": + return button.getUser(); + } + return null; + } + + @Override + public Object getAutocompleteFocusedOption(Object context) { + AutocompleteInteraction interaction = (AutocompleteInteraction) context; + return getOptionValue(interaction.getFocusedOption()); + } + +} diff --git a/wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordWrapper.java b/wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordWrapper.java index a0f7337..1bb3697 100644 --- a/wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordWrapper.java +++ b/wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordWrapper.java @@ -10,6 +10,7 @@ import java.util.Set; import org.javacord.api.DiscordApi; import org.javacord.api.interaction.ApplicationCommandInteraction; +import org.javacord.api.interaction.AutocompleteInteraction; import org.javacord.api.interaction.ButtonInteraction; import org.javacord.api.interaction.SlashCommandBuilder; import org.javacord.api.interaction.SlashCommandInteraction; @@ -25,15 +26,17 @@ import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOptio import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOptionChoice; import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup; -import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType; +import net.tomatentum.marinara.wrapper.ContextObjectProvider; import net.tomatentum.marinara.wrapper.LibraryWrapper; public class JavacordWrapper extends LibraryWrapper { private DiscordApi api; + private JavacordContextObjectProvider contextObjectProvider; public JavacordWrapper(DiscordApi api) { this.api = api; + this.contextObjectProvider = new JavacordContextObjectProvider(); api.addInteractionCreateListener((e) -> handleInteraction(e.getInteraction())); } @@ -43,6 +46,8 @@ public class JavacordWrapper extends LibraryWrapper { return InteractionType.COMMAND; if (ButtonInteraction.class.isAssignableFrom(clazz)) return InteractionType.BUTTON; + if (AutocompleteInteraction.class.isAssignableFrom(clazz)) + return InteractionType.AUTOCOMPLETE; return null; } @@ -68,22 +73,6 @@ public class JavacordWrapper extends LibraryWrapper { api.bulkOverwriteGlobalApplicationCommands(globalCommands); } - @Override - public Object convertCommandOption(Object context, SlashCommandOptionType type, String optionName) { - if (!(context instanceof SlashCommandInteraction)) - return null; - SlashCommandInteraction interaction = (SlashCommandInteraction) context; - if (!interaction.getArguments().isEmpty()) - return getOptionValue(interaction.getOptionByName(optionName).get(), type); - - SlashCommandInteractionOption subCommandOption = interaction.getOptions().getFirst(); - - if (!subCommandOption.getOptions().isEmpty()) - subCommandOption = subCommandOption.getOptions().getFirst(); - - return getOptionValue(subCommandOption.getOptionByName(optionName).get(), type); - } - @Override public ExecutableSlashCommandDefinition getCommandDefinition(Object context) { if (!(context instanceof SlashCommandInteraction)) @@ -153,31 +142,6 @@ public class JavacordWrapper extends LibraryWrapper { return org.javacord.api.interaction.SlashCommandOption.createWithChoices(type, option.name(), option.description(), option.required(), choices); } - private Object getOptionValue(SlashCommandInteractionOption option, SlashCommandOptionType type) { - switch (type) { - case ATTACHMENT: - return option.getAttachmentValue().get(); - case BOOLEAN: - return option.getBooleanValue().get(); - case CHANNEL: - return option.getChannelValue().get(); - case DECIMAL: - return option.getDecimalValue().get(); - case LONG: - return option.getLongValue().get(); - case MENTIONABLE: - return option.getMentionableValue().get(); - case ROLE: - return option.getRoleValue().get(); - case STRING: - return option.getStringValue().get(); - case USER: - return option.getUserValue().get(); - default: - return null; - } - } - @Override public String getButtonId(Object context) { ButtonInteraction button = (ButtonInteraction) context; @@ -185,20 +149,8 @@ public class JavacordWrapper extends LibraryWrapper { } @Override - public Object getComponentContextObject(Object context, Class type) { - ButtonInteraction button = (ButtonInteraction) context; - switch (type.getName()) { - case "org.javacord.api.entity.channel.TextChannel": - return button.getChannel().orElse(null); - case "org.javacord.api.entity.message.Message": - return button.getMessage(); - case "org.javacord.api.entity.server.Server": - return button.getServer().orElse(null); - case "org.javacord.api.entity.user.User": - return button.getUser(); - } - return null; + public ContextObjectProvider getContextObjectProvider() { + return contextObjectProvider; } - }