Compare commits

...

4 Commits

Author SHA1 Message Date
272225ac07 feat(structure): add Structure abstractions and API
All checks were successful
github-mirror / push-github (push) Successful in 1m43s
Build / Gradle-Build (push) Successful in 15s
Test / Gradle-Test (push) Successful in 26s
2025-04-22 14:22:27 +02:00
42a675dc96 feat(method): implement MethodParser rework
All checks were successful
github-mirror / push-github (push) Successful in 13s
Build / Gradle-Build (push) Successful in 14s
Test / Gradle-Test (push) Successful in 25s
2025-04-16 00:00:47 +02:00
eea1597b15 fix(wrapper): also remove LoggerUtil in wrapper 2025-04-16 00:00:25 +02:00
83b446e6fb refactor(logger): remove LoggerUtil and add simplelogger for tests
Some checks failed
github-mirror / push-github (push) Successful in 1m48s
Build / Gradle-Build (push) Failing after 12s
Test / Gradle-Test (push) Failing after 13s
2025-04-14 02:09:33 +02:00
39 changed files with 470 additions and 228 deletions

View File

@@ -8,13 +8,14 @@ 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" }
slf4j = { module = "org.slf4j:slf4j-api", version.ref = "slf4j"} slf4j = { module = "org.slf4j:slf4j-api", version.ref = "slf4j"}
slf4j-simple = { module = "org.slf4j:slf4j-simple", version.ref = "slf4j"}
javacord = { module = "org.javacord:javacord", version.ref = "javacord"} 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"}

View File

@@ -4,6 +4,7 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.tomatentum.cutin.MethodExecutor; import net.tomatentum.cutin.MethodExecutor;
import net.tomatentum.cutin.ProcessorMethodExecutor; import net.tomatentum.cutin.ProcessorMethodExecutor;
@@ -18,14 +19,13 @@ 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.interaction.processor.AutocompleteInteractionProcessor; import net.tomatentum.marinara.interaction.processor.AutocompleteInteractionProcessor;
import net.tomatentum.marinara.interaction.processor.DirectInteractionProcessor; import net.tomatentum.marinara.interaction.processor.DirectInteractionProcessor;
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.wrapper.IdentifierProvider;
import net.tomatentum.marinara.wrapper.LibraryWrapper; import net.tomatentum.marinara.wrapper.LibraryWrapper;
public class Marinara { public class Marinara {
private Logger logger = LoggerUtil.getLogger(getClass()); private Logger logger = LoggerFactory.getLogger(getClass());
public static Marinara load(LibraryWrapper wrapper) { public static Marinara load(LibraryWrapper wrapper) {
return new Marinara(wrapper); return new Marinara(wrapper);

View File

@@ -3,9 +3,9 @@ package net.tomatentum.marinara.checks;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.tomatentum.cutin.method.ReflectedMethod; import net.tomatentum.cutin.method.ReflectedMethod;
import net.tomatentum.marinara.util.LoggerUtil;
public record AppliedCheck( public record AppliedCheck(
Annotation annotation, Annotation annotation,
@@ -13,7 +13,7 @@ public record AppliedCheck(
ReflectedMethod<CheckMethodIdentifier, CheckExecutionContext> postExec ReflectedMethod<CheckMethodIdentifier, CheckExecutionContext> postExec
) { ) {
private static Logger logger = LoggerUtil.getLogger(AppliedCheck.class); private static Logger logger = LoggerFactory.getLogger(AppliedCheck.class);
public boolean pre(Object context) { public boolean pre(Object context) {
logger.debug("Running InteractionCheck preExec {} with annotation {}", preExec(), annotation()); logger.debug("Running InteractionCheck preExec {} with annotation {}", preExec(), annotation());

View File

@@ -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));
} }
} }

View File

@@ -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 "";
} }

View File

@@ -5,6 +5,7 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.tomatentum.marinara.interaction.commands.annotation.CommandChoices; import net.tomatentum.marinara.interaction.commands.annotation.CommandChoices;
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption; import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
@@ -14,7 +15,6 @@ import net.tomatentum.marinara.interaction.commands.choice.EnumChoices;
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.interaction.ident.SlashCommandIdentifier; import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
import net.tomatentum.marinara.util.LoggerUtil;
public class SlashCommandDefinition { public class SlashCommandDefinition {
@@ -30,7 +30,7 @@ public class SlashCommandDefinition {
private RootCommandIdentifier rootIdentifier; private RootCommandIdentifier rootIdentifier;
private boolean isRootCommand; private boolean isRootCommand;
private Logger logger = LoggerUtil.getLogger(getClass()); private Logger logger = LoggerFactory.getLogger(getClass());
public SlashCommandDefinition(RootCommandIdentifier rootIdentifier) { public SlashCommandDefinition(RootCommandIdentifier rootIdentifier) {
this.entries = new HashSet<>(); this.entries = new HashSet<>();

View File

@@ -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()
); );
} }

View File

@@ -91,8 +91,8 @@ public class InteractionIdentifier {
@Override @Override
public String toString() { public String toString() {
if (parent() == null) if (parent() == null)
return name(); return name() + " - " + type();
return "%s.%s".formatted(name(), parent().toString()); return "%s:%s".formatted(name(), parent().toString());
} }
public static class Builder { public static class Builder {

View File

@@ -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(
InteractionHandler handler, Method method,
ContextObjectProvider cop InteractionHandler handler,
List<AppliedCheck> appliedChecks,
String autocompleteRef,
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()
); );
} }

View File

@@ -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)
); );
} }

