Compare commits
	
		
			11 Commits
		
	
	
		
			dev
			...
			0b7b607a23
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 0b7b607a23 | |||
| 991d1c047b | |||
| ec17952375 | |||
| 0114cffcbd | |||
| 450f1fdaa1 | |||
| 92540576df | |||
| 8a3cde52fd | |||
| 8495659364 | |||
| 0973016a74 | |||
| 0590789359 | |||
| 2647a1f0b4 | 
| @@ -8,7 +8,11 @@ allprojects { | |||||||
|     group = "net.tomatentum.Marinara" |     group = "net.tomatentum.Marinara" | ||||||
|     version = "1.0.0-RC1" + (if (!project.hasProperty("release")) ("-" + getGitHash()) else "") |     version = "1.0.0-RC1" + (if (!project.hasProperty("release")) ("-" + getGitHash()) else "") | ||||||
|     description = "A simple but powerful, library-agnostic Discord Interaction Wrapper." |     description = "A simple but powerful, library-agnostic Discord Interaction Wrapper." | ||||||
|  |     plugins.withType<JavaPlugin> { | ||||||
|  |         tasks.withType<Jar>().configureEach { | ||||||
|  |             archiveBaseName.set("marinara-" + archiveBaseName.get()) | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| subprojects { | subprojects { | ||||||
|   | |||||||
| @@ -2,8 +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.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; | ||||||
|  |  | ||||||
| @@ -11,30 +18,46 @@ 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); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     private LibraryWrapper wrapper; | ||||||
|  |     private ReflectedMethodFactory reflectedMethodFactory; | ||||||
|     private InteractionRegistry registry; |     private InteractionRegistry registry; | ||||||
|     private InteractionCheckRegistry checkRegistry; |     private InteractionCheckRegistry checkRegistry; | ||||||
|     private LibraryWrapper wrapper; |     private InteractionExecutor interactionExecutor; | ||||||
|  |  | ||||||
|     private Marinara(LibraryWrapper wrapper) { |     private Marinara(LibraryWrapper wrapper) { | ||||||
|         this.wrapper = wrapper; |         this.wrapper = wrapper; | ||||||
|  |         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!"); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public LibraryWrapper getWrapper() { | ||||||
|  |         return this.wrapper; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     public InteractionRegistry getRegistry() { |     public InteractionRegistry getRegistry() { | ||||||
|         return registry; |         return this.registry; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public InteractionCheckRegistry getCheckRegistry() { |     public InteractionCheckRegistry getCheckRegistry() { | ||||||
|         return checkRegistry; |         return this.checkRegistry; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public LibraryWrapper getWrapper() { |     public ReflectedMethodFactory getReflectedMethodFactory() { | ||||||
|         return wrapper; |         return this.reflectedMethodFactory; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public InteractionExecutor getInteractionExecutor() { | ||||||
|  |         return interactionExecutor; | ||||||
|  |     } | ||||||
|  |      | ||||||
| } | } | ||||||
|   | |||||||
| @@ -8,5 +8,5 @@ import java.lang.annotation.Target; | |||||||
| @Target({ElementType.METHOD}) | @Target({ElementType.METHOD}) | ||||||
| @Retention(RetentionPolicy.RUNTIME) | @Retention(RetentionPolicy.RUNTIME) | ||||||
| public @interface AutoComplete { | public @interface AutoComplete { | ||||||
|      |     public String value(); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -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); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -0,0 +1,65 @@ | |||||||
|  | package net.tomatentum.marinara.interaction.components.methods; | ||||||
|  |  | ||||||
|  | import java.lang.reflect.Method; | ||||||
|  | import java.util.List; | ||||||
|  | import java.util.Optional; | ||||||
|  |  | ||||||
|  | import net.tomatentum.marinara.Marinara; | ||||||
|  | import net.tomatentum.marinara.interaction.InteractionHandler; | ||||||
|  | import net.tomatentum.marinara.interaction.InteractionType; | ||||||
|  | import net.tomatentum.marinara.interaction.annotation.Button; | ||||||
|  | import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | ||||||
|  | import net.tomatentum.marinara.interaction.methods.InteractionMethod; | ||||||
|  | import net.tomatentum.marinara.parser.AnnotationParser; | ||||||
|  | import net.tomatentum.marinara.parser.ButtonParser; | ||||||
|  | import net.tomatentum.marinara.reflection.ReflectedMethod; | ||||||
|  |  | ||||||
|  | public class ButtonInteractionMethod extends InteractionMethod { | ||||||
|  |  | ||||||
|  |     private String customId; | ||||||
|  |  | ||||||
|  |     private ButtonInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) { | ||||||
|  |         super(method, handler, marinara); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public Object getParameter(Object context, int index) { | ||||||
|  |         Class<?> type = method().getParameterTypes()[index+1]; | ||||||
|  |         return marinara.getWrapper().getContextObjectProvider().getComponentContextObject(context, type); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public InteractionIdentifier identifier() { | ||||||
|  |         return InteractionIdentifier.builder() | ||||||
|  |             .name(customId) | ||||||
|  |             .description("Button") | ||||||
|  |             .type(InteractionType.BUTTON) | ||||||
|  |             .build(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public static class Factory extends InteractionMethod.Factory { | ||||||
|  |  | ||||||
|  |         @Override | ||||||
|  |         public Optional<ReflectedMethod> produce(Marinara marinara, Method method, Object containingObject) { | ||||||
|  |             ReflectedMethod rMethod = null; | ||||||
|  |             if (method.isAnnotationPresent(Button.class) && | ||||||
|  |                 (containingObject instanceof InteractionHandler) | ||||||
|  |                 ) | ||||||
|  |                 rMethod = new ButtonInteractionMethod(method, (InteractionHandler) containingObject, marinara); | ||||||
|  |  | ||||||
|  |             return Optional.ofNullable(rMethod); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         @Override | ||||||
|  |         public void addParser(ReflectedMethod method, List<AnnotationParser> parser) { | ||||||
|  |             super.addParser(method, parser); | ||||||
|  |  | ||||||
|  |             ButtonInteractionMethod imethod = (ButtonInteractionMethod) method; | ||||||
|  |             parser.add( | ||||||
|  |                 new ButtonParser(method.method(), x -> imethod.customId = x) | ||||||
|  |             ); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |     } | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -80,14 +80,19 @@ public class InteractionIdentifier { | |||||||
|             return false; |             return false; | ||||||
|         if (!name().equals(ident.name())) |         if (!name().equals(ident.name())) | ||||||
|             return false; |             return false; | ||||||
|         return Objects.equals(ident, obj); |         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 { | ||||||
|   | |||||||
| @@ -28,7 +28,7 @@ public class RootCommandIdentifier extends SlashCommandIdentifier { | |||||||
|         private String description; |         private String description; | ||||||
|         private SlashCommandOption[] options; |         private SlashCommandOption[] options; | ||||||
|         private long[] serverIds; |         private long[] serverIds; | ||||||
|  |         private String[] autocompleteRef; | ||||||
|  |  | ||||||
|         public InteractionIdentifier parent() { |         public InteractionIdentifier parent() { | ||||||
|             return parent; |             return parent; | ||||||
| @@ -75,12 +75,21 @@ public class RootCommandIdentifier extends SlashCommandIdentifier { | |||||||
|             return this; |             return this; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         public SlashCommandIdentifier build(boolean autocomplete) { |         public String[] autocompleteRef() { | ||||||
|  |             return this.autocompleteRef; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         public Builder autocompleteRef(String[] autocompleteRef) { | ||||||
|  |             this.autocompleteRef = autocompleteRef; | ||||||
|  |             return this; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         public SlashCommandIdentifier build() { | ||||||
|             return new RootCommandIdentifier( |             return new RootCommandIdentifier( | ||||||
|                 parent,  |                 parent,  | ||||||
|                 name,  |                 name,  | ||||||
|                 description,  |                 description,  | ||||||
|                 autocomplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND,  |                 InteractionType.COMMAND,  | ||||||
|                 options,  |                 options,  | ||||||
|                 serverIds); |                 serverIds); | ||||||
|         } |         } | ||||||
|   | |||||||
| @@ -12,8 +12,7 @@ public class SlashCommandIdentifier extends InteractionIdentifier { | |||||||
|             String name,  |             String name,  | ||||||
|             String description, |             String description, | ||||||
|             InteractionType type, |             InteractionType type, | ||||||
|             SlashCommandOption[] options |             SlashCommandOption[] options) { | ||||||
|             ) { |  | ||||||
|         super(parent, name, description, type); |         super(parent, name, description, type); | ||||||
|         this.options = options; |         this.options = options; | ||||||
|     } |     } | ||||||
| @@ -27,6 +26,7 @@ public class SlashCommandIdentifier extends InteractionIdentifier { | |||||||
|         private String name; |         private String name; | ||||||
|         private String description; |         private String description; | ||||||
|         private SlashCommandOption[] options; |         private SlashCommandOption[] options; | ||||||
|  |         private String[] autocompleteRef; | ||||||
|  |  | ||||||
|         public InteractionIdentifier parent() { |         public InteractionIdentifier parent() { | ||||||
|             return parent; |             return parent; | ||||||
| @@ -64,12 +64,21 @@ public class SlashCommandIdentifier extends InteractionIdentifier { | |||||||
|             return this; |             return this; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         public SlashCommandIdentifier build(boolean autocomplete) { |         public String[] autocompleteRef() { | ||||||
|  |             return this.autocompleteRef; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         public Builder autocompleteRef(String[] autocompleteRef) { | ||||||
|  |             this.autocompleteRef = autocompleteRef; | ||||||
|  |             return this; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         public SlashCommandIdentifier build() { | ||||||
|             return new SlashCommandIdentifier( |             return new SlashCommandIdentifier( | ||||||
|                 parent,  |                 parent,  | ||||||
|                 name,  |                 name,  | ||||||
|                 description,  |                 description,  | ||||||
|                 autocomplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND,  |                 InteractionType.COMMAND,  | ||||||
|                 options); |                 options); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,44 +1,77 @@ | |||||||
| package net.tomatentum.marinara.interaction.methods; | package net.tomatentum.marinara.interaction.methods; | ||||||
|  |  | ||||||
| import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||||
|  | import java.util.List; | ||||||
|  | import java.util.Optional; | ||||||
|  |  | ||||||
| import net.tomatentum.marinara.Marinara; | import net.tomatentum.marinara.Marinara; | ||||||
| import net.tomatentum.marinara.interaction.InteractionHandler; | import net.tomatentum.marinara.interaction.InteractionHandler; | ||||||
|  | import net.tomatentum.marinara.interaction.InteractionType; | ||||||
|  | import net.tomatentum.marinara.interaction.annotation.AutoComplete; | ||||||
|  | import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; | ||||||
|  | import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; | ||||||
| import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | ||||||
| import net.tomatentum.marinara.parser.AnnotationParser; | import net.tomatentum.marinara.parser.AnnotationParser; | ||||||
| import net.tomatentum.marinara.parser.SlashCommandParser; | import net.tomatentum.marinara.parser.AutocompleteParser; | ||||||
|  | import net.tomatentum.marinara.reflection.ReflectedMethod; | ||||||
|  |  | ||||||
| public class AutoCompleteInteractionMethod extends InteractionMethod { | public class AutoCompleteInteractionMethod extends InteractionMethod { | ||||||
|  |  | ||||||
|     private InteractionIdentifier interactionIdentifier; |     private String autocompleteRef; | ||||||
|  |  | ||||||
|     public AutoCompleteInteractionMethod(Method method,  |     private AutoCompleteInteractionMethod(Method method,  | ||||||
|         InteractionHandler handler,  |         InteractionHandler handler,  | ||||||
|         Marinara marinara |         Marinara marinara | ||||||
|         ) { |         ) { | ||||||
|         super(method, handler, marinara); |         super(method, handler, marinara); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public AnnotationParser[] parsers() { |  | ||||||
|         return new AnnotationParser[] {  |  | ||||||
|             new SlashCommandParser(method, true, (x) -> { this.interactionIdentifier = x; } )  |  | ||||||
|         }; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @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 | ||||||
|     public InteractionIdentifier identifier() { |     public InteractionIdentifier identifier() { | ||||||
|         return interactionIdentifier; |         return InteractionIdentifier.builder() | ||||||
|  |             .type(InteractionType.AUTOCOMPLETE) | ||||||
|  |             .name(autocompleteRef) | ||||||
|  |             .description("AUTOCOMPLETE") | ||||||
|  |             .build(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public static class Factory extends InteractionMethod.Factory { | ||||||
|  |  | ||||||
|  |         @Override | ||||||
|  |         public Optional<ReflectedMethod> produce(Marinara marinara, Method method, Object containingObject) { | ||||||
|  |             ReflectedMethod rMethod = null; | ||||||
|  |             if ((containingObject instanceof InteractionHandler iHandler) && | ||||||
|  |                 method.isAnnotationPresent(AutoComplete.class) && | ||||||
|  |                 !(method.isAnnotationPresent(SlashCommand.class) || | ||||||
|  |                 method.isAnnotationPresent(SubCommand.class))) | ||||||
|  |                 rMethod = new AutoCompleteInteractionMethod(method, iHandler, marinara); | ||||||
|  |  | ||||||
|  |             return Optional.ofNullable(rMethod); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         @Override | ||||||
|  |         public void addParser(ReflectedMethod method, List<AnnotationParser> parser) { | ||||||
|  |             super.addParser(method, parser); | ||||||
|  |  | ||||||
|  |             parser.add( | ||||||
|  |                 new AutocompleteParser(method.method(), x -> ((AutoCompleteInteractionMethod) method).autocompleteRef = x[0]) | ||||||
|  |             ); | ||||||
|  |         } | ||||||
|  |  | ||||||
|     } |     } | ||||||
|      |      | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,42 +0,0 @@ | |||||||
| package net.tomatentum.marinara.interaction.methods; |  | ||||||
|  |  | ||||||
| import java.lang.reflect.Method; |  | ||||||
|  |  | ||||||
| import net.tomatentum.marinara.Marinara; |  | ||||||
| import net.tomatentum.marinara.interaction.InteractionHandler; |  | ||||||
| import net.tomatentum.marinara.interaction.InteractionType; |  | ||||||
| import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; |  | ||||||
| import net.tomatentum.marinara.parser.AnnotationParser; |  | ||||||
| import net.tomatentum.marinara.parser.ButtonParser; |  | ||||||
|  |  | ||||||
| public class ButtonInteractionMethod extends InteractionMethod { |  | ||||||
|  |  | ||||||
|     private String customId; |  | ||||||
|  |  | ||||||
|     ButtonInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) { |  | ||||||
|         super(method, handler, marinara); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public AnnotationParser[] parsers() { |  | ||||||
|         return new AnnotationParser[] { |  | ||||||
|             new ButtonParser(method, (x) -> { this.customId = x; } ) |  | ||||||
|         }; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public Object getParameter(Object context, int index) { |  | ||||||
|         Class<?> type = method().getParameterTypes()[index+1]; |  | ||||||
|         return marinara.getWrapper().getContextObjectProvider().getComponentContextObject(context, type); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public InteractionIdentifier identifier() { |  | ||||||
|         return InteractionIdentifier.builder() |  | ||||||
|             .name(customId) |  | ||||||
|             .description("Button") |  | ||||||
|             .type(InteractionType.BUTTON) |  | ||||||
|             .build(); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
| } |  | ||||||
| @@ -1,105 +1,66 @@ | |||||||
| package net.tomatentum.marinara.interaction.methods; | package net.tomatentum.marinara.interaction.methods; | ||||||
|  |  | ||||||
| import java.lang.reflect.InvocationTargetException; |  | ||||||
| import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||||
| import java.security.InvalidParameterException; |  | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| import java.util.Arrays; |  | ||||||
| import java.util.List; | import java.util.List; | ||||||
|  |  | ||||||
| import org.slf4j.Logger; |  | ||||||
|  |  | ||||||
| import net.tomatentum.marinara.Marinara; | import net.tomatentum.marinara.Marinara; | ||||||
| import net.tomatentum.marinara.checks.AppliedCheck; | import net.tomatentum.marinara.checks.AppliedCheck; | ||||||
| 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.Button; |  | ||||||
| import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; |  | ||||||
| import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; |  | ||||||
| import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | ||||||
| import net.tomatentum.marinara.parser.AnnotationParser; | import net.tomatentum.marinara.parser.AnnotationParser; | ||||||
| import net.tomatentum.marinara.parser.InteractionCheckParser; | import net.tomatentum.marinara.parser.InteractionCheckParser; | ||||||
| import net.tomatentum.marinara.util.LoggerUtil; | import net.tomatentum.marinara.reflection.ReflectedMethod; | ||||||
| import net.tomatentum.marinara.util.ReflectionUtil; | import net.tomatentum.marinara.reflection.ReflectedMethodFactory; | ||||||
|  |  | ||||||
| public abstract class InteractionMethod { | public abstract class InteractionMethod extends ReflectedMethod { | ||||||
|  |  | ||||||
|     public static InteractionMethod create(Method method, InteractionHandler handler, Marinara marinara) { |  | ||||||
|         if (method.isAnnotationPresent(AutoComplete.class)) |  | ||||||
|             return new AutoCompleteInteractionMethod(method, handler, marinara); |  | ||||||
|         if (method.isAnnotationPresent(SlashCommand.class) || method.isAnnotationPresent(SubCommand.class)) |  | ||||||
|             return new SlashCommandInteractionMethod(method, handler, marinara); |  | ||||||
|         if (method.isAnnotationPresent(Button.class)) |  | ||||||
|             return new ButtonInteractionMethod(method, handler, marinara); |  | ||||||
|         return null; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     protected Method method; |  | ||||||
|     protected InteractionHandler handler; |  | ||||||
|     protected Marinara marinara; |     protected Marinara marinara; | ||||||
|     protected List<AnnotationParser> parsers; |  | ||||||
|     protected List<AppliedCheck> appliedChecks; |     protected List<AppliedCheck> appliedChecks; | ||||||
|  |  | ||||||
|     private Logger logger = LoggerUtil.getLogger(getClass()); |  | ||||||
|  |  | ||||||
|     protected InteractionMethod( |     protected InteractionMethod( | ||||||
|         Method method,  |         Method method,  | ||||||
|         InteractionHandler handler,  |         InteractionHandler handler,  | ||||||
|         Marinara marinara |         Marinara marinara | ||||||
|         ) { |         ) { | ||||||
|         if (!Arrays.asList(handler.getClass().getMethods()).contains(method)) |         super(method, handler); | ||||||
|             throw new InvalidParameterException("Method does not apply to specified handler"); |  | ||||||
|  |  | ||||||
|         this.method = method; |  | ||||||
|         this.handler = handler; |  | ||||||
|         this.marinara = marinara; |         this.marinara = marinara; | ||||||
|         this.parsers = new ArrayList<>(Arrays.asList(parsers())); |  | ||||||
|         this.appliedChecks = new ArrayList<>(); |         this.appliedChecks = new ArrayList<>(); | ||||||
|  |  | ||||||
|         parsers.add(new InteractionCheckParser(method, appliedChecks::add, marinara.getCheckRegistry())); |  | ||||||
|  |  | ||||||
|         parsers.stream().forEach(AnnotationParser::parse); |  | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     public void run(Object context) { |     @Override | ||||||
|  |     public Object run(Object context) { | ||||||
|  |         Object result = null; | ||||||
|         if (this.appliedChecks.stream().filter(x -> !x.pre(context)).count() > 0) |         if (this.appliedChecks.stream().filter(x -> !x.pre(context)).count() > 0) | ||||||
|             return; |             return null; | ||||||
|  |  | ||||||
|         method.setAccessible(true); |         result = super.run(context); | ||||||
|         try { |  | ||||||
|             method.invoke(handler, getParameters(context)); |  | ||||||
|         }catch (IllegalAccessException | InvocationTargetException ex) { |  | ||||||
|             logger.error("InteractionMethod failed to run", ex); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         this.appliedChecks.forEach(x -> x.post(context)); |         this.appliedChecks.forEach(x -> x.post(context)); | ||||||
|  |  | ||||||
|  |         return result; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public abstract AnnotationParser[] parsers(); |  | ||||||
|  |  | ||||||
|     public abstract Object getParameter(Object context, int index); |  | ||||||
|  |  | ||||||
|     public abstract InteractionIdentifier identifier(); |     public abstract InteractionIdentifier identifier(); | ||||||
|  |  | ||||||
|     public Method method() { |     public Marinara marinara() { | ||||||
|         return method; |         return this.marinara; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private Object[] getParameters(Object context) { |     public List<AppliedCheck> appliedChecks() { | ||||||
|         int parameterCount = method.getParameterCount(); |         return this.appliedChecks; | ||||||
|         List<Object> parameters = new ArrayList<>(); |  | ||||||
|          |  | ||||||
|         for (int i = 0; i < parameterCount; i++) { |  | ||||||
|             Object parameter; |  | ||||||
|             if (i == 0) { |  | ||||||
|                 parameter = context; |  | ||||||
|             }else |  | ||||||
|                 parameter = getParameter(context, i-1); |  | ||||||
|  |  | ||||||
|             logger.trace("Found parameter {}={} for method {}", parameter != null ? parameter.getClass().toString() : " ", parameter, ReflectionUtil.getFullMethodName(method)); |  | ||||||
|             parameters.add(parameter);    |  | ||||||
|     } |     } | ||||||
|         return parameters.toArray(); |  | ||||||
|  |     public abstract static class Factory implements ReflectedMethodFactory.Factory { | ||||||
|  |  | ||||||
|  |         @Override | ||||||
|  |         public void addParser(ReflectedMethod method, List<AnnotationParser> parser) { | ||||||
|  |             InteractionMethod imethod = (InteractionMethod) method; | ||||||
|  |             parser.add( | ||||||
|  |                 new InteractionCheckParser(method.method(), imethod.appliedChecks::add, imethod.marinara().getCheckRegistry()) | ||||||
|  |             ); | ||||||
|  |         } | ||||||
|  |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,29 +1,27 @@ | |||||||
| package net.tomatentum.marinara.interaction.methods; | package net.tomatentum.marinara.interaction.methods; | ||||||
|  |  | ||||||
| import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||||
|  | import java.util.List; | ||||||
|  | import java.util.Optional; | ||||||
|  |  | ||||||
| import net.tomatentum.marinara.Marinara; | import net.tomatentum.marinara.Marinara; | ||||||
| import net.tomatentum.marinara.interaction.InteractionHandler; | import net.tomatentum.marinara.interaction.InteractionHandler; | ||||||
|  | import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; | ||||||
|  | import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; | ||||||
| import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | ||||||
| import net.tomatentum.marinara.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.SlashCommandParser; | import net.tomatentum.marinara.parser.SlashCommandParser; | ||||||
|  | import net.tomatentum.marinara.reflection.ReflectedMethod; | ||||||
|  |  | ||||||
| public class SlashCommandInteractionMethod extends InteractionMethod { | public class SlashCommandInteractionMethod extends InteractionMethod { | ||||||
|  |  | ||||||
|     private SlashCommandIdentifier interactionIdentifier; |     private SlashCommandIdentifier interactionIdentifier; | ||||||
|  |  | ||||||
|     SlashCommandInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) { |     private SlashCommandInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) { | ||||||
|         super(method, handler, marinara); |         super(method, handler, marinara); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public AnnotationParser[] parsers() { |  | ||||||
|         return new AnnotationParser[] {  |  | ||||||
|             new SlashCommandParser(method, false, (x) -> { this.interactionIdentifier = x; } )  |  | ||||||
|         }; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public Object getParameter(Object context, int index) { |     public Object getParameter(Object context, int index) { | ||||||
|         return marinara.getWrapper().getContextObjectProvider().convertCommandOption(context, interactionIdentifier.options()[index].name()); |         return marinara.getWrapper().getContextObjectProvider().convertCommandOption(context, interactionIdentifier.options()[index].name()); | ||||||
| @@ -34,4 +32,27 @@ public class SlashCommandInteractionMethod extends InteractionMethod { | |||||||
|         return interactionIdentifier; |         return interactionIdentifier; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public static class Factory extends InteractionMethod.Factory { | ||||||
|  |  | ||||||
|  |         @Override | ||||||
|  |         public Optional<ReflectedMethod> produce(Marinara marinara, Method method, Object containingObject) { | ||||||
|  |             ReflectedMethod rMethod = null; | ||||||
|  |             if ((containingObject instanceof InteractionHandler iHandler) &&  | ||||||
|  |                 (method.isAnnotationPresent(SlashCommand.class) || | ||||||
|  |                 method.isAnnotationPresent(SubCommand.class))) | ||||||
|  |                 rMethod = new SlashCommandInteractionMethod(method, iHandler, marinara); | ||||||
|  |             return Optional.ofNullable(rMethod); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         @Override | ||||||
|  |         public void addParser(ReflectedMethod method, List<AnnotationParser> parser) { | ||||||
|  |             super.addParser(method, parser); | ||||||
|  |  | ||||||
|  |             parser.add( | ||||||
|  |                 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); | ||||||
|  |      | ||||||
|  | } | ||||||
| @@ -0,0 +1,33 @@ | |||||||
|  | package net.tomatentum.marinara.parser; | ||||||
|  |  | ||||||
|  | import java.lang.reflect.Method; | ||||||
|  | import java.util.Arrays; | ||||||
|  | import java.util.function.Consumer; | ||||||
|  |  | ||||||
|  | import net.tomatentum.marinara.interaction.annotation.AutoComplete; | ||||||
|  |  | ||||||
|  | public class AutocompleteParser implements AnnotationParser { | ||||||
|  |  | ||||||
|  |     private Method method; | ||||||
|  |     private Consumer<String[]> consumer; | ||||||
|  |  | ||||||
|  |     public AutocompleteParser(Method method, Consumer<String[]> consumer) { | ||||||
|  |         this.method = method; | ||||||
|  |         this.consumer = consumer; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public void parse() { | ||||||
|  |         String[] autocompletes = Arrays.stream(this.method.getAnnotationsByType(AutoComplete.class)) | ||||||
|  |             .map(AutoComplete::value) | ||||||
|  |             .toArray(String[]::new); | ||||||
|  |             this.consumer.accept(autocompletes); | ||||||
|  |  | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public Method getMethod() { | ||||||
|  |         return this.method; | ||||||
|  |     } | ||||||
|  |      | ||||||
|  | } | ||||||
| @@ -17,14 +17,12 @@ import net.tomatentum.marinara.util.ReflectionUtil; | |||||||
| public class SlashCommandParser implements AnnotationParser { | public class SlashCommandParser implements AnnotationParser { | ||||||
|  |  | ||||||
|     private Method method; |     private Method method; | ||||||
|     private boolean isAutoComplete; |  | ||||||
|     private Consumer<SlashCommandIdentifier> consumer; |     private Consumer<SlashCommandIdentifier> consumer; | ||||||
|  |  | ||||||
|     private Logger logger = LoggerUtil.getLogger(getClass()); |     private Logger logger = LoggerUtil.getLogger(getClass()); | ||||||
|  |  | ||||||
|     public SlashCommandParser(Method method, boolean isAutoComplete, Consumer<SlashCommandIdentifier> consumer) { |     public SlashCommandParser(Method method, Consumer<SlashCommandIdentifier> consumer) { | ||||||
|         this.method = method; |         this.method = method; | ||||||
|         this.isAutoComplete = isAutoComplete; |  | ||||||
|         this.consumer = consumer; |         this.consumer = consumer; | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -38,14 +36,14 @@ public class SlashCommandParser implements AnnotationParser { | |||||||
|             .description(cmd.description()) |             .description(cmd.description()) | ||||||
|             .options(cmd.options()) |             .options(cmd.options()) | ||||||
|             .serverIds(cmd.serverIds()) |             .serverIds(cmd.serverIds()) | ||||||
|             .build(isAutoComplete); |             .build(); | ||||||
|  |  | ||||||
|         if (ReflectionUtil.isAnnotationPresent(method, SubCommandGroup.class)) { |         if (ReflectionUtil.isAnnotationPresent(method, SubCommandGroup.class)) { | ||||||
|             SubCommandGroup cmdGroup = ReflectionUtil.getAnnotation(method, SubCommandGroup.class); |             SubCommandGroup cmdGroup = ReflectionUtil.getAnnotation(method, SubCommandGroup.class); | ||||||
|             lastIdentifier = InteractionIdentifier.builder() |             lastIdentifier = InteractionIdentifier.builder() | ||||||
|                 .name(cmdGroup.name()) |                 .name(cmdGroup.name()) | ||||||
|                 .description(cmdGroup.description()) |                 .description(cmdGroup.description()) | ||||||
|                 .type(isAutoComplete ? InteractionType.AUTOCOMPLETE : InteractionType.COMMAND) |                 .type(InteractionType.COMMAND) | ||||||
|                 .parent(lastIdentifier) |                 .parent(lastIdentifier) | ||||||
|                 .build(); |                 .build(); | ||||||
|         } |         } | ||||||
| @@ -56,7 +54,7 @@ public class SlashCommandParser implements AnnotationParser { | |||||||
|                 .name(subCmd.name()) |                 .name(subCmd.name()) | ||||||
|                 .description(subCmd.description()) |                 .description(subCmd.description()) | ||||||
|                 .options(subCmd.options()) |                 .options(subCmd.options()) | ||||||
|                 .build(isAutoComplete); |                 .build(); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         logger.trace("Parsed using SlashCommandParser for method {} with the result: {}", ReflectionUtil.getFullMethodName(method), lastIdentifier.toString()); |         logger.trace("Parsed using SlashCommandParser for method {} with the result: {}", ReflectionUtil.getFullMethodName(method), lastIdentifier.toString()); | ||||||
|   | |||||||
| @@ -0,0 +1,66 @@ | |||||||
|  | package net.tomatentum.marinara.reflection; | ||||||
|  |  | ||||||
|  | import java.lang.reflect.InvocationTargetException; | ||||||
|  | import java.lang.reflect.Method; | ||||||
|  | import java.security.InvalidParameterException; | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.Arrays; | ||||||
|  | import java.util.List; | ||||||
|  |  | ||||||
|  | import org.slf4j.Logger; | ||||||
|  |  | ||||||
|  | import net.tomatentum.marinara.util.LoggerUtil; | ||||||
|  | import net.tomatentum.marinara.util.ReflectionUtil; | ||||||
|  |  | ||||||
|  | public abstract class ReflectedMethod { | ||||||
|  |  | ||||||
|  |     private Logger logger = LoggerUtil.getLogger(getClass()); | ||||||
|  |  | ||||||
|  |     private Method method; | ||||||
|  |     private Object containingObject; | ||||||
|  |  | ||||||
|  |     public ReflectedMethod(Method method, Object containingObject) { | ||||||
|  |         if (!Arrays.asList(containingObject.getClass().getMethods()).contains(method)) | ||||||
|  |             throw new InvalidParameterException("Method does not apply to specified handler"); | ||||||
|  |         this.method = method; | ||||||
|  |         this.containingObject = containingObject; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public abstract Object getParameter(Object context, int index); | ||||||
|  |  | ||||||
|  |     public Object run(Object context) { | ||||||
|  |         method.setAccessible(true); | ||||||
|  |         try { | ||||||
|  |             return method.invoke(containingObject, getParameters(context)); | ||||||
|  |         }catch (IllegalAccessException | InvocationTargetException ex) { | ||||||
|  |             logger.error("ReflectedMethod failed to run", ex); | ||||||
|  |             return null; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public Method method() { | ||||||
|  |         return this.method; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public Object containingObject() { | ||||||
|  |         return this.containingObject; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     private Object[] getParameters(Object context) { | ||||||
|  |         int parameterCount = method.getParameterCount(); | ||||||
|  |         List<Object> parameters = new ArrayList<>(); | ||||||
|  |          | ||||||
|  |         for (int i = 0; i < parameterCount; i++) { | ||||||
|  |             Object parameter; | ||||||
|  |             if (i == 0) { | ||||||
|  |                 parameter = context; | ||||||
|  |             }else | ||||||
|  |                 parameter = getParameter(context, i-1); | ||||||
|  |  | ||||||
|  |             logger.trace("Found parameter {}={} for method {}", parameter != null ? parameter.getClass().toString() : " ", parameter, ReflectionUtil.getFullMethodName(method)); | ||||||
|  |             parameters.add(parameter);    | ||||||
|  |         } | ||||||
|  |         return parameters.toArray(); | ||||||
|  |     } | ||||||
|  |      | ||||||
|  | } | ||||||
| @@ -0,0 +1,20 @@ | |||||||
|  | package net.tomatentum.marinara.reflection; | ||||||
|  |  | ||||||
|  | import java.lang.reflect.Method; | ||||||
|  | import java.util.List; | ||||||
|  | import java.util.Optional; | ||||||
|  |  | ||||||
|  | import net.tomatentum.marinara.Marinara; | ||||||
|  | import net.tomatentum.marinara.parser.AnnotationParser; | ||||||
|  |  | ||||||
|  | public interface ReflectedMethodFactory { | ||||||
|  |     Optional<ReflectedMethod> produce(Method method, Object containingClass); | ||||||
|  |     ReflectedMethodFactory addFactory(Factory factory); | ||||||
|  |  | ||||||
|  |     public interface Factory { | ||||||
|  |  | ||||||
|  |         Optional<ReflectedMethod> produce(Marinara marinara, Method method, Object containingObject); | ||||||
|  |         void addParser(ReflectedMethod method, List<AnnotationParser> parser); | ||||||
|  |  | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -0,0 +1,62 @@ | |||||||
|  | package net.tomatentum.marinara.reflection; | ||||||
|  |  | ||||||
|  | import java.lang.reflect.Method; | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.List; | ||||||
|  | import java.util.Optional; | ||||||
|  |  | ||||||
|  | import org.slf4j.Logger; | ||||||
|  |  | ||||||
|  | import net.tomatentum.marinara.Marinara; | ||||||
|  | import net.tomatentum.marinara.parser.AnnotationParser; | ||||||
|  | import net.tomatentum.marinara.util.LoggerUtil; | ||||||
|  | import net.tomatentum.marinara.util.ReflectionUtil; | ||||||
|  |  | ||||||
|  | public class ReflectedMethodFactoryImpl implements ReflectedMethodFactory { | ||||||
|  |  | ||||||
|  |     private Logger logger = LoggerUtil.getLogger(getClass()); | ||||||
|  |  | ||||||
|  |     private Marinara marinara; | ||||||
|  |     private List<Factory> factories; | ||||||
|  |  | ||||||
|  |     public ReflectedMethodFactoryImpl(Marinara marinara) { | ||||||
|  |         this(marinara, new ArrayList<>()); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public ReflectedMethodFactoryImpl(Marinara marinara, List<Factory> factories) { | ||||||
|  |         this.marinara = marinara; | ||||||
|  |         this.factories = factories; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public Optional<ReflectedMethod> produce(Method method, Object containingClass) { | ||||||
|  |         Optional<ReflectedMethod> imethod = this.factories.stream() | ||||||
|  |             .map(f -> factoryProduce(f, method, containingClass)) | ||||||
|  |             .filter(Optional::isPresent) | ||||||
|  |             .map(Optional::get) | ||||||
|  |             .findFirst(); | ||||||
|  |  | ||||||
|  |         if (imethod.isEmpty()) { | ||||||
|  |             logger.debug("Could not produce a ReflectedMethod for Method {}", ReflectionUtil.getFullMethodName(method)); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return imethod; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public ReflectedMethodFactory addFactory(Factory factory) { | ||||||
|  |         this.factories.add(factory); | ||||||
|  |         return this; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     private Optional<ReflectedMethod> factoryProduce(Factory factory, Method method, Object containingClass) { | ||||||
|  |         List<AnnotationParser> parser = new ArrayList<>(); | ||||||
|  |         Optional<ReflectedMethod> m = factory.produce(this.marinara, method, containingClass); | ||||||
|  |         m.ifPresent(x -> { | ||||||
|  |             factory.addParser(x, parser);  | ||||||
|  |             parser.forEach(AnnotationParser::parse); | ||||||
|  |         }); | ||||||
|  |         return m; | ||||||
|  |     } | ||||||
|  |      | ||||||
|  | } | ||||||
| @@ -1,7 +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.Set; | import java.util.Set; | ||||||
|  | import java.util.stream.Stream; | ||||||
|  |  | ||||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||||
|  |  | ||||||
| @@ -11,6 +14,17 @@ import net.tomatentum.marinara.interaction.methods.InteractionMethod; | |||||||
| import net.tomatentum.marinara.util.LoggerUtil; | import net.tomatentum.marinara.util.LoggerUtil; | ||||||
|  |  | ||||||
| public class InteractionEntry { | public class InteractionEntry { | ||||||
|  |  | ||||||
|  |     public static InteractionEntry findEntry(Set<InteractionEntry> entries, InteractionIdentifier identifier) { | ||||||
|  |         Optional<InteractionEntry> oentry = entries.stream() | ||||||
|  |             .filter(i -> i.identifier().equals(identifier)) | ||||||
|  |             .findFirst(); | ||||||
|  |  | ||||||
|  |         InteractionEntry entry = oentry.orElse(new InteractionEntry(identifier)); | ||||||
|  |         if (oentry.isEmpty()) entries.add(entry); | ||||||
|  |         return entry; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     private InteractionIdentifier identifier; |     private InteractionIdentifier identifier; | ||||||
|     private Set<InteractionMethod> methods; |     private Set<InteractionMethod> methods; | ||||||
|  |  | ||||||
| @@ -22,19 +36,25 @@ public class InteractionEntry { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     public InteractionEntry addMethod(InteractionMethod method) { |     public InteractionEntry addMethod(InteractionMethod method) { | ||||||
|         if (!method.identifier().equals(this.identifier)) |         InteractionIdentifier midentifier = method.identifier(); | ||||||
|             throw new IllegalArgumentException("Method's identifier did not match the entry's identifier"); |  | ||||||
|  |         if (!this.identifier().equals(midentifier)) | ||||||
|  |             throw new IllegalArgumentException("Method's identifier did not equal the entry's identifier"); | ||||||
|  |  | ||||||
|         this.methods.add(method); |         this.methods.add(method); | ||||||
|         InteractionIdentifier.tryAddDescriptions(identifier, method.identifier()); |         InteractionIdentifier.tryAddDescriptions(midentifier, midentifier); | ||||||
|  |         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 | ||||||
| @@ -45,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); | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -13,41 +13,41 @@ import net.tomatentum.marinara.Marinara; | |||||||
| import net.tomatentum.marinara.interaction.InteractionHandler; | import net.tomatentum.marinara.interaction.InteractionHandler; | ||||||
| import net.tomatentum.marinara.interaction.InteractionType; | import net.tomatentum.marinara.interaction.InteractionType; | ||||||
| import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition; | import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition; | ||||||
|  | import net.tomatentum.marinara.interaction.components.methods.ButtonInteractionMethod; | ||||||
| import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | 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.InteractionMethod; | import net.tomatentum.marinara.interaction.methods.InteractionMethod; | ||||||
|  | import net.tomatentum.marinara.interaction.methods.SlashCommandInteractionMethod; | ||||||
|  | import net.tomatentum.marinara.reflection.ReflectedMethod; | ||||||
|  |  | ||||||
| public class InteractionRegistry { | 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.getReflectedMethodFactory() | ||||||
|         marinara.getWrapper().subscribeInteractions(this::handle); |             .addFactory(new AutoCompleteInteractionMethod.Factory()) | ||||||
|  |             .addFactory(new SlashCommandInteractionMethod.Factory()) | ||||||
|  |             .addFactory(new ButtonInteractionMethod.Factory()); | ||||||
|  |  | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     /* |  | ||||||
|      * TODO: Maybe relocate InteractionEntry checking to another class with description merging. |  | ||||||
|      */ |  | ||||||
|     public void addInteractions(InteractionHandler interactionHandler) { |     public void addInteractions(InteractionHandler interactionHandler) { | ||||||
|         for (Method method : interactionHandler.getClass().getMethods()) { |         for (Method method : interactionHandler.getClass().getDeclaredMethods()) { | ||||||
|             InteractionMethod iMethod = InteractionMethod.create(method, interactionHandler, marinara); |             Optional<ReflectedMethod> rMethod = this.marinara.getReflectedMethodFactory().produce(method, interactionHandler); | ||||||
|             if (iMethod != null) { |             rMethod.ifPresent(x -> { | ||||||
|                 Optional<InteractionEntry> oentry = this.interactions.stream() |                 if (x instanceof InteractionMethod) { | ||||||
|                     .filter(i -> i.identifier().equals(iMethod.identifier())) |                     InteractionMethod iMethod = (InteractionMethod) x; | ||||||
|                     .findFirst(); |                     InteractionEntry.findEntry(interactions, iMethod.identifier()).addMethod(iMethod); | ||||||
|  |  | ||||||
|                 InteractionEntry entry = oentry.orElse(new InteractionEntry(iMethod.identifier())).addMethod(iMethod); |  | ||||||
|                 if (oentry.isEmpty()) this.interactions.add(entry); |  | ||||||
|                     logger.debug("Added {} method from {}", iMethod.method().getName(), interactionHandler.getClass().getSimpleName());                     |                     logger.debug("Added {} method from {}", iMethod.method().getName(), interactionHandler.getClass().getSimpleName());                     | ||||||
|                 } |                 } | ||||||
|  |             }); | ||||||
|         } |         } | ||||||
|         logger.info("Added all Interactions from {}", interactionHandler.getClass().getSimpleName()); |         logger.info("Added all Interactions from {}", interactionHandler.getClass().getSimpleName()); | ||||||
|     } |     } | ||||||
| @@ -68,13 +68,13 @@ public class InteractionRegistry { | |||||||
|         marinara.getWrapper().getRegisterer().register(defs); |         marinara.getWrapper().getRegisterer().register(defs); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public void handle(Object context) { |     public Optional<InteractionEntry> findFor(InteractionIdentifier identifier) { | ||||||
|         logger.debug("Received {} interaction ", context); |         return this.interactions.stream() | ||||||
|         interactions.forEach((e) -> { |             .filter(x -> x.identifier().equals(identifier)) | ||||||
|             if (this.identifierProvider.provide(context).equals(e.identifier())) { |             .findFirst(); | ||||||
|                 logger.info("Running {} interaction using {}\ncontext: {}", e.type(), e.toString(), context.toString()); |  | ||||||
|                 e.runAll(context); |  | ||||||
|     } |     } | ||||||
|         }); |  | ||||||
|  |     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); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -29,7 +29,7 @@ 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); | ||||||
| @@ -42,7 +42,7 @@ public class IdentifierProvider { | |||||||
|             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); | ||||||
|   | |||||||
| @@ -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); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -13,26 +13,28 @@ public class AutocompleteIdentifierConverter implements IdentifierProvider.Conve | |||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public InteractionIdentifier convert(ChatInputAutoCompleteEvent context) { |     public InteractionIdentifier convert(ChatInputAutoCompleteEvent context) { | ||||||
|  |         InteractionIdentifier last = InteractionIdentifier.builder() | ||||||
|  |             .type(InteractionType.AUTOCOMPLETE) | ||||||
|  |             .name(context.getCommandName()) | ||||||
|  |             .build(); | ||||||
|  |  | ||||||
|         List<ApplicationCommandInteractionOption> options = Discord4JWrapper.SUB_FILTER.apply(context.getOptions()); |         List<ApplicationCommandInteractionOption> options = Discord4JWrapper.SUB_FILTER.apply(context.getOptions()); | ||||||
|         String commandName = context.getCommandName(); |  | ||||||
|  |  | ||||||
|         if (!options.isEmpty()) { |         if (!options.isEmpty()) { | ||||||
|             List<ApplicationCommandInteractionOption> sub_options = Discord4JWrapper.SUB_FILTER.apply(options.getFirst().getOptions()); |             last = InteractionIdentifier.builder() | ||||||
|             if (!sub_options.isEmpty()) |                 .type(InteractionType.AUTOCOMPLETE) | ||||||
|                 return InteractionIdentifier.createHierarchy( |                 .name(options.getFirst().getName()) | ||||||
|                     InteractionType.AUTOCOMPLETE,  |                 .parent(last) | ||||||
|                     commandName,  |                 .build(); | ||||||
|                     options.getFirst().getName(), |  | ||||||
|                     sub_options.getFirst().getName()); |             List<ApplicationCommandInteractionOption> subOptions = Discord4JWrapper.SUB_FILTER.apply(options.getFirst().getOptions()); | ||||||
|             else |             if (!subOptions.isEmpty()) | ||||||
|                 return InteractionIdentifier.createHierarchy( |                 last = InteractionIdentifier.builder() | ||||||
|                     InteractionType.AUTOCOMPLETE,  |                     .type(InteractionType.AUTOCOMPLETE) | ||||||
|                     commandName,  |                     .name(subOptions.getFirst().getName()) | ||||||
|                     options.getFirst().getName()); |                     .parent(last) | ||||||
|         }else |                     .build(); | ||||||
|             return InteractionIdentifier.createHierarchy( |         } | ||||||
|                 InteractionType.AUTOCOMPLETE,  |         return last; | ||||||
|                 commandName);  |  | ||||||
|     } |     } | ||||||
|      |      | ||||||
| } | } | ||||||
|   | |||||||
| @@ -13,27 +13,28 @@ public class SlashCommandIdentifierConverter implements IdentifierProvider.Conve | |||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public InteractionIdentifier convert(ChatInputInteractionEvent context) { |     public InteractionIdentifier convert(ChatInputInteractionEvent context) { | ||||||
|  |         InteractionIdentifier last = InteractionIdentifier.builder() | ||||||
|  |             .type(InteractionType.COMMAND) | ||||||
|  |             .name(context.getCommandName()) | ||||||
|  |             .build(); | ||||||
|  |  | ||||||
|         List<ApplicationCommandInteractionOption> options = Discord4JWrapper.SUB_FILTER.apply(context.getOptions()); |         List<ApplicationCommandInteractionOption> options = Discord4JWrapper.SUB_FILTER.apply(context.getOptions()); | ||||||
|         String commandName = context.getCommandName(); |  | ||||||
|  |  | ||||||
|         if (!options.isEmpty()) { |         if (!options.isEmpty()) { | ||||||
|             List<ApplicationCommandInteractionOption> sub_options = Discord4JWrapper.SUB_FILTER.apply(options.getFirst().getOptions()); |             last = InteractionIdentifier.builder() | ||||||
|             if (!sub_options.isEmpty()) |                 .type(InteractionType.COMMAND) | ||||||
|                 return InteractionIdentifier.createHierarchy( |                 .name(options.getFirst().getName()) | ||||||
|                     InteractionType.COMMAND,  |                 .parent(last) | ||||||
|                     commandName,  |                 .build(); | ||||||
|                     options.getFirst().getName(), |  | ||||||
|                     sub_options.getFirst().getName()); |  | ||||||
|             else |  | ||||||
|                 return InteractionIdentifier.createHierarchy( |  | ||||||
|                     InteractionType.COMMAND,  |  | ||||||
|                     commandName,  |  | ||||||
|                     options.getFirst().getName()); |  | ||||||
|         }else |  | ||||||
|             return InteractionIdentifier.createHierarchy( |  | ||||||
|                 InteractionType.COMMAND,  |  | ||||||
|                 commandName);  |  | ||||||
|  |  | ||||||
|  |             List<ApplicationCommandInteractionOption> subOptions = Discord4JWrapper.SUB_FILTER.apply(options.getFirst().getOptions()); | ||||||
|  |             if (!subOptions.isEmpty()) | ||||||
|  |                 last = InteractionIdentifier.builder() | ||||||
|  |                     .type(InteractionType.COMMAND) | ||||||
|  |                     .name(subOptions.getFirst().getName()) | ||||||
|  |                     .parent(last) | ||||||
|  |                     .build(); | ||||||
|  |         } | ||||||
|  |         return last; | ||||||
|     } |     } | ||||||
|      |      | ||||||
| } | } | ||||||
|   | |||||||
| @@ -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(); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -5,18 +5,36 @@ import static org.junit.jupiter.api.Assertions.assertEquals; | |||||||
| import java.util.Collections; | import java.util.Collections; | ||||||
|  |  | ||||||
| import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent; | import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent; | ||||||
|  | import discord4j.core.event.domain.interaction.ChatInputInteractionEvent; | ||||||
|  | import 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( | ||||||
|     @AutoComplete |         name = "test", | ||||||
|     public void autocomplete(ChatInputAutoCompleteEvent context, String value) { |         options = @SlashCommandOption( | ||||||
|  |                 name = "foo", | ||||||
|  |                 type = SlashCommandOptionType.STRING, | ||||||
|  |                 autocompletes = @AutoComplete("testAuto") | ||||||
|  |             ) | ||||||
|  |         ) | ||||||
|  |     @AutoComplete("testAuto") | ||||||
|  |     public void exec(ChatInputInteractionEvent context) { | ||||||
|  |  | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     @AutoComplete("testAuto") | ||||||
|  |     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); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -13,25 +13,28 @@ public class AutocompleteIdentifierConverter implements IdentifierProvider.Conve | |||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public InteractionIdentifier convert(AutocompleteInteraction context) { |     public InteractionIdentifier convert(AutocompleteInteraction context) { | ||||||
|  |         InteractionIdentifier last = InteractionIdentifier.builder() | ||||||
|  |             .type(InteractionType.AUTOCOMPLETE) | ||||||
|  |             .name(context.getCommandName()) | ||||||
|  |             .build(); | ||||||
|  |  | ||||||
|         List<SlashCommandInteractionOption> options = context.getOptions(); |         List<SlashCommandInteractionOption> options = context.getOptions(); | ||||||
|         String commandName = context.getCommandName(); |  | ||||||
|         if (!options.isEmpty()) { |         if (!options.isEmpty()) { | ||||||
|             List<SlashCommandInteractionOption> sub_options = context.getOptions().getFirst().getOptions(); |             last = InteractionIdentifier.builder() | ||||||
|             if (!sub_options.isEmpty()) |                 .type(InteractionType.AUTOCOMPLETE) | ||||||
|                 return InteractionIdentifier.createHierarchy( |                 .name(options.getFirst().getName()) | ||||||
|                     InteractionType.AUTOCOMPLETE,  |                 .parent(last) | ||||||
|                     commandName,  |                 .build(); | ||||||
|                     options.getFirst().getName(), |  | ||||||
|                     sub_options.getFirst().getName()); |             List<SlashCommandInteractionOption> subOptions = context.getOptions().getFirst().getOptions(); | ||||||
|             else |             if (!subOptions.isEmpty()) | ||||||
|                 return InteractionIdentifier.createHierarchy( |                 last = InteractionIdentifier.builder() | ||||||
|                     InteractionType.AUTOCOMPLETE,  |                     .type(InteractionType.AUTOCOMPLETE) | ||||||
|                     commandName,  |                     .name(subOptions.getFirst().getName()) | ||||||
|                     options.getFirst().getName()); |                     .parent(last) | ||||||
|         }else |                     .build(); | ||||||
|             return InteractionIdentifier.createHierarchy( |         } | ||||||
|                 InteractionType.AUTOCOMPLETE,  |         return last; | ||||||
|                 commandName);  |  | ||||||
|     } |     } | ||||||
|      |      | ||||||
| } | } | ||||||
|   | |||||||
| @@ -13,26 +13,28 @@ public class SlashCommandIdentifierConverter implements IdentifierProvider.Conve | |||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public InteractionIdentifier convert(SlashCommandInteraction context) { |     public InteractionIdentifier convert(SlashCommandInteraction context) { | ||||||
|  |         InteractionIdentifier last = InteractionIdentifier.builder() | ||||||
|  |             .type(InteractionType.COMMAND) | ||||||
|  |             .name(context.getCommandName()) | ||||||
|  |             .build(); | ||||||
|  |  | ||||||
|         List<SlashCommandInteractionOption> options = context.getOptions(); |         List<SlashCommandInteractionOption> options = context.getOptions(); | ||||||
|         String commandName = context.getCommandName(); |  | ||||||
|         if (!options.isEmpty()) { |         if (!options.isEmpty()) { | ||||||
|             List<SlashCommandInteractionOption> sub_options = context.getOptions().getFirst().getOptions(); |             last = InteractionIdentifier.builder() | ||||||
|             if (!sub_options.isEmpty()) |                 .type(InteractionType.COMMAND) | ||||||
|                 return InteractionIdentifier.createHierarchy( |                 .name(options.getFirst().getName()) | ||||||
|                     InteractionType.COMMAND,  |                 .parent(last) | ||||||
|                     commandName,  |                 .build(); | ||||||
|                     options.getFirst().getName(), |  | ||||||
|                     sub_options.getFirst().getName()); |  | ||||||
|             else |  | ||||||
|                 return InteractionIdentifier.createHierarchy( |  | ||||||
|                     InteractionType.COMMAND,  |  | ||||||
|                     commandName,  |  | ||||||
|                     options.getFirst().getName()); |  | ||||||
|         }else |  | ||||||
|             return InteractionIdentifier.createHierarchy( |  | ||||||
|                 InteractionType.COMMAND,  |  | ||||||
|                 commandName);  |  | ||||||
|                  |                  | ||||||
|  |             List<SlashCommandInteractionOption> subOptions = context.getOptions().getFirst().getOptions(); | ||||||
|  |             if (!subOptions.isEmpty()) | ||||||
|  |                 last = InteractionIdentifier.builder() | ||||||
|  |                     .type(InteractionType.COMMAND) | ||||||
|  |                     .name(subOptions.getFirst().getName()) | ||||||
|  |                     .parent(last) | ||||||
|  |                     .build(); | ||||||
|  |         } | ||||||
|  |         return last; | ||||||
|     } |     } | ||||||
|      |      | ||||||
| } | } | ||||||
|   | |||||||
| @@ -4,16 +4,32 @@ import static org.junit.jupiter.api.Assertions.assertEquals; | |||||||
|  |  | ||||||
| import java.util.Collections; | import java.util.Collections; | ||||||
|  |  | ||||||
|  | import org.javacord.api.event.interaction.SlashCommandCreateEvent; | ||||||
| import org.javacord.api.interaction.AutocompleteInteraction; | import org.javacord.api.interaction.AutocompleteInteraction; | ||||||
|  | import 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( | ||||||
|     @AutoComplete |         name = "test", | ||||||
|  |         options = @SlashCommandOption( | ||||||
|  |                 name = "foo", | ||||||
|  |                 type = SlashCommandOptionType.STRING, | ||||||
|  |                 autocompletes = @AutoComplete("testAuto") | ||||||
|  |             ) | ||||||
|  |         ) | ||||||
|  |     @AutoComplete("testAuto") | ||||||
|  |     public void exec(SlashCommandInteraction context) { | ||||||
|  |  | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @AutoComplete("testAuto") | ||||||
|     public void autocomplete(AutocompleteInteraction context, String value) { |     public void autocomplete(AutocompleteInteraction context, String value) { | ||||||
|         System.out.println("Success!"); |         System.out.println("Success!"); | ||||||
|         assertEquals(value, "test"); |         assertEquals(value, "test"); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user