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})
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoComplete {
public String value();
}

View File

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

View File

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

View File

@ -5,15 +5,18 @@ import java.util.List;
import net.tomatentum.marinara.Marinara;
import net.tomatentum.marinara.interaction.InteractionHandler;
import net.tomatentum.marinara.interaction.InteractionType;
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.parser.AnnotationParser;
import net.tomatentum.marinara.parser.SlashCommandParser;
import net.tomatentum.marinara.parser.AutocompleteParser;
import net.tomatentum.marinara.reflection.ReflectedMethod;
public class AutoCompleteInteractionMethod extends InteractionMethod {
private InteractionIdentifier interactionIdentifier;
private String autocompleteRef;
private AutoCompleteInteractionMethod(Method method,
InteractionHandler handler,
@ -34,16 +37,21 @@ public class AutoCompleteInteractionMethod extends InteractionMethod {
@Override
public InteractionIdentifier identifier() {
return interactionIdentifier;
return InteractionIdentifier.builder()
.type(InteractionType.AUTOCOMPLETE)
.name(autocompleteRef)
.description("AUTOCOMPLETE")
.build();
}
public static class Factory extends InteractionMethod.Factory {
@Override
public ReflectedMethod produce(Marinara marinara, Method method, Object containingObject) {
if (!method.isAnnotationPresent(AutoComplete.class) ||
!(containingObject instanceof InteractionHandler)
)
if (!(containingObject instanceof InteractionHandler) ||
!method.isAnnotationPresent(AutoComplete.class) ||
(method.isAnnotationPresent(SlashCommand.class) ||
method.isAnnotationPresent(SubCommand.class)))
return null;
return new AutoCompleteInteractionMethod(method, (InteractionHandler) containingObject, marinara);
@ -55,7 +63,7 @@ public class AutoCompleteInteractionMethod extends InteractionMethod {
AutoCompleteInteractionMethod imethod = (AutoCompleteInteractionMethod) method;
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.SlashCommandIdentifier;
import net.tomatentum.marinara.parser.AnnotationParser;
import net.tomatentum.marinara.parser.AutocompleteParser;
import net.tomatentum.marinara.parser.SlashCommandParser;
import net.tomatentum.marinara.reflection.ReflectedMethod;
@ -35,10 +36,9 @@ public class SlashCommandInteractionMethod extends InteractionMethod {
@Override
public ReflectedMethod produce(Marinara marinara, Method method, Object containingObject) {
if (!(method.isAnnotationPresent(SlashCommand.class) ||
method.isAnnotationPresent(SubCommand.class)) ||
!(containingObject instanceof InteractionHandler)
)
if (!(containingObject instanceof InteractionHandler) ||
!(method.isAnnotationPresent(SlashCommand.class) ||
method.isAnnotationPresent(SubCommand.class)))
return null;
return new SlashCommandInteractionMethod(method, (InteractionHandler) containingObject, marinara);
@ -50,7 +50,10 @@ public class SlashCommandInteractionMethod extends InteractionMethod {
SlashCommandInteractionMethod imethod = (SlashCommandInteractionMethod) method;
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 {
private Method method;
private boolean isAutoComplete;
private Consumer<SlashCommandIdentifier> consumer;
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.isAutoComplete = isAutoComplete;
this.consumer = consumer;
}
@ -38,14 +36,14 @@ public class SlashCommandParser implements AnnotationParser {
.description(cmd.description())
.options(cmd.options())
.serverIds(cmd.serverIds())
.build(isAutoComplete);
.build();
if (ReflectionUtil.isAnnotationPresent(method, SubCommandGroup.class)) {
SubCommandGroup cmdGroup = ReflectionUtil.getAnnotation(method, SubCommandGroup.class);
lastIdentifier = InteractionIdentifier.builder()
.name(cmdGroup.name())
.description(cmdGroup.description())
.type(isAutoComplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND)
.type(InteractionType.COMMAND)
.parent(lastIdentifier)
.build();
}
@ -56,7 +54,7 @@ public class SlashCommandParser implements AnnotationParser {
.name(subCmd.name())
.description(subCmd.description())
.options(subCmd.options())
.build(isAutoComplete);
.build();
}
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.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.slf4j.Logger;
@ -72,10 +73,16 @@ public class InteractionRegistry {
public void handle(Object context) {
logger.debug("Received {} interaction ", context);
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());
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 net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.registry.InteractionRegistry;
import net.tomatentum.marinara.util.LoggerUtil;
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(
converter.keySet().stream().filter(x -> x.isAssignableFrom(context.getClass())).toArray(Class<?>[]::new),
context.getClass());
@ -47,12 +48,12 @@ public class IdentifierProvider {
@SuppressWarnings("unchecked")
Converter<Object> conv = (Converter<Object>) converter.get(type);
return conv.convert(context);
return conv.convert(context, registry);
}
@FunctionalInterface
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> {
@ -64,8 +65,8 @@ public class IdentifierProvider {
}
@Override
public InteractionIdentifier convert(T context) {
return this.converter.convert(context);
public InteractionIdentifier convert(T context, InteractionRegistry registry) {
return this.converter.convert(context, registry);
}
}

View File

@ -1,38 +1,53 @@
package net.tomatentum.marinara.wrapper.discord4j.identifierconverter;
import java.util.List;
import java.util.Optional;
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.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.discord4j.Discord4JWrapper;
public class AutocompleteIdentifierConverter implements IdentifierProvider.Converter<ChatInputAutoCompleteEvent> {
@Override
public InteractionIdentifier convert(ChatInputAutoCompleteEvent context) {
public InteractionIdentifier convert(ChatInputAutoCompleteEvent context, InteractionRegistry registry) {
List<ApplicationCommandInteractionOption> options = Discord4JWrapper.SUB_FILTER.apply(context.getOptions());
String commandName = context.getCommandName();
InteractionIdentifier ident;
if (!options.isEmpty()) {
List<ApplicationCommandInteractionOption> sub_options = Discord4JWrapper.SUB_FILTER.apply(options.getFirst().getOptions());
if (!sub_options.isEmpty())
return InteractionIdentifier.createHierarchy(
InteractionType.AUTOCOMPLETE,
ident = InteractionIdentifier.createHierarchy(
InteractionType.COMMAND,
commandName,
options.getFirst().getName(),
sub_options.getFirst().getName());
else
return InteractionIdentifier.createHierarchy(
InteractionType.AUTOCOMPLETE,
ident = InteractionIdentifier.createHierarchy(
InteractionType.COMMAND,
commandName,
options.getFirst().getName());
}else
return InteractionIdentifier.createHierarchy(
InteractionType.AUTOCOMPLETE,
ident = InteractionIdentifier.createHierarchy(
InteractionType.COMMAND,
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 net.tomatentum.marinara.interaction.InteractionType;
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.registry.InteractionRegistry;
import net.tomatentum.marinara.wrapper.IdentifierProvider;
public class ButtonIdentifierConverter implements IdentifierProvider.Converter<ButtonInteractionEvent> {
@Override
public InteractionIdentifier convert(ButtonInteractionEvent context) {
public InteractionIdentifier convert(ButtonInteractionEvent context, InteractionRegistry registry) {
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 net.tomatentum.marinara.interaction.InteractionType;
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.registry.InteractionRegistry;
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) {
public InteractionIdentifier convert(ChatInputInteractionEvent context, InteractionRegistry registry) {
List<ApplicationCommandInteractionOption> options = Discord4JWrapper.SUB_FILTER.apply(context.getOptions());
String commandName = context.getCommandName();

View File

@ -5,14 +5,20 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Collections;
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.annotation.AutoComplete;
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
public class TestAutocomplete implements InteractionHandler {
@SlashCommand(name = "test")
@AutoComplete
@AutoComplete("testAuto")
public void exec(ChatInputInteractionEvent context) {
}
@AutoComplete("testAuto")
public void autocomplete(ChatInputAutoCompleteEvent context, String value) {
System.out.println("Success!");
assertEquals(value, "test");

View File

@ -1,37 +1,53 @@
package net.tomatentum.marinara.wrapper.javacord.identifierconverter;
import java.util.List;
import java.util.Optional;
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.interaction.ident.SlashCommandIdentifier;
import net.tomatentum.marinara.registry.InteractionEntry;
import net.tomatentum.marinara.registry.InteractionRegistry;
import net.tomatentum.marinara.wrapper.IdentifierProvider;
public class AutocompleteIdentifierConverter implements IdentifierProvider.Converter<AutocompleteInteraction> {
@Override
public InteractionIdentifier convert(AutocompleteInteraction context) {
public InteractionIdentifier convert(AutocompleteInteraction context, InteractionRegistry registry) {
List<SlashCommandInteractionOption> options = context.getOptions();
String commandName = context.getCommandName();
InteractionIdentifier ident;
if (!options.isEmpty()) {
List<SlashCommandInteractionOption> sub_options = context.getOptions().getFirst().getOptions();
if (!sub_options.isEmpty())
return InteractionIdentifier.createHierarchy(
InteractionType.AUTOCOMPLETE,
ident = InteractionIdentifier.createHierarchy(
InteractionType.COMMAND,
commandName,
options.getFirst().getName(),
sub_options.getFirst().getName());
else
return InteractionIdentifier.createHierarchy(
InteractionType.AUTOCOMPLETE,
ident = InteractionIdentifier.createHierarchy(
InteractionType.COMMAND,
commandName,
options.getFirst().getName());
}else
return InteractionIdentifier.createHierarchy(
InteractionType.AUTOCOMPLETE,
ident = InteractionIdentifier.createHierarchy(
InteractionType.COMMAND,
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.ident.InteractionIdentifier;
import net.tomatentum.marinara.registry.InteractionRegistry;
import net.tomatentum.marinara.wrapper.IdentifierProvider;
public class ButtonIdentifierConverter implements IdentifierProvider.Converter<ButtonInteraction> {
@Override
public InteractionIdentifier convert(ButtonInteraction context) {
public InteractionIdentifier convert(ButtonInteraction context, InteractionRegistry registry) {
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.ident.InteractionIdentifier;
import net.tomatentum.marinara.registry.InteractionRegistry;
import net.tomatentum.marinara.wrapper.IdentifierProvider;
public class SlashCommandIdentifierConverter implements IdentifierProvider.Converter<SlashCommandInteraction> {
@Override
public InteractionIdentifier convert(SlashCommandInteraction context) {
public InteractionIdentifier convert(SlashCommandInteraction context, InteractionRegistry registry) {
List<SlashCommandInteractionOption> options = context.getOptions();
String commandName = context.getCommandName();
if (!options.isEmpty()) {

View File

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