View File

@@ -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()
); );
} }

View File

@@ -3,17 +3,17 @@ package net.tomatentum.marinara.interaction.processor;
import java.util.Set; import java.util.Set;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.tomatentum.cutin.MethodProcessor; import net.tomatentum.cutin.MethodProcessor;
import net.tomatentum.cutin.container.MethodContainer; import net.tomatentum.cutin.container.MethodContainer;
import net.tomatentum.marinara.interaction.InteractionType; import net.tomatentum.marinara.interaction.InteractionType;
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.util.LoggerUtil;
import net.tomatentum.marinara.wrapper.IdentifierProvider; import net.tomatentum.marinara.wrapper.IdentifierProvider;
public abstract class InteractionMethodProcessor implements MethodProcessor<InteractionIdentifier, Object> { public abstract class InteractionMethodProcessor implements MethodProcessor<InteractionIdentifier, Object> {
private Logger logger = LoggerUtil.getLogger(getClass()); private Logger logger = LoggerFactory.getLogger(getClass());
private IdentifierProvider provider; private IdentifierProvider provider;
private Set<InteractionType> types; private Set<InteractionType> types;

View File

@@ -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;
} }
} }

View File

@@ -1,32 +1,27 @@
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 net.tomatentum.cutin.MethodParser; import net.tomatentum.cutin.MethodParser;
import net.tomatentum.cutin.util.ReflectionUtil; import net.tomatentum.cutin.util.ReflectionUtil;
import net.tomatentum.marinara.interaction.annotation.Button; import net.tomatentum.marinara.interaction.annotation.Button;
import net.tomatentum.marinara.util.LoggerUtil;
public class ButtonParser implements MethodParser { public class ButtonParser implements MethodParser {
private Method method; private Logger logger = LoggerFactory.getLogger(getClass());
private Consumer<String> consumer;
private Logger logger = LoggerUtil.getLogger(getClass()); public ButtonParser() {
public ButtonParser(Method method, Consumer<String> consumer) {
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();
} }
} }

View File

@@ -1,8 +1,11 @@
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.LoggerFactory;
import io.leangen.geantyref.GenericTypeReflector; import io.leangen.geantyref.GenericTypeReflector;
import net.tomatentum.cutin.MethodParser; import net.tomatentum.cutin.MethodParser;
@@ -10,19 +13,16 @@ import net.tomatentum.marinara.checks.InteractionCheck;
public class InteractionCheckClassParser implements MethodParser { public class InteractionCheckClassParser implements MethodParser {
private Class<? extends InteractionCheck<?>> interactionCheckType; private Logger logger = LoggerFactory.getLogger(getClass());
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;
this.annotationTypeConsumer.accept(typeParam); Type typeParam = type.getActualTypeArguments().length == 1 ? type.getActualTypeArguments()[0] : null;
if (typeParam != null)
logger.trace("Parsed InteractionCheck Annotation {}", typeParam);
return typeParam;
} }
} }

View File

@@ -3,9 +3,10 @@ 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 net.tomatentum.cutin.MethodParser; import net.tomatentum.cutin.MethodParser;
import net.tomatentum.cutin.container.MethodContainer; import net.tomatentum.cutin.container.MethodContainer;
@@ -14,36 +15,35 @@ 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.checks.CheckMethodIdentifier.CheckMethodType; import net.tomatentum.marinara.checks.CheckMethodIdentifier.CheckMethodType;
import net.tomatentum.marinara.util.LoggerUtil;
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 = LoggerUtil.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;
} }
} }

