Compare commits

..

No commits in common. "450f1fdaa1cefb574af7f2f80ea2c4f356a33dfd" and "8a3cde52fd9dc4da95d190888e81bbe8d44ea615" have entirely different histories.

18 changed files with 57 additions and 187 deletions

View File

@ -8,5 +8,5 @@ import java.lang.annotation.Target;
@Target({ElementType.METHOD}) @Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface AutoComplete { public @interface AutoComplete {
public String value();
} }

View File

@ -73,7 +73,7 @@ public class InteractionIdentifier {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj == null || !(obj instanceof InteractionIdentifier)) if (!(obj instanceof InteractionIdentifier))
return false; return false;
InteractionIdentifier ident = (InteractionIdentifier) obj; InteractionIdentifier ident = (InteractionIdentifier) obj;
if (!type().equals(ident.type())) if (!type().equals(ident.type()))

View File

@ -13,9 +13,8 @@ public class RootCommandIdentifier extends SlashCommandIdentifier {
String description, String description,
InteractionType type, InteractionType type,
SlashCommandOption[] options, SlashCommandOption[] options,
long[] serverIds, long[] serverIds) {
String[] autocompleteRef) { super(parent, name, description, type, options);
super(parent, name, description, type, options, autocompleteRef);
this.serverIds = serverIds; this.serverIds = serverIds;
} }
@ -29,7 +28,7 @@ public class RootCommandIdentifier extends SlashCommandIdentifier {
private String description; private String description;
private SlashCommandOption[] options; private SlashCommandOption[] options;
private long[] serverIds; private long[] serverIds;
private String[] autocompleteRef;
public InteractionIdentifier parent() { public InteractionIdentifier parent() {
return parent; return parent;
@ -76,24 +75,14 @@ public class RootCommandIdentifier extends SlashCommandIdentifier {
return this; return this;
} }
public String[] autocompleteRef() { public SlashCommandIdentifier build(boolean autocomplete) {
return this.autocompleteRef;
}
public Builder autocompleteRef(String[] autocompleteRef) {
this.autocompleteRef = autocompleteRef;
return this;
}
public SlashCommandIdentifier build() {
return new RootCommandIdentifier( return new RootCommandIdentifier(
parent, parent,
name, name,
description, description,
InteractionType.COMMAND, autocomplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND,
options, options,
serverIds, serverIds);
autocompleteRef);
} }
} }

View File

