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