View File

@@ -1,9 +1,9 @@
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 net.tomatentum.cutin.MethodParser; import net.tomatentum.cutin.MethodParser;
import net.tomatentum.cutin.util.ReflectionUtil; import net.tomatentum.cutin.util.ReflectionUtil;
@@ -12,23 +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;
import net.tomatentum.marinara.util.LoggerUtil;
public class SlashCommandParser implements MethodParser { public class SlashCommandParser implements MethodParser {
private Method method; private Logger logger = LoggerFactory.getLogger(getClass());
private Consumer<SlashCommandIdentifier> consumer;
private Logger logger = LoggerUtil.getLogger(getClass()); public SlashCommandParser() {
public SlashCommandParser(Method method, Consumer<SlashCommandIdentifier> consumer) {
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");
} }
} }

View File

@@ -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));
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -0,0 +1,5 @@
package net.tomatentum.marinara.structure.annotation;
public @interface ComponentStructure {
public String customId();
}

View File

@@ -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;
}
}

View File

@@ -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();
}
}

View File

@@ -1,17 +0,0 @@
package net.tomatentum.marinara.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.NOPLoggerFactory;
public class LoggerUtil {
public static Logger getLogger(String name) {
if (LoggerFactory.getILoggerFactory() instanceof NOPLoggerFactory)
return new SimpleLogger(name);
return LoggerFactory.getLogger(name);
}
public static Logger getLogger(Class<?> clazz) {
return getLogger(clazz.getName());
}
}

View File

@@ -1,55 +0,0 @@
package net.tomatentum.marinara.util;
import org.slf4j.Marker;
import org.slf4j.event.Level;
import org.slf4j.helpers.LegacyAbstractLogger;
import org.slf4j.helpers.MessageFormatter;
public class SimpleLogger extends LegacyAbstractLogger {
private String name;
public SimpleLogger(String name) {
this.name = name;
}
@Override
public boolean isTraceEnabled() {
return true;
}
@Override
public boolean isDebugEnabled() {
return true;
}
@Override
public boolean isInfoEnabled() {
return true;
}
@Override
public boolean isWarnEnabled() {
return true;
}
@Override
public boolean isErrorEnabled() {
return true;
}
@Override
protected String getFullyQualifiedCallerName() {
return this.name;
}
@Override
protected void handleNormalizedLoggingCall(Level level, Marker marker, String messagePattern, Object[] arguments,
Throwable throwable) {
String formatted = MessageFormatter.basicArrayFormat(messagePattern, arguments);
System.out.println("[%s] %s => %s".formatted(level, this.name, formatted));
}
}

View File

@@ -5,6 +5,7 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition; import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption; import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
@@ -12,7 +13,6 @@ import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOptio
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.interaction.ident.SlashCommandIdentifier; import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
import net.tomatentum.marinara.util.LoggerUtil;
public class CommandConverter<A extends Object, O extends Object, C extends Object> { public class CommandConverter<A extends Object, O extends Object, C extends Object> {
@@ -20,7 +20,7 @@ public class CommandConverter<A extends Object, O extends Object, C extends Obje
return new CommandConverter<>(spec); return new CommandConverter<>(spec);
} }
private Logger logger = LoggerUtil.getLogger(getClass()); private Logger logger = LoggerFactory.getLogger(getClass());
private Spec<A, O, C> spec; private Spec<A, O, C> spec;

View File

