refactor(autocomplete): implement autocompleteRefs and remove SlashCommand annotation on Autocomplete Method
All checks were successful
github-mirror / push-github (push) Successful in 1m41s
Build / Gradle-Build (push) Successful in 34s
Test / Gradle-Test (push) Successful in 48s

This commit is contained in:
tueem 2025-03-31 10:36:49 +02:00
parent 92540576df
commit 450f1fdaa1
Signed by: tueem
GPG Key ID: 65C8667EC17A88FB
17 changed files with 186 additions and 56 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

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

View File

@ -6,27 +6,39 @@ 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;
@ -64,13 +76,23 @@ public class SlashCommandIdentifier extends InteractionIdentifier {
return this; return this;
} }
public SlashCommandIdentifier build(boolean autocomplete) { public String[] autocompleteRef() {
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,
autocomplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND, InteractionType.COMMAND,
options); options,
autocompleteRef);
} }
} }

View File

@ -5,15 +5,18 @@ 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.SlashCommandParser; import net.tomatentum.marinara.parser.AutocompleteParser;
import net.tomatentum.marinara.reflection.ReflectedMethod; import net.tomatentum.marinara.reflection.ReflectedMethod;
public class AutoCompleteInteractionMethod extends InteractionMethod { public class AutoCompleteInteractionMethod extends InteractionMethod {
private InteractionIdentifier interactionIdentifier; private String autocompleteRef;
private AutoCompleteInteractionMethod(Method method, private AutoCompleteInteractionMethod(Method method,
InteractionHandler handler, InteractionHandler handler,
@ -34,16 +37,21 @@ public class AutoCompleteInteractionMethod extends InteractionMethod {
@Override @Override
public InteractionIdentifier identifier() { public InteractionIdentifier identifier() {
return interactionIdentifier; return InteractionIdentifier.builder()
.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 (!method.isAnnotationPresent(AutoComplete.class) || if (!(containingObject instanceof InteractionHandler) ||
!(containingObject instanceof InteractionHandler) !method.isAnnotationPresent(AutoComplete.class) ||
) (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);
@ -55,7 +63,7 @@ public class AutoCompleteInteractionMethod extends InteractionMethod {
AutoCompleteInteractionMethod imethod = (AutoCompleteInteractionMethod) method; AutoCompleteInteractionMethod imethod = (AutoCompleteInteractionMethod) method;
parser.add( parser.add(
new SlashCommandParser(method.method(), true, x -> imethod.interactionIdentifier = x) new AutocompleteParser(method.method(), x -> imethod.autocompleteRef = x[0])
); );
} }

View File

@ -10,6 +10,7 @@ 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;
@ -35,10 +36,9 @@ 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 (!(method.isAnnotationPresent(SlashCommand.class) || if (!(containingObject instanceof InteractionHandler) ||
method.isAnnotationPresent(SubCommand.class)) || !(method.isAnnotationPresent(SlashCommand.class) ||
!(containingObject instanceof InteractionHandler) method.isAnnotationPresent(SubCommand.class)))
)
return null; return null;
return new SlashCommandInteractionMethod(method, (InteractionHandler) containingObject, marinara); return new SlashCommandInteractionMethod(method, (InteractionHandler) containingObject, marinara);
@ -50,7 +50,10 @@ public class SlashCommandInteractionMethod extends InteractionMethod {
SlashCommandInteractionMethod imethod = (SlashCommandInteractionMethod) method; SlashCommandInteractionMethod imethod = (SlashCommandInteractionMethod) method;
parser.add( parser.add(
new SlashCommandParser(method.method(), false, x -> imethod.interactionIdentifier = x) new SlashCommandParser(method.method(), x -> imethod.interactionIdentifier = x)
);
parser.add(
new AutocompleteParser(method.method(), x -> imethod.interactionIdentifier.autocompleteRef(x))
); );
} }

View File

@ -0,0 +1,33 @@
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,14 +17,12 @@ 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, boolean isAutoComplete, Consumer<SlashCommandIdentifier> consumer) { public SlashCommandParser(Method method, Consumer<SlashCommandIdentifier> consumer) {
this.method = method; this.method = method;
this.isAutoComplete = isAutoComplete;
this.consumer = consumer; this.consumer = consumer;
} }
@ -38,14 +36,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(isAutoComplete); .build();
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(isAutoComplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND) .type(InteractionType.COMMAND)
.parent(lastIdentifier) .parent(lastIdentifier)
.build(); .build();
} }
@ -56,7 +54,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(isAutoComplete); .build();
} }
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,6 +4,7 @@ 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;
@ -72,10 +73,16 @@ 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 (this.identifierProvider.provide(context).equals(e.identifier())) { if (e.identifier().equals(this.identifierProvider.provide(context, this))) {
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,6 +11,7 @@ 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;
@ -36,7 +37,7 @@ public class IdentifierProvider {
} }
} }
public InteractionIdentifier provide(Object context) { public InteractionIdentifier provide(Object context, InteractionRegistry registry) {
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());
@ -47,12 +48,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); return conv.convert(context, registry);
} }
@FunctionalInterface @FunctionalInterface
public interface Converter<T extends Object> { public interface Converter<T extends Object> {
InteractionIdentifier convert(T context); InteractionIdentifier convert(T context, InteractionRegistry registry);
} }
public static class LambdaWrapper<T extends Object> implements Converter<T> { public static class LambdaWrapper<T extends Object> implements Converter<T> {
@ -64,8 +65,8 @@ public class IdentifierProvider {
} }
@Override @Override
public InteractionIdentifier convert(T context) { public InteractionIdentifier convert(T context, InteractionRegistry registry) {
return this.converter.convert(context); return this.converter.convert(context, registry);
} }
} }

