create seperate class for ContextObjectProviders and renamed some context parameters from parameter to context.

First parts of AutocompleteInteraction added
This commit is contained in:
Tueem 2024-12-15 15:08:34 +01:00
parent f32c7045a1
commit 7a2c15d877
Signed by: tueem
GPG Key ID: F2CE0513D231AD7A
6 changed files with 134 additions and 63 deletions
lib/src/main/java/net/tomatentum/marinara
wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord

@ -24,9 +24,9 @@ public class ButtonInteractionMethod extends InteractionMethod {
} }
@Override @Override
public Object getParameter(Object parameter, int index) { public Object getParameter(Object context, int index) {
Class<?> type = getMethod().getParameterTypes()[index+1]; Class<?> type = getMethod().getParameterTypes()[index+1];
return marinara.getWrapper().getComponentContextObject(parameter, type); return marinara.getWrapper().getContextObjectProvider().getComponentContextObject(context, type);
} }
@Override @Override
@ -38,4 +38,5 @@ public class ButtonInteractionMethod extends InteractionMethod {
public InteractionType getType() { public InteractionType getType() {
return InteractionType.BUTTON; return InteractionType.BUTTON;
} }
} }

@ -26,7 +26,7 @@ public class SlashCommandInteractionMethod extends InteractionMethod {
@Override @Override
public Object getParameter(Object context, int index) { 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 @Override

@ -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);
}

@ -6,7 +6,6 @@ import java.util.function.Consumer;
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition; import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition; import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition;
import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType;
import net.tomatentum.marinara.interaction.InteractionType; import net.tomatentum.marinara.interaction.InteractionType;
public abstract class LibraryWrapper { public abstract class LibraryWrapper {
@ -17,7 +16,6 @@ public abstract class LibraryWrapper {
interactionSubscriber = new ArrayList<>(); interactionSubscriber = new ArrayList<>();
} }
public void handleInteraction(Object context) { public void handleInteraction(Object context) {
interactionSubscriber.forEach((o) -> o.accept(context)); interactionSubscriber.forEach((o) -> o.accept(context));
} }
@ -32,9 +30,10 @@ public abstract class LibraryWrapper {
public abstract InteractionType getInteractionType(Class<?> clazz); public abstract InteractionType getInteractionType(Class<?> clazz);
public abstract void registerSlashCommands(SlashCommandDefinition[] defs); public abstract void registerSlashCommands(SlashCommandDefinition[] defs);
public abstract Object convertCommandOption(Object context, SlashCommandOptionType type, String optionName);
public abstract ExecutableSlashCommandDefinition getCommandDefinition(Object context); public abstract ExecutableSlashCommandDefinition getCommandDefinition(Object context);
public abstract String getButtonId(Object context); public abstract String getButtonId(Object context);
public abstract Object getComponentContextObject(Object context, Class<?> type);
public abstract ContextObjectProvider getContextObjectProvider();
} }

@ -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());
}
}

@ -10,6 +10,7 @@ import java.util.Set;
import org.javacord.api.DiscordApi; import org.javacord.api.DiscordApi;
import org.javacord.api.interaction.ApplicationCommandInteraction; import org.javacord.api.interaction.ApplicationCommandInteraction;
import org.javacord.api.interaction.AutocompleteInteraction;
import org.javacord.api.interaction.ButtonInteraction; import org.javacord.api.interaction.ButtonInteraction;
import org.javacord.api.interaction.SlashCommandBuilder; import org.javacord.api.interaction.SlashCommandBuilder;
import org.javacord.api.interaction.SlashCommandInteraction; 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.SlashCommandOptionChoice;
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup; 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; import net.tomatentum.marinara.wrapper.LibraryWrapper;
public class JavacordWrapper extends LibraryWrapper { public class JavacordWrapper extends LibraryWrapper {
private DiscordApi api; private DiscordApi api;
private JavacordContextObjectProvider contextObjectProvider;
public JavacordWrapper(DiscordApi api) { public JavacordWrapper(DiscordApi api) {
this.api = api; this.api = api;
this.contextObjectProvider = new JavacordContextObjectProvider();
api.addInteractionCreateListener((e) -> handleInteraction(e.getInteraction())); api.addInteractionCreateListener((e) -> handleInteraction(e.getInteraction()));
} }
@ -43,6 +46,8 @@ public class JavacordWrapper extends LibraryWrapper {
return InteractionType.COMMAND; return InteractionType.COMMAND;
if (ButtonInteraction.class.isAssignableFrom(clazz)) if (ButtonInteraction.class.isAssignableFrom(clazz))
return InteractionType.BUTTON; return InteractionType.BUTTON;
if (AutocompleteInteraction.class.isAssignableFrom(clazz))
return InteractionType.AUTOCOMPLETE;
return null; return null;
} }
@ -68,22 +73,6 @@ public class JavacordWrapper extends LibraryWrapper {
api.bulkOverwriteGlobalApplicationCommands(globalCommands); 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 @Override
public ExecutableSlashCommandDefinition getCommandDefinition(Object context) { public ExecutableSlashCommandDefinition getCommandDefinition(Object context) {
if (!(context instanceof SlashCommandInteraction)) 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); 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 @Override
public String getButtonId(Object context) { public String getButtonId(Object context) {
ButtonInteraction button = (ButtonInteraction) context; ButtonInteraction button = (ButtonInteraction) context;
@ -185,20 +149,8 @@ public class JavacordWrapper extends LibraryWrapper {
} }
@Override @Override
public Object getComponentContextObject(Object context, Class<?> type) { public ContextObjectProvider getContextObjectProvider() {
ButtonInteraction button = (ButtonInteraction) context; return contextObjectProvider;
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;
}
} }