Compare commits
	
		
			3 Commits
		
	
	
		
			83b446e6fb
			...
			feat/messa
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 272225ac07 | |||
| 42a675dc96 | |||
| eea1597b15 | 
| @@ -8,7 +8,7 @@ javacord = "3.8.0" | |||||||
| discord4j = "3.2.7" | discord4j = "3.2.7" | ||||||
| geantyref = "2.0.0" | geantyref = "2.0.0" | ||||||
| mockito = "5.15.2" | mockito = "5.15.2" | ||||||
| cutin = "0.1.1-cad019e" | cutin = "0.2.0" | ||||||
|  |  | ||||||
| [libraries] | [libraries] | ||||||
| junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" } | junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" } | ||||||
| @@ -18,4 +18,4 @@ javacord = { module = "org.javacord:javacord", version.ref = "javacord"} | |||||||
| discord4j = { module = "com.discord4j:discord4j-core", version.ref = "discord4j"} | discord4j = { module = "com.discord4j:discord4j-core", version.ref = "discord4j"} | ||||||
| geantyref = { module = "io.leangen.geantyref:geantyref", version.ref = "geantyref"} | geantyref = { module = "io.leangen.geantyref:geantyref", version.ref = "geantyref"} | ||||||
| mockito = {module = "org.mockito:mockito-core", version.ref = "mockito"} | mockito = {module = "org.mockito:mockito-core", version.ref = "mockito"} | ||||||
| cutin = {module = "net.tomatentum.cutin:lib-dev", version.ref = "cutin"} | cutin = {module = "net.tomatentum.cutin:lib", version.ref = "cutin"} | ||||||
|   | |||||||
| @@ -1,12 +1,12 @@ | |||||||
| package net.tomatentum.marinara.checks; | package net.tomatentum.marinara.checks; | ||||||
|  |  | ||||||
| import java.util.List; |  | ||||||
| import java.util.Optional; | import java.util.Optional; | ||||||
|  | import java.util.Set; | ||||||
|  |  | ||||||
| import net.tomatentum.cutin.MethodParser; | import net.tomatentum.cutin.MethodParser; | ||||||
|  | import net.tomatentum.cutin.ReflectedMethodFactory.ParserResults; | ||||||
| import net.tomatentum.cutin.container.MethodContainer; | import net.tomatentum.cutin.container.MethodContainer; | ||||||
| import net.tomatentum.cutin.method.BestCandidateMethod; | import net.tomatentum.cutin.method.BestCandidateMethod; | ||||||
| import net.tomatentum.cutin.method.ReflectedMethod; |  | ||||||
| import net.tomatentum.marinara.checks.CheckMethodIdentifier.CheckMethodType; | import net.tomatentum.marinara.checks.CheckMethodIdentifier.CheckMethodType; | ||||||
| import net.tomatentum.marinara.parser.InteractionCheckClassParser; | import net.tomatentum.marinara.parser.InteractionCheckClassParser; | ||||||
|  |  | ||||||
| @@ -14,8 +14,13 @@ public class InteractionCheckMethod extends BestCandidateMethod<CheckMethodIdent | |||||||
|  |  | ||||||
|     private CheckMethodIdentifier identifier; |     private CheckMethodIdentifier identifier; | ||||||
|  |  | ||||||
|     public InteractionCheckMethod(String methodName, Object containingObject) { |     public InteractionCheckMethod( | ||||||
|  |             String methodName,  | ||||||
|  |             Object containingObject, | ||||||
|  |             CheckMethodIdentifier identifier | ||||||
|  |         ) { | ||||||
|         super(methodName, containingObject); |         super(methodName, containingObject); | ||||||
|  |         this.identifier = identifier; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
| @@ -44,21 +49,24 @@ public class InteractionCheckMethod extends BestCandidateMethod<CheckMethodIdent | |||||||
|             this.type = type; |             this.type = type; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         @SuppressWarnings("unchecked") |  | ||||||
|         @Override |         @Override | ||||||
|         public void addParser(ReflectedMethod<CheckMethodIdentifier, CheckExecutionContext> method, List<MethodParser> parsers) { |         public void addParser(Set<MethodParser> parsers) { | ||||||
|             parsers.add( |             parsers.add( | ||||||
|                 new InteractionCheckClassParser((Class<InteractionCheck<?>>) method.containingObject().getClass(), |                 new InteractionCheckClassParser() | ||||||
|                     a -> ((InteractionCheckMethod) method).identifier = new CheckMethodIdentifier(a, type)) |  | ||||||
|             ); |             ); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         @Override |         @Override | ||||||
|         protected Optional<BestCandidateMethod<CheckMethodIdentifier, CheckExecutionContext>> bcProduce(String methodName, |         protected Optional<BestCandidateMethod<CheckMethodIdentifier, CheckExecutionContext>> bcProduce( | ||||||
|                 Object containingObject) { |                     String methodName, | ||||||
|             if (!(containingObject instanceof InteractionCheck)) |                     Object containingObject, | ||||||
|                 return Optional.empty(); |                     ParserResults parserResults | ||||||
|             return Optional.of(new InteractionCheckMethod(methodName, containingObject)); |                 ) { | ||||||
|  |  | ||||||
|  |             CheckMethodIdentifier identifier = new CheckMethodIdentifier(parserResults.get(InteractionCheckClassParser.class), type); | ||||||
|  |             if (identifier.annotationType() == null) | ||||||
|  |                 return null; | ||||||
|  |             return Optional.of(new InteractionCheckMethod(methodName, containingObject, identifier)); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -5,8 +5,15 @@ 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.structure.data.ButtonStructureData.ButtonStyle; | ||||||
|  |  | ||||||
| @Target({ElementType.METHOD}) | @Target({ElementType.METHOD}) | ||||||
| @Retention(RetentionPolicy.RUNTIME) | @Retention(RetentionPolicy.RUNTIME) | ||||||
| public @interface Button { | public @interface Button { | ||||||
|     public String value(); //aka customId |     public String value(); //aka customId | ||||||
|  |     public String label() default "default_button"; | ||||||
|  |     public ButtonStyle style() default ButtonStyle.PRIMARY; | ||||||
|  |     public String url() default ""; | ||||||
|  |     public boolean disabled() default false; | ||||||
|  |     public String emoji() default ""; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -3,18 +3,21 @@ package net.tomatentum.marinara.interaction.components.methods; | |||||||
| import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.Optional; | import java.util.Optional; | ||||||
|  | import java.util.Set; | ||||||
|  |  | ||||||
| import net.tomatentum.cutin.MethodParser; | import net.tomatentum.cutin.MethodParser; | ||||||
|  | import net.tomatentum.cutin.ReflectedMethodFactory.ParserResults; | ||||||
| import net.tomatentum.cutin.container.MethodContainer; | import net.tomatentum.cutin.container.MethodContainer; | ||||||
| import net.tomatentum.cutin.method.ReflectedMethod; | import net.tomatentum.cutin.method.ReflectedMethod; | ||||||
|  | import net.tomatentum.marinara.checks.AppliedCheck; | ||||||
| import net.tomatentum.marinara.checks.CheckExecutionContext; | import net.tomatentum.marinara.checks.CheckExecutionContext; | ||||||
| import net.tomatentum.marinara.checks.CheckMethodIdentifier; | import net.tomatentum.marinara.checks.CheckMethodIdentifier; | ||||||
| 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.annotation.Button; |  | ||||||
| import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | ||||||
| import net.tomatentum.marinara.interaction.methods.InteractionMethod; | import net.tomatentum.marinara.interaction.methods.InteractionMethod; | ||||||
| import net.tomatentum.marinara.parser.ButtonParser; | import net.tomatentum.marinara.parser.ButtonParser; | ||||||
|  | import net.tomatentum.marinara.parser.InteractionCheckParser; | ||||||
| import net.tomatentum.marinara.wrapper.ContextObjectProvider; | import net.tomatentum.marinara.wrapper.ContextObjectProvider; | ||||||
|  |  | ||||||
| public class ButtonInteractionMethod extends InteractionMethod { | public class ButtonInteractionMethod extends InteractionMethod { | ||||||
| @@ -22,8 +25,15 @@ public class ButtonInteractionMethod extends InteractionMethod { | |||||||
|     private String customId; |     private String customId; | ||||||
|     private ContextObjectProvider cop; |     private ContextObjectProvider cop; | ||||||
|  |  | ||||||
|     private ButtonInteractionMethod(Method method, InteractionHandler handler, ContextObjectProvider cop) { |     private ButtonInteractionMethod( | ||||||
|         super(method, handler); |             Method method,  | ||||||
|  |             InteractionHandler handler, | ||||||
|  |             List<AppliedCheck> appliedChecks, | ||||||
|  |             String customId,  | ||||||
|  |             ContextObjectProvider cop | ||||||
|  |         ) { | ||||||
|  |         super(method, handler, appliedChecks); | ||||||
|  |         this.customId = customId; | ||||||
|         this.cop = cop; |         this.cop = cop; | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -56,22 +66,25 @@ public class ButtonInteractionMethod extends InteractionMethod { | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         @Override |         @Override | ||||||
|         public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject) { |         public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject, ParserResults parserResults) { | ||||||
|             ButtonInteractionMethod rMethod = null; |             if (!(containingObject instanceof InteractionHandler)) return Optional.empty(); | ||||||
|             if (method.isAnnotationPresent(Button.class) && |             String customId = parserResults.get(ButtonParser.class); | ||||||
|                 (containingObject instanceof InteractionHandler iHandler) |             if (customId == null) return Optional.empty(); | ||||||
|                 ) |             return Optional.of(new ButtonInteractionMethod( | ||||||
|                 rMethod = new ButtonInteractionMethod(method, iHandler, this.cop); |                 method,  | ||||||
|  |                 (InteractionHandler) containingObject, | ||||||
|             return Optional.ofNullable(rMethod); |                 parserResults.get(InteractionCheckParser.class), | ||||||
|  |                 customId, | ||||||
|  |                 this.cop | ||||||
|  |             )); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         @Override |         @Override | ||||||
|         public void addParser(ReflectedMethod<InteractionIdentifier, Object> method, List<MethodParser> parser) { |         public void addParser(Set<MethodParser> parser) { | ||||||
|             super.addParser(method, parser); |             super.addParser(parser); | ||||||
|  |  | ||||||
|             parser.add( |             parser.add( | ||||||
|                 new ButtonParser(method.method(), x -> ((ButtonInteractionMethod) method).customId = x) |                 new ButtonParser() | ||||||
|             ); |             ); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -3,19 +3,20 @@ package net.tomatentum.marinara.interaction.methods; | |||||||
| import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.Optional; | import java.util.Optional; | ||||||
|  | import java.util.Set; | ||||||
|  |  | ||||||
| import net.tomatentum.cutin.MethodParser; | import net.tomatentum.cutin.MethodParser; | ||||||
|  | import net.tomatentum.cutin.ReflectedMethodFactory.ParserResults; | ||||||
| import net.tomatentum.cutin.container.MethodContainer; | import net.tomatentum.cutin.container.MethodContainer; | ||||||
| import net.tomatentum.cutin.method.ReflectedMethod; | import net.tomatentum.cutin.method.ReflectedMethod; | ||||||
|  | import net.tomatentum.marinara.checks.AppliedCheck; | ||||||
| import net.tomatentum.marinara.checks.CheckExecutionContext; | import net.tomatentum.marinara.checks.CheckExecutionContext; | ||||||
| import net.tomatentum.marinara.checks.CheckMethodIdentifier; | import net.tomatentum.marinara.checks.CheckMethodIdentifier; | ||||||
| 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.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.AutocompleteParser; | import net.tomatentum.marinara.parser.AutocompleteParser; | ||||||
|  | import net.tomatentum.marinara.parser.InteractionCheckParser; | ||||||
| import net.tomatentum.marinara.wrapper.ContextObjectProvider; | import net.tomatentum.marinara.wrapper.ContextObjectProvider; | ||||||
|  |  | ||||||
| public class AutoCompleteInteractionMethod extends InteractionMethod { | public class AutoCompleteInteractionMethod extends InteractionMethod { | ||||||
| @@ -23,11 +24,15 @@ public class AutoCompleteInteractionMethod extends InteractionMethod { | |||||||
|     private String autocompleteRef; |     private String autocompleteRef; | ||||||
|     private ContextObjectProvider cop; |     private ContextObjectProvider cop; | ||||||
|  |  | ||||||
|     private AutoCompleteInteractionMethod(Method method,  |     private AutoCompleteInteractionMethod( | ||||||
|  |             Method method,  | ||||||
|             InteractionHandler handler, |             InteractionHandler handler, | ||||||
|  |             List<AppliedCheck> appliedChecks, | ||||||
|  |             String autocompleteRef, | ||||||
|             ContextObjectProvider cop |             ContextObjectProvider cop | ||||||
|         ) { |         ) { | ||||||
|         super(method, handler); |         super(method, handler, appliedChecks); | ||||||
|  |         this.autocompleteRef = autocompleteRef; | ||||||
|         this.cop = cop; |         this.cop = cop; | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -64,23 +69,27 @@ public class AutoCompleteInteractionMethod extends InteractionMethod { | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         @Override |         @Override | ||||||
|         public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject) { |         public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject, ParserResults parserResults) { | ||||||
|             AutoCompleteInteractionMethod rMethod = null; |             if (!(containingObject instanceof InteractionHandler)) return Optional.empty(); | ||||||
|             if ((containingObject instanceof InteractionHandler iHandler) && |             String[] autocompletes = parserResults.get(AutocompleteParser.class); | ||||||
|                 method.isAnnotationPresent(AutoComplete.class) && |             if (autocompletes.length <= 0) return Optional.empty(); | ||||||
|                 !(method.isAnnotationPresent(SlashCommand.class) || |              | ||||||
|                 method.isAnnotationPresent(SubCommand.class))) |             return Optional.of(new AutoCompleteInteractionMethod( | ||||||
|                 rMethod = new AutoCompleteInteractionMethod(method, iHandler, cop); |                 method,  | ||||||
|  |                 (InteractionHandler) containingObject, | ||||||
|  |                 parserResults.get(InteractionCheckParser.class), | ||||||
|  |                 autocompletes[0], | ||||||
|  |                 cop | ||||||
|  |             )); | ||||||
|  |  | ||||||
|             return Optional.ofNullable(rMethod); |  | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         @Override |         @Override | ||||||
|         public void addParser(ReflectedMethod<InteractionIdentifier, Object> method, List<MethodParser> parser) { |         public void addParser(Set<MethodParser> parser) { | ||||||
|             super.addParser(method, parser); |             super.addParser(parser); | ||||||
|  |  | ||||||
|             parser.add( |             parser.add( | ||||||
|                 new AutocompleteParser(method.method(), x -> ((AutoCompleteInteractionMethod) method).autocompleteRef = x[0]) |                 new AutocompleteParser() | ||||||
|             ); |             ); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,8 +1,8 @@ | |||||||
| 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.ArrayList; |  | ||||||
| import java.util.List; | import java.util.List; | ||||||
|  | import java.util.Set; | ||||||
|  |  | ||||||
| import net.tomatentum.cutin.MethodParser; | import net.tomatentum.cutin.MethodParser; | ||||||
| import net.tomatentum.cutin.ReflectedMethodFactory; | import net.tomatentum.cutin.ReflectedMethodFactory; | ||||||
| @@ -21,10 +21,11 @@ public abstract class InteractionMethod extends ReflectedMethod<InteractionIdent | |||||||
|  |  | ||||||
|     protected InteractionMethod( |     protected InteractionMethod( | ||||||
|         Method method,  |         Method method,  | ||||||
|         InteractionHandler handler |         InteractionHandler handler, | ||||||
|  |         List<AppliedCheck> appliedChecks | ||||||
|         ) { |         ) { | ||||||
|         super(method, handler); |         super(method, handler); | ||||||
|         this.appliedChecks = new ArrayList<>(); |         this.appliedChecks = appliedChecks; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
| @@ -60,10 +61,9 @@ public abstract class InteractionMethod extends ReflectedMethod<InteractionIdent | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         @Override |         @Override | ||||||
|         public void addParser(ReflectedMethod<InteractionIdentifier, Object> method, List<MethodParser> parser) { |         public void addParser(Set<MethodParser> parser) { | ||||||
|             InteractionMethod imethod = (InteractionMethod) method; |  | ||||||
|             parser.add( |             parser.add( | ||||||
|                 new InteractionCheckParser(method.method(), imethod.appliedChecks::add, this.checkContainer) |                 new InteractionCheckParser(this.checkContainer) | ||||||
|             ); |             ); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -3,27 +3,36 @@ package net.tomatentum.marinara.interaction.methods; | |||||||
| import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.Optional; | import java.util.Optional; | ||||||
|  | import java.util.Set; | ||||||
|  |  | ||||||
| import net.tomatentum.cutin.MethodParser; | import net.tomatentum.cutin.MethodParser; | ||||||
|  | import net.tomatentum.cutin.ReflectedMethodFactory.ParserResults; | ||||||
| import net.tomatentum.cutin.container.MethodContainer; | import net.tomatentum.cutin.container.MethodContainer; | ||||||
| import net.tomatentum.cutin.method.ReflectedMethod; | import net.tomatentum.cutin.method.ReflectedMethod; | ||||||
|  | import net.tomatentum.marinara.checks.AppliedCheck; | ||||||
| import net.tomatentum.marinara.checks.CheckExecutionContext; | import net.tomatentum.marinara.checks.CheckExecutionContext; | ||||||
| import net.tomatentum.marinara.checks.CheckMethodIdentifier; | import net.tomatentum.marinara.checks.CheckMethodIdentifier; | ||||||
| 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.InteractionCheckParser; | ||||||
| import net.tomatentum.marinara.parser.SlashCommandParser; | import net.tomatentum.marinara.parser.SlashCommandParser; | ||||||
| import net.tomatentum.marinara.wrapper.ContextObjectProvider; | import net.tomatentum.marinara.wrapper.ContextObjectProvider; | ||||||
|  |  | ||||||
| public class SlashCommandInteractionMethod extends InteractionMethod { | public class SlashCommandInteractionMethod extends InteractionMethod { | ||||||
|  |  | ||||||
|     private SlashCommandIdentifier interactionIdentifier; |     private SlashCommandIdentifier identifier; | ||||||
|     private ContextObjectProvider cop; |     private ContextObjectProvider cop; | ||||||
|  |  | ||||||
|     private SlashCommandInteractionMethod(Method method, InteractionHandler handler, ContextObjectProvider cop) { |     private SlashCommandInteractionMethod( | ||||||
|         super(method, handler); |             Method method,  | ||||||
|  |             InteractionHandler handler, | ||||||
|  |             List<AppliedCheck> appliedChecks, | ||||||
|  |             SlashCommandIdentifier identifier, | ||||||
|  |             ContextObjectProvider cop | ||||||
|  |         ) { | ||||||
|  |         super(method, handler, appliedChecks); | ||||||
|  |         this.identifier = identifier; | ||||||
|         this.cop = cop; |         this.cop = cop; | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -31,14 +40,14 @@ public class SlashCommandInteractionMethod extends InteractionMethod { | |||||||
|     public Object getParameter(Object context, int index) { |     public Object getParameter(Object context, int index) { | ||||||
|         Object superResult = super.getParameter(context, index); |         Object superResult = super.getParameter(context, index); | ||||||
|         if (superResult == null) |         if (superResult == null) | ||||||
|             return this.cop.convertCommandOption(context, interactionIdentifier.options()[index-1].name()); |             return this.cop.convertCommandOption(context, identifier.options()[index-1].name()); | ||||||
|         else |         else | ||||||
|             return superResult; |             return superResult; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public InteractionIdentifier identifier() { |     public InteractionIdentifier identifier() { | ||||||
|         return interactionIdentifier; |         return identifier; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public static class Factory extends InteractionMethod.Factory { |     public static class Factory extends InteractionMethod.Factory { | ||||||
| @@ -51,21 +60,25 @@ public class SlashCommandInteractionMethod extends InteractionMethod { | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         @Override |         @Override | ||||||
|         public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject) { |         public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject, ParserResults parserResults) { | ||||||
|             SlashCommandInteractionMethod rMethod = null; |             if (!(containingObject instanceof InteractionHandler)) return Optional.empty(); | ||||||
|             if ((containingObject instanceof InteractionHandler iHandler) &&  |             SlashCommandIdentifier ident = parserResults.get(SlashCommandParser.class); | ||||||
|                 (method.isAnnotationPresent(SlashCommand.class) || |             if (ident == null) return Optional.empty(); | ||||||
|                 method.isAnnotationPresent(SubCommand.class))) |             return Optional.of(new SlashCommandInteractionMethod( | ||||||
|                 rMethod = new SlashCommandInteractionMethod(method, iHandler, cop); |                 method,  | ||||||
|             return Optional.ofNullable(rMethod); |                 (InteractionHandler) containingObject, | ||||||
|  |                 parserResults.get(InteractionCheckParser.class), | ||||||
|  |                 ident, | ||||||
|  |                 cop | ||||||
|  |             )); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         @Override |         @Override | ||||||
|         public void addParser(ReflectedMethod<InteractionIdentifier, Object> method, List<MethodParser> parser) { |         public void addParser(Set<MethodParser> parser) { | ||||||
|             super.addParser(method, parser); |             super.addParser(parser); | ||||||
|  |  | ||||||
|             parser.add( |             parser.add( | ||||||
|                 new SlashCommandParser(method.method(), x -> ((SlashCommandInteractionMethod) method).interactionIdentifier = x) |                 new SlashCommandParser() | ||||||
|             ); |             ); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -2,7 +2,6 @@ package net.tomatentum.marinara.parser; | |||||||
|  |  | ||||||
| import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||||
| import java.util.Arrays; | import java.util.Arrays; | ||||||
| import java.util.function.Consumer; |  | ||||||
|  |  | ||||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
| @@ -15,21 +14,14 @@ public class AutocompleteParser implements MethodParser { | |||||||
|  |  | ||||||
|     private Logger logger = LoggerFactory.getLogger(getClass()); |     private Logger logger = LoggerFactory.getLogger(getClass()); | ||||||
|  |  | ||||||
|     private Method method; |  | ||||||
|     private Consumer<String[]> consumer; |  | ||||||
|  |  | ||||||
|     public AutocompleteParser(Method method, Consumer<String[]> consumer) { |  | ||||||
|         this.method = method; |  | ||||||
|         this.consumer = consumer; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void parse() { |     public Object parse(Method method, Object containingObject) { | ||||||
|         String[] autocompletes = Arrays.stream(this.method.getAnnotationsByType(AutoComplete.class)) |         String[] autoCompletes = Arrays.stream(method.getAnnotationsByType(AutoComplete.class)) | ||||||
|             .map(AutoComplete::value) |             .map(AutoComplete::value) | ||||||
|             .toArray(String[]::new); |             .toArray(String[]::new); | ||||||
|         logger.trace("Parsed AutoComplete annotation {} for method {}", autocompletes, ReflectionUtil.getFullMethodName(method)); |         if (autoCompletes.length > 0) | ||||||
|         this.consumer.accept(autocompletes); |             logger.trace("Parsed AutoComplete annotations {} for method {}", autoCompletes, ReflectionUtil.getFullMethodName(method)); | ||||||
|  |         return autoCompletes; | ||||||
|     } |     } | ||||||
|      |      | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,7 +1,6 @@ | |||||||
| package net.tomatentum.marinara.parser; | package net.tomatentum.marinara.parser; | ||||||
|  |  | ||||||
| import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||||
| import java.util.function.Consumer; |  | ||||||
|  |  | ||||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
| @@ -12,21 +11,17 @@ import net.tomatentum.marinara.interaction.annotation.Button; | |||||||
|  |  | ||||||
| public class ButtonParser implements MethodParser { | public class ButtonParser implements MethodParser { | ||||||
|      |      | ||||||
|     private Method method; |  | ||||||
|     private Consumer<String> consumer; |  | ||||||
|  |  | ||||||
|     private Logger logger = LoggerFactory.getLogger(getClass()); |     private Logger logger = LoggerFactory.getLogger(getClass()); | ||||||
|  |  | ||||||
|     public ButtonParser(Method method, Consumer<String> consumer) { |     public ButtonParser() { | ||||||
|         this.method = method; |  | ||||||
|         this.consumer = consumer; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void parse() { |     public Object parse(Method method, Object containingObject) { | ||||||
|         Button button = this.method.getAnnotation(Button.class); |         Button button = method.getAnnotation(Button.class); | ||||||
|  |         if (button == null) return null; | ||||||
|         logger.trace("Parsed Button annotation {} for method {}", button, ReflectionUtil.getFullMethodName(method)); |         logger.trace("Parsed Button annotation {} for method {}", button, ReflectionUtil.getFullMethodName(method)); | ||||||
|         this.consumer.accept(button.value()); |         return button.value(); | ||||||
|     } |     } | ||||||
|      |      | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,8 +1,8 @@ | |||||||
| package net.tomatentum.marinara.parser; | package net.tomatentum.marinara.parser; | ||||||
|  |  | ||||||
|  | import java.lang.reflect.Method; | ||||||
| import java.lang.reflect.ParameterizedType; | import java.lang.reflect.ParameterizedType; | ||||||
| import java.lang.reflect.Type; | import java.lang.reflect.Type; | ||||||
| import java.util.function.Consumer; |  | ||||||
|  |  | ||||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
| @@ -15,20 +15,14 @@ public class InteractionCheckClassParser implements MethodParser { | |||||||
|  |  | ||||||
|     private Logger logger = LoggerFactory.getLogger(getClass()); |     private Logger logger = LoggerFactory.getLogger(getClass()); | ||||||
|  |  | ||||||
|     private Class<? extends InteractionCheck<?>> interactionCheckType; |  | ||||||
|     private Consumer<Type> annotationTypeConsumer; |  | ||||||
|  |  | ||||||
|     public InteractionCheckClassParser(Class<? extends InteractionCheck<?>> interactionCheckType, Consumer<Type> annotationTypeConsumer) { |  | ||||||
|         this.interactionCheckType = interactionCheckType; |  | ||||||
|         this.annotationTypeConsumer = annotationTypeConsumer; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void parse() { |     public Object parse(Method method, Object containingObject) { | ||||||
|         ParameterizedType type = (ParameterizedType) GenericTypeReflector.getExactSuperType(interactionCheckType, InteractionCheck.class); |         ParameterizedType type = (ParameterizedType) GenericTypeReflector.getExactSuperType(containingObject.getClass(), InteractionCheck.class); | ||||||
|         Type typeParam = type.getActualTypeArguments()[0]; |         if (type == null) return null; | ||||||
|  |         Type typeParam = type.getActualTypeArguments().length == 1 ? type.getActualTypeArguments()[0] : null; | ||||||
|  |         if (typeParam != null) | ||||||
|             logger.trace("Parsed InteractionCheck Annotation {}", typeParam); |             logger.trace("Parsed InteractionCheck Annotation {}", typeParam); | ||||||
|         this.annotationTypeConsumer.accept(typeParam); |         return typeParam; | ||||||
|     } |     } | ||||||
|      |      | ||||||
| } | } | ||||||
|   | |||||||
| @@ -3,7 +3,7 @@ package net.tomatentum.marinara.parser; | |||||||
| import java.lang.annotation.Annotation; | import java.lang.annotation.Annotation; | ||||||
| import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||||
| import java.util.Arrays; | import java.util.Arrays; | ||||||
| import java.util.function.Consumer; | import java.util.Objects; | ||||||
|  |  | ||||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
| @@ -19,31 +19,31 @@ import net.tomatentum.marinara.checks.CheckMethodIdentifier.CheckMethodType; | |||||||
| public class InteractionCheckParser implements MethodParser { | public class InteractionCheckParser implements MethodParser { | ||||||
|  |  | ||||||
|     private MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer; |     private MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer; | ||||||
|     private Method method; |  | ||||||
|     private Consumer<AppliedCheck> consumer; |  | ||||||
|  |  | ||||||
|     private Logger logger = LoggerFactory.getLogger(getClass()); |     private Logger logger = LoggerFactory.getLogger(getClass()); | ||||||
|  |  | ||||||
|     public InteractionCheckParser(Method method, Consumer<AppliedCheck> consumer, MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer) { |     public InteractionCheckParser(MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer) { | ||||||
|         this.checkContainer = checkContainer; |         this.checkContainer = checkContainer; | ||||||
|         this.method = method; |  | ||||||
|         this.consumer = consumer; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void parse() { |     public Object parse(Method method, Object containingObject) { | ||||||
|         Annotation[] annotations = method.getAnnotations(); |         Annotation[] annotations = method.getAnnotations(); | ||||||
|         Arrays.stream(annotations).forEach(this::convertAnnotation); |         return Arrays.stream(annotations) | ||||||
|  |             .map(a -> convertAnnotation(a, method)) | ||||||
|  |             .filter(Objects::nonNull) | ||||||
|  |             .toList(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private void convertAnnotation(Annotation annotation) { |     private AppliedCheck convertAnnotation(Annotation annotation, Method method) { | ||||||
|             var preExec = this.checkContainer.findFirstFor(new CheckMethodIdentifier(annotation.annotationType(), CheckMethodType.PRE)); |             var preExec = this.checkContainer.findFirstFor(new CheckMethodIdentifier(annotation.annotationType(), CheckMethodType.PRE)); | ||||||
|             var postExec = this.checkContainer.findFirstFor(new CheckMethodIdentifier(annotation.annotationType(), CheckMethodType.POST)); |             var postExec = this.checkContainer.findFirstFor(new CheckMethodIdentifier(annotation.annotationType(), CheckMethodType.POST)); | ||||||
|             if (preExec.isPresent() && postExec.isPresent())  { |             if (preExec.isPresent() && postExec.isPresent())  { | ||||||
|                 AppliedCheck appliedCheck = new AppliedCheck(annotation, preExec.get(), postExec.get()); |                 AppliedCheck appliedCheck = new AppliedCheck(annotation, preExec.get(), postExec.get()); | ||||||
|                 logger.trace("Parsed InteractionCheck {} for annotation {} for method {}", preExec.get().containingObject(), annotation, ReflectionUtil.getFullMethodName(method)); |                 logger.trace("Parsed InteractionCheck {} for annotation {} for method {}", preExec.get().containingObject(), annotation, ReflectionUtil.getFullMethodName(method)); | ||||||
|                 consumer.accept(appliedCheck); |                 return appliedCheck; | ||||||
|             } |             } | ||||||
|  |             return null; | ||||||
|     } |     } | ||||||
|      |      | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,7 +1,6 @@ | |||||||
| package net.tomatentum.marinara.parser; | package net.tomatentum.marinara.parser; | ||||||
|  |  | ||||||
| import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||||
| import java.util.function.Consumer; |  | ||||||
|  |  | ||||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
| @@ -13,22 +12,17 @@ import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; | |||||||
| import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; | import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; | ||||||
| import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup; | import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup; | ||||||
| import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | ||||||
| import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier; |  | ||||||
|  |  | ||||||
| public class SlashCommandParser implements MethodParser { | public class SlashCommandParser implements MethodParser { | ||||||
|  |  | ||||||
|     private Method method; |  | ||||||
|     private Consumer<SlashCommandIdentifier> consumer; |  | ||||||
|  |  | ||||||
|     private Logger logger = LoggerFactory.getLogger(getClass()); |     private Logger logger = LoggerFactory.getLogger(getClass()); | ||||||
|  |  | ||||||
|     public SlashCommandParser(Method method, Consumer<SlashCommandIdentifier> consumer) { |     public SlashCommandParser() { | ||||||
|         this.method = method; |  | ||||||
|         this.consumer = consumer; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void parse() { |     public Object parse(Method method, Object containingObject) { | ||||||
|  |         if (!method.isAnnotationPresent(SlashCommand.class) && !method.isAnnotationPresent(SubCommand.class)) return null; | ||||||
|         this.checkValidCommandMethod(method); |         this.checkValidCommandMethod(method); | ||||||
|  |  | ||||||
|         SlashCommand cmd = ReflectionUtil.getAnnotation(method, SlashCommand.class); |         SlashCommand cmd = ReflectionUtil.getAnnotation(method, SlashCommand.class); | ||||||
| @@ -59,21 +53,21 @@ public class SlashCommandParser implements MethodParser { | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         logger.trace("Parsed using SlashCommandParser for method {} with the result: {}", ReflectionUtil.getFullMethodName(method), lastIdentifier); |         logger.trace("Parsed using SlashCommandParser for method {} with the result: {}", ReflectionUtil.getFullMethodName(method), lastIdentifier); | ||||||
|         consumer.accept((SlashCommandIdentifier) lastIdentifier); |         return lastIdentifier; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private void checkValidCommandMethod(Method method) { |     private void checkValidCommandMethod(Method method) { | ||||||
|         if (method.isAnnotationPresent(SlashCommand.class) &&  |         if (method.isAnnotationPresent(SlashCommand.class) &&  | ||||||
|             method.getDeclaringClass().isAnnotationPresent(SlashCommand.class)) { |             method.getDeclaringClass().isAnnotationPresent(SlashCommand.class)) { | ||||||
|             throw new RuntimeException(method.getName() + ": Can't have ApplicationCommand Annotation on Class and Method"); |             throw new RuntimeException(method.getName() + ": Can't have SlashCommand Annotation on Class and Method"); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         if (!ReflectionUtil.isAnnotationPresent(method, SlashCommand.class)) |         if (!ReflectionUtil.isAnnotationPresent(method, SlashCommand.class)) | ||||||
|             throw new RuntimeException(method.getName() + ": Missing ApplicationCommand Annotation on either Class or Method"); |             throw new RuntimeException(method.getName() + ": Missing SlashCommand Annotation on either Class or Method"); | ||||||
|  |  | ||||||
|         if ((method.isAnnotationPresent(SubCommand.class) &&  |         if ((method.isAnnotationPresent(SubCommand.class) &&  | ||||||
|             !ReflectionUtil.isAnnotationPresent(method, SlashCommand.class))) { |             !ReflectionUtil.isAnnotationPresent(method, SlashCommand.class))) { | ||||||
|             throw new RuntimeException(method.getName() + ": Missing ApplicationCommand Annotation on either Method or Class"); |             throw new RuntimeException(method.getName() + ": Missing SlashCommand Annotation on either Method or Class"); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|      |      | ||||||
|   | |||||||
| @@ -0,0 +1,64 @@ | |||||||
|  | package net.tomatentum.marinara.structure; | ||||||
|  |  | ||||||
|  | import java.lang.reflect.Method; | ||||||
|  | import java.util.Optional; | ||||||
|  | import java.util.Set; | ||||||
|  |  | ||||||
|  | import net.tomatentum.cutin.MethodParser; | ||||||
|  | import net.tomatentum.cutin.ReflectedMethodFactory; | ||||||
|  | import net.tomatentum.cutin.ReflectedMethodFactory.ParserResults; | ||||||
|  | import net.tomatentum.cutin.method.ReflectedMethod; | ||||||
|  | import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | ||||||
|  | import net.tomatentum.marinara.structure.parser.ComponentStructureParser; | ||||||
|  |  | ||||||
|  | public class ComponentStructureMethod extends ReflectedMethod<InteractionIdentifier, Void> { | ||||||
|  |  | ||||||
|  |     private InteractionIdentifier identifier; | ||||||
|  |  | ||||||
|  |     private ComponentStructureMethod( | ||||||
|  |             Method method,  | ||||||
|  |             Object containingObject, | ||||||
|  |             InteractionIdentifier identifier | ||||||
|  |         ) { | ||||||
|  |         super(method, containingObject); | ||||||
|  |         this.identifier = identifier; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public Object getParameter(Void context, int index) { | ||||||
|  |         return null; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public InteractionIdentifier identifier() { | ||||||
|  |         return this.identifier; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public static class Factory implements ReflectedMethodFactory.Factory<InteractionIdentifier, Void> { | ||||||
|  |  | ||||||
|  |         private Class<? extends Object> buttonClass; | ||||||
|  |  | ||||||
|  |         public Factory(Class<? extends Object> buttonClass) { | ||||||
|  |             this.buttonClass = buttonClass; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         @Override | ||||||
|  |         public void addParser(Set<MethodParser> parser) { | ||||||
|  |             parser.add(new ComponentStructureParser(buttonClass)); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         @Override | ||||||
|  |         public Optional<ReflectedMethod<InteractionIdentifier, Void>> produce( | ||||||
|  |                 Method method,  | ||||||
|  |                 Object containingObject, | ||||||
|  |                 ParserResults parserResults) { | ||||||
|  |             InteractionIdentifier identifier = parserResults.get(ComponentStructureParser.class); | ||||||
|  |             if (identifier == null) | ||||||
|  |                 return Optional.empty(); | ||||||
|  |              | ||||||
|  |             return Optional.of(new ComponentStructureMethod(method, containingObject, identifier)); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |     } | ||||||
|  |      | ||||||
|  | } | ||||||
| @@ -0,0 +1,39 @@ | |||||||
|  | package net.tomatentum.marinara.structure; | ||||||
|  |  | ||||||
|  | import java.util.Optional; | ||||||
|  |  | ||||||
|  | import org.slf4j.Logger; | ||||||
|  | import org.slf4j.LoggerFactory; | ||||||
|  |  | ||||||
|  | import net.tomatentum.cutin.ReflectedMethodFactoryImpl; | ||||||
|  | import net.tomatentum.cutin.container.LoneMethodContainer; | ||||||
|  | import net.tomatentum.cutin.method.ReflectedMethod; | ||||||
|  | import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | ||||||
|  |  | ||||||
|  | public class MethodStructureProvider<B extends Object>  | ||||||
|  |     extends LoneMethodContainer<InteractionIdentifier, Void> | ||||||
|  |     implements StructureProvider<B> { | ||||||
|  |  | ||||||
|  |     private Logger logger = LoggerFactory.getLogger(getClass()); | ||||||
|  |  | ||||||
|  |     private Class<? extends B> buttonClass; | ||||||
|  |  | ||||||
|  |     public MethodStructureProvider(Class<? extends B> buttonClass) { | ||||||
|  |         super(new ReflectedMethodFactoryImpl<>()); | ||||||
|  |         super.factory().addFactory(new ComponentStructureMethod.Factory(buttonClass)); | ||||||
|  |         this.buttonClass = buttonClass; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public B button(InteractionIdentifier identifier) { | ||||||
|  |         Optional<ReflectedMethod<InteractionIdentifier, Void>> method = super.findFirstFor(identifier); | ||||||
|  |         if (method.isEmpty()) return null; | ||||||
|  |         try { | ||||||
|  |             return buttonClass.cast(method.get().run(null)); | ||||||
|  |         }catch (ClassCastException ex) { | ||||||
|  |             logger.warn("Structure Method {} return type did not match expected {}", method.get(), buttonClass); | ||||||
|  |             return null; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |      | ||||||
|  | } | ||||||
| @@ -0,0 +1,31 @@ | |||||||
|  | package net.tomatentum.marinara.structure; | ||||||
|  |  | ||||||
|  | import java.util.Map; | ||||||
|  | import java.util.TreeMap; | ||||||
|  |  | ||||||
|  | import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | ||||||
|  |  | ||||||
|  | public class PriorityStructureProvider<B extends Object> implements StructureProvider<B> { | ||||||
|  |  | ||||||
|  |     private Map<Short, StructureProvider<B>> providerMap; | ||||||
|  |  | ||||||
|  |     public PriorityStructureProvider() { | ||||||
|  |         this.providerMap = new TreeMap<>(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public PriorityStructureProvider<B> addProvider(StructureProvider<B> provider, short priority) { | ||||||
|  |         this.providerMap.put(priority, provider); | ||||||
|  |         return this; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public B button(InteractionIdentifier identifier) { | ||||||
|  |         for (short priority : this.providerMap.keySet()) { | ||||||
|  |             B result = this.providerMap.get(priority).button(identifier); | ||||||
|  |             if (result != null) | ||||||
|  |                 return result; | ||||||
|  |         } | ||||||
|  |         return null; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -0,0 +1,31 @@ | |||||||
|  | package net.tomatentum.marinara.structure; | ||||||
|  |  | ||||||
|  | import java.util.HashMap; | ||||||
|  | import java.util.Map; | ||||||
|  |  | ||||||
|  | import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | ||||||
|  | import net.tomatentum.marinara.structure.data.ButtonStructureData; | ||||||
|  | import net.tomatentum.marinara.wrapper.ComponentStructureConverter; | ||||||
|  |  | ||||||
|  | public class StaticStructureProvider<B extends Object> implements StructureProvider<B> { | ||||||
|  |  | ||||||
|  |     private ComponentStructureConverter<B> converter; | ||||||
|  |  | ||||||
|  |     private Map<InteractionIdentifier, B> buttonMap; | ||||||
|  |  | ||||||
|  |     public StaticStructureProvider(ComponentStructureConverter<B> converter) { | ||||||
|  |         this.converter = converter; | ||||||
|  |         this.buttonMap = new HashMap<>(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public StaticStructureProvider<B> addButton(InteractionIdentifier identifier, ButtonStructureData data) { | ||||||
|  |         this.buttonMap.put(identifier, this.converter.convertButton(data)); | ||||||
|  |         return this; | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     @Override | ||||||
|  |     public B button(InteractionIdentifier identifier) { | ||||||
|  |         return this.buttonMap.get(identifier); | ||||||
|  |     } | ||||||
|  |      | ||||||
|  | } | ||||||
| @@ -0,0 +1,7 @@ | |||||||
|  | package net.tomatentum.marinara.structure; | ||||||
|  |  | ||||||
|  | import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | ||||||
|  |  | ||||||
|  | public interface StructureProvider<B extends Object> { | ||||||
|  |     B button(InteractionIdentifier identifier); | ||||||
|  | } | ||||||
| @@ -0,0 +1,5 @@ | |||||||
|  | package net.tomatentum.marinara.structure.annotation; | ||||||
|  |  | ||||||
|  | public @interface ComponentStructure { | ||||||
|  |     public String customId(); | ||||||
|  | } | ||||||
| @@ -0,0 +1,26 @@ | |||||||
|  | package net.tomatentum.marinara.structure.data; | ||||||
|  |  | ||||||
|  | import net.tomatentum.marinara.interaction.annotation.Button; | ||||||
|  |  | ||||||
|  | public record ButtonStructureData( | ||||||
|  |     String customId,  | ||||||
|  |     String label, | ||||||
|  |     ButtonStyle style, | ||||||
|  |     String url, | ||||||
|  |     boolean disabled, | ||||||
|  |     String emoji | ||||||
|  |     ) { | ||||||
|  |  | ||||||
|  |     public ButtonStructureData(Button button) { | ||||||
|  |         this(button.value(), button.label(), button.style(), button.url(), button.disabled(), button.emoji()); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public enum ButtonStyle { | ||||||
|  |         PRIMARY, | ||||||
|  |         SECONDARY, | ||||||
|  |         SUCCESS, | ||||||
|  |         DANGER, | ||||||
|  |         LINK; | ||||||
|  |     } | ||||||
|  |      | ||||||
|  | } | ||||||
| @@ -0,0 +1,44 @@ | |||||||
|  | package net.tomatentum.marinara.structure.parser; | ||||||
|  |  | ||||||
|  | import java.lang.reflect.Method; | ||||||
|  |  | ||||||
|  | import org.slf4j.Logger; | ||||||
|  | import org.slf4j.LoggerFactory; | ||||||
|  |  | ||||||
|  | import net.tomatentum.cutin.MethodParser; | ||||||
|  | import net.tomatentum.cutin.util.ReflectionUtil; | ||||||
|  | import net.tomatentum.marinara.interaction.InteractionType; | ||||||
|  | import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; | ||||||
|  | import net.tomatentum.marinara.structure.annotation.ComponentStructure; | ||||||
|  |  | ||||||
|  | public class ComponentStructureParser implements MethodParser { | ||||||
|  |  | ||||||
|  |     private Logger logger = LoggerFactory.getLogger(getClass()); | ||||||
|  |  | ||||||
|  |     private Class<? extends Object> buttonClass; | ||||||
|  |  | ||||||
|  |     public ComponentStructureParser(Class<? extends Object> buttonClass) { | ||||||
|  |         this.buttonClass = buttonClass; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public Object parse(Method method, Object containingObject) { | ||||||
|  |         if (!method.isAnnotationPresent(ComponentStructure.class))  | ||||||
|  |             return null; | ||||||
|  |         InteractionIdentifier.Builder builder = InteractionIdentifier.builder() | ||||||
|  |             .name(method.getAnnotation(ComponentStructure.class).customId()); | ||||||
|  |  | ||||||
|  |         if (buttonClass.isAssignableFrom(method.getReturnType())) | ||||||
|  |             builder.type(InteractionType.BUTTON); | ||||||
|  |  | ||||||
|  |         if (builder.type() == null) { | ||||||
|  |             logger.error("Structure Method {} return type did not match any of the required {}", String.join(","), buttonClass); | ||||||
|  |             return null; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         logger.trace("Parsed Structure Method of {} with result {}", ReflectionUtil.getFullMethodName(method), builder.build()); | ||||||
|  |         return builder.build(); | ||||||
|  |  | ||||||
|  |     } | ||||||
|  |      | ||||||
|  | } | ||||||
| @@ -0,0 +1,9 @@ | |||||||
|  | package net.tomatentum.marinara.wrapper; | ||||||
|  |  | ||||||
|  | import net.tomatentum.marinara.structure.data.ButtonStructureData; | ||||||
|  |  | ||||||
|  | public interface ComponentStructureConverter<B extends Object> { | ||||||
|  |  | ||||||
|  |     B convertButton(ButtonStructureData data); | ||||||
|  |      | ||||||
|  | } | ||||||
| @@ -4,6 +4,7 @@ import java.util.List; | |||||||
| import java.util.function.UnaryOperator; | import java.util.function.UnaryOperator; | ||||||
|  |  | ||||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||||
|  | import org.slf4j.LoggerFactory; | ||||||
|  |  | ||||||
| import discord4j.core.GatewayDiscordClient; | import discord4j.core.GatewayDiscordClient; | ||||||
| import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent; | import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent; | ||||||
| @@ -13,7 +14,6 @@ import discord4j.core.object.command.ApplicationCommandOption.Type; | |||||||
| import discord4j.discordjson.json.ApplicationCommandOptionChoiceData; | 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.wrapper.CommandConverter; | import net.tomatentum.marinara.wrapper.CommandConverter; | ||||||
| import net.tomatentum.marinara.wrapper.CommandRegisterer; | import net.tomatentum.marinara.wrapper.CommandRegisterer; | ||||||
| import net.tomatentum.marinara.wrapper.ContextObjectProvider; | import net.tomatentum.marinara.wrapper.ContextObjectProvider; | ||||||
| @@ -37,7 +37,7 @@ public class Discord4JWrapper extends LibraryWrapper { | |||||||
|     private Discord4JContextObjectProvider contextObjectProvider; |     private Discord4JContextObjectProvider contextObjectProvider; | ||||||
|     private CommandRegisterer<ApplicationCommandRequest> commandRegisterer; |     private CommandRegisterer<ApplicationCommandRequest> commandRegisterer; | ||||||
|  |  | ||||||
|     private Logger logger = LoggerUtil.getLogger(getClass()); |     private Logger logger = LoggerFactory.getLogger(getClass()); | ||||||
|  |  | ||||||
|     public Discord4JWrapper(GatewayDiscordClient api) { |     public Discord4JWrapper(GatewayDiscordClient api) { | ||||||
|         this.contextObjectProvider = new Discord4JContextObjectProvider(); |         this.contextObjectProvider = new Discord4JContextObjectProvider(); | ||||||
|   | |||||||
| @@ -21,6 +21,7 @@ import discord4j.core.object.command.ApplicationCommandOption.Type; | |||||||
| import net.tomatentum.marinara.Marinara; | 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; | ||||||
|  |  | ||||||
| @TestInstance(Lifecycle.PER_CLASS) | @TestInstance(Lifecycle.PER_CLASS) | ||||||
| class SlashCommandTest { | class SlashCommandTest { | ||||||
|  |  | ||||||
|   | |||||||
| @@ -7,12 +7,12 @@ 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.javacord.api.interaction.SlashCommandOptionChoice; | ||||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||||
|  | import org.slf4j.LoggerFactory; | ||||||
|  |  | ||||||
| import net.tomatentum.marinara.wrapper.CommandConverter; | import net.tomatentum.marinara.wrapper.CommandConverter; | ||||||
| import net.tomatentum.marinara.wrapper.CommandRegisterer; | import net.tomatentum.marinara.wrapper.CommandRegisterer; | ||||||
| import net.tomatentum.marinara.wrapper.ContextObjectProvider; | import net.tomatentum.marinara.wrapper.ContextObjectProvider; | ||||||
| import net.tomatentum.marinara.wrapper.IdentifierProvider; | import net.tomatentum.marinara.wrapper.IdentifierProvider; | ||||||
| import net.tomatentum.marinara.util.LoggerUtil; |  | ||||||
| import net.tomatentum.marinara.wrapper.LibraryWrapper; | import net.tomatentum.marinara.wrapper.LibraryWrapper; | ||||||
| import net.tomatentum.marinara.wrapper.javacord.identifierconverter.AutocompleteIdentifierConverter; | import net.tomatentum.marinara.wrapper.javacord.identifierconverter.AutocompleteIdentifierConverter; | ||||||
| import net.tomatentum.marinara.wrapper.javacord.identifierconverter.ButtonIdentifierConverter; | import net.tomatentum.marinara.wrapper.javacord.identifierconverter.ButtonIdentifierConverter; | ||||||
| @@ -23,7 +23,7 @@ public class JavacordWrapper extends LibraryWrapper { | |||||||
|     private JavacordContextObjectProvider contextObjectProvider; |     private JavacordContextObjectProvider contextObjectProvider; | ||||||
|     private CommandRegisterer<SlashCommandBuilder> commandRegisterer; |     private CommandRegisterer<SlashCommandBuilder> commandRegisterer; | ||||||
|      |      | ||||||
|     private Logger logger = LoggerUtil.getLogger(getClass()); |     private Logger logger = LoggerFactory.getLogger(getClass()); | ||||||
|  |  | ||||||
|     public JavacordWrapper(DiscordApi api) { |     public JavacordWrapper(DiscordApi api) { | ||||||
|         this.contextObjectProvider = new JavacordContextObjectProvider(); |         this.contextObjectProvider = new JavacordContextObjectProvider(); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user