@ -6,39 +6,27 @@ import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOptio
public class SlashCommandIdentifier extends InteractionIdentifier { public class SlashCommandIdentifier extends InteractionIdentifier {
private SlashCommandOption[] options; private SlashCommandOption[] options;
private String[] autocompleteRef;
protected SlashCommandIdentifier( protected SlashCommandIdentifier(
InteractionIdentifier parent, InteractionIdentifier parent,
String name, String name,
String description, String description,
InteractionType type, InteractionType type,
SlashCommandOption[] options, SlashCommandOption[] options
String[] autocompleteRef
) { ) {
super(parent, name, description, type); super(parent, name, description, type);
this.options = options; this.options = options;
this.autocompleteRef = autocompleteRef;
} }
public SlashCommandOption[] options() { public SlashCommandOption[] options() {
return this.options; return this.options;
} }
public String[] autocompleteRef() {
return this.autocompleteRef;
}
public SlashCommandIdentifier autocompleteRef(String[] autocompleteRef) {
this.autocompleteRef = autocompleteRef;
return this;
}
public static class Builder { public static class Builder {
private InteractionIdentifier parent; private InteractionIdentifier parent;
private String name; private String name;
private String description; private String description;
private SlashCommandOption[] options; private SlashCommandOption[] options;
private String[] autocompleteRef;
public InteractionIdentifier parent() { public InteractionIdentifier parent() {
return parent; return parent;
@ -76,23 +64,13 @@ public class SlashCommandIdentifier extends InteractionIdentifier {
return this; return this;
} }
public String[] autocompleteRef() { public SlashCommandIdentifier build(boolean autocomplete) {
return this.autocompleteRef;
}
public Builder autocompleteRef(String[] autocompleteRef) {
this.autocompleteRef = autocompleteRef;
return this;
}
public SlashCommandIdentifier build() {
return new SlashCommandIdentifier( return new SlashCommandIdentifier(
parent, parent,
name, name,
description, description,
InteractionType.COMMAND, autocomplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND,
options, options);
autocompleteRef);
} }
} }

View File

@ -5,18 +5,15 @@ import java.util.List;
import net.tomatentum.marinara.Marinara; import net.tomatentum.marinara.Marinara;
import net.tomatentum.marinara.interaction.InteractionHandler; import net.tomatentum.marinara.interaction.InteractionHandler;
import net.tomatentum.marinara.interaction.InteractionType;
import net.tomatentum.marinara.interaction.annotation.AutoComplete; import net.tomatentum.marinara.interaction.annotation.AutoComplete;
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.parser.AnnotationParser; import net.tomatentum.marinara.parser.AnnotationParser;
import net.tomatentum.marinara.parser.AutocompleteParser; import net.tomatentum.marinara.parser.SlashCommandParser;
import net.tomatentum.marinara.reflection.ReflectedMethod; import net.tomatentum.marinara.reflection.ReflectedMethod;
public class AutoCompleteInteractionMethod extends InteractionMethod { public class AutoCompleteInteractionMethod extends InteractionMethod {
private String autocompleteRef; private InteractionIdentifier interactionIdentifier;
private AutoCompleteInteractionMethod(Method method, private AutoCompleteInteractionMethod(Method method,
InteractionHandler handler, InteractionHandler handler,
@ -37,21 +34,16 @@ public class AutoCompleteInteractionMethod extends InteractionMethod {
@Override @Override
public InteractionIdentifier identifier() { public InteractionIdentifier identifier() {
return InteractionIdentifier.builder() return interactionIdentifier;
.type(InteractionType.AUTOCOMPLETE)
.name(autocompleteRef)
.description("AUTOCOMPLETE")
.build();
} }
public static class Factory extends InteractionMethod.Factory { public static class Factory extends InteractionMethod.Factory {
@Override @Override
public ReflectedMethod produce(Marinara marinara, Method method, Object containingObject) { public ReflectedMethod produce(Marinara marinara, Method method, Object containingObject) {
if (!(containingObject instanceof InteractionHandler) || if (!method.isAnnotationPresent(AutoComplete.class) ||
!method.isAnnotationPresent(AutoComplete.class) || !(containingObject instanceof InteractionHandler)
(method.isAnnotationPresent(SlashCommand.class) || )
method.isAnnotationPresent(SubCommand.class)))
return null; return null;
return new AutoCompleteInteractionMethod(method, (InteractionHandler) containingObject, marinara); return new AutoCompleteInteractionMethod(method, (InteractionHandler) containingObject, marinara);
@ -63,7 +55,7 @@ public class AutoCompleteInteractionMethod extends InteractionMethod {
AutoCompleteInteractionMethod imethod = (AutoCompleteInteractionMethod) method; AutoCompleteInteractionMethod imethod = (AutoCompleteInteractionMethod) method;
parser.add( parser.add(
new AutocompleteParser(method.method(), x -> imethod.autocompleteRef = x[0]) new SlashCommandParser(method.method(), true, x -> imethod.interactionIdentifier = x)
); );
} }

View File

@ -10,7 +10,6 @@ import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier; import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
import net.tomatentum.marinara.parser.AnnotationParser; import net.tomatentum.marinara.parser.AnnotationParser;
import net.tomatentum.marinara.parser.AutocompleteParser;
import net.tomatentum.marinara.parser.SlashCommandParser; import net.tomatentum.marinara.parser.SlashCommandParser;
import net.tomatentum.marinara.reflection.ReflectedMethod; import net.tomatentum.marinara.reflection.ReflectedMethod;
@ -36,9 +35,10 @@ public class SlashCommandInteractionMethod extends InteractionMethod {
@Override @Override
public ReflectedMethod produce(Marinara marinara, Method method, Object containingObject) { public ReflectedMethod produce(Marinara marinara, Method method, Object containingObject) {
if (!(containingObject instanceof InteractionHandler) || if (!(method.isAnnotationPresent(SlashCommand.class) ||
!(method.isAnnotationPresent(SlashCommand.class) || method.isAnnotationPresent(SubCommand.class)) ||
method.isAnnotationPresent(SubCommand.class))) !(containingObject instanceof InteractionHandler)
)
return null; return null;
return new SlashCommandInteractionMethod(method, (InteractionHandler) containingObject, marinara); return new SlashCommandInteractionMethod(method, (InteractionHandler) containingObject, marinara);
@ -50,10 +50,7 @@ public class SlashCommandInteractionMethod extends InteractionMethod {
SlashCommandInteractionMethod imethod = (SlashCommandInteractionMethod) method; SlashCommandInteractionMethod imethod = (SlashCommandInteractionMethod) method;
parser.add( parser.add(
new SlashCommandParser(method.method(), x -> imethod.interactionIdentifier = x) new SlashCommandParser(method.method(), false, x -> imethod.interactionIdentifier = x)
);
parser.add(
new AutocompleteParser(method.method(), x -> imethod.interactionIdentifier.autocompleteRef(x))
); );
} }

View File

@ -1,33 +0,0 @@
package net.tomatentum.marinara.parser;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.function.Consumer;
import net.tomatentum.marinara.interaction.annotation.AutoComplete;
public class AutocompleteParser implements AnnotationParser {
private Method method;
private Consumer<String[]> consumer;
public AutocompleteParser(Method method, Consumer<String[]> consumer) {
this.method = method;
this.consumer = consumer;
}
@Override
public void parse() {
String[] autocompletes = Arrays.stream(this.method.getAnnotationsByType(AutoComplete.class))
.map(AutoComplete::value)
.toArray(String[]::new);
this.consumer.accept(autocompletes);
}
@Override
public Method getMethod() {
return this.method;
}
}

View File

@ -17,12 +17,14 @@ import net.tomatentum.marinara.util.ReflectionUtil;
public class SlashCommandParser implements AnnotationParser { public class SlashCommandParser implements AnnotationParser {
private Method method; private Method method;
private boolean isAutoComplete;
private Consumer<SlashCommandIdentifier> consumer; private Consumer<SlashCommandIdentifier> consumer;
private Logger logger = LoggerUtil.getLogger(getClass()); private Logger logger = LoggerUtil.getLogger(getClass());
public SlashCommandParser(Method method, Consumer<SlashCommandIdentifier> consumer) { public SlashCommandParser(Method method, boolean isAutoComplete, Consumer<SlashCommandIdentifier> consumer) {
this.method = method; this.method = method;
this.isAutoComplete = isAutoComplete;
this.consumer = consumer; this.consumer = consumer;
} }
@ -36,14 +38,14 @@ public class SlashCommandParser implements AnnotationParser {
.description(cmd.description()) .description(cmd.description())
.options(cmd.options()) .options(cmd.options())
.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);
lastIdentifier = InteractionIdentifier.builder() lastIdentifier = InteractionIdentifier.builder()
.name(cmdGroup.name()) .name(cmdGroup.name())
.description(cmdGroup.description()) .description(cmdGroup.description())
.type(InteractionType.COMMAND) .type(isAutoComplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND)
.parent(lastIdentifier) .parent(lastIdentifier)
.build(); .build();
} }
@ -54,7 +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())
.build(); .build(isAutoComplete);
} }
logger.trace("Parsed using SlashCommandParser for method {} with the result: {}", ReflectionUtil.getFullMethodName(method), lastIdentifier.toString()); logger.trace("Parsed using SlashCommandParser for method {} with the result: {}", ReflectionUtil.getFullMethodName(method), lastIdentifier.toString());

View File

@ -4,7 +4,6 @@ import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -73,16 +72,10 @@ public class InteractionRegistry {
public void handle(Object context) { public void handle(Object context) {
logger.debug("Received {} interaction ", context); logger.debug("Received {} interaction ", context);
interactions.forEach((e) -> { interactions.forEach((e) -> {
if (e.identifier().equals(this.identifierProvider.provide(context, this))) { 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);
} }
}); });
} }
public Optional<InteractionEntry> findFor(InteractionIdentifier identifier) {
return this.interactions.stream()
.filter(x -> x.identifier().equals(identifier))
.findFirst();
}
} }

View File

@ -11,7 +11,6 @@ import org.slf4j.Logger;
import io.leangen.geantyref.GenericTypeReflector; import io.leangen.geantyref.GenericTypeReflector;
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.registry.InteractionRegistry;
import net.tomatentum.marinara.util.LoggerUtil; import net.tomatentum.marinara.util.LoggerUtil;
import net.tomatentum.marinara.util.ReflectionUtil; import net.tomatentum.marinara.util.ReflectionUtil;
@ -37,7 +36,7 @@ public class IdentifierProvider {
} }
} }
public InteractionIdentifier provide(Object context, InteractionRegistry registry) { public InteractionIdentifier provide(Object context) {
Type type = ReflectionUtil.getMostSpecificClass( Type type = ReflectionUtil.getMostSpecificClass(
converter.keySet().stream().filter(x -> x.isAssignableFrom(context.getClass())).toArray(Class<?>[]::new), converter.keySet().stream().filter(x -> x.isAssignableFrom(context.getClass())).toArray(Class<?>[]::new),
context.getClass()); context.getClass());
@ -48,12 +47,12 @@ public class IdentifierProvider {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Converter<Object> conv = (Converter<Object>) converter.get(type); Converter<Object> conv = (Converter<Object>) converter.get(type);
return conv.convert(context, registry); return conv.convert(context);
} }
@FunctionalInterface @FunctionalInterface
public interface Converter<T extends Object> { public interface Converter<T extends Object> {
InteractionIdentifier convert(T context, InteractionRegistry registry); InteractionIdentifier convert(T context);
} }
public static class LambdaWrapper<T extends Object> implements Converter<T> { public static class LambdaWrapper<T extends Object> implements Converter<T> {
@ -65,8 +64,8 @@ public class IdentifierProvider {
} }
@Override @Override
public InteractionIdentifier convert(T context, InteractionRegistry registry) { public InteractionIdentifier convert(T context) {
return this.converter.convert(context, registry); return this.converter.convert(context);
} }
} }

View File

@ -1,53 +1,38 @@
package net.tomatentum.marinara.wrapper.discord4j.identifierconverter; package net.tomatentum.marinara.wrapper.discord4j.identifierconverter;
import java.util.List; import java.util.List;
import java.util.Optional;
import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent; import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent;
import discord4j.core.object.command.ApplicationCommandInteractionOption; import discord4j.core.object.command.ApplicationCommandInteractionOption;
import net.tomatentum.marinara.interaction.InteractionType; import net.tomatentum.marinara.interaction.InteractionType;
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
import net.tomatentum.marinara.registry.InteractionEntry;
import net.tomatentum.marinara.registry.InteractionRegistry;
import net.tomatentum.marinara.wrapper.IdentifierProvider; import net.tomatentum.marinara.wrapper.IdentifierProvider;
import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper; import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper;
public class AutocompleteIdentifierConverter implements IdentifierProvider.Converter<ChatInputAutoCompleteEvent> { public class AutocompleteIdentifierConverter implements IdentifierProvider.Converter<ChatInputAutoCompleteEvent> {
@Override @Override
public InteractionIdentifier convert(ChatInputAutoCompleteEvent context, InteractionRegistry registry) { public InteractionIdentifier convert(ChatInputAutoCompleteEvent context) {
List<ApplicationCommandInteractionOption> options = Discord4JWrapper.SUB_FILTER.apply(context.getOptions()); List<ApplicationCommandInteractionOption> options = Discord4JWrapper.SUB_FILTER.apply(context.getOptions());
String commandName = context.getCommandName(); String commandName = context.getCommandName();
InteractionIdentifier ident;
if (!options.isEmpty()) { if (!options.isEmpty()) {
List<ApplicationCommandInteractionOption> sub_options = Discord4JWrapper.SUB_FILTER.apply(options.getFirst().getOptions()); List<ApplicationCommandInteractionOption> sub_options = Discord4JWrapper.SUB_FILTER.apply(options.getFirst().getOptions());
if (!sub_options.isEmpty()) if (!sub_options.isEmpty())
ident = InteractionIdentifier.createHierarchy( return InteractionIdentifier.createHierarchy(
InteractionType.COMMAND, InteractionType.AUTOCOMPLETE,
commandName, commandName,
options.getFirst().getName(), options.getFirst().getName(),
sub_options.getFirst().getName()); sub_options.getFirst().getName());
else else
ident = InteractionIdentifier.createHierarchy( return InteractionIdentifier.createHierarchy(
InteractionType.COMMAND, InteractionType.AUTOCOMPLETE,
commandName, commandName,
options.getFirst().getName()); options.getFirst().getName());
}else }else
ident = InteractionIdentifier.createHierarchy( return InteractionIdentifier.createHierarchy(
InteractionType.COMMAND, InteractionType.AUTOCOMPLETE,
commandName); commandName);
Optional<InteractionEntry> entry = registry.findFor(ident);
if (entry.isPresent() && entry.get().identifier() instanceof SlashCommandIdentifier) {
SlashCommandIdentifier sIdent = (SlashCommandIdentifier) entry.get().identifier();
return InteractionIdentifier.builder()
.type(InteractionType.AUTOCOMPLETE)
.name(sIdent.autocompleteRef()[0])
.build();
}
return null;
} }
} }

View File

@ -3,13 +3,12 @@ package net.tomatentum.marinara.wrapper.discord4j.identifierconverter;
import discord4j.core.event.domain.interaction.ButtonInteractionEvent; import discord4j.core.event.domain.interaction.ButtonInteractionEvent;
import net.tomatentum.marinara.interaction.InteractionType; import net.tomatentum.marinara.interaction.InteractionType;
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.registry.InteractionRegistry;
import net.tomatentum.marinara.wrapper.IdentifierProvider; import net.tomatentum.marinara.wrapper.IdentifierProvider;
public class ButtonIdentifierConverter implements IdentifierProvider.Converter<ButtonInteractionEvent> { public class ButtonIdentifierConverter implements IdentifierProvider.Converter<ButtonInteractionEvent> {
@Override @Override
public InteractionIdentifier convert(ButtonInteractionEvent context, InteractionRegistry registry) { public InteractionIdentifier convert(ButtonInteractionEvent context) {
return InteractionIdentifier.builder().name(context.getCustomId()).type(InteractionType.BUTTON).build(); return InteractionIdentifier.builder().name(context.getCustomId()).type(InteractionType.BUTTON).build();
} }

View File

@ -6,14 +6,13 @@ import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
import discord4j.core.object.command.ApplicationCommandInteractionOption; import discord4j.core.object.command.ApplicationCommandInteractionOption;
import net.tomatentum.marinara.interaction.InteractionType; import net.tomatentum.marinara.interaction.InteractionType;
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.registry.InteractionRegistry;
import net.tomatentum.marinara.wrapper.IdentifierProvider; import net.tomatentum.marinara.wrapper.IdentifierProvider;
import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper; import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper;
public class SlashCommandIdentifierConverter implements IdentifierProvider.Converter<ChatInputInteractionEvent> { public class SlashCommandIdentifierConverter implements IdentifierProvider.Converter<ChatInputInteractionEvent> {
@Override @Override
public InteractionIdentifier convert(ChatInputInteractionEvent context, InteractionRegistry registry) { public InteractionIdentifier convert(ChatInputInteractionEvent context) {
List<ApplicationCommandInteractionOption> options = Discord4JWrapper.SUB_FILTER.apply(context.getOptions()); List<ApplicationCommandInteractionOption> options = Discord4JWrapper.SUB_FILTER.apply(context.getOptions());
String commandName = context.getCommandName(); String commandName = context.getCommandName();

View File

@ -5,7 +5,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Collections; import java.util.Collections;
import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent; import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent;
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
import net.tomatentum.marinara.interaction.InteractionHandler; import net.tomatentum.marinara.interaction.InteractionHandler;
import net.tomatentum.marinara.interaction.annotation.AutoComplete; import net.tomatentum.marinara.interaction.annotation.AutoComplete;
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
@ -13,12 +12,7 @@ import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
public class TestAutocomplete implements InteractionHandler { public class TestAutocomplete implements InteractionHandler {
@SlashCommand(name = "test") @SlashCommand(name = "test")
@AutoComplete("testAuto") @AutoComplete
public void exec(ChatInputInteractionEvent context) {
}
@AutoComplete("testAuto")
public void autocomplete(ChatInputAutoCompleteEvent context, String value) { public void autocomplete(ChatInputAutoCompleteEvent context, String value) {
System.out.println("Success!"); System.out.println("Success!");
assertEquals(value, "test"); assertEquals(value, "test");

View File

@ -1,53 +1,37 @@
package net.tomatentum.marinara.wrapper.javacord.identifierconverter; package net.tomatentum.marinara.wrapper.javacord.identifierconverter;
import java.util.List; import java.util.List;
import java.util.Optional;
import org.javacord.api.interaction.AutocompleteInteraction; import org.javacord.api.interaction.AutocompleteInteraction;
import org.javacord.api.interaction.SlashCommandInteractionOption; import org.javacord.api.interaction.SlashCommandInteractionOption;
import net.tomatentum.marinara.interaction.InteractionType; import net.tomatentum.marinara.interaction.InteractionType;
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
import net.tomatentum.marinara.registry.InteractionEntry;
import net.tomatentum.marinara.registry.InteractionRegistry;
import net.tomatentum.marinara.wrapper.IdentifierProvider; import net.tomatentum.marinara.wrapper.IdentifierProvider;
public class AutocompleteIdentifierConverter implements IdentifierProvider.Converter<AutocompleteInteraction> { public class AutocompleteIdentifierConverter implements IdentifierProvider.Converter<AutocompleteInteraction> {
@Override @Override
public InteractionIdentifier convert(AutocompleteInteraction context, InteractionRegistry registry) { public InteractionIdentifier convert(AutocompleteInteraction context) {
List<SlashCommandInteractionOption> options = context.getOptions(); List<SlashCommandInteractionOption> options = context.getOptions();
String commandName = context.getCommandName(); String commandName = context.getCommandName();
InteractionIdentifier ident;
if (!options.isEmpty()) { if (!options.isEmpty()) {
List<SlashCommandInteractionOption> sub_options = context.getOptions().getFirst().getOptions(); List<SlashCommandInteractionOption> sub_options = context.getOptions().getFirst().getOptions();
if (!sub_options.isEmpty()) if (!sub_options.isEmpty())
ident = InteractionIdentifier.createHierarchy( return InteractionIdentifier.createHierarchy(
InteractionType.COMMAND, InteractionType.AUTOCOMPLETE,
commandName, commandName,
options.getFirst().getName(), options.getFirst().getName(),
sub_options.getFirst().getName()); sub_options.getFirst().getName());
else else
ident = InteractionIdentifier.createHierarchy( return InteractionIdentifier.createHierarchy(
InteractionType.COMMAND, InteractionType.AUTOCOMPLETE,
commandName, commandName,
options.getFirst().getName()); options.getFirst().getName());
}else }else
ident = InteractionIdentifier.createHierarchy( return InteractionIdentifier.createHierarchy(
InteractionType.COMMAND, InteractionType.AUTOCOMPLETE,
commandName); commandName);
Optional<InteractionEntry> entry = registry.findFor(ident);
if (entry.isPresent() && entry.get().identifier() instanceof SlashCommandIdentifier) {
SlashCommandIdentifier sIdent = (SlashCommandIdentifier) entry.get().identifier();
return InteractionIdentifier.builder()
.type(InteractionType.AUTOCOMPLETE)
.name(sIdent.autocompleteRef()[0])
.build();
}
return null;
} }
} }

View File

@ -4,13 +4,12 @@ import org.javacord.api.interaction.ButtonInteraction;
import net.tomatentum.marinara.interaction.InteractionType; import net.tomatentum.marinara.interaction.InteractionType;
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.registry.InteractionRegistry;
import net.tomatentum.marinara.wrapper.IdentifierProvider; import net.tomatentum.marinara.wrapper.IdentifierProvider;
public class ButtonIdentifierConverter implements IdentifierProvider.Converter<ButtonInteraction> { public class ButtonIdentifierConverter implements IdentifierProvider.Converter<ButtonInteraction> {
@Override @Override
public InteractionIdentifier convert(ButtonInteraction context, InteractionRegistry registry) { public InteractionIdentifier convert(ButtonInteraction context) {
return InteractionIdentifier.builder().name(context.getCustomId()).type(InteractionType.BUTTON).build(); return InteractionIdentifier.builder().name(context.getCustomId()).type(InteractionType.BUTTON).build();
} }

View File

@ -7,13 +7,12 @@ import org.javacord.api.interaction.SlashCommandInteractionOption;
import net.tomatentum.marinara.interaction.InteractionType; import net.tomatentum.marinara.interaction.InteractionType;
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.registry.InteractionRegistry;
import net.tomatentum.marinara.wrapper.IdentifierProvider; import net.tomatentum.marinara.wrapper.IdentifierProvider;
public class SlashCommandIdentifierConverter implements IdentifierProvider.Converter<SlashCommandInteraction> { public class SlashCommandIdentifierConverter implements IdentifierProvider.Converter<SlashCommandInteraction> {
@Override @Override
public InteractionIdentifier convert(SlashCommandInteraction context, InteractionRegistry registry) { public InteractionIdentifier convert(SlashCommandInteraction context) {
List<SlashCommandInteractionOption> options = context.getOptions(); List<SlashCommandInteractionOption> options = context.getOptions();
String commandName = context.getCommandName(); String commandName = context.getCommandName();
if (!options.isEmpty()) { if (!options.isEmpty()) {

View File

@ -4,7 +4,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Collections; import java.util.Collections;
import org.javacord.api.event.interaction.SlashCommandCreateEvent;
import org.javacord.api.interaction.AutocompleteInteraction; import org.javacord.api.interaction.AutocompleteInteraction;
import net.tomatentum.marinara.interaction.InteractionHandler; import net.tomatentum.marinara.interaction.InteractionHandler;
@ -14,12 +13,7 @@ import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
public class TestAutocomplete implements InteractionHandler { public class TestAutocomplete implements InteractionHandler {
@SlashCommand(name = "test") @SlashCommand(name = "test")
@AutoComplete("testAuto") @AutoComplete
public void exec(SlashCommandCreateEvent context) {
}
@AutoComplete("testAuto")
public void autocomplete(AutocompleteInteraction context, String value) { public void autocomplete(AutocompleteInteraction context, String value) {
System.out.println("Success!"); System.out.println("Success!");
assertEquals(value, "test"); assertEquals(value, "test");