@@ -5,9 +5,9 @@ import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition; import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
import net.tomatentum.marinara.util.LoggerUtil;
import net.tomatentum.marinara.util.ObjectAggregator; import net.tomatentum.marinara.util.ObjectAggregator;
public class CommandRegisterer<A extends Object> { public class CommandRegisterer<A extends Object> {
@@ -16,7 +16,7 @@ public class CommandRegisterer<A extends Object> {
return new CommandRegisterer<A>(strategy, converter); return new CommandRegisterer<A>(strategy, converter);
} }
private Logger logger = LoggerUtil.getLogger(getClass()); private Logger logger = LoggerFactory.getLogger(getClass());
private Strategy<A> strategy; private Strategy<A> strategy;
private CommandConverter<A, ?, ?> converter; private CommandConverter<A, ?, ?> converter;
@@ -29,13 +29,17 @@ public class CommandRegisterer<A extends Object> {
public void register(SlashCommandDefinition[] slashDefs) { public void register(SlashCommandDefinition[] slashDefs) {
Set<ServerCommandList<A>> serverCommands = new ObjectAggregator<SlashCommandDefinition, Long, ServerCommandList<A>>( Set<ServerCommandList<A>> serverCommands = new ObjectAggregator<SlashCommandDefinition, Long, ServerCommandList<A>>(
def -> Arrays.stream(def.serverIds()).boxed().toList(), def -> Arrays.stream(def.serverIds()).boxed().toList(),
(l, o) -> l.add(converter.convert(o)), (l, o) -> {
logger.debug("Added {} for server ({}) registration.", o.rootIdentifier(), l.serverId());
l.add(converter.convert(o));
},
ServerCommandList::new) ServerCommandList::new)
.aggregate(Arrays.asList(slashDefs)).stream() .aggregate(Arrays.asList(slashDefs)).stream()
.collect(Collectors.toSet()); .collect(Collectors.toSet());
Set<A> globalCommands = Arrays.stream(slashDefs) Set<A> globalCommands = Arrays.stream(slashDefs)
.filter(x -> x.serverIds().length <= 0) .filter(x -> x.serverIds().length <= 0)
.peek(c -> logger.debug("Added {} for global registration.", c.rootIdentifier()))
.map(converter::convert) .map(converter::convert)
.collect(Collectors.toSet()); .collect(Collectors.toSet());

View File

@@ -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);
}

View File

@@ -8,11 +8,11 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.leangen.geantyref.GenericTypeReflector; import io.leangen.geantyref.GenericTypeReflector;
import net.tomatentum.cutin.util.ReflectionUtil; import net.tomatentum.cutin.util.ReflectionUtil;
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier; import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
import net.tomatentum.marinara.util.LoggerUtil;
public class IdentifierProvider { public class IdentifierProvider {
@@ -20,8 +20,9 @@ public class IdentifierProvider {
return new IdentifierProvider(Arrays.asList(converter)); return new IdentifierProvider(Arrays.asList(converter));
} }
private Logger logger = LoggerFactory.getLogger(getClass());
private Map<Class<?>, Converter<?>> converter; private Map<Class<?>, Converter<?>> converter;
private Logger logger = LoggerUtil.getLogger(getClass());
private IdentifierProvider(List<Converter<?>> converter) { private IdentifierProvider(List<Converter<?>> converter) {
this.converter = new HashMap<>(); this.converter = new HashMap<>();
@@ -47,7 +48,9 @@ public class IdentifierProvider {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Converter<Object> conv = (Converter<Object>) converter.get(type); Converter<Object> conv = (Converter<Object>) converter.get(type);
return conv.convert(context); InteractionIdentifier result = conv.convert(context);
logger.trace("Converted {} to {} using {}", context, result, conv);
return result;
} }
@FunctionalInterface @FunctionalInterface

View File

@@ -17,6 +17,8 @@ dependencies {
testImplementation(libs.discord4j) testImplementation(libs.discord4j)
testRuntimeOnly("org.junit.platform:junit-platform-launcher") testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testImplementation(libs.slf4j.simple)
implementation(libs.slf4j) implementation(libs.slf4j)
implementation(libs.discord4j) { implementation(libs.discord4j) {
exclude(module="discord4j-voice") exclude(module="discord4j-voice")

View File

@@ -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();

View File

@@ -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 {

View File

@@ -0,0 +1,7 @@
# SLF4J's SimpleLogger configuration file
# Simple implementation of Logger that sends all enabled log messages, for all defined loggers, to System.err.
# Default logging detail level for all instances of SimpleLogger.
# Must be one of ("trace", "debug", "info", "warn", or "error").
# If not specified, defaults to "info".
org.slf4j.simpleLogger.defaultLogLevel=trace

View File

@@ -16,6 +16,8 @@ dependencies {
testImplementation(libs.mockito) testImplementation(libs.mockito)
testRuntimeOnly("org.junit.platform:junit-platform-launcher") testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testImplementation(libs.slf4j.simple)
implementation(libs.slf4j) implementation(libs.slf4j)
implementation(libs.javacord) implementation(libs.javacord)
implementation(libs.geantyref) implementation(libs.geantyref)

View File

@@ -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();

View File

@@ -0,0 +1,7 @@
# SLF4J's SimpleLogger configuration file
# Simple implementation of Logger that sends all enabled log messages, for all defined loggers, to System.err.
# Default logging detail level for all instances of SimpleLogger.
# Must be one of ("trace", "debug", "info", "warn", or "error").
# If not specified, defaults to "info".
org.slf4j.simpleLogger.defaultLogLevel=trace