feat(core): add InteractionProcessors and InteractionExecutors and improve Autocomplete Interaction
This commit is contained in:
		| @@ -2,10 +2,15 @@ package net.tomatentum.marinara; | ||||
|  | ||||
| import org.slf4j.Logger; | ||||
|  | ||||
| import net.tomatentum.marinara.interaction.InteractionType; | ||||
| import net.tomatentum.marinara.interaction.processor.AutocompleteInteractionProcessor; | ||||
| import net.tomatentum.marinara.interaction.processor.DirectInteractionProcessor; | ||||
| import net.tomatentum.marinara.reflection.ReflectedMethodFactory; | ||||
| import net.tomatentum.marinara.reflection.ReflectedMethodFactoryImpl; | ||||
| import net.tomatentum.marinara.registry.InteractionCheckRegistry; | ||||
| import net.tomatentum.marinara.registry.InteractionExecutor; | ||||
| import net.tomatentum.marinara.registry.InteractionRegistry; | ||||
| import net.tomatentum.marinara.registry.ProcessorInteractionExecutor; | ||||
| import net.tomatentum.marinara.util.LoggerUtil; | ||||
| import net.tomatentum.marinara.wrapper.LibraryWrapper; | ||||
|  | ||||
| @@ -13,7 +18,7 @@ public class Marinara { | ||||
|  | ||||
|     private Logger logger = LoggerUtil.getLogger(getClass()); | ||||
|      | ||||
|     public static <T extends LibraryWrapper> Marinara load(LibraryWrapper wrapper) { | ||||
|     public static Marinara load(LibraryWrapper wrapper) { | ||||
|         return new Marinara(wrapper); | ||||
|     } | ||||
|  | ||||
| @@ -21,12 +26,17 @@ public class Marinara { | ||||
|     private ReflectedMethodFactory reflectedMethodFactory; | ||||
|     private InteractionRegistry registry; | ||||
|     private InteractionCheckRegistry checkRegistry; | ||||
|     private InteractionExecutor interactionExecutor; | ||||
|  | ||||
|     private Marinara(LibraryWrapper wrapper) { | ||||
|         this.wrapper = wrapper; | ||||
|         this.reflectedMethodFactory = new ReflectedMethodFactoryImpl(this); | ||||
|         this.registry = new InteractionRegistry(this); | ||||
|         this.checkRegistry = new InteractionCheckRegistry(); | ||||
|         this.interactionExecutor = new ProcessorInteractionExecutor(wrapper.createIdentifierProvider(), this) | ||||
|             .addProcessor(new DirectInteractionProcessor(InteractionType.COMMAND, InteractionType.BUTTON)) | ||||
|             .addProcessor(new AutocompleteInteractionProcessor()); | ||||
|         wrapper.subscribeInteractions(this.interactionExecutor::handle); | ||||
|         logger.info("Marinara loaded successfully!"); | ||||
|     } | ||||
|  | ||||
| @@ -45,4 +55,9 @@ public class Marinara { | ||||
|     public ReflectedMethodFactory getReflectedMethodFactory() { | ||||
|         return this.reflectedMethodFactory; | ||||
|     } | ||||
|  | ||||
|     public InteractionExecutor getInteractionExecutor() { | ||||
|         return interactionExecutor; | ||||
|     } | ||||
|      | ||||
| } | ||||
|   | ||||
| @@ -5,6 +5,7 @@ import java.lang.annotation.Retention; | ||||
| import java.lang.annotation.RetentionPolicy; | ||||
| import java.lang.annotation.Target; | ||||
|  | ||||
| import net.tomatentum.marinara.interaction.annotation.AutoComplete; | ||||
| import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType; | ||||
|  | ||||
| @Target({ElementType.ANNOTATION_TYPE}) | ||||
| @@ -14,11 +15,11 @@ public @interface SlashCommandOption { | ||||
|     public String description() default ""; | ||||
|     public SlashCommandOptionType type() default SlashCommandOptionType.STRING; | ||||
|     public boolean required() default false; | ||||
|     public boolean autocomplete() default false; | ||||
|     public AutoComplete[] autocompletes() default {}; | ||||
|     public Range range() default @Range; | ||||
|     public CommandChoices choices() default @CommandChoices; | ||||
|  | ||||
|     public static enum PlaceHolderEnum { | ||||
|     public enum PlaceHolderEnum { | ||||
|  | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -0,0 +1,16 @@ | ||||
| package net.tomatentum.marinara.interaction.commands.option; | ||||
|  | ||||
| import java.util.Arrays; | ||||
|  | ||||
| import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption; | ||||
|  | ||||
| public record AutocompleteOptionData(String name, Object input) { | ||||
|      | ||||
|     public String[] getAutocompleteRefs(SlashCommandOption[] options) { | ||||
|         return Arrays.stream(options) | ||||
|             .filter(o -> o.name().equals(this.name())) | ||||
|             .flatMap(o -> Arrays.stream(o.autocompletes())) | ||||
|             .map(a -> a.value()) | ||||
|             .toArray(String[]::new); | ||||
|     } | ||||
| } | ||||
| @@ -73,7 +73,7 @@ public class InteractionIdentifier { | ||||
|  | ||||
|     @Override | ||||
|     public boolean equals(Object obj) { | ||||
|         if (obj == null || !(obj instanceof InteractionIdentifier)) | ||||
|         if (!(obj instanceof InteractionIdentifier)) | ||||
|             return false; | ||||
|         InteractionIdentifier ident = (InteractionIdentifier) obj; | ||||
|         if (!type().equals(ident.type())) | ||||
| @@ -83,11 +83,16 @@ public class InteractionIdentifier { | ||||
|         return Objects.equals(parent(), ident.parent()); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public int hashCode() { | ||||
|         return Objects.hash(type(), name(), parent()); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public String toString() { | ||||
|         if (parent() == null) | ||||
|             return name(); | ||||
|         return "{}.{}".formatted(name(), parent().toString()); | ||||
|         return "%s.%s".formatted(name(), parent().toString()); | ||||
|     } | ||||
|  | ||||
|     public static class Builder { | ||||
|   | ||||
| @@ -13,9 +13,8 @@ public class RootCommandIdentifier extends SlashCommandIdentifier { | ||||
|             String description,  | ||||
|             InteractionType type, | ||||
|             SlashCommandOption[] options,  | ||||
|             long[] serverIds, | ||||
|             String[] autocompleteRef) { | ||||
|         super(parent, name, description, type, options, autocompleteRef); | ||||
|             long[] serverIds) { | ||||
|         super(parent, name, description, type, options); | ||||
|         this.serverIds = serverIds; | ||||
|     } | ||||
|  | ||||
| @@ -92,8 +91,7 @@ public class RootCommandIdentifier extends SlashCommandIdentifier { | ||||
|                 description,  | ||||
|                 InteractionType.COMMAND,  | ||||
|                 options,  | ||||
|                 serverIds, | ||||
|                 autocompleteRef); | ||||
|                 serverIds); | ||||
|         } | ||||
|  | ||||
|     } | ||||
|   | ||||
| @@ -6,33 +6,21 @@ 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, | ||||
|             String[] autocompleteRef | ||||
|             ) { | ||||
|             SlashCommandOption[] options) { | ||||
|         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; | ||||
| @@ -91,8 +79,7 @@ public class SlashCommandIdentifier extends InteractionIdentifier { | ||||
|                 name,  | ||||
|                 description,  | ||||
|                 InteractionType.COMMAND,  | ||||
|                 options, | ||||
|                 autocompleteRef); | ||||
|                 options); | ||||
|         } | ||||
|  | ||||
|     } | ||||
|   | ||||
| @@ -29,11 +29,15 @@ public class AutoCompleteInteractionMethod extends InteractionMethod { | ||||
|     @Override | ||||
|     public Object getParameter(Object context, int index) { | ||||
|         Class<?> type = method().getParameterTypes()[index+1]; | ||||
|         Object autocompleteOptionValue = marinara.getWrapper().getContextObjectProvider().getAutocompleteFocusedOption(context); | ||||
|         if (autocompleteOptionValue != null) | ||||
|         Object contextObject = marinara.getWrapper().getContextObjectProvider().getInteractionContextObject(context, type); | ||||
|         if (contextObject != null) | ||||
|             return contextObject; | ||||
|  | ||||
|         Object autocompleteOptionValue = marinara.getWrapper().getContextObjectProvider().getAutocompleteFocusedOption(context).input(); | ||||
|         if (type.isInstance(autocompleteOptionValue)) | ||||
|             return autocompleteOptionValue; | ||||
|  | ||||
|         return marinara.getWrapper().getContextObjectProvider().getInteractionContextObject(context, type); | ||||
|         return null; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
| @@ -50,11 +54,11 @@ public class AutoCompleteInteractionMethod extends InteractionMethod { | ||||
|         @Override | ||||
|         public Optional<ReflectedMethod> produce(Marinara marinara, Method method, Object containingObject) { | ||||
|             ReflectedMethod rMethod = null; | ||||
|             if ((containingObject instanceof InteractionHandler) && | ||||
|             if ((containingObject instanceof InteractionHandler iHandler) && | ||||
|                 method.isAnnotationPresent(AutoComplete.class) && | ||||
|                 !(method.isAnnotationPresent(SlashCommand.class) || | ||||
|                 method.isAnnotationPresent(SubCommand.class))) | ||||
|                 rMethod = new AutoCompleteInteractionMethod(method, (InteractionHandler) containingObject, marinara); | ||||
|                 rMethod = new AutoCompleteInteractionMethod(method, iHandler, marinara); | ||||
|  | ||||
|             return Optional.ofNullable(rMethod); | ||||
|         } | ||||
| @@ -63,9 +67,8 @@ public class AutoCompleteInteractionMethod extends InteractionMethod { | ||||
|         public void addParser(ReflectedMethod method, List<AnnotationParser> parser) { | ||||
|             super.addParser(method, parser); | ||||
|  | ||||
|             AutoCompleteInteractionMethod imethod = (AutoCompleteInteractionMethod) method; | ||||
|             parser.add( | ||||
|                 new AutocompleteParser(method.method(), x -> imethod.autocompleteRef = x[0]) | ||||
|                 new AutocompleteParser(method.method(), x -> ((AutoCompleteInteractionMethod) method).autocompleteRef = x[0]) | ||||
|             ); | ||||
|         } | ||||
|  | ||||
|   | ||||
| @@ -51,7 +51,7 @@ public abstract class InteractionMethod extends ReflectedMethod { | ||||
|         return this.appliedChecks; | ||||
|     } | ||||
|  | ||||
|     public static abstract class Factory implements ReflectedMethodFactory.Factory { | ||||
|     public abstract static class Factory implements ReflectedMethodFactory.Factory { | ||||
|  | ||||
|         @Override | ||||
|         public void addParser(ReflectedMethod method, List<AnnotationParser> parser) { | ||||
|   | ||||
| @@ -11,7 +11,6 @@ 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; | ||||
|  | ||||
| @@ -38,10 +37,10 @@ public class SlashCommandInteractionMethod extends InteractionMethod { | ||||
|         @Override | ||||
|         public Optional<ReflectedMethod> produce(Marinara marinara, Method method, Object containingObject) { | ||||
|             ReflectedMethod rMethod = null; | ||||
|             if ((containingObject instanceof InteractionHandler) &&  | ||||
|             if ((containingObject instanceof InteractionHandler iHandler) &&  | ||||
|                 (method.isAnnotationPresent(SlashCommand.class) || | ||||
|                 method.isAnnotationPresent(SubCommand.class))) | ||||
|                 rMethod = new SlashCommandInteractionMethod(method, (InteractionHandler) containingObject, marinara); | ||||
|                 rMethod = new SlashCommandInteractionMethod(method, iHandler, marinara); | ||||
|             return Optional.ofNullable(rMethod); | ||||
|         } | ||||
|  | ||||
| @@ -49,12 +48,8 @@ public class SlashCommandInteractionMethod extends InteractionMethod { | ||||
|         public void addParser(ReflectedMethod method, List<AnnotationParser> parser) { | ||||
|             super.addParser(method, parser); | ||||
|  | ||||
|             SlashCommandInteractionMethod imethod = (SlashCommandInteractionMethod) method; | ||||
|             parser.add( | ||||
|                 new SlashCommandParser(method.method(), x -> imethod.interactionIdentifier = x) | ||||
|             ); | ||||
|             parser.add( | ||||
|                 new AutocompleteParser(method.method(), x -> imethod.interactionIdentifier.autocompleteRef(x)) | ||||
|                 new SlashCommandParser(method.method(), x -> ((SlashCommandInteractionMethod) method).interactionIdentifier = x) | ||||
|             ); | ||||
|         } | ||||
|  | ||||
|   | ||||
| @@ -0,0 +1,46 @@ | ||||
| package net.tomatentum.marinara.interaction.processor; | ||||
|  | ||||
| import java.util.Arrays; | ||||
| import java.util.List; | ||||
| import java.util.Objects; | ||||
| import java.util.Optional; | ||||
|  | ||||
| import net.tomatentum.marinara.Marinara; | ||||
| 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; | ||||
|  | ||||
| public class AutocompleteInteractionProcessor implements InteractionProcessor { | ||||
|  | ||||
|     @Override | ||||
|     public void process(Object context, InteractionIdentifier identifier, Marinara marinara) { | ||||
|         if (!identifier.type().equals(InteractionType.AUTOCOMPLETE)) | ||||
|             return; | ||||
|         Optional<InteractionEntry> entry = marinara.getRegistry().findFor(convertToCommandIdentifier(identifier)); | ||||
|         if (entry.isPresent() && entry.get().identifier() instanceof SlashCommandIdentifier sIdent) { | ||||
|             List<String> autocompleteRefs = Arrays.asList(marinara.getWrapper().getContextObjectProvider() | ||||
|                 .getAutocompleteFocusedOption(context).getAutocompleteRefs(sIdent.options())); | ||||
|             List<Object> results = marinara.getRegistry().interactions().stream() | ||||
|                 .filter(e -> e.type().equals(InteractionType.AUTOCOMPLETE)) | ||||
|                 .filter(e -> autocompleteRefs.contains(e.identifier().name())) | ||||
|                 .map(e -> e.runAll(context)) | ||||
|                 .flatMap(Arrays::stream) | ||||
|                 .filter(Objects::nonNull) | ||||
|                 .toList(); | ||||
|             if (!results.isEmpty()) | ||||
|                 marinara.getWrapper().respondAutocomplete(context, results); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private InteractionIdentifier convertToCommandIdentifier(InteractionIdentifier identifier) { | ||||
|         if (Objects.isNull(identifier)) | ||||
|             return null; | ||||
|         return InteractionIdentifier.builder() | ||||
|             .type(InteractionType.COMMAND) | ||||
|             .name(identifier.name()) | ||||
|             .parent(convertToCommandIdentifier(identifier.parent())) | ||||
|             .build(); | ||||
|     } | ||||
|      | ||||
| } | ||||
| @@ -0,0 +1,32 @@ | ||||
| package net.tomatentum.marinara.interaction.processor; | ||||
|  | ||||
| import java.util.Set; | ||||
|  | ||||
| import org.slf4j.Logger; | ||||
|  | ||||
| import net.tomatentum.marinara.Marinara; | ||||
| import net.tomatentum.marinara.interaction.InteractionType; | ||||
| import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | ||||
| import net.tomatentum.marinara.util.LoggerUtil; | ||||
|  | ||||
| public class DirectInteractionProcessor implements InteractionProcessor { | ||||
|  | ||||
|     private Logger logger = LoggerUtil.getLogger(getClass()); | ||||
|     private Set<InteractionType> types; | ||||
|  | ||||
|     public DirectInteractionProcessor(InteractionType... types) { | ||||
|         this.types = Set.of(types); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void process(Object context, InteractionIdentifier identifier, Marinara marinara) { | ||||
|         if (!types.contains(identifier.type())) | ||||
|             return; | ||||
|         logger.debug("Processing {} : {} with context {}", identifier, identifier.type(), context); | ||||
|         marinara.getRegistry().interactions().stream() | ||||
|             .filter(e -> e.identifier().equals(identifier)) | ||||
|             .findFirst() | ||||
|             .ifPresent(e -> e.runAll(context)); | ||||
|     } | ||||
|      | ||||
| } | ||||
| @@ -0,0 +1,10 @@ | ||||
| package net.tomatentum.marinara.interaction.processor; | ||||
|  | ||||
| import net.tomatentum.marinara.Marinara; | ||||
| import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | ||||
|  | ||||
| public interface InteractionProcessor { | ||||
|  | ||||
|     void process(Object context, InteractionIdentifier identifier, Marinara marinara); | ||||
|      | ||||
| } | ||||
| @@ -1,8 +1,10 @@ | ||||
| package net.tomatentum.marinara.registry; | ||||
|  | ||||
| import java.util.Arrays; | ||||
| import java.util.HashSet; | ||||
| import java.util.Optional; | ||||
| import java.util.Set; | ||||
| import java.util.stream.Stream; | ||||
|  | ||||
| import org.slf4j.Logger; | ||||
|  | ||||
| @@ -34,22 +36,25 @@ public class InteractionEntry { | ||||
|     } | ||||
|  | ||||
|     public InteractionEntry addMethod(InteractionMethod method) { | ||||
|         InteractionIdentifier identifier = method.identifier(); | ||||
|         InteractionIdentifier midentifier = method.identifier(); | ||||
|  | ||||
|         if (!this.identifier().equals(identifier)) | ||||
|         if (!this.identifier().equals(midentifier)) | ||||
|             throw new IllegalArgumentException("Method's identifier did not equal the entry's identifier"); | ||||
|  | ||||
|         this.methods.add(method); | ||||
|         InteractionIdentifier.tryAddDescriptions(identifier, identifier); | ||||
|         InteractionIdentifier.tryAddDescriptions(midentifier, midentifier); | ||||
|         logger.debug("Added method {} to entry {}", method.method().getName(), this.identifier); | ||||
|         return this; | ||||
|     } | ||||
|  | ||||
|     public void runAll(Object context) { | ||||
|         this.methods.stream().forEach(x -> { | ||||
|             logger.debug("Running Method {} from {} with context {}", x.toString(), this.toString(), context.toString()); | ||||
|             x.run(context); | ||||
|         }); | ||||
|     public Object[] runAll(Object context) { | ||||
|         return this.methods.stream() | ||||
|             .map(x -> { | ||||
|                 logger.debug("Running Method {} from {} with context {}", x, this, context); | ||||
|                 return x.run(context); | ||||
|             }) | ||||
|             .flatMap(o -> o instanceof Object[] oArray ? Arrays.stream(oArray) : Stream.<Object>of(o)) | ||||
|             .toArray(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
| @@ -60,6 +65,11 @@ public class InteractionEntry { | ||||
|         return other.identifier().equals(identifier()); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public int hashCode() { | ||||
|         return this.identifier().hashCode(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public String toString() { | ||||
|         return "InteractionEntry(%s)".formatted(identifier().toString()); | ||||
|   | ||||
| @@ -0,0 +1,7 @@ | ||||
| package net.tomatentum.marinara.registry; | ||||
|  | ||||
| public interface InteractionExecutor { | ||||
|  | ||||
|     void handle(Object context); | ||||
|  | ||||
| } | ||||
| @@ -18,7 +18,6 @@ import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | ||||
| import net.tomatentum.marinara.interaction.ident.RootCommandIdentifier; | ||||
| import net.tomatentum.marinara.util.LoggerUtil; | ||||
| import net.tomatentum.marinara.util.ObjectAggregator; | ||||
| import net.tomatentum.marinara.wrapper.IdentifierProvider; | ||||
| import net.tomatentum.marinara.interaction.methods.AutoCompleteInteractionMethod; | ||||
| import net.tomatentum.marinara.interaction.methods.InteractionMethod; | ||||
| import net.tomatentum.marinara.interaction.methods.SlashCommandInteractionMethod; | ||||
| @@ -28,13 +27,10 @@ public class InteractionRegistry { | ||||
|     private Logger logger = LoggerUtil.getLogger(getClass()); | ||||
|     private Set<InteractionEntry> interactions; | ||||
|     private Marinara marinara; | ||||
|     private IdentifierProvider identifierProvider; | ||||
|  | ||||
|     public InteractionRegistry(Marinara marinara) { | ||||
|         this.interactions = new HashSet<>(); | ||||
|         this.marinara = marinara; | ||||
|         this.identifierProvider = marinara.getWrapper().createIdentifierProvider(); | ||||
|         marinara.getWrapper().subscribeInteractions(this::handle); | ||||
|         marinara.getReflectedMethodFactory() | ||||
|             .addFactory(new AutoCompleteInteractionMethod.Factory()) | ||||
|             .addFactory(new SlashCommandInteractionMethod.Factory()) | ||||
| @@ -72,19 +68,13 @@ public class InteractionRegistry { | ||||
|         marinara.getWrapper().getRegisterer().register(defs); | ||||
|     } | ||||
|  | ||||
|     public void handle(Object context) { | ||||
|         logger.debug("Received {} interaction ", context); | ||||
|         interactions.forEach((e) -> { | ||||
|             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(); | ||||
|     } | ||||
|  | ||||
|     public Set<InteractionEntry> interactions() { | ||||
|         return this.interactions; | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -0,0 +1,41 @@ | ||||
| package net.tomatentum.marinara.registry; | ||||
|  | ||||
| import java.util.HashSet; | ||||
| import java.util.Set; | ||||
|  | ||||
| import org.slf4j.Logger; | ||||
|  | ||||
| import net.tomatentum.marinara.Marinara; | ||||
| import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | ||||
| import net.tomatentum.marinara.interaction.processor.InteractionProcessor; | ||||
| import net.tomatentum.marinara.util.LoggerUtil; | ||||
| import net.tomatentum.marinara.wrapper.IdentifierProvider; | ||||
|  | ||||
| public class ProcessorInteractionExecutor implements InteractionExecutor { | ||||
|  | ||||
|     private Logger logger = LoggerUtil.getLogger(getClass()); | ||||
|  | ||||
|     private Set<InteractionProcessor> processors; | ||||
|     private IdentifierProvider identifierProvider; | ||||
|     private Marinara marinara; | ||||
|  | ||||
|     public ProcessorInteractionExecutor(IdentifierProvider identifierProvider, Marinara marinara) { | ||||
|         this.processors = new HashSet<>(); | ||||
|         this.identifierProvider = identifierProvider; | ||||
|         this.marinara = marinara; | ||||
|     } | ||||
|  | ||||
|     public ProcessorInteractionExecutor addProcessor(InteractionProcessor processor) { | ||||
|         this.processors.add(processor); | ||||
|         return this; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void handle(Object context) { | ||||
|         logger.debug("Received {} interaction ", context); | ||||
|         InteractionIdentifier identifier = this.identifierProvider.provide(context); | ||||
|         logger.debug("Processing {} : {} interaction ", identifier, identifier.type()); | ||||
|         processors.forEach(x -> x.process(context, identifier, this.marinara)); | ||||
|     } | ||||
|      | ||||
| } | ||||
| @@ -1,5 +1,7 @@ | ||||
| package net.tomatentum.marinara.wrapper; | ||||
|  | ||||
| import net.tomatentum.marinara.interaction.commands.option.AutocompleteOptionData; | ||||
|  | ||||
| public interface ContextObjectProvider { | ||||
|  | ||||
|     public Object convertCommandOption(Object context, String optionName); | ||||
| @@ -7,5 +9,5 @@ public interface ContextObjectProvider { | ||||
|     public Object getComponentContextObject(Object context, Class<?> type); | ||||
|     public Object getInteractionContextObject(Object context, Class<?> type); | ||||
|  | ||||
|     public Object getAutocompleteFocusedOption(Object context); | ||||
|     public AutocompleteOptionData getAutocompleteFocusedOption(Object context); | ||||
| } | ||||
|   | ||||
| @@ -11,7 +11,6 @@ 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; | ||||
|  | ||||
| @@ -30,30 +29,30 @@ public class IdentifierProvider { | ||||
|             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]; | ||||
|             Type parameterType = ((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, InteractionRegistry registry) { | ||||
|     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()); | ||||
|             logger.debug("No Identifier converter found for context {}", context.getClass()); | ||||
|  | ||||
|         @SuppressWarnings("unchecked") | ||||
|         Converter<Object> conv = (Converter<Object>) converter.get(type); | ||||
|  | ||||
|         return conv.convert(context, registry); | ||||
|         return conv.convert(context); | ||||
|     } | ||||
|  | ||||
|     @FunctionalInterface | ||||
|     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> { | ||||
| @@ -65,8 +64,8 @@ public class IdentifierProvider { | ||||
|         } | ||||
|  | ||||
|         @Override | ||||
|         public InteractionIdentifier convert(T context, InteractionRegistry registry) { | ||||
|             return this.converter.convert(context, registry); | ||||
|         public InteractionIdentifier convert(T context) { | ||||
|             return this.converter.convert(context); | ||||
|         } | ||||
|  | ||||
|     } | ||||
|   | ||||
| @@ -12,7 +12,7 @@ public abstract class LibraryWrapper { | ||||
|     } | ||||
|  | ||||
|     public void handleInteraction(Object context) { | ||||
|         interactionSubscriber.forEach((o) -> o.accept(context)); | ||||
|         interactionSubscriber.forEach(o -> o.accept(context)); | ||||
|     } | ||||
|  | ||||
|     public void subscribeInteractions(Consumer<Object> consumer) { | ||||
| @@ -25,5 +25,6 @@ public abstract class LibraryWrapper { | ||||
|     public abstract CommandRegisterer<?> getRegisterer();   | ||||
|     public abstract IdentifierProvider createIdentifierProvider(); | ||||
|     public abstract ContextObjectProvider getContextObjectProvider(); | ||||
|     public abstract void respondAutocomplete(Object context, List<Object> options); | ||||
|  | ||||
| } | ||||
| @@ -5,7 +5,9 @@ import java.util.List; | ||||
| import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent; | ||||
| import discord4j.core.event.domain.interaction.ChatInputInteractionEvent; | ||||
| import discord4j.core.event.domain.interaction.ComponentInteractionEvent; | ||||
| import discord4j.core.event.domain.interaction.InteractionCreateEvent; | ||||
| import discord4j.core.object.command.ApplicationCommandInteractionOption; | ||||
| import net.tomatentum.marinara.interaction.commands.option.AutocompleteOptionData; | ||||
| import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType; | ||||
| import net.tomatentum.marinara.wrapper.ContextObjectProvider; | ||||
|  | ||||
| @@ -79,24 +81,25 @@ public class Discord4JContextObjectProvider implements ContextObjectProvider { | ||||
|  | ||||
|     @Override | ||||
|     public Object getInteractionContextObject(Object context, Class<?> type) { | ||||
|         ComponentInteractionEvent componentInteractionEvent = (ComponentInteractionEvent) context; | ||||
|         InteractionCreateEvent interactionEvent = (InteractionCreateEvent) context; | ||||
|         switch (type.getName()) { | ||||
|             case "discord4j.core.object.entity.channel.MessageChannel": | ||||
|                 return componentInteractionEvent.getInteraction().getChannel().block(); | ||||
|                 return interactionEvent.getInteraction().getChannel().block(); | ||||
|             case "discord4j.core.object.entity.Guild": | ||||
|                 return componentInteractionEvent.getInteraction().getGuild().block(); | ||||
|                 return interactionEvent.getInteraction().getGuild().block(); | ||||
|             case "discord4j.core.object.entity.Member": | ||||
|                 return componentInteractionEvent.getInteraction().getMember().orElse(null); | ||||
|                 return interactionEvent.getInteraction().getMember().orElse(null); | ||||
|             case "discord4j.core.object.entity.User": | ||||
|                 return componentInteractionEvent.getInteraction().getUser(); | ||||
|                 return interactionEvent.getInteraction().getUser(); | ||||
|             default: | ||||
|                 return null; | ||||
|         } | ||||
|         return null; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public Object getAutocompleteFocusedOption(Object context) { | ||||
|         ChatInputAutoCompleteEvent interaction = (ChatInputAutoCompleteEvent) context; | ||||
|         return getOptionValue(interaction.getFocusedOption()); | ||||
|     public AutocompleteOptionData getAutocompleteFocusedOption(Object context) { | ||||
|         ApplicationCommandInteractionOption option = ((ChatInputAutoCompleteEvent) context).getFocusedOption(); | ||||
|         return new AutocompleteOptionData(option.getName(), getOptionValue(option)); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -55,10 +55,10 @@ public class Discord4JConverterSpec implements CommandConverter.Spec<Application | ||||
|             .name(option.name()) | ||||
|             .description(option.description()) | ||||
|             .required(option.required()) | ||||
|             .autocomplete(option.autocomplete()) | ||||
| 			.minLength(Double.valueOf(option.range().min()).intValue()) | ||||
|             .autocomplete(option.autocompletes().length > 0) | ||||
| 			.minLength((int) option.range().min()) | ||||
| 			.minValue(option.range().min()) | ||||
| 			.maxLength(Double.valueOf(option.range().max()).intValue()) | ||||
| 			.maxLength((int)option.range().max()) | ||||
| 			.maxValue(option.range().max()) | ||||
|             .choices(choices) | ||||
|             .build(); | ||||
|   | ||||
| @@ -1,14 +1,16 @@ | ||||
| package net.tomatentum.marinara.wrapper.discord4j; | ||||
|  | ||||
| import java.util.List; | ||||
| import java.util.function.Function; | ||||
| import java.util.function.UnaryOperator; | ||||
|  | ||||
| import org.slf4j.Logger; | ||||
|  | ||||
| import discord4j.core.GatewayDiscordClient; | ||||
| import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent; | ||||
| import discord4j.core.event.domain.interaction.InteractionCreateEvent; | ||||
| import discord4j.core.object.command.ApplicationCommandInteractionOption; | ||||
| import discord4j.core.object.command.ApplicationCommandOption.Type; | ||||
| import discord4j.discordjson.json.ApplicationCommandOptionChoiceData; | ||||
| import discord4j.discordjson.json.ApplicationCommandRequest; | ||||
|  | ||||
| import net.tomatentum.marinara.util.LoggerUtil; | ||||
| @@ -22,13 +24,12 @@ import net.tomatentum.marinara.wrapper.discord4j.identifierconverter.ButtonIdent | ||||
| import net.tomatentum.marinara.wrapper.discord4j.identifierconverter.SlashCommandIdentifierConverter; | ||||
|  | ||||
| public class Discord4JWrapper extends LibraryWrapper { | ||||
|  | ||||
|     public static final Function<List<ApplicationCommandInteractionOption>, List<ApplicationCommandInteractionOption>> SUB_FILTER = (i) -> | ||||
|     public static final UnaryOperator<List<ApplicationCommandInteractionOption>> SUB_FILTER = i -> | ||||
|         i.stream() | ||||
|             .filter(o -> o.getType().equals(Type.SUB_COMMAND) || o.getType().equals(Type.SUB_COMMAND_GROUP)) | ||||
|             .toList(); | ||||
|      | ||||
|     public static final Function<List<ApplicationCommandInteractionOption>, List<ApplicationCommandInteractionOption>> ARG_FILTER = (i) -> | ||||
|     public static final UnaryOperator<List<ApplicationCommandInteractionOption>> ARG_FILTER = i -> | ||||
|             i.stream() | ||||
|                 .filter(o -> !o.getType().equals(Type.SUB_COMMAND) && !o.getType().equals(Type.SUB_COMMAND_GROUP)) | ||||
|                 .toList(); | ||||
| @@ -71,4 +72,15 @@ public class Discord4JWrapper extends LibraryWrapper { | ||||
|         return this.contextObjectProvider; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void respondAutocomplete(Object context, List<Object> options) { | ||||
|         if (context instanceof ChatInputAutoCompleteEvent event) { | ||||
|             List<ApplicationCommandOptionChoiceData> choices = options.stream() | ||||
|                 .filter(ApplicationCommandOptionChoiceData.class::isInstance) | ||||
|                 .map(o -> (ApplicationCommandOptionChoiceData)o) | ||||
|                 .toList(); | ||||
|             event.respondWithSuggestions(choices); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,53 +1,39 @@ | ||||
| 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, InteractionRegistry registry) { | ||||
|     public InteractionIdentifier convert(ChatInputAutoCompleteEvent context) { | ||||
|         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()) | ||||
|                 ident = InteractionIdentifier.createHierarchy( | ||||
|                     InteractionType.COMMAND,  | ||||
|             List<ApplicationCommandInteractionOption> subOptions = Discord4JWrapper.SUB_FILTER.apply(options.getFirst().getOptions()); | ||||
|             if (!subOptions.isEmpty()) | ||||
|                 return InteractionIdentifier.createHierarchy( | ||||
|                     InteractionType.AUTOCOMPLETE,  | ||||
|                     commandName,  | ||||
|                     options.getFirst().getName(), | ||||
|                     sub_options.getFirst().getName()); | ||||
|                     subOptions.getFirst().getName()); | ||||
|             else | ||||
|                 ident = InteractionIdentifier.createHierarchy( | ||||
|                     InteractionType.COMMAND,  | ||||
|                 return InteractionIdentifier.createHierarchy( | ||||
|                     InteractionType.AUTOCOMPLETE,  | ||||
|                     commandName,  | ||||
|                     options.getFirst().getName()); | ||||
|         }else | ||||
|             ident = InteractionIdentifier.createHierarchy( | ||||
|                 InteractionType.COMMAND,  | ||||
|             return InteractionIdentifier.createHierarchy( | ||||
|                 InteractionType.AUTOCOMPLETE,  | ||||
|                 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; | ||||
|     } | ||||
|      | ||||
| } | ||||
|   | ||||
| @@ -3,13 +3,12 @@ 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, InteractionRegistry registry) { | ||||
|     public InteractionIdentifier convert(ButtonInteractionEvent context) { | ||||
|         return InteractionIdentifier.builder().name(context.getCustomId()).type(InteractionType.BUTTON).build(); | ||||
|     } | ||||
|      | ||||
|   | ||||
| @@ -6,14 +6,13 @@ 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, InteractionRegistry registry) { | ||||
|     public InteractionIdentifier convert(ChatInputInteractionEvent context) { | ||||
|         List<ApplicationCommandInteractionOption> options = Discord4JWrapper.SUB_FILTER.apply(context.getOptions()); | ||||
|         String commandName = context.getCommandName(); | ||||
|  | ||||
|   | ||||
| @@ -18,10 +18,10 @@ import net.tomatentum.marinara.Marinara; | ||||
| import net.tomatentum.marinara.wrapper.LibraryWrapper; | ||||
| import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper; | ||||
|  | ||||
| public class AutoCompleteTest { | ||||
| class AutoCompleteTest { | ||||
|      | ||||
|     @Test | ||||
|     public void testAutocomplete() { | ||||
|     void testAutocomplete() { | ||||
|         ApplicationCommandInteractionOption optionMock = mock(); | ||||
|         ChatInputAutoCompleteEvent autoCompleteEventMock = mock(); | ||||
|  | ||||
|   | ||||
| @@ -6,23 +6,35 @@ import java.util.Collections; | ||||
|  | ||||
| import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent; | ||||
| import discord4j.core.event.domain.interaction.ChatInputInteractionEvent; | ||||
| import discord4j.discordjson.json.ApplicationCommandOptionChoiceData; | ||||
| import net.tomatentum.marinara.interaction.InteractionHandler; | ||||
| import net.tomatentum.marinara.interaction.annotation.AutoComplete; | ||||
| import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; | ||||
| import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption; | ||||
| import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType; | ||||
|  | ||||
| public class TestAutocomplete implements InteractionHandler { | ||||
|  | ||||
|     @SlashCommand(name = "test") | ||||
|     @SlashCommand( | ||||
|         name = "test", | ||||
|         options = @SlashCommandOption( | ||||
|                 name = "foo", | ||||
|                 type = SlashCommandOptionType.STRING, | ||||
|                 autocompletes = @AutoComplete("testAuto") | ||||
|             ) | ||||
|         ) | ||||
|     @AutoComplete("testAuto") | ||||
|     public void exec(ChatInputInteractionEvent context) { | ||||
|  | ||||
|     } | ||||
|      | ||||
|     @AutoComplete("testAuto") | ||||
|     public void autocomplete(ChatInputAutoCompleteEvent context, String value) { | ||||
|     public ApplicationCommandOptionChoiceData[] autocomplete(ChatInputAutoCompleteEvent context, String value) { | ||||
|         System.out.println("Success!"); | ||||
|         assertEquals(value, "test"); | ||||
|         context.respondWithSuggestions(Collections.emptyList()); | ||||
|         assertEquals("test", value); | ||||
|         return new ApplicationCommandOptionChoiceData[]{ | ||||
|             ApplicationCommandOptionChoiceData.builder().name("TestValue").value("test").build() | ||||
|         }; | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -2,10 +2,12 @@ package net.tomatentum.marinara.wrapper.javacord; | ||||
|  | ||||
| import org.javacord.api.interaction.AutocompleteInteraction; | ||||
| import org.javacord.api.interaction.ButtonInteraction; | ||||
| import org.javacord.api.interaction.InteractionBase; | ||||
| import org.javacord.api.interaction.SlashCommandInteraction; | ||||
| import org.javacord.api.interaction.SlashCommandInteractionOption; | ||||
| import org.javacord.api.interaction.SlashCommandOptionType; | ||||
|  | ||||
| import net.tomatentum.marinara.interaction.commands.option.AutocompleteOptionData; | ||||
| import net.tomatentum.marinara.wrapper.ContextObjectProvider; | ||||
|  | ||||
| public class JavacordContextObjectProvider implements ContextObjectProvider { | ||||
| @@ -16,14 +18,14 @@ public class JavacordContextObjectProvider implements ContextObjectProvider { | ||||
|             return null; | ||||
|         SlashCommandInteraction interaction = (SlashCommandInteraction) context; | ||||
|         if (!interaction.getArguments().isEmpty()) | ||||
|             return getOptionValue(interaction.getOptionByName(optionName).get()); | ||||
|             return getOptionValue(interaction.getOptionByName(optionName).orElse(null)); | ||||
|  | ||||
|         SlashCommandInteractionOption subCommandOption = interaction.getOptions().getFirst(); | ||||
|  | ||||
|         if (!subCommandOption.getOptions().isEmpty()) | ||||
|             subCommandOption = subCommandOption.getOptions().getFirst(); | ||||
|  | ||||
|         return getOptionValue(subCommandOption.getOptionByName(optionName).get()); | ||||
|         return getOptionValue(subCommandOption.getOptionByName(optionName).orElse(null)); | ||||
|     } | ||||
|  | ||||
|     private Object getOptionValue(SlashCommandInteractionOption option) { | ||||
| @@ -89,7 +91,7 @@ public class JavacordContextObjectProvider implements ContextObjectProvider { | ||||
|  | ||||
|     @Override | ||||
|     public Object getInteractionContextObject(Object context, Class<?> type) { | ||||
|         ButtonInteraction button = (ButtonInteraction) context; | ||||
|         InteractionBase button = (InteractionBase) context; | ||||
|         switch (type.getName()) { | ||||
|             case "org.javacord.api.entity.channel.TextChannel": | ||||
|                 return button.getChannel().orElse(null); | ||||
| @@ -102,9 +104,9 @@ public class JavacordContextObjectProvider implements ContextObjectProvider { | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public Object getAutocompleteFocusedOption(Object context) { | ||||
|         AutocompleteInteraction interaction = (AutocompleteInteraction) context; | ||||
|         return getOptionValue(interaction.getFocusedOption()); | ||||
|     public AutocompleteOptionData getAutocompleteFocusedOption(Object context) { | ||||
|         SlashCommandInteractionOption option = ((AutocompleteInteraction) context).getFocusedOption(); | ||||
|         return new AutocompleteOptionData(option.getName(), getOptionValue(option)); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -50,10 +50,10 @@ public class JavacordConverterSpec implements CommandConverter.Spec<SlashCommand | ||||
|             .setName(option.name()) | ||||
|             .setDescription(option.description()) | ||||
|             .setRequired(option.required()) | ||||
|             .setAutocompletable(option.autocomplete()) | ||||
| 			.setMinLength(Double.valueOf(option.range().min()).longValue()) | ||||
|             .setAutocompletable(option.autocompletes().length > 0) | ||||
| 			.setMinLength((long) option.range().min()) | ||||
| 			.setDecimalMinValue(option.range().min()) | ||||
| 			.setMaxLength(Double.valueOf(option.range().max()).longValue()) | ||||
| 			.setMaxLength((long) option.range().max()) | ||||
| 			.setDecimalMaxValue(option.range().max()) | ||||
|             .setChoices(choices) | ||||
| 			.build(); | ||||
|   | ||||
| @@ -1,7 +1,11 @@ | ||||
| package net.tomatentum.marinara.wrapper.javacord; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| import org.javacord.api.DiscordApi; | ||||
| import org.javacord.api.interaction.AutocompleteInteraction; | ||||
| import org.javacord.api.interaction.SlashCommandBuilder; | ||||
| import org.javacord.api.interaction.SlashCommandOptionChoice; | ||||
| import org.slf4j.Logger; | ||||
|  | ||||
| import net.tomatentum.marinara.wrapper.CommandConverter; | ||||
| @@ -27,7 +31,7 @@ public class JavacordWrapper extends LibraryWrapper { | ||||
|  | ||||
|         if (api != null) { | ||||
|             this.commandRegisterer = CommandRegisterer.of(new JavacordRegistererStrategy(api), converter); | ||||
|             api.addInteractionCreateListener((e) -> handleInteraction(e.getInteraction())); | ||||
|             api.addInteractionCreateListener(e -> handleInteraction(e.getInteraction())); | ||||
|         }else | ||||
|             logger.warn("DiscordApi was null so no Events were subscribed to."); | ||||
|         logger.info("Javacord wrapper loaded!"); | ||||
| @@ -52,4 +56,15 @@ public class JavacordWrapper extends LibraryWrapper { | ||||
|         return contextObjectProvider; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void respondAutocomplete(Object context, List<Object> options) { | ||||
|         if (context instanceof AutocompleteInteraction interaction) { | ||||
|             List<SlashCommandOptionChoice> choices = options.stream() | ||||
|                 .filter(SlashCommandOptionChoice.class::isInstance) | ||||
|                 .map(o -> (SlashCommandOptionChoice)o) | ||||
|                 .toList(); | ||||
|             interaction.respondWithChoices(choices); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -1,53 +1,39 @@ | ||||
| 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, InteractionRegistry registry) { | ||||
|     public InteractionIdentifier convert(AutocompleteInteraction context) { | ||||
|         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()) | ||||
|                 ident = InteractionIdentifier.createHierarchy( | ||||
|                     InteractionType.COMMAND,  | ||||
|             List<SlashCommandInteractionOption> subOptions = context.getOptions().getFirst().getOptions(); | ||||
|             if (!subOptions.isEmpty()) | ||||
|                 return InteractionIdentifier.createHierarchy( | ||||
|                     InteractionType.AUTOCOMPLETE,  | ||||
|                     commandName,  | ||||
|                     options.getFirst().getName(), | ||||
|                     sub_options.getFirst().getName()); | ||||
|                     subOptions.getFirst().getName()); | ||||
|             else | ||||
|                 ident = InteractionIdentifier.createHierarchy( | ||||
|                     InteractionType.COMMAND,  | ||||
|                 return InteractionIdentifier.createHierarchy( | ||||
|                     InteractionType.AUTOCOMPLETE,  | ||||
|                     commandName,  | ||||
|                     options.getFirst().getName()); | ||||
|         }else | ||||
|             ident = InteractionIdentifier.createHierarchy( | ||||
|                 InteractionType.COMMAND,  | ||||
|             return InteractionIdentifier.createHierarchy( | ||||
|                 InteractionType.AUTOCOMPLETE,  | ||||
|                 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; | ||||
|     } | ||||
|      | ||||
| } | ||||
|   | ||||
| @@ -4,13 +4,12 @@ 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, InteractionRegistry registry) { | ||||
|     public InteractionIdentifier convert(ButtonInteraction context) { | ||||
|         return InteractionIdentifier.builder().name(context.getCustomId()).type(InteractionType.BUTTON).build(); | ||||
|     } | ||||
|      | ||||
|   | ||||
| @@ -7,13 +7,12 @@ 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, InteractionRegistry registry) { | ||||
|     public InteractionIdentifier convert(SlashCommandInteraction context) { | ||||
|         List<SlashCommandInteractionOption> options = context.getOptions(); | ||||
|         String commandName = context.getCommandName(); | ||||
|         if (!options.isEmpty()) { | ||||
|   | ||||
| @@ -6,17 +6,27 @@ import java.util.Collections; | ||||
|  | ||||
| import org.javacord.api.event.interaction.SlashCommandCreateEvent; | ||||
| import org.javacord.api.interaction.AutocompleteInteraction; | ||||
| import org.javacord.api.interaction.SlashCommandInteraction; | ||||
|  | ||||
| import net.tomatentum.marinara.interaction.InteractionHandler; | ||||
| import net.tomatentum.marinara.interaction.annotation.AutoComplete; | ||||
| import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; | ||||
| import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption; | ||||
| import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType; | ||||
|  | ||||
| public class TestAutocomplete implements InteractionHandler { | ||||
|      | ||||
|     @SlashCommand(name = "test") | ||||
|     @SlashCommand( | ||||
|         name = "test", | ||||
|         options = @SlashCommandOption( | ||||
|                 name = "foo", | ||||
|                 type = SlashCommandOptionType.STRING, | ||||
|                 autocompletes = @AutoComplete("testAuto") | ||||
|             ) | ||||
|         ) | ||||
|     @AutoComplete("testAuto") | ||||
|     public void exec(SlashCommandCreateEvent context) { | ||||
|          | ||||
|     public void exec(SlashCommandInteraction context) { | ||||
|  | ||||
|     } | ||||
|  | ||||
|     @AutoComplete("testAuto") | ||||
|   | ||||
		Reference in New Issue
	
	Block a user