create seperate class for ContextObjectProviders and renamed some context parameters from parameter to context.
First parts of AutocompleteInteraction added
This commit is contained in:
parent
f32c7045a1
commit
7a2c15d877
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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.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();
|
||||
|
||||
}
|
108
wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordContextObjectProvider.java
Normal file
108
wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordContextObjectProvider.java
Normal file
@ -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.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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user