View File

@ -1,38 +1,53 @@
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) { public InteractionIdentifier convert(ChatInputAutoCompleteEvent context, InteractionRegistry registry) {
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())
return InteractionIdentifier.createHierarchy( ident = InteractionIdentifier.createHierarchy(
InteractionType.AUTOCOMPLETE, InteractionType.COMMAND,
commandName, commandName,
options.getFirst().getName(), options.getFirst().getName(),
sub_options.getFirst().getName()); sub_options.getFirst().getName());
else else
return InteractionIdentifier.createHierarchy( ident = InteractionIdentifier.createHierarchy(
InteractionType.AUTOCOMPLETE, InteractionType.COMMAND,
commandName, commandName,
options.getFirst().getName()); options.getFirst().getName());
}else }else
return InteractionIdentifier.createHierarchy( ident = InteractionIdentifier.createHierarchy(
InteractionType.AUTOCOMPLETE, InteractionType.COMMAND,
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,12 +3,13 @@ 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) { public InteractionIdentifier convert(ButtonInteractionEvent context, InteractionRegistry registry) {
return InteractionIdentifier.builder().name(context.getCustomId()).type(InteractionType.BUTTON).build(); return InteractionIdentifier.builder().name(context.getCustomId()).type(InteractionType.BUTTON).build();
} }

View File

@ -6,13 +6,14 @@ 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) { public InteractionIdentifier convert(ChatInputInteractionEvent context, InteractionRegistry registry) {
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,14 +5,20 @@ 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;
public class TestAutocomplete implements InteractionHandler { public class TestAutocomplete implements InteractionHandler {
@SlashCommand(name = "test") @SlashCommand(name = "test")
@AutoComplete @AutoComplete("testAuto")
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,37 +1,53 @@
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) { public InteractionIdentifier convert(AutocompleteInteraction context, InteractionRegistry registry) {
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())
return InteractionIdentifier.createHierarchy( ident = InteractionIdentifier.createHierarchy(
InteractionType.AUTOCOMPLETE, InteractionType.COMMAND,
commandName, commandName,
options.getFirst().getName(), options.getFirst().getName(),
sub_options.getFirst().getName()); sub_options.getFirst().getName());
else else
return InteractionIdentifier.createHierarchy( ident = InteractionIdentifier.createHierarchy(
InteractionType.AUTOCOMPLETE, InteractionType.COMMAND,
commandName, commandName,
options.getFirst().getName()); options.getFirst().getName());
}else }else
return InteractionIdentifier.createHierarchy( ident = InteractionIdentifier.createHierarchy(
InteractionType.AUTOCOMPLETE, InteractionType.COMMAND,
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,12 +4,13 @@ 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) { public InteractionIdentifier convert(ButtonInteraction context, InteractionRegistry registry) {
return InteractionIdentifier.builder().name(context.getCustomId()).type(InteractionType.BUTTON).build(); return InteractionIdentifier.builder().name(context.getCustomId()).type(InteractionType.BUTTON).build();
} }

View File

@ -7,12 +7,13 @@ 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) { public InteractionIdentifier convert(SlashCommandInteraction context, InteractionRegistry registry) {
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,6 +4,7 @@ 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;
@ -13,7 +14,12 @@ 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 @AutoComplete("testAuto")
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");