add IdentifierProvider and wrapper implementations
This commit is contained in:
parent
f940f48566
commit
432cf78a2e
@ -1,5 +1,6 @@
|
|||||||
package net.tomatentum.marinara.interaction.ident;
|
package net.tomatentum.marinara.interaction.ident;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
||||||
|
|
||||||
public class RootCommandIdentifier extends SlashCommandIdentifier {
|
public class RootCommandIdentifier extends SlashCommandIdentifier {
|
||||||
@ -10,10 +11,10 @@ public class RootCommandIdentifier extends SlashCommandIdentifier {
|
|||||||
InteractionIdentifier parent,
|
InteractionIdentifier parent,
|
||||||
String name,
|
String name,
|
||||||
String description,
|
String description,
|
||||||
|
InteractionType type,
|
||||||
SlashCommandOption[] options,
|
SlashCommandOption[] options,
|
||||||
boolean isAutocomplete,
|
|
||||||
long[] serverIds) {
|
long[] serverIds) {
|
||||||
super(parent, name, description, options, isAutocomplete);
|
super(parent, name, description, type, options);
|
||||||
this.serverIds = serverIds;
|
this.serverIds = serverIds;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,7 +27,6 @@ public class RootCommandIdentifier extends SlashCommandIdentifier {
|
|||||||
private String name;
|
private String name;
|
||||||
private String description;
|
private String description;
|
||||||
private SlashCommandOption[] options;
|
private SlashCommandOption[] options;
|
||||||
private boolean isAutocomplete = false;
|
|
||||||
private long[] serverIds;
|
private long[] serverIds;
|
||||||
|
|
||||||
|
|
||||||
@ -66,15 +66,6 @@ public class RootCommandIdentifier extends SlashCommandIdentifier {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean autocomplete() {
|
|
||||||
return this.isAutocomplete;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Builder autocomplete(boolean isAutocomplete) {
|
|
||||||
this.isAutocomplete = isAutocomplete;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long[] serverIds() {
|
public long[] serverIds() {
|
||||||
return this.serverIds;
|
return this.serverIds;
|
||||||
}
|
}
|
||||||
@ -84,8 +75,14 @@ public class RootCommandIdentifier extends SlashCommandIdentifier {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SlashCommandIdentifier build() {
|
public SlashCommandIdentifier build(boolean autocomplete) {
|
||||||
return new RootCommandIdentifier(parent, name, description, options, isAutocomplete, serverIds);
|
return new RootCommandIdentifier(
|
||||||
|
parent,
|
||||||
|
name,
|
||||||
|
description,
|
||||||
|
autocomplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND,
|
||||||
|
options,
|
||||||
|
serverIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,10 +11,10 @@ public class SlashCommandIdentifier extends InteractionIdentifier {
|
|||||||
InteractionIdentifier parent,
|
InteractionIdentifier parent,
|
||||||
String name,
|
String name,
|
||||||
String description,
|
String description,
|
||||||
SlashCommandOption[] options,
|
InteractionType type,
|
||||||
boolean isAutocomplete
|
SlashCommandOption[] options
|
||||||
) {
|
) {
|
||||||
super(parent, name, description, isAutocomplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND);
|
super(parent, name, description, type);
|
||||||
this.options = options;
|
this.options = options;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,7 +27,6 @@ public class SlashCommandIdentifier extends InteractionIdentifier {
|
|||||||
private String name;
|
private String name;
|
||||||
private String description;
|
private String description;
|
||||||
private SlashCommandOption[] options;
|
private SlashCommandOption[] options;
|
||||||
private boolean isAutocomplete = false;
|
|
||||||
|
|
||||||
public InteractionIdentifier parent() {
|
public InteractionIdentifier parent() {
|
||||||
return parent;
|
return parent;
|
||||||
@ -65,17 +64,13 @@ public class SlashCommandIdentifier extends InteractionIdentifier {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean autocomplete() {
|
public SlashCommandIdentifier build(boolean autocomplete) {
|
||||||
return this.isAutocomplete;
|
return new SlashCommandIdentifier(
|
||||||
}
|
parent,
|
||||||
|
name,
|
||||||
public Builder autocomplete(boolean isAutocomplete) {
|
description,
|
||||||
this.isAutocomplete = isAutocomplete;
|
autocomplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND,
|
||||||
return this;
|
options);
|
||||||
}
|
|
||||||
|
|
||||||
public SlashCommandIdentifier build() {
|
|
||||||
return new SlashCommandIdentifier(parent, name, description, options, isAutocomplete);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -37,9 +37,8 @@ public class SlashCommandParser implements AnnotationParser {
|
|||||||
.name(cmd.name())
|
.name(cmd.name())
|
||||||
.description(cmd.description())
|
.description(cmd.description())
|
||||||
.options(cmd.options())
|
.options(cmd.options())
|
||||||
.autocomplete(isAutoComplete)
|
|
||||||
.serverIds(cmd.serverIds())
|
.serverIds(cmd.serverIds())
|
||||||
.build();
|
.build(isAutoComplete);
|
||||||
|
|
||||||
if (ReflectionUtil.isAnnotationPresent(method, SubCommandGroup.class)) {
|
if (ReflectionUtil.isAnnotationPresent(method, SubCommandGroup.class)) {
|
||||||
SubCommandGroup cmdGroup = ReflectionUtil.getAnnotation(method, SubCommandGroup.class);
|
SubCommandGroup cmdGroup = ReflectionUtil.getAnnotation(method, SubCommandGroup.class);
|
||||||
@ -57,8 +56,7 @@ public class SlashCommandParser implements AnnotationParser {
|
|||||||
.name(subCmd.name())
|
.name(subCmd.name())
|
||||||
.description(subCmd.description())
|
.description(subCmd.description())
|
||||||
.options(subCmd.options())
|
.options(subCmd.options())
|
||||||
.autocomplete(isAutoComplete)
|
.build(isAutoComplete);
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.trace("Parsed using SlashCommandParser for method {} with the result:\n{}", ReflectionUtil.getFullMethodName(method), lastIdentifier.toString());
|
logger.trace("Parsed using SlashCommandParser for method {} with the result:\n{}", ReflectionUtil.getFullMethodName(method), lastIdentifier.toString());
|
||||||
|
@ -16,17 +16,19 @@ import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
|||||||
import net.tomatentum.marinara.interaction.ident.RootCommandIdentifier;
|
import net.tomatentum.marinara.interaction.ident.RootCommandIdentifier;
|
||||||
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
|
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
import net.tomatentum.marinara.util.LoggerUtil;
|
||||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
import net.tomatentum.marinara.interaction.methods.InteractionMethod;
|
import net.tomatentum.marinara.interaction.methods.InteractionMethod;
|
||||||
|
|
||||||
public class InteractionRegistry {
|
public class InteractionRegistry {
|
||||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||||
private Set<InteractionEntry> interactions;
|
private Set<InteractionEntry> interactions;
|
||||||
private Marinara marinara;
|
private Marinara marinara;
|
||||||
|
private IdentifierProvider identifierProvider;
|
||||||
|
|
||||||
public InteractionRegistry(Marinara marinara) {
|
public InteractionRegistry(Marinara marinara) {
|
||||||
this.interactions = new HashSet<>();
|
this.interactions = new HashSet<>();
|
||||||
this.marinara = marinara;
|
this.marinara = marinara;
|
||||||
|
this.identifierProvider = marinara.getWrapper().createIdentifierProvider();
|
||||||
marinara.getWrapper().subscribeInteractions(this::handle);
|
marinara.getWrapper().subscribeInteractions(this::handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,9 +76,8 @@ public class InteractionRegistry {
|
|||||||
|
|
||||||
public void handle(Object context) {
|
public void handle(Object context) {
|
||||||
logger.debug("Received {} interaction ", context);
|
logger.debug("Received {} interaction ", context);
|
||||||
LibraryWrapper wrapper = marinara.getWrapper();
|
|
||||||
interactions.forEach((e) -> {
|
interactions.forEach((e) -> {
|
||||||
if (wrapper.getInteractionIdentifier(context).equals(e.identifier())) {
|
if (this.identifierProvider.provide(context).equals(e.identifier())) {
|
||||||
logger.info("Running {} interaction using {}\ncontext: {}", e.type(), e.toString(), context.toString());
|
logger.info("Running {} interaction using {}\ncontext: {}", e.type(), e.toString(), context.toString());
|
||||||
e.runAll(context);
|
e.runAll(context);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,72 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper;
|
||||||
|
|
||||||
|
import java.lang.reflect.ParameterizedType;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
import io.leangen.geantyref.GenericTypeReflector;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.util.LoggerUtil;
|
||||||
|
import net.tomatentum.marinara.util.ReflectionUtil;
|
||||||
|
|
||||||
|
public class IdentifierProvider {
|
||||||
|
|
||||||
|
public static IdentifierProvider of(Converter<?>... converter) {
|
||||||
|
return new IdentifierProvider(Arrays.asList(converter));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<Class<?>, Converter<?>> converter;
|
||||||
|
private Logger logger = LoggerUtil.getLogger(getClass());
|
||||||
|
|
||||||
|
private IdentifierProvider(List<Converter<?>> converter) {
|
||||||
|
this.converter = new HashMap<>();
|
||||||
|
for (Converter<?> conv : converter) {
|
||||||
|
if (conv.getClass().getName().contains("$$Lambda"))
|
||||||
|
throw new IllegalArgumentException("Lambdas cannot be used for IdentifierConverter because of Type erasure.");
|
||||||
|
Type type = GenericTypeReflector.getExactSuperType(conv.getClass(), Converter.class);
|
||||||
|
Type parameterType = (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0];
|
||||||
|
if (!(parameterType instanceof Class))
|
||||||
|
throw new IllegalArgumentException("Only full Class types are supported by IdentiferConverters");
|
||||||
|
this.converter.put((Class<?>) parameterType, conv);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public InteractionIdentifier provide(Object context) {
|
||||||
|
Type type = ReflectionUtil.getMostSpecificClass(
|
||||||
|
converter.keySet().stream().filter(x -> x.isAssignableFrom(context.getClass())).toArray(Class<?>[]::new),
|
||||||
|
context.getClass());
|
||||||
|
|
||||||
|
if (type == null)
|
||||||
|
logger.debug("No Identifier converter found for context {}", context.getClass().toString());
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Converter<Object> conv = (Converter<Object>) converter.get(type);
|
||||||
|
|
||||||
|
return conv.convert(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Converter<T extends Object> {
|
||||||
|
InteractionIdentifier convert(T context);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class LambdaWrapper<T extends Object> implements Converter<T> {
|
||||||
|
|
||||||
|
private Converter<T> converter;
|
||||||
|
|
||||||
|
LambdaWrapper(Converter<T> converter) {
|
||||||
|
this.converter = converter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InteractionIdentifier convert(T context) {
|
||||||
|
return this.converter.convert(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -5,14 +5,13 @@ import java.util.List;
|
|||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
||||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
|
||||||
|
|
||||||
public abstract class LibraryWrapper {
|
public abstract class LibraryWrapper {
|
||||||
|
|
||||||
private List<Consumer<Object>> interactionSubscriber;
|
private List<Consumer<Object>> interactionSubscriber;
|
||||||
|
|
||||||
protected LibraryWrapper() {
|
protected LibraryWrapper() {
|
||||||
interactionSubscriber = new ArrayList<>();
|
this.interactionSubscriber = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleInteraction(Object context) {
|
public void handleInteraction(Object context) {
|
||||||
@ -27,8 +26,8 @@ public abstract class LibraryWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public abstract void registerSlashCommands(SlashCommandDefinition[] defs);
|
public abstract void registerSlashCommands(SlashCommandDefinition[] defs);
|
||||||
public abstract InteractionIdentifier getInteractionIdentifier(Object context);
|
|
||||||
|
|
||||||
|
public abstract IdentifierProvider createIdentifierProvider();
|
||||||
public abstract ContextObjectProvider getContextObjectProvider();
|
public abstract ContextObjectProvider getContextObjectProvider();
|
||||||
|
|
||||||
}
|
}
|
@ -8,9 +8,6 @@ import java.util.function.Function;
|
|||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
import discord4j.core.GatewayDiscordClient;
|
import discord4j.core.GatewayDiscordClient;
|
||||||
import discord4j.core.event.domain.interaction.ButtonInteractionEvent;
|
|
||||||
import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent;
|
|
||||||
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
|
|
||||||
import discord4j.core.event.domain.interaction.InteractionCreateEvent;
|
import discord4j.core.event.domain.interaction.InteractionCreateEvent;
|
||||||
import discord4j.core.object.command.ApplicationCommandInteractionOption;
|
import discord4j.core.object.command.ApplicationCommandInteractionOption;
|
||||||
import discord4j.core.object.command.ApplicationCommandOption.Type;
|
import discord4j.core.object.command.ApplicationCommandOption.Type;
|
||||||
@ -18,13 +15,15 @@ import discord4j.discordjson.json.ApplicationCommandOptionChoiceData;
|
|||||||
import discord4j.discordjson.json.ApplicationCommandOptionData;
|
import discord4j.discordjson.json.ApplicationCommandOptionData;
|
||||||
import discord4j.discordjson.json.ApplicationCommandRequest;
|
import discord4j.discordjson.json.ApplicationCommandRequest;
|
||||||
|
|
||||||
import net.tomatentum.marinara.interaction.InteractionType;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
||||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
import net.tomatentum.marinara.util.LoggerUtil;
|
||||||
import net.tomatentum.marinara.wrapper.CommandConverter;
|
import net.tomatentum.marinara.wrapper.CommandConverter;
|
||||||
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||||
|
import net.tomatentum.marinara.wrapper.discord4j.identifierconverter.AutocompleteIdentifierConverter;
|
||||||
|
import net.tomatentum.marinara.wrapper.discord4j.identifierconverter.ButtonIdentifierConverter;
|
||||||
|
import net.tomatentum.marinara.wrapper.discord4j.identifierconverter.SlashCommandIdentifierConverter;
|
||||||
|
|
||||||
public class Discord4JWrapper extends LibraryWrapper {
|
public class Discord4JWrapper extends LibraryWrapper {
|
||||||
|
|
||||||
@ -82,49 +81,12 @@ public class Discord4JWrapper extends LibraryWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InteractionIdentifier getInteractionIdentifier(Object context) {
|
public IdentifierProvider createIdentifierProvider() {
|
||||||
|
return IdentifierProvider.of(
|
||||||
if (context instanceof ButtonInteractionEvent) {
|
new SlashCommandIdentifierConverter(),
|
||||||
ButtonInteractionEvent interaction = (ButtonInteractionEvent) context;
|
new AutocompleteIdentifierConverter(),
|
||||||
return InteractionIdentifier.builder().name(interaction.getCustomId()).type(InteractionType.BUTTON).build();
|
new ButtonIdentifierConverter()
|
||||||
}
|
);
|
||||||
|
|
||||||
List<ApplicationCommandInteractionOption> options;
|
|
||||||
String commandName;
|
|
||||||
boolean isAutocomplete = false;
|
|
||||||
|
|
||||||
if (context instanceof ChatInputInteractionEvent) {
|
|
||||||
ChatInputInteractionEvent interaction = (ChatInputInteractionEvent) context;
|
|
||||||
options = SUB_FILTER.apply(interaction.getOptions());
|
|
||||||
commandName = interaction.getCommandName();
|
|
||||||
}else if (context instanceof ChatInputAutoCompleteEvent) {
|
|
||||||
ChatInputAutoCompleteEvent interaction = (ChatInputAutoCompleteEvent) context;
|
|
||||||
options = SUB_FILTER.apply(interaction.getOptions());
|
|
||||||
commandName = interaction.getCommandName();
|
|
||||||
isAutocomplete = true;
|
|
||||||
}else
|
|
||||||
return null;
|
|
||||||
|
|
||||||
InteractionIdentifier last = InteractionIdentifier.slashBuilder().name(commandName).autocomplete(isAutocomplete).build();
|
|
||||||
|
|
||||||
if (!options.isEmpty()) {
|
|
||||||
List<ApplicationCommandInteractionOption> sub_options = SUB_FILTER.apply(options.getFirst().getOptions());
|
|
||||||
if (!sub_options.isEmpty()) {
|
|
||||||
last = InteractionIdentifier.builder()
|
|
||||||
.name(options.getFirst().getName())
|
|
||||||
.type(isAutocomplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND)
|
|
||||||
.parent(last).build();
|
|
||||||
last = InteractionIdentifier.slashBuilder()
|
|
||||||
.name(sub_options.getFirst().getName())
|
|
||||||
.autocomplete(isAutocomplete)
|
|
||||||
.parent(last).build();
|
|
||||||
}else
|
|
||||||
last = InteractionIdentifier.slashBuilder()
|
|
||||||
.name(options.getFirst().getName())
|
|
||||||
.autocomplete(isAutocomplete)
|
|
||||||
.parent(last).build();
|
|
||||||
}
|
|
||||||
return last;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -0,0 +1,38 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper.discord4j.identifierconverter;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent;
|
||||||
|
import discord4j.core.object.command.ApplicationCommandInteractionOption;
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper;
|
||||||
|
|
||||||
|
public class AutocompleteIdentifierConverter implements IdentifierProvider.Converter<ChatInputAutoCompleteEvent> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InteractionIdentifier convert(ChatInputAutoCompleteEvent context) {
|
||||||
|
List<ApplicationCommandInteractionOption> options = Discord4JWrapper.SUB_FILTER.apply(context.getOptions());
|
||||||
|
String commandName = context.getCommandName();
|
||||||
|
|
||||||
|
if (!options.isEmpty()) {
|
||||||
|
List<ApplicationCommandInteractionOption> sub_options = Discord4JWrapper.SUB_FILTER.apply(options.getFirst().getOptions());
|
||||||
|
if (!sub_options.isEmpty())
|
||||||
|
return InteractionIdentifier.createHierarchy(
|
||||||
|
InteractionType.AUTOCOMPLETE,
|
||||||
|
commandName,
|
||||||
|
options.getFirst().getName(),
|
||||||
|
sub_options.getFirst().getName());
|
||||||
|
else
|
||||||
|
return InteractionIdentifier.createHierarchy(
|
||||||
|
InteractionType.AUTOCOMPLETE,
|
||||||
|
commandName,
|
||||||
|
options.getFirst().getName());
|
||||||
|
}else
|
||||||
|
return InteractionIdentifier.createHierarchy(
|
||||||
|
InteractionType.AUTOCOMPLETE,
|
||||||
|
commandName);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper.discord4j.identifierconverter;
|
||||||
|
|
||||||
|
import discord4j.core.event.domain.interaction.ButtonInteractionEvent;
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
|
||||||
|
public class ButtonIdentifierConverter implements IdentifierProvider.Converter<ButtonInteractionEvent> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InteractionIdentifier convert(ButtonInteractionEvent context) {
|
||||||
|
return InteractionIdentifier.builder().name(context.getCustomId()).type(InteractionType.BUTTON).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper.discord4j.identifierconverter;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
|
||||||
|
import discord4j.core.object.command.ApplicationCommandInteractionOption;
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper;
|
||||||
|
|
||||||
|
public class SlashCommandIdentifierConverter implements IdentifierProvider.Converter<ChatInputInteractionEvent> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InteractionIdentifier convert(ChatInputInteractionEvent context) {
|
||||||
|
List<ApplicationCommandInteractionOption> options = Discord4JWrapper.SUB_FILTER.apply(context.getOptions());
|
||||||
|
String commandName = context.getCommandName();
|
||||||
|
|
||||||
|
if (!options.isEmpty()) {
|
||||||
|
List<ApplicationCommandInteractionOption> sub_options = Discord4JWrapper.SUB_FILTER.apply(options.getFirst().getOptions());
|
||||||
|
if (!sub_options.isEmpty())
|
||||||
|
return InteractionIdentifier.createHierarchy(
|
||||||
|
InteractionType.COMMAND,
|
||||||
|
commandName,
|
||||||
|
options.getFirst().getName(),
|
||||||
|
sub_options.getFirst().getName());
|
||||||
|
else
|
||||||
|
return InteractionIdentifier.createHierarchy(
|
||||||
|
InteractionType.COMMAND,
|
||||||
|
commandName,
|
||||||
|
options.getFirst().getName());
|
||||||
|
}else
|
||||||
|
return InteractionIdentifier.createHierarchy(
|
||||||
|
InteractionType.COMMAND,
|
||||||
|
commandName);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -2,26 +2,23 @@ package net.tomatentum.marinara.wrapper.javacord;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.javacord.api.DiscordApi;
|
import org.javacord.api.DiscordApi;
|
||||||
import org.javacord.api.interaction.AutocompleteInteraction;
|
|
||||||
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.SlashCommandInteractionOption;
|
|
||||||
import org.javacord.api.interaction.SlashCommandOption;
|
import org.javacord.api.interaction.SlashCommandOption;
|
||||||
import org.javacord.api.interaction.SlashCommandOptionChoice;
|
import org.javacord.api.interaction.SlashCommandOptionChoice;
|
||||||
|
|
||||||
import net.tomatentum.marinara.interaction.InteractionType;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
||||||
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
|
||||||
import net.tomatentum.marinara.wrapper.CommandConverter;
|
import net.tomatentum.marinara.wrapper.CommandConverter;
|
||||||
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
import net.tomatentum.marinara.util.LoggerUtil;
|
||||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||||
|
import net.tomatentum.marinara.wrapper.javacord.identifierconverter.AutocompleteIdentifierConverter;
|
||||||
|
import net.tomatentum.marinara.wrapper.javacord.identifierconverter.ButtonIdentifierConverter;
|
||||||
|
import net.tomatentum.marinara.wrapper.javacord.identifierconverter.SlashCommandIdentifierConverter;
|
||||||
|
|
||||||
public class JavacordWrapper extends LibraryWrapper {
|
public class JavacordWrapper extends LibraryWrapper {
|
||||||
|
|
||||||
@ -65,47 +62,12 @@ public class JavacordWrapper extends LibraryWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InteractionIdentifier getInteractionIdentifier(Object context) {
|
public IdentifierProvider createIdentifierProvider() {
|
||||||
if (context instanceof ButtonInteraction) {
|
return IdentifierProvider.of(
|
||||||
ButtonInteraction button = (ButtonInteraction) context;
|
new SlashCommandIdentifierConverter(),
|
||||||
return InteractionIdentifier.builder().name(button.getCustomId()).type(InteractionType.BUTTON).build();
|
new AutocompleteIdentifierConverter(),
|
||||||
}
|
new ButtonIdentifierConverter()
|
||||||
|
);
|
||||||
if (!(context instanceof SlashCommandInteraction))
|
|
||||||
return null;
|
|
||||||
|
|
||||||
boolean isAutocomplete = false;
|
|
||||||
|
|
||||||
if (context instanceof AutocompleteInteraction)
|
|
||||||
isAutocomplete = true;
|
|
||||||
|
|
||||||
SlashCommandInteraction interaction = (SlashCommandInteraction) context;
|
|
||||||
InteractionIdentifier lastIdentifier = InteractionIdentifier.rootBuilder()
|
|
||||||
.name(interaction.getCommandName())
|
|
||||||
.autocomplete(isAutocomplete)
|
|
||||||
.build();
|
|
||||||
List<SlashCommandInteractionOption> options = interaction.getOptions();
|
|
||||||
if (!options.isEmpty()) {
|
|
||||||
if (!options.getFirst().getArguments().isEmpty()) {
|
|
||||||
lastIdentifier = InteractionIdentifier.builder()
|
|
||||||
.name(options.getFirst().getName())
|
|
||||||
.type(isAutocomplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND)
|
|
||||||
.parent(lastIdentifier)
|
|
||||||
.build();
|
|
||||||
lastIdentifier = InteractionIdentifier.slashBuilder()
|
|
||||||
.name(options.getFirst().getOptions().getFirst().getName())
|
|
||||||
.autocomplete(isAutocomplete)
|
|
||||||
.parent(lastIdentifier)
|
|
||||||
.build();
|
|
||||||
}else
|
|
||||||
lastIdentifier = InteractionIdentifier.slashBuilder()
|
|
||||||
.name(options.getFirst().getName())
|
|
||||||
.autocomplete(isAutocomplete)
|
|
||||||
.parent(lastIdentifier)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
return lastIdentifier;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -0,0 +1,37 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper.javacord.identifierconverter;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.javacord.api.interaction.AutocompleteInteraction;
|
||||||
|
import org.javacord.api.interaction.SlashCommandInteractionOption;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
|
||||||
|
public class AutocompleteIdentifierConverter implements IdentifierProvider.Converter<AutocompleteInteraction> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InteractionIdentifier convert(AutocompleteInteraction context) {
|
||||||
|
List<SlashCommandInteractionOption> options = context.getOptions();
|
||||||
|
String commandName = context.getCommandName();
|
||||||
|
if (!options.isEmpty()) {
|
||||||
|
List<SlashCommandInteractionOption> sub_options = context.getOptions().getFirst().getOptions();
|
||||||
|
if (!sub_options.isEmpty())
|
||||||
|
return InteractionIdentifier.createHierarchy(
|
||||||
|
InteractionType.AUTOCOMPLETE,
|
||||||
|
commandName,
|
||||||
|
options.getFirst().getName(),
|
||||||
|
sub_options.getFirst().getName());
|
||||||
|
else
|
||||||
|
return InteractionIdentifier.createHierarchy(
|
||||||
|
InteractionType.AUTOCOMPLETE,
|
||||||
|
commandName,
|
||||||
|
options.getFirst().getName());
|
||||||
|
}else
|
||||||
|
return InteractionIdentifier.createHierarchy(
|
||||||
|
InteractionType.AUTOCOMPLETE,
|
||||||
|
commandName);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper.javacord.identifierconverter;
|
||||||
|
|
||||||
|
import org.javacord.api.interaction.ButtonInteraction;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
|
||||||
|
public class ButtonIdentifierConverter implements IdentifierProvider.Converter<ButtonInteraction> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InteractionIdentifier convert(ButtonInteraction context) {
|
||||||
|
return InteractionIdentifier.builder().name(context.getCustomId()).type(InteractionType.BUTTON).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper.javacord.identifierconverter;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.javacord.api.interaction.SlashCommandInteraction;
|
||||||
|
import org.javacord.api.interaction.SlashCommandInteractionOption;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
|
||||||
|
public class SlashCommandIdentifierConverter implements IdentifierProvider.Converter<SlashCommandInteraction> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InteractionIdentifier convert(SlashCommandInteraction context) {
|
||||||
|
List<SlashCommandInteractionOption> options = context.getOptions();
|
||||||
|
String commandName = context.getCommandName();
|
||||||
|
if (!options.isEmpty()) {
|
||||||
|
List<SlashCommandInteractionOption> sub_options = context.getOptions().getFirst().getOptions();
|
||||||
|
if (!sub_options.isEmpty())
|
||||||
|
return InteractionIdentifier.createHierarchy(
|
||||||
|
InteractionType.COMMAND,
|
||||||
|
commandName,
|
||||||
|
options.getFirst().getName(),
|
||||||
|
sub_options.getFirst().getName());
|
||||||
|
else
|
||||||
|
return InteractionIdentifier.createHierarchy(
|
||||||
|
InteractionType.COMMAND,
|
||||||
|
commandName,
|
||||||
|
options.getFirst().getName());
|
||||||
|
}else
|
||||||
|
return InteractionIdentifier.createHierarchy(
|
||||||
|
InteractionType.COMMAND,
|
||||||
|
commandName);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user