Compare commits
53 Commits
ba560cb909
...
feat/messa
Author | SHA1 | Date | |
---|---|---|---|
272225ac07
|
|||
42a675dc96
|
|||
eea1597b15
|
|||
83b446e6fb
|
|||
ef9384336a
|
|||
ebf5600e29
|
|||
070319853a
|
|||
60ead419e2
|
|||
0b7b607a23
|
|||
991d1c047b
|
|||
ec17952375
|
|||
0114cffcbd
|
|||
450f1fdaa1
|
|||
92540576df
|
|||
8a3cde52fd
|
|||
8495659364
|
|||
0973016a74
|
|||
0590789359
|
|||
2647a1f0b4
|
|||
a3c5eb62ac | |||
996f854ff7
|
|||
d2eec8b07c
|
|||
caa2ee7089
|
|||
2e5979e6e4 | |||
ab1eb74e85
|
|||
a5737b9eaa
|
|||
faca21724c
|
|||
4c5e28b679
|
|||
33f355e6ea | |||
d32ac62b4a
|
|||
e7c35d9308
|
|||
d4a91f3251
|
|||
bce4ce7812
|
|||
bae077654e
|
|||
203498de68
|
|||
24df1731da
|
|||
e3fc10a1ce
|
|||
78cacb7eb6
|
|||
7287d44645
|
|||
630c8ddee5
|
|||
4e27e6ce56
|
|||
432cf78a2e
|
|||
f940f48566
|
|||
f4ee258eb1
|
|||
56b668851b
|
|||
823402e0cd
|
|||
91b1df8d5b
|
|||
76d7e9ef4f | |||
236c584da8
|
|||
f6db113deb
|
|||
9058629af5
|
|||
8f14b0feb9
|
|||
3e02e7b85b |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -6,3 +6,4 @@ build
|
|||||||
.vscode
|
.vscode
|
||||||
lib/bin
|
lib/bin
|
||||||
wrapper/javacord/bin
|
wrapper/javacord/bin
|
||||||
|
wrapper/discord4j/bin
|
||||||
|
@@ -8,7 +8,19 @@ allprojects {
|
|||||||
group = "net.tomatentum.Marinara"
|
group = "net.tomatentum.Marinara"
|
||||||
version = "1.0.0-RC1" + (if (!project.hasProperty("release")) ("-" + getGitHash()) else "")
|
version = "1.0.0-RC1" + (if (!project.hasProperty("release")) ("-" + getGitHash()) else "")
|
||||||
description = "A simple but powerful, library-agnostic Discord Interaction Wrapper."
|
description = "A simple but powerful, library-agnostic Discord Interaction Wrapper."
|
||||||
|
plugins.withType<JavaPlugin> {
|
||||||
|
tasks.withType<Jar>().configureEach {
|
||||||
|
archiveBaseName.set("marinara-" + archiveBaseName.get())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
// Use Maven Central for resolving dependencies.
|
||||||
|
mavenCentral()
|
||||||
|
maven {
|
||||||
|
url = uri("https://git.tomatentum.net/api/packages/tueem/maven")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
subprojects {
|
subprojects {
|
||||||
|
@@ -3,16 +3,19 @@
|
|||||||
|
|
||||||
[versions]
|
[versions]
|
||||||
junit-jupiter = "5.10.2"
|
junit-jupiter = "5.10.2"
|
||||||
log4j = "2.24.1"
|
slf4j = "2.0.17"
|
||||||
javacord = "3.8.0"
|
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.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" }
|
||||||
log4j = { module = "org.apache.logging.log4j:log4j-api", version.ref = "log4j"}
|
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", version.ref = "cutin"}
|
||||||
|
@@ -10,18 +10,14 @@ plugins {
|
|||||||
`java-library`
|
`java-library`
|
||||||
}
|
}
|
||||||
|
|
||||||
repositories {
|
|
||||||
// Use Maven Central for resolving dependencies.
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
// Use JUnit Jupiter for testing.
|
// Use JUnit Jupiter for testing.
|
||||||
testImplementation(libs.junit.jupiter)
|
testImplementation(libs.junit.jupiter)
|
||||||
|
|
||||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||||
implementation(libs.log4j)
|
implementation(libs.slf4j)
|
||||||
implementation(libs.geantyref)
|
implementation(libs.geantyref)
|
||||||
|
api(libs.cutin)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply a specific Java toolchain to ease working on different environments.
|
// Apply a specific Java toolchain to ease working on different environments.
|
||||||
|
@@ -1,40 +1,85 @@
|
|||||||
package net.tomatentum.marinara;
|
package net.tomatentum.marinara;
|
||||||
|
|
||||||
import org.apache.logging.log4j.Logger;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import net.tomatentum.marinara.registry.InteractionCheckRegistry;
|
import org.slf4j.Logger;
|
||||||
import net.tomatentum.marinara.registry.InteractionRegistry;
|
import org.slf4j.LoggerFactory;
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
|
||||||
|
import net.tomatentum.cutin.MethodExecutor;
|
||||||
|
import net.tomatentum.cutin.ProcessorMethodExecutor;
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
|
import net.tomatentum.marinara.checks.CheckExecutionContext;
|
||||||
|
import net.tomatentum.marinara.checks.CheckMethodIdentifier;
|
||||||
|
import net.tomatentum.marinara.container.InteractionCheckContainer;
|
||||||
|
import net.tomatentum.marinara.container.InteractionMethodContainer;
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.RootCommandIdentifier;
|
||||||
|
import net.tomatentum.marinara.interaction.processor.AutocompleteInteractionProcessor;
|
||||||
|
import net.tomatentum.marinara.interaction.processor.DirectInteractionProcessor;
|
||||||
|
import net.tomatentum.marinara.util.ObjectAggregator;
|
||||||
|
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 <T extends LibraryWrapper> Marinara load(LibraryWrapper wrapper) {
|
public static Marinara load(LibraryWrapper wrapper) {
|
||||||
return new Marinara(wrapper);
|
return new Marinara(wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
private InteractionRegistry registry;
|
|
||||||
private InteractionCheckRegistry checkRegistry;
|
|
||||||
private LibraryWrapper wrapper;
|
private LibraryWrapper wrapper;
|
||||||
|
private MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer;
|
||||||
|
private MethodContainer<InteractionIdentifier, Object> interactionContainer;
|
||||||
|
private MethodExecutor<Object> interactionExecutor;
|
||||||
|
|
||||||
private Marinara(LibraryWrapper wrapper) {
|
private Marinara(LibraryWrapper wrapper) {
|
||||||
this.wrapper = wrapper;
|
this.wrapper = wrapper;
|
||||||
this.registry = new InteractionRegistry(this);
|
this.checkContainer = new InteractionCheckContainer();
|
||||||
this.checkRegistry = new InteractionCheckRegistry();
|
this.interactionContainer = new InteractionMethodContainer(getCheckContainer(), getWrapper().getContextObjectProvider());
|
||||||
|
IdentifierProvider provider = wrapper.createIdentifierProvider();
|
||||||
|
ProcessorMethodExecutor<InteractionIdentifier, Object> exec = new ProcessorMethodExecutor<>(getInteractionContainer());
|
||||||
|
exec
|
||||||
|
.addProcessor(new DirectInteractionProcessor(provider, InteractionType.COMMAND, InteractionType.BUTTON))
|
||||||
|
.addProcessor(new AutocompleteInteractionProcessor(getWrapper(), provider));
|
||||||
|
this.interactionExecutor = exec;
|
||||||
|
wrapper.subscribeInteractions(this.interactionExecutor::handle);
|
||||||
logger.info("Marinara loaded successfully!");
|
logger.info("Marinara loaded successfully!");
|
||||||
}
|
}
|
||||||
|
|
||||||
public InteractionRegistry getRegistry() {
|
//TODO move to future interactionstructure module
|
||||||
return registry;
|
public void registerCommands() {
|
||||||
}
|
List<InteractionIdentifier> slashIdentifiers = getInteractionContainer().identifiers().stream()
|
||||||
|
.filter(i -> i.type().equals(InteractionType.COMMAND))
|
||||||
|
.toList();
|
||||||
|
|
||||||
public InteractionCheckRegistry getCheckRegistry() {
|
SlashCommandDefinition[] defs = new ObjectAggregator<InteractionIdentifier, RootCommandIdentifier, SlashCommandDefinition>(
|
||||||
return checkRegistry;
|
i -> Arrays.asList((RootCommandIdentifier)i.rootNode()),
|
||||||
|
SlashCommandDefinition::addIdentifier,
|
||||||
|
SlashCommandDefinition::new)
|
||||||
|
.aggregate(slashIdentifiers)
|
||||||
|
.toArray(SlashCommandDefinition[]::new);
|
||||||
|
|
||||||
|
wrapper.getRegisterer().register(defs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public LibraryWrapper getWrapper() {
|
public LibraryWrapper getWrapper() {
|
||||||
return wrapper;
|
return this.wrapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MethodContainer<InteractionIdentifier, Object> getInteractionContainer() {
|
||||||
|
return this.interactionContainer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MethodContainer<CheckMethodIdentifier, CheckExecutionContext> getCheckContainer() {
|
||||||
|
return this.checkContainer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MethodExecutor<Object> getInteractionExecutor() {
|
||||||
|
return interactionExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,50 +1,28 @@
|
|||||||
package net.tomatentum.marinara.checks;
|
package net.tomatentum.marinara.checks;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
|
||||||
|
|
||||||
public record AppliedCheck(InteractionCheck<?> check, Annotation annotation) {
|
public record AppliedCheck(
|
||||||
|
Annotation annotation,
|
||||||
|
ReflectedMethod<CheckMethodIdentifier, CheckExecutionContext> preExec,
|
||||||
|
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) {
|
||||||
Method[] methods = Arrays.stream(check.getClass().getMethods())
|
logger.debug("Running InteractionCheck preExec {} with annotation {}", preExec(), annotation());
|
||||||
.filter(x -> x.getName().equals("preExec"))
|
return (boolean) preExec().run(new CheckExecutionContext(annotation, context));
|
||||||
.filter(x -> !x.isBridge())
|
|
||||||
.toArray(s -> new Method[s]);
|
|
||||||
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.annotationType());
|
|
||||||
method.setAccessible(true);
|
|
||||||
try {
|
|
||||||
logger.debug("Executing pre check {} with context {}", check.getClass().getName(), context.toString());
|
|
||||||
boolean result = (boolean) method.invoke(check, context, annotation);
|
|
||||||
logger.debug("Pre Check {} {} with context {}", check.getClass().getName(), result ? "succeeded" : "failed", context.toString());
|
|
||||||
return result;
|
|
||||||
} catch (IllegalAccessException | InvocationTargetException | SecurityException e) {
|
|
||||||
logger.fatal(e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void post(Object context) {
|
public void post(Object context) {
|
||||||
Method[] methods = Arrays.stream(check.getClass().getMethods())
|
logger.debug("Running InteractionCheck postExec {} with annotation {}", postExec(), annotation());
|
||||||
.filter(x -> x.getName().equals("postExec"))
|
postExec().run(new CheckExecutionContext(annotation, context));
|
||||||
.filter(x -> !x.isBridge())
|
|
||||||
.toArray(s -> new Method[s]);
|
|
||||||
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.annotationType());
|
|
||||||
method.setAccessible(true);
|
|
||||||
try {
|
|
||||||
logger.debug("Executing post check {} with context {}", check.getClass().getName(), context.toString());
|
|
||||||
method.invoke(check, context, annotation);
|
|
||||||
} catch (IllegalAccessException | InvocationTargetException | SecurityException e) {
|
|
||||||
logger.fatal(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,7 @@
|
|||||||
|
package net.tomatentum.marinara.checks;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
|
||||||
|
public record CheckExecutionContext(Annotation annotation, Object originalContext) {
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,27 @@
|
|||||||
|
package net.tomatentum.marinara.checks;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
|
public record CheckMethodIdentifier(Type annotationType, CheckMethodType type) {
|
||||||
|
|
||||||
|
public enum CheckMethodType {
|
||||||
|
PRE("preExec"),
|
||||||
|
POST("postExec");
|
||||||
|
|
||||||
|
private String methodName;
|
||||||
|
|
||||||
|
private CheckMethodType(String methodName) {
|
||||||
|
this.methodName = methodName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String methodName() {
|
||||||
|
return this.methodName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final String toString() {
|
||||||
|
return "InteractionCheck(%s, %s)".formatted(annotationType, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,74 @@
|
|||||||
|
package net.tomatentum.marinara.checks;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.cutin.ReflectedMethodFactory.ParserResults;
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
|
import net.tomatentum.cutin.method.BestCandidateMethod;
|
||||||
|
import net.tomatentum.marinara.checks.CheckMethodIdentifier.CheckMethodType;
|
||||||
|
import net.tomatentum.marinara.parser.InteractionCheckClassParser;
|
||||||
|
|
||||||
|
public class InteractionCheckMethod extends BestCandidateMethod<CheckMethodIdentifier, CheckExecutionContext> {
|
||||||
|
|
||||||
|
private CheckMethodIdentifier identifier;
|
||||||
|
|
||||||
|
public InteractionCheckMethod(
|
||||||
|
String methodName,
|
||||||
|
Object containingObject,
|
||||||
|
CheckMethodIdentifier identifier
|
||||||
|
) {
|
||||||
|
super(methodName, containingObject);
|
||||||
|
this.identifier = identifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getParameter(CheckExecutionContext context, int index) {
|
||||||
|
switch (index) {
|
||||||
|
case 0:
|
||||||
|
return context.originalContext();
|
||||||
|
case 1:
|
||||||
|
return context.annotation();
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CheckMethodIdentifier identifier() {
|
||||||
|
return this.identifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class InteractionCheckMethodFactory extends BestCandidateMethod.Factory<CheckMethodIdentifier, CheckExecutionContext> {
|
||||||
|
|
||||||
|
private CheckMethodType type;
|
||||||
|
|
||||||
|
public InteractionCheckMethodFactory(MethodContainer<CheckMethodIdentifier, CheckExecutionContext> methodContainer, CheckMethodType type) {
|
||||||
|
super(methodContainer, type.methodName());
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addParser(Set<MethodParser> parsers) {
|
||||||
|
parsers.add(
|
||||||
|
new InteractionCheckClassParser()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Optional<BestCandidateMethod<CheckMethodIdentifier, CheckExecutionContext>> bcProduce(
|
||||||
|
String methodName,
|
||||||
|
Object containingObject,
|
||||||
|
ParserResults parserResults
|
||||||
|
) {
|
||||||
|
|
||||||
|
CheckMethodIdentifier identifier = new CheckMethodIdentifier(parserResults.get(InteractionCheckClassParser.class), type);
|
||||||
|
if (identifier.annotationType() == null)
|
||||||
|
return null;
|
||||||
|
return Optional.of(new InteractionCheckMethod(methodName, containingObject, identifier));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,19 @@
|
|||||||
|
package net.tomatentum.marinara.container;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.ReflectedMethodFactoryImpl;
|
||||||
|
import net.tomatentum.cutin.container.LoneMethodContainer;
|
||||||
|
import net.tomatentum.marinara.checks.CheckExecutionContext;
|
||||||
|
import net.tomatentum.marinara.checks.CheckMethodIdentifier;
|
||||||
|
import net.tomatentum.marinara.checks.CheckMethodIdentifier.CheckMethodType;
|
||||||
|
import net.tomatentum.marinara.checks.InteractionCheckMethod.InteractionCheckMethodFactory;
|
||||||
|
|
||||||
|
public class InteractionCheckContainer extends LoneMethodContainer<CheckMethodIdentifier, CheckExecutionContext> {
|
||||||
|
|
||||||
|
public InteractionCheckContainer() {
|
||||||
|
super(new ReflectedMethodFactoryImpl<>());
|
||||||
|
super.factory()
|
||||||
|
.addFactory(new InteractionCheckMethodFactory(this, CheckMethodType.PRE))
|
||||||
|
.addFactory(new InteractionCheckMethodFactory(this, CheckMethodType.POST));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,38 @@
|
|||||||
|
package net.tomatentum.marinara.container;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.ReflectedMethodFactory;
|
||||||
|
import net.tomatentum.cutin.ReflectedMethodFactoryImpl;
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
|
import net.tomatentum.cutin.container.MultiMethodContainer;
|
||||||
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
|
import net.tomatentum.marinara.checks.CheckExecutionContext;
|
||||||
|
import net.tomatentum.marinara.checks.CheckMethodIdentifier;
|
||||||
|
import net.tomatentum.marinara.interaction.components.methods.ButtonInteractionMethod;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||||
|
import net.tomatentum.marinara.interaction.methods.AutoCompleteInteractionMethod;
|
||||||
|
import net.tomatentum.marinara.interaction.methods.SlashCommandInteractionMethod;
|
||||||
|
|
||||||
|
public class InteractionMethodContainer extends MultiMethodContainer<InteractionIdentifier, Object> {
|
||||||
|
|
||||||
|
|
||||||
|
private static ReflectedMethodFactory<InteractionIdentifier, Object> createFactory(MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkRegistry, ContextObjectProvider cop) {
|
||||||
|
return new ReflectedMethodFactoryImpl<InteractionIdentifier, Object>()
|
||||||
|
.addFactory(new AutoCompleteInteractionMethod.Factory(checkRegistry, cop))
|
||||||
|
.addFactory(new SlashCommandInteractionMethod.Factory(checkRegistry, cop))
|
||||||
|
.addFactory(new ButtonInteractionMethod.Factory(checkRegistry, cop));
|
||||||
|
}
|
||||||
|
|
||||||
|
public InteractionMethodContainer(MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkRegistry, ContextObjectProvider cop) {
|
||||||
|
super(createFactory(checkRegistry, cop));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MethodContainer<InteractionIdentifier, Object> addMethod(ReflectedMethod<InteractionIdentifier, Object> method) {
|
||||||
|
super.identifiers().stream()
|
||||||
|
.filter(method.identifier()::equals)
|
||||||
|
.forEach(i -> InteractionIdentifier.tryAddDescriptions(i, method.identifier()));
|
||||||
|
return super.addMethod(method);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -8,5 +8,5 @@ import java.lang.annotation.Target;
|
|||||||
@Target({ElementType.METHOD})
|
@Target({ElementType.METHOD})
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface AutoComplete {
|
public @interface AutoComplete {
|
||||||
|
public String value();
|
||||||
}
|
}
|
||||||
|
@@ -5,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 "";
|
||||||
}
|
}
|
||||||
|
@@ -1,94 +0,0 @@
|
|||||||
package net.tomatentum.marinara.interaction.commands;
|
|
||||||
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOptionChoice;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption.PlaceHolderEnum;
|
|
||||||
|
|
||||||
public record ExecutableSlashCommandDefinition(
|
|
||||||
SlashCommand applicationCommand,
|
|
||||||
SubCommand subCommand,
|
|
||||||
SubCommandGroup subCommandGroup,
|
|
||||||
SlashCommandOption[] options) {
|
|
||||||
|
|
||||||
public static SlashCommandOptionChoice[] getActualChoices(SlashCommandOption option) {
|
|
||||||
SlashCommandOptionChoice[] choices = option.choices();
|
|
||||||
if (choices.length <= 0 && !option.choiceEnum().equals(PlaceHolderEnum.class))
|
|
||||||
choices = EnumChoices.of(option.choiceEnum()).choices();
|
|
||||||
return choices;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final boolean equals(Object o) {
|
|
||||||
if (!(o instanceof ExecutableSlashCommandDefinition))
|
|
||||||
return false;
|
|
||||||
ExecutableSlashCommandDefinition other = (ExecutableSlashCommandDefinition) o;
|
|
||||||
boolean equals = false;
|
|
||||||
|
|
||||||
if (this.applicationCommand() != null && other.applicationCommand() != null)
|
|
||||||
equals = this.applicationCommand().name().equals(other.applicationCommand().name());
|
|
||||||
|
|
||||||
if (this.subCommandGroup() != null && other.subCommandGroup() != null)
|
|
||||||
equals = this.subCommandGroup().name().equals(other.subCommandGroup().name());
|
|
||||||
|
|
||||||
if (this.subCommand() != null && other.subCommand() != null)
|
|
||||||
equals = this.subCommand().name().equals(other.subCommand().name());
|
|
||||||
|
|
||||||
return equals;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public final String toString() {
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
builder.append(applicationCommand.name());
|
|
||||||
if (subCommandGroup != null && subCommandGroup.name() != null)
|
|
||||||
builder.append("::").append(subCommandGroup.name());
|
|
||||||
if (subCommand != null && subCommand.name() != null)
|
|
||||||
builder.append("::").append(subCommand.name());
|
|
||||||
return builder.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isRootCommand() {
|
|
||||||
return subCommand == null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Builder {
|
|
||||||
private SlashCommand applicationCommand;
|
|
||||||
private SubCommand subCommand;
|
|
||||||
private SubCommandGroup subCommandGroup;
|
|
||||||
|
|
||||||
public ExecutableSlashCommandDefinition build() {
|
|
||||||
if (applicationCommand == null)
|
|
||||||
throw new IllegalArgumentException("applicationCommandName cant be null");
|
|
||||||
|
|
||||||
return new ExecutableSlashCommandDefinition(applicationCommand, subCommand, subCommandGroup, subCommand != null ? subCommand.options() : applicationCommand.options());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setApplicationCommand(SlashCommand applicationCommand) {
|
|
||||||
this.applicationCommand = applicationCommand;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSubCommand(SubCommand subCommand) {
|
|
||||||
this.subCommand = subCommand;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSubCommandGroup(SubCommandGroup subCommandGroup) {
|
|
||||||
this.subCommandGroup = subCommandGroup;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SlashCommand getApplicationCommand() {
|
|
||||||
return applicationCommand;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SubCommand getSubCommand() {
|
|
||||||
return subCommand;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SubCommandGroup getSubCommandGroup() {
|
|
||||||
return subCommandGroup;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,99 +1,112 @@
|
|||||||
package net.tomatentum.marinara.interaction.commands;
|
package net.tomatentum.marinara.interaction.commands;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.HashSet;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
import org.slf4j.Logger;
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
|
import org.slf4j.LoggerFactory;
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup;
|
|
||||||
|
import net.tomatentum.marinara.interaction.commands.annotation.CommandChoices;
|
||||||
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
||||||
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption.PlaceHolderEnum;
|
||||||
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOptionChoice;
|
||||||
|
import net.tomatentum.marinara.interaction.commands.choice.EnumChoices;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.RootCommandIdentifier;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
|
||||||
|
|
||||||
public class SlashCommandDefinition {
|
public class SlashCommandDefinition {
|
||||||
private List<ExecutableSlashCommandDefinition> executableDefinitons;
|
|
||||||
private SlashCommand slashCommand;
|
public static SlashCommandOptionChoice[] getActualChoices(SlashCommandOption option) {
|
||||||
|
CommandChoices choices = option.choices();
|
||||||
|
SlashCommandOptionChoice[] actualChoices = choices.value();
|
||||||
|
if (choices.value().length <= 0 && !choices.cenum().equals(PlaceHolderEnum.class))
|
||||||
|
actualChoices = EnumChoices.of(choices.cenum()).choices();
|
||||||
|
return actualChoices;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<InteractionIdentifier> entries;
|
||||||
|
private RootCommandIdentifier rootIdentifier;
|
||||||
private boolean isRootCommand;
|
private boolean isRootCommand;
|
||||||
|
|
||||||
public SlashCommandDefinition(SlashCommand applicationCommand) {
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
this.executableDefinitons = new ArrayList<>();
|
|
||||||
this.slashCommand = applicationCommand;
|
public SlashCommandDefinition(RootCommandIdentifier rootIdentifier) {
|
||||||
|
this.entries = new HashSet<>();
|
||||||
|
this.rootIdentifier = rootIdentifier;
|
||||||
|
this.isRootCommand = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SlashCommandDefinition addExecutableCommand(ExecutableSlashCommandDefinition def) {
|
public SlashCommandDefinition addIdentifier(InteractionIdentifier identifier) {
|
||||||
if (def.applicationCommand() != null) {
|
RootCommandIdentifier rootIdentifier = (RootCommandIdentifier) identifier.rootNode();
|
||||||
if (slashCommand == null)
|
|
||||||
this.slashCommand = def.applicationCommand();
|
if (!this.rootIdentifier.equals(rootIdentifier))
|
||||||
if (!this.slashCommand.name().equals(def.applicationCommand().name()))
|
throw new IllegalArgumentException("Root Node did not match.");
|
||||||
throw new IllegalArgumentException(def + ": has a non matching Application Command description. Please edit it to equal all other descriptions or remove it to use other definitions descriptions");
|
|
||||||
|
if (this.rootIdentifier.description() == null)
|
||||||
|
this.rootIdentifier = rootIdentifier;
|
||||||
|
|
||||||
|
if (!isRootCommand)
|
||||||
|
this.isRootCommand = identifier.parent() == null ? true : false;
|
||||||
|
|
||||||
|
if ((isRootCommand && identifier.parent() != null) || (!isRootCommand && identifier.parent() == null)) {
|
||||||
|
throw new IllegalArgumentException(identifier.toString() + ": cannot have subcommands and rootcommand definitions together");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (executableDefinitons.isEmpty())
|
entries.add(identifier);
|
||||||
this.isRootCommand = def.isRootCommand();
|
this.logger.debug("Added identifer {} to command {}", identifier, rootIdentifier);
|
||||||
|
|
||||||
if ((isRootCommand && !def.isRootCommand()) || (!isRootCommand && def.isRootCommand())) {
|
|
||||||
throw new IllegalArgumentException(def + ": cannot have subcommands and rootcommand definitions together");
|
|
||||||
}
|
|
||||||
|
|
||||||
executableDefinitons.add(def);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SubCommandGroup[] getSubCommandGroups() {
|
public SlashCommandIdentifier[] getSubCommandGroups() {
|
||||||
List<SubCommandGroup> subCommandGroups = Arrays.stream(getExecutableDefinitons())
|
if (isRootCommand)
|
||||||
.filter((x) -> x.subCommandGroup() != null)
|
|
||||||
.map((x) -> x.subCommandGroup())
|
|
||||||
.toList();
|
|
||||||
|
|
||||||
HashMap<String, SubCommandGroup> subCommandGroupMap = new HashMap<>();
|
|
||||||
subCommandGroups.forEach((x) -> {
|
|
||||||
SubCommandGroup current = subCommandGroupMap.get(x.name());
|
|
||||||
if (current == null || (current.description().isBlank() && !x.description().isBlank()))
|
|
||||||
subCommandGroupMap.put(x.name(), x);
|
|
||||||
});
|
|
||||||
|
|
||||||
return subCommandGroupMap.values().toArray(new SubCommandGroup[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public SubCommand[] getSubCommands(String groupName) {
|
|
||||||
List<SubCommand> subCommands;
|
|
||||||
if (groupName == null)
|
|
||||||
subCommands = Arrays.stream(getExecutableDefinitons())
|
|
||||||
.filter((x) -> x.subCommandGroup() == null && x.subCommand() != null)
|
|
||||||
.map((x) -> x.subCommand())
|
|
||||||
.toList();
|
|
||||||
else
|
|
||||||
subCommands = Arrays.stream(getExecutableDefinitons())
|
|
||||||
.filter((x) -> x.subCommandGroup().name().equals(groupName) && x.subCommand() != null)
|
|
||||||
.map((x) -> x.subCommand())
|
|
||||||
.toList();
|
|
||||||
|
|
||||||
HashMap<String, SubCommand> subCommandMap = new HashMap<>();
|
|
||||||
subCommands.forEach((x) -> {
|
|
||||||
SubCommand current = subCommandMap.get(x.name());
|
|
||||||
if (current == null || (current.description().isBlank() && !x.description().isBlank()))
|
|
||||||
subCommandMap.put(x.name(), x);
|
|
||||||
});
|
|
||||||
|
|
||||||
return subCommandMap.values().toArray(new SubCommand[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public SlashCommand getFullSlashCommand() {
|
|
||||||
if (isRootCommand())
|
|
||||||
return getSlashCommand();
|
|
||||||
for (ExecutableSlashCommandDefinition executableSlashCommandDefinition : executableDefinitons) {
|
|
||||||
if (executableSlashCommandDefinition.options().length > 0)
|
|
||||||
return executableSlashCommandDefinition.applicationCommand();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
List<InteractionIdentifier> subCommandGroups = entries().stream()
|
||||||
|
.filter(x -> x.parent().parent() != null)
|
||||||
|
.map(x -> x.parent())
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
return subCommandGroups.toArray(SlashCommandIdentifier[]::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SlashCommand getSlashCommand() {
|
public SlashCommandIdentifier[] getSubCommands() {
|
||||||
return slashCommand;
|
if (isRootCommand)
|
||||||
|
return null;
|
||||||
|
return entries.stream().filter(x -> x.parent() instanceof RootCommandIdentifier).toArray(SlashCommandIdentifier[]::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ExecutableSlashCommandDefinition[] getExecutableDefinitons() {
|
public SlashCommandIdentifier[] getSubCommands(String groupName) {
|
||||||
return executableDefinitons.toArray(new ExecutableSlashCommandDefinition[0]);
|
if (isRootCommand)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
List<InteractionIdentifier> subCommands = entries().stream()
|
||||||
|
.filter(x -> x.parent().parent() != null && x.parent().name().equals(groupName))
|
||||||
|
.map(x -> x.parent().parent())
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
return subCommands.toArray(SlashCommandIdentifier[]::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (!(obj instanceof SlashCommandDefinition))
|
||||||
|
return false;
|
||||||
|
SlashCommandDefinition other = (SlashCommandDefinition) obj;
|
||||||
|
return this.rootIdentifier().equals(other.rootIdentifier());
|
||||||
|
}
|
||||||
|
|
||||||
|
public long[] serverIds() {
|
||||||
|
return rootIdentifier().serverIds();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<InteractionIdentifier> entries() {
|
||||||
|
return this.entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RootCommandIdentifier rootIdentifier() {
|
||||||
|
return rootIdentifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isRootCommand() {
|
public boolean isRootCommand() {
|
||||||
|
@@ -0,0 +1,8 @@
|
|||||||
|
package net.tomatentum.marinara.interaction.commands.annotation;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption.PlaceHolderEnum;
|
||||||
|
|
||||||
|
public @interface CommandChoices {
|
||||||
|
public SlashCommandOptionChoice[] value() default {};
|
||||||
|
public Class<? extends Enum<?>> cenum() default PlaceHolderEnum.class;
|
||||||
|
}
|
@@ -0,0 +1,6 @@
|
|||||||
|
package net.tomatentum.marinara.interaction.commands.annotation;
|
||||||
|
|
||||||
|
public @interface Range {
|
||||||
|
public double min() default Double.MIN_VALUE;
|
||||||
|
public double max() default Double.MAX_VALUE;
|
||||||
|
}
|
@@ -5,6 +5,7 @@ import java.lang.annotation.Retention;
|
|||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.interaction.annotation.AutoComplete;
|
||||||
import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType;
|
import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType;
|
||||||
|
|
||||||
@Target({ElementType.ANNOTATION_TYPE})
|
@Target({ElementType.ANNOTATION_TYPE})
|
||||||
@@ -14,11 +15,11 @@ public @interface SlashCommandOption {
|
|||||||
public String description() default "";
|
public String description() default "";
|
||||||
public SlashCommandOptionType type() default SlashCommandOptionType.STRING;
|
public SlashCommandOptionType type() default SlashCommandOptionType.STRING;
|
||||||
public boolean required() default false;
|
public boolean required() default false;
|
||||||
public boolean autocomplete() default false;
|
public AutoComplete[] autocompletes() default {};
|
||||||
public SlashCommandOptionChoice[] choices() default {};
|
public Range range() default @Range;
|
||||||
public Class<? extends Enum<?>> choiceEnum() default PlaceHolderEnum.class;
|
public CommandChoices choices() default @CommandChoices;
|
||||||
|
|
||||||
public static enum PlaceHolderEnum {
|
public enum PlaceHolderEnum {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
package net.tomatentum.marinara.interaction.commands;
|
package net.tomatentum.marinara.interaction.commands.choice;
|
||||||
|
|
||||||
public interface ChoiceValueProvider<T> {
|
public interface ChoiceValueProvider<T> {
|
||||||
T getChoiceValue();
|
T getChoiceValue();
|
@@ -1,4 +1,4 @@
|
|||||||
package net.tomatentum.marinara.interaction.commands;
|
package net.tomatentum.marinara.interaction.commands.choice;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
@@ -0,0 +1,16 @@
|
|||||||
|
package net.tomatentum.marinara.interaction.commands.option;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
||||||
|
|
||||||
|
public record AutocompleteOptionData(String name, Object input) {
|
||||||
|
|
||||||
|
public String[] getAutocompleteRefs(SlashCommandOption[] options) {
|
||||||
|
return Arrays.stream(options)
|
||||||
|
.filter(o -> o.name().equals(this.name()))
|
||||||
|
.flatMap(o -> Arrays.stream(o.autocompletes()))
|
||||||
|
.map(a -> a.value())
|
||||||
|
.toArray(String[]::new);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,93 @@
|
|||||||
|
package net.tomatentum.marinara.interaction.components.methods;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.cutin.ReflectedMethodFactory.ParserResults;
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
|
import net.tomatentum.marinara.checks.AppliedCheck;
|
||||||
|
import net.tomatentum.marinara.checks.CheckExecutionContext;
|
||||||
|
import net.tomatentum.marinara.checks.CheckMethodIdentifier;
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.interaction.methods.InteractionMethod;
|
||||||
|
import net.tomatentum.marinara.parser.ButtonParser;
|
||||||
|
import net.tomatentum.marinara.parser.InteractionCheckParser;
|
||||||
|
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||||
|
|
||||||
|
public class ButtonInteractionMethod extends InteractionMethod {
|
||||||
|
|
||||||
|
private String customId;
|
||||||
|
private ContextObjectProvider cop;
|
||||||
|
|
||||||
|
private ButtonInteractionMethod(
|
||||||
|
Method method,
|
||||||
|
InteractionHandler handler,
|
||||||
|
List<AppliedCheck> appliedChecks,
|
||||||
|
String customId,
|
||||||
|
ContextObjectProvider cop
|
||||||
|
) {
|
||||||
|
super(method, handler, appliedChecks);
|
||||||
|
this.customId = customId;
|
||||||
|
this.cop = cop;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getParameter(Object context, int index) {
|
||||||
|
Class<?> type = method().getParameterTypes()[index];
|
||||||
|
Object superResult = super.getParameter(context, index);
|
||||||
|
if (superResult == null)
|
||||||
|
return this.cop.getComponentContextObject(context, type);
|
||||||
|
else
|
||||||
|
return superResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InteractionIdentifier identifier() {
|
||||||
|
return InteractionIdentifier.builder()
|
||||||
|
.name(customId)
|
||||||
|
.description("Button")
|
||||||
|
.type(InteractionType.BUTTON)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Factory extends InteractionMethod.Factory {
|
||||||
|
|
||||||
|
private ContextObjectProvider cop;
|
||||||
|
|
||||||
|
public Factory(MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer, ContextObjectProvider cop) {
|
||||||
|
super(checkContainer);
|
||||||
|
this.cop = cop;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject, ParserResults parserResults) {
|
||||||
|
if (!(containingObject instanceof InteractionHandler)) return Optional.empty();
|
||||||
|
String customId = parserResults.get(ButtonParser.class);
|
||||||
|
if (customId == null) return Optional.empty();
|
||||||
|
return Optional.of(new ButtonInteractionMethod(
|
||||||
|
method,
|
||||||
|
(InteractionHandler) containingObject,
|
||||||
|
parserResults.get(InteractionCheckParser.class),
|
||||||
|
customId,
|
||||||
|
this.cop
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addParser(Set<MethodParser> parser) {
|
||||||
|
super.addParser(parser);
|
||||||
|
|
||||||
|
parser.add(
|
||||||
|
new ButtonParser()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,146 @@
|
|||||||
|
package net.tomatentum.marinara.interaction.ident;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
|
||||||
|
public class InteractionIdentifier {
|
||||||
|
|
||||||
|
public static InteractionIdentifier.Builder builder() {
|
||||||
|
return new InteractionIdentifier.Builder();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RootCommandIdentifier.Builder rootBuilder() {
|
||||||
|
return new RootCommandIdentifier.Builder();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SlashCommandIdentifier.Builder slashBuilder() {
|
||||||
|
return new SlashCommandIdentifier.Builder();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static InteractionIdentifier createHierarchy(InteractionType type, String... names) {
|
||||||
|
InteractionIdentifier last = null;
|
||||||
|
for (String string : names) {
|
||||||
|
last = builder().name(string).type(type).parent(last).build();
|
||||||
|
}
|
||||||
|
return last;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void tryAddDescriptions(InteractionIdentifier receiver, InteractionIdentifier provider) {
|
||||||
|
if (receiver == null || provider == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (receiver.description().isBlank())
|
||||||
|
receiver.description = provider.description();
|
||||||
|
tryAddDescriptions(receiver.parent(), provider.parent());
|
||||||
|
}
|
||||||
|
|
||||||
|
private InteractionIdentifier parent;
|
||||||
|
private String name;
|
||||||
|
private String description;
|
||||||
|
private InteractionType type;
|
||||||
|
|
||||||
|
InteractionIdentifier(InteractionIdentifier parent, String name, String description, InteractionType type) {
|
||||||
|
this.parent = parent;
|
||||||
|
this.name = name;
|
||||||
|
this.description = description;
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public InteractionIdentifier rootNode() { return rootNode(this); }
|
||||||
|
|
||||||
|
private InteractionIdentifier rootNode(InteractionIdentifier identifier) {
|
||||||
|
if (identifier.parent() == null)
|
||||||
|
return identifier;
|
||||||
|
return rootNode(identifier.parent());
|
||||||
|
}
|
||||||
|
|
||||||
|
public String name() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String description() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public InteractionIdentifier parent() {
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public InteractionType type() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (!(obj instanceof InteractionIdentifier))
|
||||||
|
return false;
|
||||||
|
InteractionIdentifier ident = (InteractionIdentifier) obj;
|
||||||
|
if (!type().equals(ident.type()))
|
||||||
|
return false;
|
||||||
|
if (!name().equals(ident.name()))
|
||||||
|
return false;
|
||||||
|
return Objects.equals(parent(), ident.parent());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(type(), name(), parent());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
if (parent() == null)
|
||||||
|
return name() + " - " + type();
|
||||||
|
return "%s:%s".formatted(name(), parent().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder {
|
||||||
|
private InteractionIdentifier parent;
|
||||||
|
private String name;
|
||||||
|
private String description;
|
||||||
|
private InteractionType type;
|
||||||
|
|
||||||
|
public InteractionIdentifier parent() {
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder parent(InteractionIdentifier parent) {
|
||||||
|
this.parent = parent;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String name() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder name(String name) {
|
||||||
|
this.name = name;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String description() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder description(String description) {
|
||||||
|
this.description = description;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public InteractionType type() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder type(InteractionType type) {
|
||||||
|
this.type = type;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public InteractionIdentifier build() {
|
||||||
|
return new InteractionIdentifier(parent, name, description, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,99 @@
|
|||||||
|
package net.tomatentum.marinara.interaction.ident;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
||||||
|
|
||||||
|
public class RootCommandIdentifier extends SlashCommandIdentifier {
|
||||||
|
|
||||||
|
private long[] serverIds;
|
||||||
|
|
||||||
|
public RootCommandIdentifier(
|
||||||
|
InteractionIdentifier parent,
|
||||||
|
String name,
|
||||||
|
String description,
|
||||||
|
InteractionType type,
|
||||||
|
SlashCommandOption[] options,
|
||||||
|
long[] serverIds) {
|
||||||
|
super(parent, name, description, type, options);
|
||||||
|
this.serverIds = serverIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long[] serverIds() {
|
||||||
|
return serverIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder {
|
||||||
|
private InteractionIdentifier parent;
|
||||||
|
private String name;
|
||||||
|
private String description;
|
||||||
|
private SlashCommandOption[] options;
|
||||||
|
private long[] serverIds;
|
||||||
|
private String[] autocompleteRef;
|
||||||
|
|
||||||
|
public InteractionIdentifier parent() {
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder parent(InteractionIdentifier parent) {
|
||||||
|
this.parent = parent;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String name() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder name(String name) {
|
||||||
|
this.name = name;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String description() {
|
||||||
|
return this.description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder description(String description) {
|
||||||
|
this.description = description;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SlashCommandOption[] options() {
|
||||||
|
return this.options;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder options(SlashCommandOption[] options) {
|
||||||
|
this.options = options;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long[] serverIds() {
|
||||||
|
return this.serverIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder serverIds(long[] serverIds) {
|
||||||
|
this.serverIds = serverIds;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] autocompleteRef() {
|
||||||
|
return this.autocompleteRef;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder autocompleteRef(String[] autocompleteRef) {
|
||||||
|
this.autocompleteRef = autocompleteRef;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SlashCommandIdentifier build() {
|
||||||
|
return new RootCommandIdentifier(
|
||||||
|
parent,
|
||||||
|
name,
|
||||||
|
description,
|
||||||
|
InteractionType.COMMAND,
|
||||||
|
options,
|
||||||
|
serverIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,87 @@
|
|||||||
|
package net.tomatentum.marinara.interaction.ident;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
||||||
|
|
||||||
|
public class SlashCommandIdentifier extends InteractionIdentifier {
|
||||||
|
|
||||||
|
private SlashCommandOption[] options;
|
||||||
|
|
||||||
|
protected SlashCommandIdentifier(
|
||||||
|
InteractionIdentifier parent,
|
||||||
|
String name,
|
||||||
|
String description,
|
||||||
|
InteractionType type,
|
||||||
|
SlashCommandOption[] options) {
|
||||||
|
super(parent, name, description, type);
|
||||||
|
this.options = options;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SlashCommandOption[] options() {
|
||||||
|
return this.options;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder {
|
||||||
|
private InteractionIdentifier parent;
|
||||||
|
private String name;
|
||||||
|
private String description;
|
||||||
|
private SlashCommandOption[] options;
|
||||||
|
private String[] autocompleteRef;
|
||||||
|
|
||||||
|
public InteractionIdentifier parent() {
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder parent(InteractionIdentifier parent) {
|
||||||
|
this.parent = parent;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String name() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder name(String name) {
|
||||||
|
this.name = name;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String description() {
|
||||||
|
return this.description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder description(String description) {
|
||||||
|
this.description = description;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SlashCommandOption[] options() {
|
||||||
|
return this.options;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder options(SlashCommandOption[] options) {
|
||||||
|
this.options = options;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] autocompleteRef() {
|
||||||
|
return this.autocompleteRef;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder autocompleteRef(String[] autocompleteRef) {
|
||||||
|
this.autocompleteRef = autocompleteRef;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SlashCommandIdentifier build() {
|
||||||
|
return new SlashCommandIdentifier(
|
||||||
|
parent,
|
||||||
|
name,
|
||||||
|
description,
|
||||||
|
InteractionType.COMMAND,
|
||||||
|
options);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -1,51 +1,98 @@
|
|||||||
package net.tomatentum.marinara.interaction.methods;
|
package net.tomatentum.marinara.interaction.methods;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import net.tomatentum.marinara.Marinara;
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.cutin.ReflectedMethodFactory.ParserResults;
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
|
import net.tomatentum.marinara.checks.AppliedCheck;
|
||||||
|
import net.tomatentum.marinara.checks.CheckExecutionContext;
|
||||||
|
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.commands.ExecutableSlashCommandDefinition;
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
import net.tomatentum.marinara.parser.AutocompleteParser;
|
||||||
import net.tomatentum.marinara.parser.SlashCommandParser;
|
import net.tomatentum.marinara.parser.InteractionCheckParser;
|
||||||
|
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||||
|
|
||||||
public class AutoCompleteInteractionMethod extends InteractionMethod {
|
public class AutoCompleteInteractionMethod extends InteractionMethod {
|
||||||
|
|
||||||
private ExecutableSlashCommandDefinition commandDefinition;
|
private String autocompleteRef;
|
||||||
|
private ContextObjectProvider cop;
|
||||||
|
|
||||||
public AutoCompleteInteractionMethod(Method method,
|
private AutoCompleteInteractionMethod(
|
||||||
|
Method method,
|
||||||
InteractionHandler handler,
|
InteractionHandler handler,
|
||||||
Marinara marinara
|
List<AppliedCheck> appliedChecks,
|
||||||
|
String autocompleteRef,
|
||||||
|
ContextObjectProvider cop
|
||||||
) {
|
) {
|
||||||
super(method, handler, marinara);
|
super(method, handler, appliedChecks);
|
||||||
}
|
this.autocompleteRef = autocompleteRef;
|
||||||
|
this.cop = cop;
|
||||||
@Override
|
|
||||||
public AnnotationParser[] getParsers() {
|
|
||||||
return new AnnotationParser[] {
|
|
||||||
new SlashCommandParser(method, (x) -> { this.commandDefinition = x; } )
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getParameter(Object context, int index) {
|
public Object getParameter(Object context, int index) {
|
||||||
Class<?> type = getMethod().getParameterTypes()[index+1];
|
Class<?> type = method().getParameterTypes()[index];
|
||||||
Object autocompleteOptionValue = marinara.getWrapper().getContextObjectProvider().getAutocompleteFocusedOption(context);
|
Object contextObject = this.cop.getInteractionContextObject(context, type);
|
||||||
if (autocompleteOptionValue != null)
|
if (contextObject != null)
|
||||||
|
return contextObject;
|
||||||
|
|
||||||
|
Object autocompleteOptionValue = this.cop.getAutocompleteFocusedOption(context).input();
|
||||||
|
if (type.isInstance(autocompleteOptionValue))
|
||||||
return autocompleteOptionValue;
|
return autocompleteOptionValue;
|
||||||
|
|
||||||
return marinara.getWrapper().getContextObjectProvider().getComponentContextObject(context, type);
|
return super.getParameter(context, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canRun(Object context) {
|
public InteractionIdentifier identifier() {
|
||||||
ExecutableSlashCommandDefinition other = marinara.getWrapper().getCommandDefinition(context);
|
return InteractionIdentifier.builder()
|
||||||
return commandDefinition.equals(other);
|
.type(InteractionType.AUTOCOMPLETE)
|
||||||
|
.name(autocompleteRef)
|
||||||
|
.description("AUTOCOMPLETE")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Factory extends InteractionMethod.Factory {
|
||||||
|
|
||||||
|
private ContextObjectProvider cop;
|
||||||
|
|
||||||
|
public Factory(MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer, ContextObjectProvider cop) {
|
||||||
|
super(checkContainer);
|
||||||
|
this.cop = cop;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InteractionType getType() {
|
public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject, ParserResults parserResults) {
|
||||||
return InteractionType.AUTOCOMPLETE;
|
if (!(containingObject instanceof InteractionHandler)) return Optional.empty();
|
||||||
|
String[] autocompletes = parserResults.get(AutocompleteParser.class);
|
||||||
|
if (autocompletes.length <= 0) return Optional.empty();
|
||||||
|
|
||||||
|
return Optional.of(new AutoCompleteInteractionMethod(
|
||||||
|
method,
|
||||||
|
(InteractionHandler) containingObject,
|
||||||
|
parserResults.get(InteractionCheckParser.class),
|
||||||
|
autocompletes[0],
|
||||||
|
cop
|
||||||
|
));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addParser(Set<MethodParser> parser) {
|
||||||
|
super.addParser(parser);
|
||||||
|
|
||||||
|
parser.add(
|
||||||
|
new AutocompleteParser()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,42 +0,0 @@
|
|||||||
package net.tomatentum.marinara.interaction.methods;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
|
|
||||||
import net.tomatentum.marinara.Marinara;
|
|
||||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
|
||||||
import net.tomatentum.marinara.interaction.InteractionType;
|
|
||||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
|
||||||
import net.tomatentum.marinara.parser.ButtonParser;
|
|
||||||
|
|
||||||
public class ButtonInteractionMethod extends InteractionMethod {
|
|
||||||
|
|
||||||
private String customId;
|
|
||||||
|
|
||||||
ButtonInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) {
|
|
||||||
super(method, handler, marinara);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public AnnotationParser[] getParsers() {
|
|
||||||
return new AnnotationParser[] {
|
|
||||||
new ButtonParser(method, (x) -> { this.customId = x; } )
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object getParameter(Object context, int index) {
|
|
||||||
Class<?> type = getMethod().getParameterTypes()[index+1];
|
|
||||||
return marinara.getWrapper().getContextObjectProvider().getComponentContextObject(context, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean canRun(Object context) {
|
|
||||||
return marinara.getWrapper().getButtonId(context).equals(customId);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public InteractionType getType() {
|
|
||||||
return InteractionType.BUTTON;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,106 +1,72 @@
|
|||||||
package net.tomatentum.marinara.interaction.methods;
|
package net.tomatentum.marinara.interaction.methods;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.security.InvalidParameterException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import org.apache.logging.log4j.Logger;
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.cutin.ReflectedMethodFactory;
|
||||||
import net.tomatentum.marinara.Marinara;
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
import net.tomatentum.marinara.checks.AppliedCheck;
|
import net.tomatentum.marinara.checks.AppliedCheck;
|
||||||
|
import net.tomatentum.marinara.checks.CheckExecutionContext;
|
||||||
|
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.ident.InteractionIdentifier;
|
||||||
import net.tomatentum.marinara.interaction.annotation.AutoComplete;
|
|
||||||
import net.tomatentum.marinara.interaction.annotation.Button;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
|
|
||||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
|
||||||
import net.tomatentum.marinara.parser.InteractionCheckParser;
|
import net.tomatentum.marinara.parser.InteractionCheckParser;
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
|
||||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
|
||||||
|
|
||||||
public abstract class InteractionMethod {
|
public abstract class InteractionMethod extends ReflectedMethod<InteractionIdentifier, Object> {
|
||||||
|
|
||||||
public static InteractionMethod create(Method method, InteractionHandler handler, Marinara marinara) {
|
protected List<AppliedCheck> appliedChecks;
|
||||||
if (method.isAnnotationPresent(AutoComplete.class))
|
|
||||||
return new AutoCompleteInteractionMethod(method, handler, marinara);
|
protected InteractionMethod(
|
||||||
if (method.isAnnotationPresent(SlashCommand.class) || method.isAnnotationPresent(SubCommand.class))
|
Method method,
|
||||||
return new SlashCommandInteractionMethod(method, handler, marinara);
|
InteractionHandler handler,
|
||||||
if (method.isAnnotationPresent(Button.class))
|
List<AppliedCheck> appliedChecks
|
||||||
return new ButtonInteractionMethod(method, handler, marinara);
|
) {
|
||||||
|
super(method, handler);
|
||||||
|
this.appliedChecks = appliedChecks;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getParameter(Object context, int index) {
|
||||||
|
if (index == 0)
|
||||||
|
return context;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Method method;
|
@Override
|
||||||
protected InteractionHandler handler;
|
public Object run(Object context) {
|
||||||
protected Marinara marinara;
|
Object result = null;
|
||||||
protected List<AnnotationParser> parsers;
|
|
||||||
protected List<AppliedCheck> appliedChecks;
|
|
||||||
|
|
||||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
|
||||||
|
|
||||||
protected InteractionMethod(Method method,
|
|
||||||
InteractionHandler handler,
|
|
||||||
Marinara marinara
|
|
||||||
) {
|
|
||||||
if (!Arrays.asList(handler.getClass().getMethods()).contains(method))
|
|
||||||
throw new InvalidParameterException("Method does not apply to specified handler");
|
|
||||||
|
|
||||||
this.method = method;
|
|
||||||
this.handler = handler;
|
|
||||||
this.marinara = marinara;
|
|
||||||
this.parsers = new ArrayList<>(Arrays.asList(getParsers()));
|
|
||||||
this.appliedChecks = new ArrayList<>();
|
|
||||||
|
|
||||||
parsers.add(new InteractionCheckParser(method, appliedChecks::add, marinara.getCheckRegistry()));
|
|
||||||
|
|
||||||
parsers.stream().forEach(AnnotationParser::parse);
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract AnnotationParser[] getParsers();
|
|
||||||
|
|
||||||
public abstract Object getParameter(Object context, int index);
|
|
||||||
|
|
||||||
public abstract boolean canRun(Object context);
|
|
||||||
|
|
||||||
public abstract InteractionType getType();
|
|
||||||
|
|
||||||
public void run(Object context) {
|
|
||||||
if (this.appliedChecks.stream().filter(x -> !x.pre(context)).count() > 0)
|
if (this.appliedChecks.stream().filter(x -> !x.pre(context)).count() > 0)
|
||||||
return;
|
return null;
|
||||||
|
|
||||||
method.setAccessible(true);
|
result = super.run(context);
|
||||||
try {
|
|
||||||
method.invoke(handler, getParameters(context));
|
|
||||||
}catch (IllegalAccessException | InvocationTargetException ex) {
|
|
||||||
logger.fatal(ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.appliedChecks.forEach(x -> x.post(context));
|
this.appliedChecks.forEach(x -> x.post(context));
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Method getMethod() {
|
public List<AppliedCheck> appliedChecks() {
|
||||||
return method;
|
return this.appliedChecks;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object[] getParameters(Object context) {
|
public abstract static class Factory implements ReflectedMethodFactory.Factory<InteractionIdentifier, Object> {
|
||||||
int parameterCount = method.getParameterCount();
|
|
||||||
List<Object> parameters = new ArrayList<>();
|
|
||||||
|
|
||||||
for (int i = 0; i < parameterCount; i++) {
|
private MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer;
|
||||||
Object parameter;
|
|
||||||
if (i == 0) {
|
|
||||||
parameter = context;
|
|
||||||
}else
|
|
||||||
parameter = getParameter(context, i-1);
|
|
||||||
|
|
||||||
logger.trace("Found parameter {}={} for method {}", parameter != null ? parameter.getClass().toString() : " ", parameter, ReflectionUtil.getFullMethodName(method));
|
protected Factory(MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer) {
|
||||||
parameters.add(parameter);
|
this.checkContainer = checkContainer;
|
||||||
}
|
}
|
||||||
return parameters.toArray();
|
|
||||||
|
@Override
|
||||||
|
public void addParser(Set<MethodParser> parser) {
|
||||||
|
parser.add(
|
||||||
|
new InteractionCheckParser(this.checkContainer)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,51 +1,87 @@
|
|||||||
package net.tomatentum.marinara.interaction.methods;
|
package net.tomatentum.marinara.interaction.methods;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import net.tomatentum.marinara.Marinara;
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.cutin.ReflectedMethodFactory.ParserResults;
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
|
import net.tomatentum.cutin.method.ReflectedMethod;
|
||||||
|
import net.tomatentum.marinara.checks.AppliedCheck;
|
||||||
|
import net.tomatentum.marinara.checks.CheckExecutionContext;
|
||||||
|
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.ident.InteractionIdentifier;
|
||||||
import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition;
|
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
|
||||||
import net.tomatentum.marinara.parser.AnnotationParser;
|
import net.tomatentum.marinara.parser.InteractionCheckParser;
|
||||||
import net.tomatentum.marinara.parser.SlashCommandParser;
|
import net.tomatentum.marinara.parser.SlashCommandParser;
|
||||||
|
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||||
|
|
||||||
public class SlashCommandInteractionMethod extends InteractionMethod {
|
public class SlashCommandInteractionMethod extends InteractionMethod {
|
||||||
|
|
||||||
private ExecutableSlashCommandDefinition commandDefinition;
|
private SlashCommandIdentifier identifier;
|
||||||
|
private ContextObjectProvider cop;
|
||||||
|
|
||||||
SlashCommandInteractionMethod(Method method, InteractionHandler handler, Marinara marinara) {
|
private SlashCommandInteractionMethod(
|
||||||
super(method, handler, marinara);
|
Method method,
|
||||||
}
|
InteractionHandler handler,
|
||||||
|
List<AppliedCheck> appliedChecks,
|
||||||
@Override
|
SlashCommandIdentifier identifier,
|
||||||
public AnnotationParser[] getParsers() {
|
ContextObjectProvider cop
|
||||||
return new AnnotationParser[] {
|
) {
|
||||||
new SlashCommandParser(method, (x) -> { this.commandDefinition = x; } )
|
super(method, handler, appliedChecks);
|
||||||
};
|
this.identifier = identifier;
|
||||||
|
this.cop = cop;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getParameter(Object context, int index) {
|
public Object getParameter(Object context, int index) {
|
||||||
return marinara.getWrapper().getContextObjectProvider().convertCommandOption(context, commandDefinition.options()[index].name());
|
Object superResult = super.getParameter(context, index);
|
||||||
|
if (superResult == null)
|
||||||
|
return this.cop.convertCommandOption(context, identifier.options()[index-1].name());
|
||||||
|
else
|
||||||
|
return superResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canRun(Object context) {
|
public InteractionIdentifier identifier() {
|
||||||
ExecutableSlashCommandDefinition other = marinara.getWrapper().getCommandDefinition(context);
|
return identifier;
|
||||||
return commandDefinition.equals(other);
|
}
|
||||||
|
|
||||||
|
public static class Factory extends InteractionMethod.Factory {
|
||||||
|
|
||||||
|
private ContextObjectProvider cop;
|
||||||
|
|
||||||
|
public Factory(MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer, ContextObjectProvider cop) {
|
||||||
|
super(checkContainer);
|
||||||
|
this.cop = cop;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InteractionType getType() {
|
public Optional<ReflectedMethod<InteractionIdentifier, Object>> produce(Method method, Object containingObject, ParserResults parserResults) {
|
||||||
return InteractionType.COMMAND;
|
if (!(containingObject instanceof InteractionHandler)) return Optional.empty();
|
||||||
|
SlashCommandIdentifier ident = parserResults.get(SlashCommandParser.class);
|
||||||
|
if (ident == null) return Optional.empty();
|
||||||
|
return Optional.of(new SlashCommandInteractionMethod(
|
||||||
|
method,
|
||||||
|
(InteractionHandler) containingObject,
|
||||||
|
parserResults.get(InteractionCheckParser.class),
|
||||||
|
ident,
|
||||||
|
cop
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
public ExecutableSlashCommandDefinition getCommandDefinition() {
|
@Override
|
||||||
return commandDefinition;
|
public void addParser(Set<MethodParser> parser) {
|
||||||
|
super.addParser(parser);
|
||||||
|
|
||||||
|
parser.add(
|
||||||
|
new SlashCommandParser()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCommandDefinition(ExecutableSlashCommandDefinition commandDefinition) {
|
|
||||||
this.commandDefinition = commandDefinition;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,54 @@
|
|||||||
|
package net.tomatentum.marinara.interaction.processor;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||||
|
|
||||||
|
public class AutocompleteInteractionProcessor extends InteractionMethodProcessor {
|
||||||
|
|
||||||
|
private LibraryWrapper wrapper;
|
||||||
|
|
||||||
|
public AutocompleteInteractionProcessor(LibraryWrapper wrapper, IdentifierProvider provider) {
|
||||||
|
super(provider, Set.of(InteractionType.AUTOCOMPLETE));
|
||||||
|
this.wrapper = wrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void processInteraction(Object context, MethodContainer<InteractionIdentifier, Object> container, InteractionIdentifier identifier) {
|
||||||
|
Optional<InteractionIdentifier> oIdent = container.identifiers().stream()
|
||||||
|
.filter(i -> convertToCommandIdentifier(identifier).equals(i))
|
||||||
|
.findFirst();
|
||||||
|
if (oIdent.isPresent() && oIdent.get() instanceof SlashCommandIdentifier sIdent) {
|
||||||
|
List<String> autocompleteRefs = Arrays.asList(this.wrapper.getContextObjectProvider()
|
||||||
|
.getAutocompleteFocusedOption(context).getAutocompleteRefs(sIdent.options()));
|
||||||
|
List<Object> results = container.methods().stream()
|
||||||
|
.filter(m -> m.identifier().type().equals(InteractionType.AUTOCOMPLETE))
|
||||||
|
.filter(m -> autocompleteRefs.contains(m.identifier().name()))
|
||||||
|
.map(m -> m.run(context))
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.toList();
|
||||||
|
if (!results.isEmpty())
|
||||||
|
this.wrapper.respondAutocomplete(context, results);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private InteractionIdentifier convertToCommandIdentifier(InteractionIdentifier identifier) {
|
||||||
|
if (Objects.isNull(identifier))
|
||||||
|
return null;
|
||||||
|
return InteractionIdentifier.builder()
|
||||||
|
.type(InteractionType.COMMAND)
|
||||||
|
.name(identifier.name())
|
||||||
|
.parent(convertToCommandIdentifier(identifier.parent()))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,22 @@
|
|||||||
|
package net.tomatentum.marinara.interaction.processor;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
|
||||||
|
public class DirectInteractionProcessor extends InteractionMethodProcessor {
|
||||||
|
|
||||||
|
|
||||||
|
public DirectInteractionProcessor(IdentifierProvider provider, InteractionType... types) {
|
||||||
|
super(provider, Set.of(types));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void processInteraction(Object context, MethodContainer<InteractionIdentifier, Object> container, InteractionIdentifier identifier) {
|
||||||
|
container.findFor(identifier).forEach(m -> m.run(context));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,39 @@
|
|||||||
|
package net.tomatentum.marinara.interaction.processor;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodProcessor;
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
|
||||||
|
public abstract class InteractionMethodProcessor implements MethodProcessor<InteractionIdentifier, Object> {
|
||||||
|
|
||||||
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
|
private IdentifierProvider provider;
|
||||||
|
private Set<InteractionType> types;
|
||||||
|
|
||||||
|
protected InteractionMethodProcessor(IdentifierProvider provider, Set<InteractionType> types) {
|
||||||
|
this.provider = provider;
|
||||||
|
this.types = types;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void process(Object context, MethodContainer<InteractionIdentifier, Object> container) {
|
||||||
|
InteractionIdentifier identifier = this.provider.provide(context);
|
||||||
|
if (!this.types.contains(identifier.type())) return;
|
||||||
|
logger.debug("Processing {} : {} with context {}", identifier, identifier.type(), context);
|
||||||
|
this.processInteraction(context, container, identifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void processInteraction(
|
||||||
|
Object context,
|
||||||
|
MethodContainer<InteractionIdentifier, Object> container,
|
||||||
|
InteractionIdentifier identifier);
|
||||||
|
|
||||||
|
}
|
@@ -1,8 +0,0 @@
|
|||||||
package net.tomatentum.marinara.parser;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
|
|
||||||
public interface AnnotationParser {
|
|
||||||
void parse();
|
|
||||||
Method getMethod();
|
|
||||||
}
|
|
@@ -0,0 +1,27 @@
|
|||||||
|
package net.tomatentum.marinara.parser;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||||
|
import net.tomatentum.marinara.interaction.annotation.AutoComplete;
|
||||||
|
|
||||||
|
public class AutocompleteParser implements MethodParser {
|
||||||
|
|
||||||
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object parse(Method method, Object containingObject) {
|
||||||
|
String[] autoCompletes = Arrays.stream(method.getAnnotationsByType(AutoComplete.class))
|
||||||
|
.map(AutoComplete::value)
|
||||||
|
.toArray(String[]::new);
|
||||||
|
if (autoCompletes.length > 0)
|
||||||
|
logger.trace("Parsed AutoComplete annotations {} for method {}", autoCompletes, ReflectionUtil.getFullMethodName(method));
|
||||||
|
return autoCompletes;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -1,36 +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.apache.logging.log4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
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;
|
|
||||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
|
||||||
|
|
||||||
public class ButtonParser implements AnnotationParser {
|
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 = getMethod().getAnnotation(Button.class);
|
Button button = method.getAnnotation(Button.class);
|
||||||
logger.trace("Parsed Button annotation {} for method {}", button.toString(), ReflectionUtil.getFullMethodName(method));
|
if (button == null) return null;
|
||||||
this.consumer.accept(button.value());
|
logger.trace("Parsed Button annotation {} for method {}", button, ReflectionUtil.getFullMethodName(method));
|
||||||
}
|
return button.value();
|
||||||
|
|
||||||
@Override
|
|
||||||
public Method getMethod() {
|
|
||||||
return this.method;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,28 @@
|
|||||||
|
package net.tomatentum.marinara.parser;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.ParameterizedType;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import io.leangen.geantyref.GenericTypeReflector;
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.marinara.checks.InteractionCheck;
|
||||||
|
|
||||||
|
public class InteractionCheckClassParser implements MethodParser {
|
||||||
|
|
||||||
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object parse(Method method, Object containingObject) {
|
||||||
|
ParameterizedType type = (ParameterizedType) GenericTypeReflector.getExactSuperType(containingObject.getClass(), InteractionCheck.class);
|
||||||
|
if (type == null) return null;
|
||||||
|
Type typeParam = type.getActualTypeArguments().length == 1 ? type.getActualTypeArguments()[0] : null;
|
||||||
|
if (typeParam != null)
|
||||||
|
logger.trace("Parsed InteractionCheck Annotation {}", typeParam);
|
||||||
|
return typeParam;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -3,49 +3,47 @@ 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.Optional;
|
import java.util.Objects;
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.cutin.container.MethodContainer;
|
||||||
|
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||||
import net.tomatentum.marinara.checks.AppliedCheck;
|
import net.tomatentum.marinara.checks.AppliedCheck;
|
||||||
import net.tomatentum.marinara.checks.InteractionCheck;
|
import net.tomatentum.marinara.checks.CheckExecutionContext;
|
||||||
import net.tomatentum.marinara.registry.InteractionCheckRegistry;
|
import net.tomatentum.marinara.checks.CheckMethodIdentifier;
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
import net.tomatentum.marinara.checks.CheckMethodIdentifier.CheckMethodType;
|
||||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
|
||||||
|
|
||||||
public class InteractionCheckParser implements AnnotationParser {
|
public class InteractionCheckParser implements MethodParser {
|
||||||
|
|
||||||
private InteractionCheckRegistry checkRegistry;
|
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, InteractionCheckRegistry checkRegistry) {
|
public InteractionCheckParser(MethodContainer<CheckMethodIdentifier, CheckExecutionContext> checkContainer) {
|
||||||
this.checkRegistry = checkRegistry;
|
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) {
|
||||||
Optional<InteractionCheck<?>> check = this.checkRegistry.getCheckFromAnnotation(annotation.annotationType());
|
var preExec = this.checkContainer.findFirstFor(new CheckMethodIdentifier(annotation.annotationType(), CheckMethodType.PRE));
|
||||||
if (check.isPresent()) {
|
var postExec = this.checkContainer.findFirstFor(new CheckMethodIdentifier(annotation.annotationType(), CheckMethodType.POST));
|
||||||
AppliedCheck appliedCheck = new AppliedCheck(check.get(), annotation);
|
if (preExec.isPresent() && postExec.isPresent()) {
|
||||||
logger.trace("Parsed InteractionCheck {} for annotation {} for method {}", check.getClass().getName(), annotation.toString(), ReflectionUtil.getFullMethodName(method));
|
AppliedCheck appliedCheck = new AppliedCheck(annotation, preExec.get(), postExec.get());
|
||||||
consumer.accept(appliedCheck);
|
logger.trace("Parsed InteractionCheck {} for annotation {} for method {}", preExec.get().containingObject(), annotation, ReflectionUtil.getFullMethodName(method));
|
||||||
|
return appliedCheck;
|
||||||
}
|
}
|
||||||
}
|
return null;
|
||||||
|
|
||||||
@Override
|
|
||||||
public Method getMethod() {
|
|
||||||
return this.method;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,70 +1,73 @@
|
|||||||
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.apache.logging.log4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition;
|
import net.tomatentum.cutin.MethodParser;
|
||||||
|
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
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.util.LoggerUtil;
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
import net.tomatentum.marinara.util.ReflectionUtil;
|
|
||||||
|
|
||||||
public class SlashCommandParser implements AnnotationParser {
|
public class SlashCommandParser implements MethodParser {
|
||||||
|
|
||||||
private Method method;
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
private Consumer<ExecutableSlashCommandDefinition> consumer;
|
|
||||||
|
|
||||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
public SlashCommandParser() {
|
||||||
|
|
||||||
public SlashCommandParser(Method method, Consumer<ExecutableSlashCommandDefinition> 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);
|
||||||
ExecutableSlashCommandDefinition.Builder builder = new ExecutableSlashCommandDefinition.Builder();
|
InteractionIdentifier lastIdentifier = InteractionIdentifier.rootBuilder()
|
||||||
builder.setApplicationCommand(cmd);
|
.name(cmd.name())
|
||||||
|
.description(cmd.description())
|
||||||
|
.options(cmd.options())
|
||||||
|
.serverIds(cmd.serverIds())
|
||||||
|
.build();
|
||||||
|
|
||||||
if (ReflectionUtil.isAnnotationPresent(method, SubCommandGroup.class)) {
|
if (ReflectionUtil.isAnnotationPresent(method, SubCommandGroup.class)) {
|
||||||
SubCommandGroup cmdGroup = ReflectionUtil.getAnnotation(method, SubCommandGroup.class);
|
SubCommandGroup cmdGroup = ReflectionUtil.getAnnotation(method, SubCommandGroup.class);
|
||||||
builder.setSubCommandGroup(cmdGroup);
|
lastIdentifier = InteractionIdentifier.builder()
|
||||||
|
.name(cmdGroup.name())
|
||||||
|
.description(cmdGroup.description())
|
||||||
|
.type(InteractionType.COMMAND)
|
||||||
|
.parent(lastIdentifier)
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ReflectionUtil.isAnnotationPresent(method, SubCommand.class)) {
|
if (ReflectionUtil.isAnnotationPresent(method, SubCommand.class)) {
|
||||||
SubCommand subCmd = ReflectionUtil.getAnnotation(method, SubCommand.class);
|
SubCommand subCmd = ReflectionUtil.getAnnotation(method, SubCommand.class);
|
||||||
builder.setSubCommand(subCmd);
|
lastIdentifier = InteractionIdentifier.slashBuilder()
|
||||||
|
.name(subCmd.name())
|
||||||
|
.description(subCmd.description())
|
||||||
|
.options(subCmd.options())
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
ExecutableSlashCommandDefinition def = builder.build();
|
logger.trace("Parsed using SlashCommandParser for method {} with the result: {}", ReflectionUtil.getFullMethodName(method), lastIdentifier);
|
||||||
|
return lastIdentifier;
|
||||||
logger.trace("Parsed using SlashCommandParser for method {} with the result:\n{}", ReflectionUtil.getFullMethodName(method), def.toString());
|
|
||||||
consumer.accept(builder.build());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Method getMethod() {
|
|
||||||
return this.method;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,40 +0,0 @@
|
|||||||
package net.tomatentum.marinara.registry;
|
|
||||||
|
|
||||||
import java.lang.reflect.ParameterizedType;
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
|
|
||||||
import io.leangen.geantyref.GenericTypeReflector;
|
|
||||||
import net.tomatentum.marinara.checks.InteractionCheck;
|
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
|
||||||
|
|
||||||
public class InteractionCheckRegistry {
|
|
||||||
|
|
||||||
private List<InteractionCheck<?>> checks;
|
|
||||||
|
|
||||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
|
||||||
|
|
||||||
public InteractionCheckRegistry() {
|
|
||||||
this.checks = new ArrayList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addCheck(InteractionCheck<?> check) {
|
|
||||||
checks.add(check);
|
|
||||||
logger.info("Registered Check {}", check.getClass().getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<InteractionCheck<?>> getCheckFromAnnotation(Type annotation) {
|
|
||||||
for (InteractionCheck<?> interactionCheck : checks) {
|
|
||||||
ParameterizedType type = (ParameterizedType) GenericTypeReflector.getExactSuperType(interactionCheck.getClass(), InteractionCheck.class);
|
|
||||||
Type typeParam = type.getActualTypeArguments()[0];
|
|
||||||
if (typeParam.equals(annotation))
|
|
||||||
return Optional.of(interactionCheck);
|
|
||||||
}
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,78 +0,0 @@
|
|||||||
package net.tomatentum.marinara.registry;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
|
|
||||||
import net.tomatentum.marinara.Marinara;
|
|
||||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
|
||||||
import net.tomatentum.marinara.interaction.InteractionType;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition;
|
|
||||||
import net.tomatentum.marinara.interaction.methods.SlashCommandInteractionMethod;
|
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
|
||||||
import net.tomatentum.marinara.interaction.methods.InteractionMethod;
|
|
||||||
|
|
||||||
public class InteractionRegistry {
|
|
||||||
private Logger logger = LoggerUtil.getLogger(getClass());
|
|
||||||
private List<InteractionMethod> interactionMethods;
|
|
||||||
private Marinara marinara;
|
|
||||||
|
|
||||||
public InteractionRegistry(Marinara marinara) {
|
|
||||||
this.interactionMethods = new ArrayList<>();
|
|
||||||
this.marinara = marinara;
|
|
||||||
marinara.getWrapper().subscribeInteractions(this::handle);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addInteractions(InteractionHandler interactionHandler) {
|
|
||||||
for (Method method : interactionHandler.getClass().getMethods()) {
|
|
||||||
InteractionMethod iMethod = InteractionMethod.create(method, interactionHandler, marinara);
|
|
||||||
if (iMethod != null) {
|
|
||||||
this.interactionMethods.add(iMethod);
|
|
||||||
logger.debug("Added {} method from {}", iMethod.getMethod().getName(), interactionHandler.getClass().getSimpleName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
logger.info("Added all Interactions from {}", interactionHandler.getClass().getSimpleName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void registerCommands() {
|
|
||||||
List<SlashCommandDefinition> defs = new ArrayList<>();
|
|
||||||
List<ExecutableSlashCommandDefinition> execDefs = interactionMethods.stream()
|
|
||||||
.filter((x) -> x.getClass().isAssignableFrom(SlashCommandInteractionMethod.class))
|
|
||||||
.map((x) -> ((SlashCommandInteractionMethod)x).getCommandDefinition())
|
|
||||||
.toList();
|
|
||||||
|
|
||||||
execDefs.forEach((def) -> {
|
|
||||||
Optional<SlashCommandDefinition> appDef = defs.stream()
|
|
||||||
.filter((x) -> x.getSlashCommand().equals(def.applicationCommand()))
|
|
||||||
.findFirst();
|
|
||||||
if (appDef.isPresent())
|
|
||||||
appDef.get().addExecutableCommand(def);
|
|
||||||
else
|
|
||||||
defs.add(new SlashCommandDefinition(def.applicationCommand()).addExecutableCommand(def));
|
|
||||||
|
|
||||||
logger.debug("Added Executable Command {}{}{} for registration",
|
|
||||||
def.applicationCommand().name(),
|
|
||||||
def.subCommandGroup() == null ? "" : "." + def.subCommandGroup().name(),
|
|
||||||
def.subCommand() == null ? "" : "." + def.subCommand().name()
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
marinara.getWrapper().registerSlashCommands(defs.toArray(SlashCommandDefinition[]::new));
|
|
||||||
logger.info("Registered all SlashCommands");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void handle(Object context) {
|
|
||||||
InteractionType type = marinara.getWrapper().getInteractionType(context);
|
|
||||||
logger.debug("Received {} interaction ", context);
|
|
||||||
interactionMethods.forEach((m) -> {
|
|
||||||
if (m.getType().equals(type) && m.canRun(context)) {
|
|
||||||
logger.info("Running {} interaction using {}\ncontext: {}", type, m.getMethod().toString(), context.toString());
|
|
||||||
m.run(context);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@@ -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();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -1,24 +0,0 @@
|
|||||||
package net.tomatentum.marinara.util;
|
|
||||||
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
import org.apache.logging.log4j.Level;
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
import org.apache.logging.log4j.simple.SimpleLogger;
|
|
||||||
import org.apache.logging.log4j.util.PropertiesUtil;
|
|
||||||
import org.apache.logging.log4j.util.ProviderUtil;
|
|
||||||
|
|
||||||
public class LoggerUtil {
|
|
||||||
public static Logger getLogger(String name) {
|
|
||||||
if (ProviderUtil.hasProviders()) {
|
|
||||||
return LogManager.getLogger(name);
|
|
||||||
}else
|
|
||||||
return new SimpleLogger(name, Level.DEBUG, true, false, true, true, "yyyy-MM-dd HH:mm:ss.SSSZ", null,
|
|
||||||
new PropertiesUtil(new Properties()), System.out);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Logger getLogger(Class<?> clazz) {
|
|
||||||
return getLogger(clazz.getName());
|
|
||||||
}
|
|
||||||
}
|
|
@@ -0,0 +1,46 @@
|
|||||||
|
package net.tomatentum.marinara.util;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public class ObjectAggregator<O, K, V> {
|
||||||
|
private Function<O, Iterable<K>> keySupplier;
|
||||||
|
private BiConsumer<V, O> valueConsumer;
|
||||||
|
private Function<K, V> defaultGenerator;
|
||||||
|
|
||||||
|
public ObjectAggregator(
|
||||||
|
Function<O, Iterable<K>> keySupplier,
|
||||||
|
BiConsumer<V, O> valueConsumer,
|
||||||
|
Function<K, V> defaultGenerator) {
|
||||||
|
this.keySupplier = keySupplier;
|
||||||
|
this.valueConsumer = valueConsumer;
|
||||||
|
this.defaultGenerator = defaultGenerator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObjectAggregator(
|
||||||
|
Function<O, Iterable<K>> keySupplier,
|
||||||
|
BiConsumer<V, O> valueConsumer,
|
||||||
|
Supplier<V> defaultGenerator) {
|
||||||
|
this.keySupplier = keySupplier;
|
||||||
|
this.valueConsumer = valueConsumer;
|
||||||
|
this.defaultGenerator = _ -> defaultGenerator.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<V> aggregate(Iterable<O> iterator) {
|
||||||
|
Map<K, V> map = new HashMap<>();
|
||||||
|
for (O element : iterator) {
|
||||||
|
Iterable<K> keys = this.keySupplier.apply(element);
|
||||||
|
for (K key : keys) {
|
||||||
|
V value = map.getOrDefault(key, this.defaultGenerator.apply(key));
|
||||||
|
this.valueConsumer.accept(value, element);
|
||||||
|
map.putIfAbsent(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return map.values();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,15 @@
|
|||||||
|
package net.tomatentum.marinara.util;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
public class ObjectListAggregator<O, K, V> extends ObjectAggregator<O, K, ArrayList<V>> {
|
||||||
|
|
||||||
|
public ObjectListAggregator(Function<O, Iterable<K>> keySupplier, Function<O, V> valueConsumer) {
|
||||||
|
super(keySupplier,
|
||||||
|
(l, o) -> l.add(valueConsumer.apply(o)),
|
||||||
|
() -> new ArrayList<>());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@@ -1,107 +0,0 @@
|
|||||||
package net.tomatentum.marinara.util;
|
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
public final class ReflectionUtil {
|
|
||||||
|
|
||||||
public static <T extends Annotation> boolean isAnnotationPresent(Method method, Class<T> annotationClass) {
|
|
||||||
if (method.isAnnotationPresent(annotationClass) || method.getDeclaringClass().isAnnotationPresent(annotationClass))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T extends Annotation> T getAnnotation(Method method, Class<T> annotationClass) {
|
|
||||||
return method.isAnnotationPresent(annotationClass) ?
|
|
||||||
method.getAnnotation(annotationClass) :
|
|
||||||
method.getDeclaringClass().getAnnotation(annotationClass);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int getCastDepth(Class<?> child, Class<?> parent) {
|
|
||||||
|
|
||||||
if (parent.equals(Object.class))
|
|
||||||
return Integer.MAX_VALUE;
|
|
||||||
|
|
||||||
if (!parent.isAssignableFrom(child)) {
|
|
||||||
throw new IllegalArgumentException("The specified class is not a child class of the specified parent.");
|
|
||||||
}
|
|
||||||
|
|
||||||
int depth = 0;
|
|
||||||
Class<?> curr = child;
|
|
||||||
List<Class<?>> parents = new ArrayList<>();
|
|
||||||
|
|
||||||
while (!curr.equals(parent)) {
|
|
||||||
depth++;
|
|
||||||
parents.add(curr.getSuperclass());
|
|
||||||
parents.addAll(Arrays.asList(curr.getInterfaces()));
|
|
||||||
|
|
||||||
for (Class<?> currParent : parents) {
|
|
||||||
if (currParent != null && parent.isAssignableFrom(currParent)) {
|
|
||||||
curr = currParent;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
parents.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
return depth;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Method getMostSpecificMethod(Method[] methods, Class<?>... parameters) {
|
|
||||||
List<Method> compatibleMethods = Arrays.stream(methods)
|
|
||||||
.filter(x -> isMethodCallable(x, parameters))
|
|
||||||
.toList();
|
|
||||||
|
|
||||||
if (compatibleMethods.size() == 0)
|
|
||||||
throw new IllegalArgumentException("There are no compatible Methods provided");
|
|
||||||
|
|
||||||
for (int i = 0; i < parameters.length; i++) {
|
|
||||||
final int currI = i;
|
|
||||||
Class<?>[] parameterTypes = compatibleMethods.stream()
|
|
||||||
.map(x -> x.getParameterTypes()[currI])
|
|
||||||
.toArray(x -> new Class[x]);
|
|
||||||
|
|
||||||
Class<?> mostSpecific = getMostSpecificClass(parameterTypes, parameters[i]);
|
|
||||||
|
|
||||||
compatibleMethods = compatibleMethods.stream()
|
|
||||||
.filter(x -> Objects.equals(x.getParameterTypes()[currI], mostSpecific))
|
|
||||||
.toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
return compatibleMethods.getFirst();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Class<?> getMostSpecificClass(Class<?>[] classes, Class<?> base) {
|
|
||||||
int min = Integer.MAX_VALUE;
|
|
||||||
Class<?> currMostSpecific = null;
|
|
||||||
for (Class<?> currClass : classes) {
|
|
||||||
int currCastDepth = getCastDepth(base, currClass);
|
|
||||||
if (currCastDepth <= min) {
|
|
||||||
min = currCastDepth;
|
|
||||||
currMostSpecific = currClass;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return currMostSpecific;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isMethodCallable(Method method, Class<?>... parameters) {
|
|
||||||
if (!Objects.equals(method.getParameterCount(), parameters.length))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
Class<?>[] methodParams = method.getParameterTypes();
|
|
||||||
for (int i = 0; i < parameters.length; i++) {
|
|
||||||
if (!methodParams[i].isAssignableFrom(parameters[i]))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getFullMethodName(Method method) {
|
|
||||||
return method.getClass().getName() + "." + method.getName();
|
|
||||||
}
|
|
||||||
}
|
|
@@ -0,0 +1,70 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
||||||
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
||||||
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOptionChoice;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.RootCommandIdentifier;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.SlashCommandIdentifier;
|
||||||
|
|
||||||
|
public class CommandConverter<A extends Object, O extends Object, C extends Object> {
|
||||||
|
|
||||||
|
public static <A, O, C> CommandConverter<A, O, C> of(Spec<A, O, C> spec) {
|
||||||
|
return new CommandConverter<>(spec);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
|
private Spec<A, O, C> spec;
|
||||||
|
|
||||||
|
CommandConverter(Spec<A, O, C> spec) {
|
||||||
|
this.spec = spec;
|
||||||
|
}
|
||||||
|
|
||||||
|
public A convert(SlashCommandDefinition def) {
|
||||||
|
logger.debug("Converting command {}", def);
|
||||||
|
List<O> options = new ArrayList<>();
|
||||||
|
if (!def.isRootCommand()) {
|
||||||
|
Arrays.stream(def.getSubCommands()).map(this::convertSubCommand).forEach(options::add);
|
||||||
|
Arrays.stream(def.getSubCommandGroups()).map(x -> this.convertSubCommandGroup(def, x)).forEach(options::add);
|
||||||
|
}else
|
||||||
|
Arrays.stream(def.rootIdentifier().options()).map(this::convertOption).forEach(options::add);
|
||||||
|
|
||||||
|
return spec.convertCommand(def.rootIdentifier(), options);
|
||||||
|
}
|
||||||
|
|
||||||
|
private O convertSubCommandGroup(SlashCommandDefinition def, InteractionIdentifier identifier) {
|
||||||
|
logger.debug("Converting subCommandGroup {} of {}", identifier, def);
|
||||||
|
SlashCommandIdentifier[] subCommands = def.getSubCommands(identifier.name());
|
||||||
|
List<O> convertedSubCommands = Arrays.stream(subCommands).map(this::convertSubCommand).toList();
|
||||||
|
return spec.convertSubCommandGroup(identifier, convertedSubCommands);
|
||||||
|
}
|
||||||
|
|
||||||
|
private O convertSubCommand(SlashCommandIdentifier identifier) {
|
||||||
|
logger.debug("Converting subCommand {}", identifier);
|
||||||
|
List<O> options = Arrays.stream(identifier.options()).map(this::convertOption).toList();
|
||||||
|
return spec.convertSubCommand(identifier, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
private O convertOption(SlashCommandOption option) {
|
||||||
|
logger.debug("Converting option {}", option);
|
||||||
|
List<C> choices = Arrays.stream(SlashCommandDefinition.getActualChoices(option)).map(spec::convertChoice).toList();
|
||||||
|
return spec.convertOption(option, choices);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static interface Spec<A extends Object, O extends Object, C extends Object> {
|
||||||
|
|
||||||
|
public A convertCommand(RootCommandIdentifier rootIdentifier, List<O> options);
|
||||||
|
public O convertSubCommandGroup(InteractionIdentifier identifier, List<O> subCommands);
|
||||||
|
public O convertSubCommand(InteractionIdentifier identifier, List<O> options);
|
||||||
|
public O convertOption(SlashCommandOption option, List<C> choices);
|
||||||
|
public C convertChoice(SlashCommandOptionChoice choice);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,55 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
||||||
|
import net.tomatentum.marinara.util.ObjectAggregator;
|
||||||
|
|
||||||
|
public class CommandRegisterer<A extends Object> {
|
||||||
|
|
||||||
|
public static <A extends Object> CommandRegisterer<A> of(Strategy<A> strategy, CommandConverter<A, ?, ?> converter) {
|
||||||
|
return new CommandRegisterer<A>(strategy, converter);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
|
private Strategy<A> strategy;
|
||||||
|
private CommandConverter<A, ?, ?> converter;
|
||||||
|
|
||||||
|
CommandRegisterer(Strategy<A> strategy, CommandConverter<A, ?, ?> converter) {
|
||||||
|
this.strategy = strategy;
|
||||||
|
this.converter = converter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void register(SlashCommandDefinition[] slashDefs) {
|
||||||
|
Set<ServerCommandList<A>> serverCommands = new ObjectAggregator<SlashCommandDefinition, Long, ServerCommandList<A>>(
|
||||||
|
def -> Arrays.stream(def.serverIds()).boxed().toList(),
|
||||||
|
(l, o) -> {
|
||||||
|
logger.debug("Added {} for server ({}) registration.", o.rootIdentifier(), l.serverId());
|
||||||
|
l.add(converter.convert(o));
|
||||||
|
},
|
||||||
|
ServerCommandList::new)
|
||||||
|
.aggregate(Arrays.asList(slashDefs)).stream()
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
|
||||||
|
Set<A> globalCommands = Arrays.stream(slashDefs)
|
||||||
|
.filter(x -> x.serverIds().length <= 0)
|
||||||
|
.peek(c -> logger.debug("Added {} for global registration.", c.rootIdentifier()))
|
||||||
|
.map(converter::convert)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
|
||||||
|
serverCommands.forEach(strategy::registerServer);
|
||||||
|
strategy.registerGlobal(globalCommands);
|
||||||
|
logger.info("Registered all SlashCommands");
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface Strategy<A extends Object> {
|
||||||
|
void registerServer(ServerCommandList<A> commands);
|
||||||
|
void registerGlobal(Set<A> defs);
|
||||||
|
}
|
||||||
|
}
|
@@ -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);
|
||||||
|
|
||||||
|
}
|
@@ -1,5 +1,7 @@
|
|||||||
package net.tomatentum.marinara.wrapper;
|
package net.tomatentum.marinara.wrapper;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.interaction.commands.option.AutocompleteOptionData;
|
||||||
|
|
||||||
public interface ContextObjectProvider {
|
public interface ContextObjectProvider {
|
||||||
|
|
||||||
public Object convertCommandOption(Object context, String optionName);
|
public Object convertCommandOption(Object context, String optionName);
|
||||||
@@ -7,5 +9,5 @@ public interface ContextObjectProvider {
|
|||||||
public Object getComponentContextObject(Object context, Class<?> type);
|
public Object getComponentContextObject(Object context, Class<?> type);
|
||||||
public Object getInteractionContextObject(Object context, Class<?> type);
|
public Object getInteractionContextObject(Object context, Class<?> type);
|
||||||
|
|
||||||
public Object getAutocompleteFocusedOption(Object context);
|
public AutocompleteOptionData getAutocompleteFocusedOption(Object context);
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,75 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper;
|
||||||
|
|
||||||
|
import java.lang.reflect.ParameterizedType;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import io.leangen.geantyref.GenericTypeReflector;
|
||||||
|
import net.tomatentum.cutin.util.ReflectionUtil;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
|
||||||
|
public class IdentifierProvider {
|
||||||
|
|
||||||
|
public static IdentifierProvider of(Converter<?>... converter) {
|
||||||
|
return new IdentifierProvider(Arrays.asList(converter));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
|
private Map<Class<?>, Converter<?>> converter;
|
||||||
|
|
||||||
|
private IdentifierProvider(List<Converter<?>> converter) {
|
||||||
|
this.converter = new HashMap<>();
|
||||||
|
for (Converter<?> conv : converter) {
|
||||||
|
if (conv.getClass().getName().contains("$$Lambda"))
|
||||||
|
throw new IllegalArgumentException("Lambdas cannot be used for IdentifierConverter because of Type erasure.");
|
||||||
|
Type type = GenericTypeReflector.getExactSuperType(conv.getClass(), Converter.class);
|
||||||
|
Type parameterType = ((ParameterizedType) type).getActualTypeArguments()[0];
|
||||||
|
if (!(parameterType instanceof Class))
|
||||||
|
throw new IllegalArgumentException("Only full Class types are supported by IdentiferConverters");
|
||||||
|
this.converter.put((Class<?>) parameterType, conv);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public InteractionIdentifier provide(Object context) {
|
||||||
|
Type type = ReflectionUtil.getMostSpecificClass(
|
||||||
|
converter.keySet().stream().filter(x -> x.isAssignableFrom(context.getClass())).toArray(Class<?>[]::new),
|
||||||
|
context.getClass());
|
||||||
|
|
||||||
|
if (type == null)
|
||||||
|
logger.debug("No Identifier converter found for context {}", context.getClass());
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Converter<Object> conv = (Converter<Object>) converter.get(type);
|
||||||
|
|
||||||
|
InteractionIdentifier result = conv.convert(context);
|
||||||
|
logger.trace("Converted {} to {} using {}", context, result, conv);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Converter<T extends Object> {
|
||||||
|
InteractionIdentifier convert(T context);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class LambdaWrapper<T extends Object> implements Converter<T> {
|
||||||
|
|
||||||
|
private Converter<T> converter;
|
||||||
|
|
||||||
|
LambdaWrapper(Converter<T> converter) {
|
||||||
|
this.converter = converter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InteractionIdentifier convert(T context) {
|
||||||
|
return this.converter.convert(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@@ -3,21 +3,16 @@ package net.tomatentum.marinara.wrapper;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition;
|
|
||||||
import net.tomatentum.marinara.interaction.InteractionType;
|
|
||||||
|
|
||||||
public abstract class LibraryWrapper {
|
public abstract class LibraryWrapper {
|
||||||
|
|
||||||
private List<Consumer<Object>> interactionSubscriber;
|
private List<Consumer<Object>> interactionSubscriber;
|
||||||
|
|
||||||
protected LibraryWrapper() {
|
protected LibraryWrapper() {
|
||||||
interactionSubscriber = new ArrayList<>();
|
this.interactionSubscriber = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleInteraction(Object context) {
|
public void handleInteraction(Object context) {
|
||||||
interactionSubscriber.forEach((o) -> o.accept(context));
|
interactionSubscriber.forEach(o -> o.accept(context));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void subscribeInteractions(Consumer<Object> consumer) {
|
public void subscribeInteractions(Consumer<Object> consumer) {
|
||||||
@@ -27,13 +22,9 @@ public abstract class LibraryWrapper {
|
|||||||
interactionSubscriber.remove(consumer);
|
interactionSubscriber.remove(consumer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract InteractionType getInteractionType(Object context);
|
public abstract CommandRegisterer<?> getRegisterer();
|
||||||
|
public abstract IdentifierProvider createIdentifierProvider();
|
||||||
public abstract void registerSlashCommands(SlashCommandDefinition[] defs);
|
|
||||||
public abstract ExecutableSlashCommandDefinition getCommandDefinition(Object context);
|
|
||||||
|
|
||||||
public abstract String getButtonId(Object context);
|
|
||||||
|
|
||||||
public abstract ContextObjectProvider getContextObjectProvider();
|
public abstract ContextObjectProvider getContextObjectProvider();
|
||||||
|
public abstract void respondAutocomplete(Object context, List<Object> options);
|
||||||
|
|
||||||
}
|
}
|
@@ -0,0 +1,16 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
public class ServerCommandList<A> extends HashSet<A>{
|
||||||
|
|
||||||
|
private long serverId;
|
||||||
|
|
||||||
|
public ServerCommandList(long serverId) {
|
||||||
|
this.serverId = serverId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long serverId() {
|
||||||
|
return serverId;
|
||||||
|
}
|
||||||
|
}
|
@@ -10,20 +10,18 @@ plugins {
|
|||||||
`java-library`
|
`java-library`
|
||||||
}
|
}
|
||||||
|
|
||||||
repositories {
|
|
||||||
// Use Maven Central for resolving dependencies.
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
// Use JUnit Jupiter for testing.
|
// Use JUnit Jupiter for testing.
|
||||||
testImplementation(libs.junit.jupiter)
|
testImplementation(libs.junit.jupiter)
|
||||||
testImplementation(libs.mockito)
|
testImplementation(libs.mockito)
|
||||||
|
testImplementation(libs.discord4j)
|
||||||
|
|
||||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||||
implementation(libs.log4j)
|
testImplementation(libs.slf4j.simple)
|
||||||
|
|
||||||
|
implementation(libs.slf4j)
|
||||||
implementation(libs.discord4j) {
|
implementation(libs.discord4j) {
|
||||||
// exclude(module="discord4j-voice")
|
exclude(module="discord4j-voice")
|
||||||
}
|
}
|
||||||
implementation(libs.geantyref)
|
implementation(libs.geantyref)
|
||||||
implementation(project(":lib"))
|
implementation(project(":lib"))
|
||||||
|
@@ -5,7 +5,9 @@ import java.util.List;
|
|||||||
import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent;
|
import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent;
|
||||||
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
|
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
|
||||||
import discord4j.core.event.domain.interaction.ComponentInteractionEvent;
|
import discord4j.core.event.domain.interaction.ComponentInteractionEvent;
|
||||||
|
import discord4j.core.event.domain.interaction.InteractionCreateEvent;
|
||||||
import discord4j.core.object.command.ApplicationCommandInteractionOption;
|
import discord4j.core.object.command.ApplicationCommandInteractionOption;
|
||||||
|
import net.tomatentum.marinara.interaction.commands.option.AutocompleteOptionData;
|
||||||
import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType;
|
import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType;
|
||||||
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||||
|
|
||||||
@@ -79,24 +81,25 @@ public class Discord4JContextObjectProvider implements ContextObjectProvider {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getInteractionContextObject(Object context, Class<?> type) {
|
public Object getInteractionContextObject(Object context, Class<?> type) {
|
||||||
ComponentInteractionEvent componentInteractionEvent = (ComponentInteractionEvent) context;
|
InteractionCreateEvent interactionEvent = (InteractionCreateEvent) context;
|
||||||
switch (type.getName()) {
|
switch (type.getName()) {
|
||||||
case "discord4j.core.object.entity.channel.MessageChannel":
|
case "discord4j.core.object.entity.channel.MessageChannel":
|
||||||
return componentInteractionEvent.getInteraction().getChannel().block();
|
return interactionEvent.getInteraction().getChannel().block();
|
||||||
case "discord4j.core.object.entity.Guild":
|
case "discord4j.core.object.entity.Guild":
|
||||||
return componentInteractionEvent.getInteraction().getGuild().block();
|
return interactionEvent.getInteraction().getGuild().block();
|
||||||
case "discord4j.core.object.entity.Member":
|
case "discord4j.core.object.entity.Member":
|
||||||
return componentInteractionEvent.getInteraction().getMember().orElse(null);
|
return interactionEvent.getInteraction().getMember().orElse(null);
|
||||||
case "discord4j.core.object.entity.User":
|
case "discord4j.core.object.entity.User":
|
||||||
return componentInteractionEvent.getInteraction().getUser();
|
return interactionEvent.getInteraction().getUser();
|
||||||
}
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getAutocompleteFocusedOption(Object context) {
|
public AutocompleteOptionData getAutocompleteFocusedOption(Object context) {
|
||||||
ChatInputAutoCompleteEvent interaction = (ChatInputAutoCompleteEvent) context;
|
ApplicationCommandInteractionOption option = ((ChatInputAutoCompleteEvent) context).getFocusedOption();
|
||||||
return getOptionValue(interaction.getFocusedOption());
|
return new AutocompleteOptionData(option.getName(), getOptionValue(option));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,79 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper.discord4j;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import discord4j.core.object.command.ApplicationCommandOption.Type;
|
||||||
|
import discord4j.discordjson.json.ApplicationCommandOptionChoiceData;
|
||||||
|
import discord4j.discordjson.json.ApplicationCommandOptionData;
|
||||||
|
import discord4j.discordjson.json.ApplicationCommandRequest;
|
||||||
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
||||||
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOptionChoice;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.RootCommandIdentifier;
|
||||||
|
import net.tomatentum.marinara.wrapper.CommandConverter;
|
||||||
|
|
||||||
|
public class Discord4JConverterSpec implements CommandConverter.Spec<ApplicationCommandRequest, ApplicationCommandOptionData, ApplicationCommandOptionChoiceData> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApplicationCommandRequest convertCommand(RootCommandIdentifier rootIdentifier,
|
||||||
|
List<ApplicationCommandOptionData> options) {
|
||||||
|
return ApplicationCommandRequest.builder()
|
||||||
|
.name(rootIdentifier.name())
|
||||||
|
.description(rootIdentifier.description())
|
||||||
|
.options(options)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApplicationCommandOptionData convertSubCommandGroup(InteractionIdentifier identifier,
|
||||||
|
List<ApplicationCommandOptionData> subCommands) {
|
||||||
|
return ApplicationCommandOptionData.builder()
|
||||||
|
.type(Type.SUB_COMMAND_GROUP.getValue())
|
||||||
|
.name(identifier.name())
|
||||||
|
.description(identifier.description())
|
||||||
|
.options(subCommands)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApplicationCommandOptionData convertSubCommand(InteractionIdentifier identifier,
|
||||||
|
List<ApplicationCommandOptionData> options) {
|
||||||
|
return ApplicationCommandOptionData.builder()
|
||||||
|
.type(Type.SUB_COMMAND_GROUP.getValue())
|
||||||
|
.name(identifier.name())
|
||||||
|
.description(identifier.description())
|
||||||
|
.options(options)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApplicationCommandOptionData convertOption(SlashCommandOption option,
|
||||||
|
List<ApplicationCommandOptionChoiceData> choices) {
|
||||||
|
Type type = Type.of(option.type().getValue());
|
||||||
|
return ApplicationCommandOptionData.builder()
|
||||||
|
.type(type.getValue())
|
||||||
|
.name(option.name())
|
||||||
|
.description(option.description())
|
||||||
|
.required(option.required())
|
||||||
|
.autocomplete(option.autocompletes().length > 0)
|
||||||
|
.minLength((int) option.range().min())
|
||||||
|
.minValue(option.range().min())
|
||||||
|
.maxLength((int)option.range().max())
|
||||||
|
.maxValue(option.range().max())
|
||||||
|
.choices(choices)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ApplicationCommandOptionChoiceData convertChoice(SlashCommandOptionChoice choice) {
|
||||||
|
var builder = ApplicationCommandOptionChoiceData.builder().name(choice.name());
|
||||||
|
if (choice.longValue() != Long.MAX_VALUE)
|
||||||
|
builder.value(choice.longValue());
|
||||||
|
if (choice.doubleValue() != Double.MAX_VALUE)
|
||||||
|
builder.value(choice.doubleValue());
|
||||||
|
if (!choice.stringValue().isEmpty())
|
||||||
|
builder.value(choice.stringValue());
|
||||||
|
return builder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,32 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper.discord4j;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import discord4j.core.GatewayDiscordClient;
|
||||||
|
import discord4j.discordjson.json.ApplicationCommandRequest;
|
||||||
|
import discord4j.rest.service.ApplicationService;
|
||||||
|
import net.tomatentum.marinara.wrapper.CommandRegisterer;
|
||||||
|
import net.tomatentum.marinara.wrapper.ServerCommandList;
|
||||||
|
|
||||||
|
public class Discord4JRegistererStrategy implements CommandRegisterer.Strategy<ApplicationCommandRequest> {
|
||||||
|
|
||||||
|
private ApplicationService appService;
|
||||||
|
private long applicationId;
|
||||||
|
|
||||||
|
public Discord4JRegistererStrategy(GatewayDiscordClient api) {
|
||||||
|
this.appService = api.getRestClient().getApplicationService();
|
||||||
|
this.applicationId = api.getRestClient().getApplicationId().block();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerServer(ServerCommandList<ApplicationCommandRequest> commands) {
|
||||||
|
appService.bulkOverwriteGuildApplicationCommand(applicationId, commands.serverId(), new ArrayList<>(commands));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerGlobal(Set<ApplicationCommandRequest> defs) {
|
||||||
|
appService.bulkOverwriteGlobalApplicationCommand(applicationId, new ArrayList<>(defs));
|
||||||
|
}
|
||||||
|
}
|
@@ -1,206 +1,70 @@
|
|||||||
package net.tomatentum.marinara.wrapper.discord4j;
|
package net.tomatentum.marinara.wrapper.discord4j;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.function.UnaryOperator;
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import discord4j.core.GatewayDiscordClient;
|
import discord4j.core.GatewayDiscordClient;
|
||||||
import discord4j.core.event.domain.interaction.ButtonInteractionEvent;
|
|
||||||
import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent;
|
import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent;
|
||||||
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
|
|
||||||
import discord4j.core.event.domain.interaction.InteractionCreateEvent;
|
import discord4j.core.event.domain.interaction.InteractionCreateEvent;
|
||||||
import discord4j.core.object.command.ApplicationCommandInteractionOption;
|
import discord4j.core.object.command.ApplicationCommandInteractionOption;
|
||||||
import discord4j.core.object.command.ApplicationCommandOption.Type;
|
import discord4j.core.object.command.ApplicationCommandOption.Type;
|
||||||
import discord4j.discordjson.json.ApplicationCommandOptionChoiceData;
|
import discord4j.discordjson.json.ApplicationCommandOptionChoiceData;
|
||||||
import discord4j.discordjson.json.ApplicationCommandOptionData;
|
|
||||||
import discord4j.discordjson.json.ApplicationCommandRequest;
|
import discord4j.discordjson.json.ApplicationCommandRequest;
|
||||||
|
|
||||||
import io.leangen.geantyref.AnnotationFormatException;
|
import net.tomatentum.marinara.wrapper.CommandConverter;
|
||||||
import io.leangen.geantyref.TypeFactory;
|
import net.tomatentum.marinara.wrapper.CommandRegisterer;
|
||||||
import net.tomatentum.marinara.interaction.InteractionType;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOptionChoice;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup;
|
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
|
||||||
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||||
|
import net.tomatentum.marinara.wrapper.discord4j.identifierconverter.AutocompleteIdentifierConverter;
|
||||||
|
import net.tomatentum.marinara.wrapper.discord4j.identifierconverter.ButtonIdentifierConverter;
|
||||||
|
import net.tomatentum.marinara.wrapper.discord4j.identifierconverter.SlashCommandIdentifierConverter;
|
||||||
|
|
||||||
public class Discord4JWrapper extends LibraryWrapper {
|
public class Discord4JWrapper extends LibraryWrapper {
|
||||||
|
public static final UnaryOperator<List<ApplicationCommandInteractionOption>> SUB_FILTER = i ->
|
||||||
public static final Function<List<ApplicationCommandInteractionOption>, List<ApplicationCommandInteractionOption>> SUB_FILTER = (i) ->
|
|
||||||
i.stream()
|
i.stream()
|
||||||
.filter(o -> o.getType().equals(Type.SUB_COMMAND) || o.getType().equals(Type.SUB_COMMAND_GROUP))
|
.filter(o -> o.getType().equals(Type.SUB_COMMAND) || o.getType().equals(Type.SUB_COMMAND_GROUP))
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
public static final Function<List<ApplicationCommandInteractionOption>, List<ApplicationCommandInteractionOption>> ARG_FILTER = (i) ->
|
public static final UnaryOperator<List<ApplicationCommandInteractionOption>> ARG_FILTER = i ->
|
||||||
i.stream()
|
i.stream()
|
||||||
.filter(o -> !o.getType().equals(Type.SUB_COMMAND) && !o.getType().equals(Type.SUB_COMMAND_GROUP))
|
.filter(o -> !o.getType().equals(Type.SUB_COMMAND) && !o.getType().equals(Type.SUB_COMMAND_GROUP))
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
private GatewayDiscordClient api;
|
|
||||||
private Discord4JContextObjectProvider contextObjectProvider;
|
private Discord4JContextObjectProvider contextObjectProvider;
|
||||||
|
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.api = api;
|
|
||||||
this.contextObjectProvider = new Discord4JContextObjectProvider();
|
this.contextObjectProvider = new Discord4JContextObjectProvider();
|
||||||
if (api != null)
|
var converter = CommandConverter.of(new Discord4JConverterSpec());
|
||||||
|
|
||||||
|
if (api != null) {
|
||||||
|
this.commandRegisterer = CommandRegisterer.of(new Discord4JRegistererStrategy(api), converter);
|
||||||
api.on(InteractionCreateEvent.class)
|
api.on(InteractionCreateEvent.class)
|
||||||
.subscribe(event -> handleInteraction(event));
|
.subscribe(event -> handleInteraction(event));
|
||||||
else
|
}else
|
||||||
logger.warn("GatewayDiscordClient was null so no Events were subscribed to.");
|
logger.warn("GatewayDiscordClient was null so no Events were subscribed to.");
|
||||||
|
|
||||||
logger.info("Discord4J wrapper loaded!");
|
logger.info("Discord4J wrapper loaded!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InteractionType getInteractionType(Object context) {
|
public CommandRegisterer<?> getRegisterer() {
|
||||||
if (ChatInputAutoCompleteEvent.class.isAssignableFrom(context.getClass()))
|
return this.commandRegisterer;
|
||||||
return InteractionType.AUTOCOMPLETE;
|
|
||||||
if (ChatInputInteractionEvent.class.isAssignableFrom(context.getClass()))
|
|
||||||
return InteractionType.COMMAND;
|
|
||||||
if (ButtonInteractionEvent.class.isAssignableFrom(context.getClass()))
|
|
||||||
return InteractionType.BUTTON;
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void registerSlashCommands(SlashCommandDefinition[] defs) {
|
public IdentifierProvider createIdentifierProvider() {
|
||||||
HashMap<Long, List<ApplicationCommandRequest>> serverCommands = new HashMap<>();
|
return IdentifierProvider.of(
|
||||||
List<ApplicationCommandRequest> globalCommands = new ArrayList<>();
|
new SlashCommandIdentifierConverter(),
|
||||||
long applicationId = api.getRestClient().getApplicationId().block();
|
new AutocompleteIdentifierConverter(),
|
||||||
|
new ButtonIdentifierConverter()
|
||||||
for (SlashCommandDefinition slashCommandDefinition : defs) {
|
);
|
||||||
ApplicationCommandRequest request = convertSlashCommand(slashCommandDefinition);
|
|
||||||
if (slashCommandDefinition.getFullSlashCommand().serverIds().length > 0) {
|
|
||||||
for (long serverId : slashCommandDefinition.getFullSlashCommand().serverIds()) {
|
|
||||||
serverCommands.putIfAbsent(serverId, new ArrayList<>());
|
|
||||||
serverCommands.get(serverId).add(request);
|
|
||||||
}
|
|
||||||
}else
|
|
||||||
globalCommands.add(request);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (long serverId : serverCommands.keySet()) {
|
|
||||||
api.getRestClient().getApplicationService().bulkOverwriteGuildApplicationCommand(applicationId, serverId, serverCommands.get(serverId));
|
|
||||||
}
|
|
||||||
api.getRestClient().getApplicationService().bulkOverwriteGlobalApplicationCommand(applicationId, globalCommands);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ExecutableSlashCommandDefinition getCommandDefinition(Object context) {
|
|
||||||
List<ApplicationCommandInteractionOption> options;
|
|
||||||
String commandName;
|
|
||||||
|
|
||||||
if (context instanceof ChatInputInteractionEvent) {
|
|
||||||
ChatInputInteractionEvent interaction = (ChatInputInteractionEvent) context;
|
|
||||||
options = SUB_FILTER.apply(interaction.getOptions());
|
|
||||||
commandName = interaction.getCommandName();
|
|
||||||
}else if (context instanceof ChatInputAutoCompleteEvent) {
|
|
||||||
ChatInputAutoCompleteEvent interaction = (ChatInputAutoCompleteEvent) context;
|
|
||||||
options = SUB_FILTER.apply(interaction.getOptions());
|
|
||||||
commandName = interaction.getCommandName();
|
|
||||||
}else
|
|
||||||
return null;
|
|
||||||
|
|
||||||
ExecutableSlashCommandDefinition.Builder builder = new ExecutableSlashCommandDefinition.Builder();
|
|
||||||
|
|
||||||
try {
|
|
||||||
builder.setApplicationCommand(TypeFactory.annotation(SlashCommand.class, Map.of("name", commandName)));
|
|
||||||
if (!options.isEmpty()) {
|
|
||||||
if (!ARG_FILTER.apply(options.getFirst().getOptions()).isEmpty()) {
|
|
||||||
builder.setSubCommandGroup(TypeFactory.annotation(SubCommandGroup.class, Map.of("name", options.getFirst().getName())));
|
|
||||||
builder.setSubCommand(TypeFactory.annotation(SubCommand.class, Map.of("name", SUB_FILTER.apply(options.getFirst().getOptions()).getFirst().getName())));
|
|
||||||
}else
|
|
||||||
builder.setSubCommand(TypeFactory.annotation(SubCommand.class, Map.of("name", options.getFirst().getName())));
|
|
||||||
}
|
|
||||||
} catch (AnnotationFormatException e) {
|
|
||||||
logger.fatal(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return builder.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
private ApplicationCommandRequest convertSlashCommand(SlashCommandDefinition def) {
|
|
||||||
List<ApplicationCommandOptionData> options = new ArrayList<>();
|
|
||||||
SlashCommand cmd = def.getFullSlashCommand();
|
|
||||||
if (!def.isRootCommand()) {
|
|
||||||
Arrays.stream(def.getSubCommands(null)).map(this::convertSubCommandDef).forEach(options::add);
|
|
||||||
Arrays.stream(def.getSubCommandGroups()).map((x) -> convertSubCommandGroupDef(def, x)).forEach(options::add);
|
|
||||||
}else {
|
|
||||||
Arrays.stream(cmd.options()).map(this::convertOptionDef).forEach(options::add);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ApplicationCommandRequest.builder()
|
|
||||||
.name(cmd.name())
|
|
||||||
.description(cmd.description())
|
|
||||||
.options(options)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
private ApplicationCommandOptionData convertSubCommandGroupDef(SlashCommandDefinition def, SubCommandGroup subGroup) {
|
|
||||||
SubCommand[] subCommands = def.getSubCommands(subGroup.name());
|
|
||||||
List<ApplicationCommandOptionData> convertedSubCommands = Arrays.stream(subCommands).map(this::convertSubCommandDef).toList();
|
|
||||||
return ApplicationCommandOptionData.builder()
|
|
||||||
.type(Type.SUB_COMMAND_GROUP.getValue())
|
|
||||||
.name(subGroup.name())
|
|
||||||
.description(subGroup.description())
|
|
||||||
.options(convertedSubCommands)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
private ApplicationCommandOptionData convertSubCommandDef(SubCommand sub) {
|
|
||||||
List<ApplicationCommandOptionData> convertedOptions = Arrays.stream(sub.options()).map(this::convertOptionDef).toList();
|
|
||||||
return ApplicationCommandOptionData.builder()
|
|
||||||
.type(Type.SUB_COMMAND_GROUP.getValue())
|
|
||||||
.name(sub.name())
|
|
||||||
.description(sub.description())
|
|
||||||
.options(convertedOptions)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
private ApplicationCommandOptionData convertOptionDef(SlashCommandOption option) {
|
|
||||||
Type type = Enum.valueOf(Type.class, option.type().toString());
|
|
||||||
return ApplicationCommandOptionData.builder()
|
|
||||||
.type(type.getValue())
|
|
||||||
.name(option.name())
|
|
||||||
.description(option.description())
|
|
||||||
.required(option.required())
|
|
||||||
.autocomplete(option.autocomplete())
|
|
||||||
.choices(convertChoices(option))
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<ApplicationCommandOptionChoiceData> convertChoices(SlashCommandOption option) {
|
|
||||||
List<ApplicationCommandOptionChoiceData> convertedChoices = new ArrayList<>();
|
|
||||||
for (SlashCommandOptionChoice choice : ExecutableSlashCommandDefinition.getActualChoices(option)) {
|
|
||||||
var builder = ApplicationCommandOptionChoiceData.builder();
|
|
||||||
builder.name(choice.name());
|
|
||||||
if (choice.longValue() != Long.MAX_VALUE)
|
|
||||||
builder.value(choice.longValue());
|
|
||||||
if (choice.doubleValue() != Double.MAX_VALUE)
|
|
||||||
builder.value(choice.doubleValue());
|
|
||||||
if (!choice.stringValue().isEmpty())
|
|
||||||
builder.value(choice.stringValue());
|
|
||||||
}
|
|
||||||
return convertedChoices;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getButtonId(Object context) {
|
|
||||||
ButtonInteractionEvent button = (ButtonInteractionEvent) context;
|
|
||||||
return button.getCustomId();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -208,4 +72,15 @@ public class Discord4JWrapper extends LibraryWrapper {
|
|||||||
return this.contextObjectProvider;
|
return this.contextObjectProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void respondAutocomplete(Object context, List<Object> options) {
|
||||||
|
if (context instanceof ChatInputAutoCompleteEvent event) {
|
||||||
|
List<ApplicationCommandOptionChoiceData> choices = options.stream()
|
||||||
|
.filter(ApplicationCommandOptionChoiceData.class::isInstance)
|
||||||
|
.map(o -> (ApplicationCommandOptionChoiceData)o)
|
||||||
|
.toList();
|
||||||
|
event.respondWithSuggestions(choices);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,40 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper.discord4j.identifierconverter;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent;
|
||||||
|
import discord4j.core.object.command.ApplicationCommandInteractionOption;
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper;
|
||||||
|
|
||||||
|
public class AutocompleteIdentifierConverter implements IdentifierProvider.Converter<ChatInputAutoCompleteEvent> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InteractionIdentifier convert(ChatInputAutoCompleteEvent context) {
|
||||||
|
InteractionIdentifier last = InteractionIdentifier.builder()
|
||||||
|
.type(InteractionType.AUTOCOMPLETE)
|
||||||
|
.name(context.getCommandName())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
List<ApplicationCommandInteractionOption> options = Discord4JWrapper.SUB_FILTER.apply(context.getOptions());
|
||||||
|
if (!options.isEmpty()) {
|
||||||
|
last = InteractionIdentifier.builder()
|
||||||
|
.type(InteractionType.AUTOCOMPLETE)
|
||||||
|
.name(options.getFirst().getName())
|
||||||
|
.parent(last)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
List<ApplicationCommandInteractionOption> subOptions = Discord4JWrapper.SUB_FILTER.apply(options.getFirst().getOptions());
|
||||||
|
if (!subOptions.isEmpty())
|
||||||
|
last = InteractionIdentifier.builder()
|
||||||
|
.type(InteractionType.AUTOCOMPLETE)
|
||||||
|
.name(subOptions.getFirst().getName())
|
||||||
|
.parent(last)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
return last;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,15 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper.discord4j.identifierconverter;
|
||||||
|
|
||||||
|
import discord4j.core.event.domain.interaction.ButtonInteractionEvent;
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
|
||||||
|
public class ButtonIdentifierConverter implements IdentifierProvider.Converter<ButtonInteractionEvent> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InteractionIdentifier convert(ButtonInteractionEvent context) {
|
||||||
|
return InteractionIdentifier.builder().name(context.getCustomId()).type(InteractionType.BUTTON).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,40 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper.discord4j.identifierconverter;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
|
||||||
|
import discord4j.core.object.command.ApplicationCommandInteractionOption;
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper;
|
||||||
|
|
||||||
|
public class SlashCommandIdentifierConverter implements IdentifierProvider.Converter<ChatInputInteractionEvent> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InteractionIdentifier convert(ChatInputInteractionEvent context) {
|
||||||
|
InteractionIdentifier last = InteractionIdentifier.builder()
|
||||||
|
.type(InteractionType.COMMAND)
|
||||||
|
.name(context.getCommandName())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
List<ApplicationCommandInteractionOption> options = Discord4JWrapper.SUB_FILTER.apply(context.getOptions());
|
||||||
|
if (!options.isEmpty()) {
|
||||||
|
last = InteractionIdentifier.builder()
|
||||||
|
.type(InteractionType.COMMAND)
|
||||||
|
.name(options.getFirst().getName())
|
||||||
|
.parent(last)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
List<ApplicationCommandInteractionOption> subOptions = Discord4JWrapper.SUB_FILTER.apply(options.getFirst().getOptions());
|
||||||
|
if (!subOptions.isEmpty())
|
||||||
|
last = InteractionIdentifier.builder()
|
||||||
|
.type(InteractionType.COMMAND)
|
||||||
|
.name(subOptions.getFirst().getName())
|
||||||
|
.parent(last)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
return last;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -18,10 +18,10 @@ import net.tomatentum.marinara.Marinara;
|
|||||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||||
import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper;
|
import net.tomatentum.marinara.wrapper.discord4j.Discord4JWrapper;
|
||||||
|
|
||||||
public class AutoCompleteTest {
|
class AutoCompleteTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAutocomplete() {
|
void testAutocomplete() {
|
||||||
ApplicationCommandInteractionOption optionMock = mock();
|
ApplicationCommandInteractionOption optionMock = mock();
|
||||||
ChatInputAutoCompleteEvent autoCompleteEventMock = mock();
|
ChatInputAutoCompleteEvent autoCompleteEventMock = mock();
|
||||||
|
|
||||||
@@ -38,7 +38,7 @@ public class AutoCompleteTest {
|
|||||||
|
|
||||||
LibraryWrapper wrapper = new Discord4JWrapper(null); //null okay as we don't use the discord API in this test.
|
LibraryWrapper wrapper = new Discord4JWrapper(null); //null okay as we don't use the discord API in this test.
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getRegistry().addInteractions(new TestAutocomplete());
|
marinara.getInteractionContainer().addAllMethods(new TestAutocomplete());
|
||||||
wrapper.handleInteraction(autoCompleteEventMock);
|
wrapper.handleInteraction(autoCompleteEventMock);
|
||||||
verify(autoCompleteEventMock).respondWithSuggestions(any());
|
verify(autoCompleteEventMock).respondWithSuggestions(any());
|
||||||
}
|
}
|
||||||
|
@@ -13,15 +13,15 @@ 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)
|
||||||
public class ButtonTest {
|
class ButtonTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testButtonExecution() {
|
void testButtonExecution() {
|
||||||
ButtonInteractionEvent buttonEventMock = CommonMocks.getButtonEventMock("test");
|
ButtonInteractionEvent buttonEventMock = CommonMocks.getButtonEventMock("test");
|
||||||
|
|
||||||
LibraryWrapper wrapper = new Discord4JWrapper(null); //null okay as we don't use the discord API in this test.
|
LibraryWrapper wrapper = new Discord4JWrapper(null); //null okay as we don't use the discord API in this test.
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getRegistry().addInteractions(new TestButton());
|
marinara.getInteractionContainer().addAllMethods(new TestButton());
|
||||||
wrapper.handleInteraction(buttonEventMock);
|
wrapper.handleInteraction(buttonEventMock);
|
||||||
assertTrue(TestButton.didRun);
|
assertTrue(TestButton.didRun);
|
||||||
}
|
}
|
||||||
|
@@ -24,16 +24,16 @@ import net.tomatentum.marinara.wrapper.discord4j.checks.PermissionCheck;
|
|||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@TestInstance(Lifecycle.PER_CLASS)
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
public class InteractionCheckTest {
|
class InteractionCheckTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInteractionCheck() {
|
void testInteractionCheck() {
|
||||||
ButtonInteractionEvent buttonEventMock = CommonMocks.getButtonEventMock("test");
|
ButtonInteractionEvent buttonEventMock = CommonMocks.getButtonEventMock("test");
|
||||||
|
|
||||||
LibraryWrapper wrapper = new Discord4JWrapper(null);
|
LibraryWrapper wrapper = new Discord4JWrapper(null);
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getCheckRegistry().addCheck(new TestInteractionCheck());
|
marinara.getCheckContainer().addAllMethods(new TestInteractionCheck());
|
||||||
marinara.getRegistry().addInteractions(new TestButton());
|
marinara.getInteractionContainer().addAllMethods(new TestButton());
|
||||||
wrapper.handleInteraction(buttonEventMock);
|
wrapper.handleInteraction(buttonEventMock);
|
||||||
|
|
||||||
assertTrue(TestInteractionCheck.preExecuted);
|
assertTrue(TestInteractionCheck.preExecuted);
|
||||||
@@ -42,7 +42,7 @@ public class InteractionCheckTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPermissionCheck() {
|
void testPermissionCheck() {
|
||||||
Member memberMock = mock();
|
Member memberMock = mock();
|
||||||
Interaction interactionMock = mock();
|
Interaction interactionMock = mock();
|
||||||
|
|
||||||
@@ -54,8 +54,8 @@ public class InteractionCheckTest {
|
|||||||
|
|
||||||
LibraryWrapper wrapper = new Discord4JWrapper(null);
|
LibraryWrapper wrapper = new Discord4JWrapper(null);
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getCheckRegistry().addCheck(new PermissionCheck());
|
marinara.getCheckContainer().addAllMethods(new PermissionCheck());
|
||||||
marinara.getRegistry().addInteractions(new TestButton());
|
marinara.getInteractionContainer().addAllMethods(new TestButton());
|
||||||
|
|
||||||
wrapper.handleInteraction(buttonEventMock);
|
wrapper.handleInteraction(buttonEventMock);
|
||||||
assertFalse(TestButton.didPermRun);
|
assertFalse(TestButton.didPermRun);
|
||||||
|
@@ -21,10 +21,11 @@ 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)
|
|
||||||
public class SlashCommandTest {
|
|
||||||
|
|
||||||
String DISCORD_TOKEN = System.getenv("DISCORD_TEST_TOKEN");
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
|
class SlashCommandTest {
|
||||||
|
|
||||||
|
private static String DISCORD_TOKEN = System.getenv("DISCORD_TEST_TOKEN");
|
||||||
GatewayDiscordClient client;
|
GatewayDiscordClient client;
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
@@ -41,8 +42,8 @@ public class SlashCommandTest {
|
|||||||
@Test
|
@Test
|
||||||
void testSlashCommand() {
|
void testSlashCommand() {
|
||||||
Marinara marinara = Marinara.load(new Discord4JWrapper(client));
|
Marinara marinara = Marinara.load(new Discord4JWrapper(client));
|
||||||
marinara.getRegistry().addInteractions(new TestCommand());
|
marinara.getInteractionContainer().addAllMethods(new TestCommand());
|
||||||
marinara.getRegistry().registerCommands();
|
marinara.registerCommands();
|
||||||
System.out.println("Success!");
|
System.out.println("Success!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +65,7 @@ public class SlashCommandTest {
|
|||||||
|
|
||||||
LibraryWrapper wrapper = new Discord4JWrapper(client);
|
LibraryWrapper wrapper = new Discord4JWrapper(client);
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getRegistry().addInteractions(new TestCommand());
|
marinara.getInteractionContainer().addAllMethods(new TestCommand());
|
||||||
|
|
||||||
wrapper.handleInteraction(eventMock);
|
wrapper.handleInteraction(eventMock);
|
||||||
}
|
}
|
||||||
|
@@ -2,21 +2,36 @@ package net.tomatentum.marinara.test.discord4j;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent;
|
import discord4j.core.event.domain.interaction.ChatInputAutoCompleteEvent;
|
||||||
|
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
|
||||||
|
import discord4j.discordjson.json.ApplicationCommandOptionChoiceData;
|
||||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||||
import net.tomatentum.marinara.interaction.annotation.AutoComplete;
|
import net.tomatentum.marinara.interaction.annotation.AutoComplete;
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
||||||
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
||||||
|
import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType;
|
||||||
|
|
||||||
public class TestAutocomplete implements InteractionHandler {
|
public class TestAutocomplete implements InteractionHandler {
|
||||||
|
|
||||||
@SlashCommand(name = "test")
|
@SlashCommand(
|
||||||
@AutoComplete
|
name = "test",
|
||||||
public void autocomplete(ChatInputAutoCompleteEvent context, String value) {
|
options = @SlashCommandOption(
|
||||||
|
name = "foo",
|
||||||
|
type = SlashCommandOptionType.STRING,
|
||||||
|
autocompletes = @AutoComplete("testAuto")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
public void exec(ChatInputInteractionEvent context) {
|
||||||
|
// Not executed just there for autocomplete to work
|
||||||
|
}
|
||||||
|
|
||||||
|
@AutoComplete("testAuto")
|
||||||
|
public ApplicationCommandOptionChoiceData[] autocomplete(ChatInputAutoCompleteEvent context, String value) {
|
||||||
System.out.println("Success!");
|
System.out.println("Success!");
|
||||||
assertEquals(value, "test");
|
assertEquals("test", value);
|
||||||
context.respondWithSuggestions(Collections.emptyList());
|
return new ApplicationCommandOptionChoiceData[]{
|
||||||
|
ApplicationCommandOptionChoiceData.builder().name("TestValue").value("test").build()
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
package net.tomatentum.marinara.test.discord4j;
|
package net.tomatentum.marinara.test.discord4j;
|
||||||
|
|
||||||
import net.tomatentum.marinara.interaction.commands.ChoiceValueProvider;
|
import net.tomatentum.marinara.interaction.commands.choice.ChoiceValueProvider;
|
||||||
|
|
||||||
public enum TestChoiceEnum implements ChoiceValueProvider<String> {
|
public enum TestChoiceEnum implements ChoiceValueProvider<String> {
|
||||||
TestValue("testValue"),
|
TestValue("testValue"),
|
||||||
|
@@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||||||
|
|
||||||
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
|
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
|
||||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||||
|
import net.tomatentum.marinara.interaction.commands.annotation.CommandChoices;
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
||||||
import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType;
|
import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType;
|
||||||
@@ -20,12 +21,12 @@ public class TestCommand implements InteractionHandler {
|
|||||||
name = "foo",
|
name = "foo",
|
||||||
description = "foo bar is very fooby",
|
description = "foo bar is very fooby",
|
||||||
type = SlashCommandOptionType.STRING,
|
type = SlashCommandOptionType.STRING,
|
||||||
choiceEnum = TestChoiceEnum.class
|
choices = @CommandChoices(cenum = TestChoiceEnum.class)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
public void exec(ChatInputInteractionEvent event, String test) {
|
public void exec(ChatInputInteractionEvent event, String test) {
|
||||||
assertEquals(test, "test");
|
assertEquals("test", test);
|
||||||
System.out.println("Success!");
|
System.out.println("Success!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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
|
@@ -10,18 +10,15 @@ plugins {
|
|||||||
`java-library`
|
`java-library`
|
||||||
}
|
}
|
||||||
|
|
||||||
repositories {
|
|
||||||
// Use Maven Central for resolving dependencies.
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
// Use JUnit Jupiter for testing.
|
// Use JUnit Jupiter for testing.
|
||||||
testImplementation(libs.junit.jupiter)
|
testImplementation(libs.junit.jupiter)
|
||||||
testImplementation(libs.mockito)
|
testImplementation(libs.mockito)
|
||||||
|
|
||||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||||
implementation(libs.log4j)
|
testImplementation(libs.slf4j.simple)
|
||||||
|
|
||||||
|
implementation(libs.slf4j)
|
||||||
implementation(libs.javacord)
|
implementation(libs.javacord)
|
||||||
implementation(libs.geantyref)
|
implementation(libs.geantyref)
|
||||||
implementation(project(":lib"))
|
implementation(project(":lib"))
|
||||||
|
@@ -2,10 +2,12 @@ package net.tomatentum.marinara.wrapper.javacord;
|
|||||||
|
|
||||||
import org.javacord.api.interaction.AutocompleteInteraction;
|
import org.javacord.api.interaction.AutocompleteInteraction;
|
||||||
import org.javacord.api.interaction.ButtonInteraction;
|
import org.javacord.api.interaction.ButtonInteraction;
|
||||||
|
import org.javacord.api.interaction.InteractionBase;
|
||||||
import org.javacord.api.interaction.SlashCommandInteraction;
|
import org.javacord.api.interaction.SlashCommandInteraction;
|
||||||
import org.javacord.api.interaction.SlashCommandInteractionOption;
|
import org.javacord.api.interaction.SlashCommandInteractionOption;
|
||||||
import org.javacord.api.interaction.SlashCommandOptionType;
|
import org.javacord.api.interaction.SlashCommandOptionType;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.interaction.commands.option.AutocompleteOptionData;
|
||||||
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||||
|
|
||||||
public class JavacordContextObjectProvider implements ContextObjectProvider {
|
public class JavacordContextObjectProvider implements ContextObjectProvider {
|
||||||
@@ -16,14 +18,14 @@ public class JavacordContextObjectProvider implements ContextObjectProvider {
|
|||||||
return null;
|
return null;
|
||||||
SlashCommandInteraction interaction = (SlashCommandInteraction) context;
|
SlashCommandInteraction interaction = (SlashCommandInteraction) context;
|
||||||
if (!interaction.getArguments().isEmpty())
|
if (!interaction.getArguments().isEmpty())
|
||||||
return getOptionValue(interaction.getOptionByName(optionName).get());
|
return getOptionValue(interaction.getOptionByName(optionName).orElse(null));
|
||||||
|
|
||||||
SlashCommandInteractionOption subCommandOption = interaction.getOptions().getFirst();
|
SlashCommandInteractionOption subCommandOption = interaction.getOptions().getFirst();
|
||||||
|
|
||||||
if (!subCommandOption.getOptions().isEmpty())
|
if (!subCommandOption.getOptions().isEmpty())
|
||||||
subCommandOption = subCommandOption.getOptions().getFirst();
|
subCommandOption = subCommandOption.getOptions().getFirst();
|
||||||
|
|
||||||
return getOptionValue(subCommandOption.getOptionByName(optionName).get());
|
return getOptionValue(subCommandOption.getOptionByName(optionName).orElse(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object getOptionValue(SlashCommandInteractionOption option) {
|
private Object getOptionValue(SlashCommandInteractionOption option) {
|
||||||
@@ -89,7 +91,7 @@ public class JavacordContextObjectProvider implements ContextObjectProvider {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getInteractionContextObject(Object context, Class<?> type) {
|
public Object getInteractionContextObject(Object context, Class<?> type) {
|
||||||
ButtonInteraction button = (ButtonInteraction) context;
|
InteractionBase button = (InteractionBase) context;
|
||||||
switch (type.getName()) {
|
switch (type.getName()) {
|
||||||
case "org.javacord.api.entity.channel.TextChannel":
|
case "org.javacord.api.entity.channel.TextChannel":
|
||||||
return button.getChannel().orElse(null);
|
return button.getChannel().orElse(null);
|
||||||
@@ -102,9 +104,9 @@ public class JavacordContextObjectProvider implements ContextObjectProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getAutocompleteFocusedOption(Object context) {
|
public AutocompleteOptionData getAutocompleteFocusedOption(Object context) {
|
||||||
AutocompleteInteraction interaction = (AutocompleteInteraction) context;
|
SlashCommandInteractionOption option = ((AutocompleteInteraction) context).getFocusedOption();
|
||||||
return getOptionValue(interaction.getFocusedOption());
|
return new AutocompleteOptionData(option.getName(), getOptionValue(option));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,77 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper.javacord;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.javacord.api.interaction.SlashCommand;
|
||||||
|
import org.javacord.api.interaction.SlashCommandBuilder;
|
||||||
|
import org.javacord.api.interaction.SlashCommandOption;
|
||||||
|
import org.javacord.api.interaction.SlashCommandOptionBuilder;
|
||||||
|
import org.javacord.api.interaction.SlashCommandOptionChoice;
|
||||||
|
import org.javacord.api.interaction.SlashCommandOptionChoiceBuilder;
|
||||||
|
import org.javacord.api.interaction.SlashCommandOptionType;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.RootCommandIdentifier;
|
||||||
|
import net.tomatentum.marinara.wrapper.CommandConverter;
|
||||||
|
|
||||||
|
public class JavacordConverterSpec implements CommandConverter.Spec<SlashCommandBuilder, SlashCommandOption, SlashCommandOptionChoice> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SlashCommandBuilder convertCommand(RootCommandIdentifier rootIdentifier, List<SlashCommandOption> options) {
|
||||||
|
return SlashCommand.with(rootIdentifier.name(), rootIdentifier.description(), options);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SlashCommandOption convertSubCommandGroup(InteractionIdentifier identifier,
|
||||||
|
List<SlashCommandOption> subCommands) {
|
||||||
|
return SlashCommandOption.createWithOptions(
|
||||||
|
SlashCommandOptionType.SUB_COMMAND_GROUP,
|
||||||
|
identifier.name(),
|
||||||
|
identifier.description(),
|
||||||
|
subCommands);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SlashCommandOption convertSubCommand(InteractionIdentifier identifier, List<SlashCommandOption> options) {
|
||||||
|
return SlashCommandOption.createWithOptions(
|
||||||
|
SlashCommandOptionType.SUB_COMMAND,
|
||||||
|
identifier.name(),
|
||||||
|
identifier.description(),
|
||||||
|
options);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SlashCommandOption convertOption(
|
||||||
|
net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption option,
|
||||||
|
List<SlashCommandOptionChoice> choices) {
|
||||||
|
SlashCommandOptionType type = SlashCommandOptionType.fromValue(option.type().getValue());
|
||||||
|
return new SlashCommandOptionBuilder()
|
||||||
|
.setType(type)
|
||||||
|
.setName(option.name())
|
||||||
|
.setDescription(option.description())
|
||||||
|
.setRequired(option.required())
|
||||||
|
.setAutocompletable(option.autocompletes().length > 0)
|
||||||
|
.setMinLength((long) option.range().min())
|
||||||
|
.setDecimalMinValue(option.range().min())
|
||||||
|
.setMaxLength((long) option.range().max())
|
||||||
|
.setDecimalMaxValue(option.range().max())
|
||||||
|
.setChoices(choices)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SlashCommandOptionChoice convertChoice(
|
||||||
|
net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOptionChoice choice) {
|
||||||
|
SlashCommandOptionChoiceBuilder builder = new SlashCommandOptionChoiceBuilder().setName(choice.name());
|
||||||
|
if (choice.longValue() != Long.MAX_VALUE)
|
||||||
|
builder.setValue(choice.longValue());
|
||||||
|
/*
|
||||||
|
not yet available
|
||||||
|
if (choice.doubleValue() != Double.MAX_VALUE)
|
||||||
|
builder.setValue(choice.doubleValue());
|
||||||
|
*/
|
||||||
|
if (!choice.stringValue().isEmpty())
|
||||||
|
builder.setValue(choice.stringValue());
|
||||||
|
return builder.build();
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,29 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper.javacord;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.javacord.api.DiscordApi;
|
||||||
|
import org.javacord.api.interaction.SlashCommandBuilder;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.wrapper.CommandRegisterer;
|
||||||
|
import net.tomatentum.marinara.wrapper.ServerCommandList;
|
||||||
|
|
||||||
|
public class JavacordRegistererStrategy implements CommandRegisterer.Strategy<SlashCommandBuilder> {
|
||||||
|
|
||||||
|
private DiscordApi api;
|
||||||
|
|
||||||
|
public JavacordRegistererStrategy(DiscordApi api) {
|
||||||
|
this.api = api;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerServer(ServerCommandList<SlashCommandBuilder> commands) {
|
||||||
|
api.bulkOverwriteServerApplicationCommands(commands.serverId(), commands);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerGlobal(Set<SlashCommandBuilder> defs) {
|
||||||
|
api.bulkOverwriteGlobalApplicationCommands(defs);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -1,180 +1,54 @@
|
|||||||
package net.tomatentum.marinara.wrapper.javacord;
|
package net.tomatentum.marinara.wrapper.javacord;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
import org.javacord.api.DiscordApi;
|
import org.javacord.api.DiscordApi;
|
||||||
import org.javacord.api.interaction.ApplicationCommandInteraction;
|
|
||||||
import org.javacord.api.interaction.AutocompleteInteraction;
|
import org.javacord.api.interaction.AutocompleteInteraction;
|
||||||
import org.javacord.api.interaction.ButtonInteraction;
|
|
||||||
import org.javacord.api.interaction.SlashCommandBuilder;
|
import org.javacord.api.interaction.SlashCommandBuilder;
|
||||||
import org.javacord.api.interaction.SlashCommandInteraction;
|
import org.javacord.api.interaction.SlashCommandOptionChoice;
|
||||||
import org.javacord.api.interaction.SlashCommandInteractionOption;
|
import org.slf4j.Logger;
|
||||||
import org.javacord.api.interaction.SlashCommandOptionBuilder;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.javacord.api.interaction.SlashCommandOptionChoiceBuilder;
|
|
||||||
import org.javacord.api.interaction.SlashCommandOptionType;
|
|
||||||
|
|
||||||
import io.leangen.geantyref.AnnotationFormatException;
|
import net.tomatentum.marinara.wrapper.CommandConverter;
|
||||||
import io.leangen.geantyref.TypeFactory;
|
import net.tomatentum.marinara.wrapper.CommandRegisterer;
|
||||||
import net.tomatentum.marinara.interaction.InteractionType;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOptionChoice;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SubCommand;
|
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup;
|
|
||||||
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
import net.tomatentum.marinara.wrapper.ContextObjectProvider;
|
||||||
import net.tomatentum.marinara.util.LoggerUtil;
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
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.ButtonIdentifierConverter;
|
||||||
|
import net.tomatentum.marinara.wrapper.javacord.identifierconverter.SlashCommandIdentifierConverter;
|
||||||
|
|
||||||
public class JavacordWrapper extends LibraryWrapper {
|
public class JavacordWrapper extends LibraryWrapper {
|
||||||
|
|
||||||
private DiscordApi api;
|
|
||||||
private JavacordContextObjectProvider contextObjectProvider;
|
private JavacordContextObjectProvider contextObjectProvider;
|
||||||
|
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.api = api;
|
|
||||||
this.contextObjectProvider = new JavacordContextObjectProvider();
|
this.contextObjectProvider = new JavacordContextObjectProvider();
|
||||||
if (api != null)
|
var converter = CommandConverter.of(new JavacordConverterSpec());
|
||||||
api.addInteractionCreateListener((e) -> handleInteraction(e.getInteraction()));
|
|
||||||
else
|
if (api != null) {
|
||||||
|
this.commandRegisterer = CommandRegisterer.of(new JavacordRegistererStrategy(api), converter);
|
||||||
|
api.addInteractionCreateListener(e -> handleInteraction(e.getInteraction()));
|
||||||
|
}else
|
||||||
logger.warn("DiscordApi was null so no Events were subscribed to.");
|
logger.warn("DiscordApi was null so no Events were subscribed to.");
|
||||||
logger.info("Javacord wrapper loaded!");
|
logger.info("Javacord wrapper loaded!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InteractionType getInteractionType(Object context) {
|
public CommandRegisterer<?> getRegisterer() {
|
||||||
if (AutocompleteInteraction.class.isAssignableFrom(context.getClass()))
|
return this.commandRegisterer;
|
||||||
return InteractionType.AUTOCOMPLETE;
|
|
||||||
if (ApplicationCommandInteraction.class.isAssignableFrom(context.getClass()))
|
|
||||||
return InteractionType.COMMAND;
|
|
||||||
if (ButtonInteraction.class.isAssignableFrom(context.getClass()))
|
|
||||||
return InteractionType.BUTTON;
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void registerSlashCommands(SlashCommandDefinition[] defs) {
|
public IdentifierProvider createIdentifierProvider() {
|
||||||
HashMap<Long, Set<SlashCommandBuilder>> serverCommands = new HashMap<>();
|
return IdentifierProvider.of(
|
||||||
Set<SlashCommandBuilder> globalCommands = new HashSet<>();
|
new SlashCommandIdentifierConverter(),
|
||||||
for (SlashCommandDefinition slashCommandDefinition : defs) {
|
new AutocompleteIdentifierConverter(),
|
||||||
SlashCommandBuilder builder = convertSlashCommand(slashCommandDefinition);
|
new ButtonIdentifierConverter()
|
||||||
if (slashCommandDefinition.getFullSlashCommand().serverIds().length > 0) {
|
);
|
||||||
for (long serverId : slashCommandDefinition.getFullSlashCommand().serverIds()) {
|
|
||||||
serverCommands.putIfAbsent(serverId, new HashSet<>());
|
|
||||||
serverCommands.get(serverId).add(builder);
|
|
||||||
}
|
|
||||||
}else
|
|
||||||
globalCommands.add(builder);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (long serverId : serverCommands.keySet()) {
|
|
||||||
api.bulkOverwriteServerApplicationCommands(serverId, serverCommands.get(serverId));
|
|
||||||
}
|
|
||||||
api.bulkOverwriteGlobalApplicationCommands(globalCommands);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ExecutableSlashCommandDefinition getCommandDefinition(Object context) {
|
|
||||||
if (!(context instanceof SlashCommandInteraction))
|
|
||||||
return null;
|
|
||||||
|
|
||||||
SlashCommandInteraction interaction = (SlashCommandInteraction) context;
|
|
||||||
ExecutableSlashCommandDefinition.Builder builder = new ExecutableSlashCommandDefinition.Builder();
|
|
||||||
List<SlashCommandInteractionOption> options = interaction.getOptions();
|
|
||||||
try {
|
|
||||||
builder.setApplicationCommand(TypeFactory.annotation(SlashCommand.class, Map.of("name", interaction.getCommandName())));
|
|
||||||
if (!options.isEmpty()) {
|
|
||||||
if (!options.getFirst().getArguments().isEmpty()) {
|
|
||||||
builder.setSubCommandGroup(TypeFactory.annotation(SubCommandGroup.class, Map.of("name", options.getFirst().getName())));
|
|
||||||
builder.setSubCommand(TypeFactory.annotation(SubCommand.class, Map.of("name", options.getFirst().getOptions().getFirst().getName())));
|
|
||||||
}else
|
|
||||||
builder.setSubCommand(TypeFactory.annotation(SubCommand.class, Map.of("name", options.getFirst().getName())));
|
|
||||||
}
|
|
||||||
} catch (AnnotationFormatException e) {
|
|
||||||
logger.fatal(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return builder.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
private SlashCommandBuilder convertSlashCommand(SlashCommandDefinition def) {
|
|
||||||
List<org.javacord.api.interaction.SlashCommandOption> options = new ArrayList<>();
|
|
||||||
SlashCommand cmd = def.getFullSlashCommand();
|
|
||||||
if (!def.isRootCommand()) {
|
|
||||||
Arrays.stream(def.getSubCommands(null)).map(this::convertSubCommandDef).forEach(options::add);
|
|
||||||
Arrays.stream(def.getSubCommandGroups()).map((x) -> convertSubCommandGroupDef(def, x)).forEach(options::add);
|
|
||||||
}else {
|
|
||||||
Arrays.stream(cmd.options()).map(this::convertOptionDef).forEach(options::add);
|
|
||||||
}
|
|
||||||
|
|
||||||
return org.javacord.api.interaction.SlashCommand.with(cmd.name(), cmd.description(), options);
|
|
||||||
}
|
|
||||||
|
|
||||||
private org.javacord.api.interaction.SlashCommandOption convertSubCommandGroupDef(SlashCommandDefinition def, SubCommandGroup subGroup) {
|
|
||||||
SubCommand[] subCommands = def.getSubCommands(subGroup.name());
|
|
||||||
List<org.javacord.api.interaction.SlashCommandOption> convertedSubCommands = Arrays.stream(subCommands).map(this::convertSubCommandDef).toList();
|
|
||||||
return org.javacord.api.interaction.SlashCommandOption.createWithOptions(
|
|
||||||
org.javacord.api.interaction.SlashCommandOptionType.SUB_COMMAND_GROUP,
|
|
||||||
subGroup.name(),
|
|
||||||
subGroup.description(),
|
|
||||||
convertedSubCommands);
|
|
||||||
}
|
|
||||||
|
|
||||||
private org.javacord.api.interaction.SlashCommandOption convertSubCommandDef(SubCommand sub) {
|
|
||||||
List<org.javacord.api.interaction.SlashCommandOption> convertedOptions = Arrays.stream(sub.options()).map(this::convertOptionDef).toList();
|
|
||||||
return org.javacord.api.interaction.SlashCommandOption.createWithOptions(
|
|
||||||
org.javacord.api.interaction.SlashCommandOptionType.SUB_COMMAND,
|
|
||||||
sub.name(),
|
|
||||||
sub.description(),
|
|
||||||
convertedOptions);
|
|
||||||
}
|
|
||||||
|
|
||||||
private org.javacord.api.interaction.SlashCommandOption convertOptionDef(SlashCommandOption option) {
|
|
||||||
SlashCommandOptionType type = SlashCommandOptionType.fromValue(option.type().getValue());
|
|
||||||
SlashCommandOptionBuilder builder = new SlashCommandOptionBuilder();
|
|
||||||
builder
|
|
||||||
.setType(type)
|
|
||||||
.setName(option.name())
|
|
||||||
.setDescription(option.description())
|
|
||||||
.setRequired(option.required())
|
|
||||||
.setAutocompletable(option.autocomplete())
|
|
||||||
.setChoices(convertChoices(option));
|
|
||||||
|
|
||||||
return builder.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<org.javacord.api.interaction.SlashCommandOptionChoice> convertChoices(SlashCommandOption option) {
|
|
||||||
List<org.javacord.api.interaction.SlashCommandOptionChoice> convertedChoices = new ArrayList<>();
|
|
||||||
for (SlashCommandOptionChoice choice : ExecutableSlashCommandDefinition.getActualChoices(option)) {
|
|
||||||
SlashCommandOptionChoiceBuilder builder = new SlashCommandOptionChoiceBuilder();
|
|
||||||
builder.setName(choice.name());
|
|
||||||
if (choice.longValue() != Long.MAX_VALUE)
|
|
||||||
builder.setValue(choice.longValue());
|
|
||||||
/*
|
|
||||||
not yet available
|
|
||||||
if (choice.doubleValue() != Double.MAX_VALUE)
|
|
||||||
builder.setValue(choice.doubleValue());
|
|
||||||
*/
|
|
||||||
if (!choice.stringValue().isEmpty())
|
|
||||||
builder.setValue(choice.stringValue());
|
|
||||||
}
|
|
||||||
return convertedChoices;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getButtonId(Object context) {
|
|
||||||
ButtonInteraction button = (ButtonInteraction) context;
|
|
||||||
return button.getCustomId();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -182,4 +56,15 @@ public class JavacordWrapper extends LibraryWrapper {
|
|||||||
return contextObjectProvider;
|
return contextObjectProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void respondAutocomplete(Object context, List<Object> options) {
|
||||||
|
if (context instanceof AutocompleteInteraction interaction) {
|
||||||
|
List<SlashCommandOptionChoice> choices = options.stream()
|
||||||
|
.filter(SlashCommandOptionChoice.class::isInstance)
|
||||||
|
.map(o -> (SlashCommandOptionChoice)o)
|
||||||
|
.toList();
|
||||||
|
interaction.respondWithChoices(choices);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,40 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper.javacord.identifierconverter;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.javacord.api.interaction.AutocompleteInteraction;
|
||||||
|
import org.javacord.api.interaction.SlashCommandInteractionOption;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
|
||||||
|
public class AutocompleteIdentifierConverter implements IdentifierProvider.Converter<AutocompleteInteraction> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InteractionIdentifier convert(AutocompleteInteraction context) {
|
||||||
|
InteractionIdentifier last = InteractionIdentifier.builder()
|
||||||
|
.type(InteractionType.AUTOCOMPLETE)
|
||||||
|
.name(context.getCommandName())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
List<SlashCommandInteractionOption> options = context.getOptions();
|
||||||
|
if (!options.isEmpty()) {
|
||||||
|
last = InteractionIdentifier.builder()
|
||||||
|
.type(InteractionType.AUTOCOMPLETE)
|
||||||
|
.name(options.getFirst().getName())
|
||||||
|
.parent(last)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
List<SlashCommandInteractionOption> subOptions = context.getOptions().getFirst().getOptions();
|
||||||
|
if (!subOptions.isEmpty())
|
||||||
|
last = InteractionIdentifier.builder()
|
||||||
|
.type(InteractionType.AUTOCOMPLETE)
|
||||||
|
.name(subOptions.getFirst().getName())
|
||||||
|
.parent(last)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
return last;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,16 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper.javacord.identifierconverter;
|
||||||
|
|
||||||
|
import org.javacord.api.interaction.ButtonInteraction;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
|
||||||
|
public class ButtonIdentifierConverter implements IdentifierProvider.Converter<ButtonInteraction> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InteractionIdentifier convert(ButtonInteraction context) {
|
||||||
|
return InteractionIdentifier.builder().name(context.getCustomId()).type(InteractionType.BUTTON).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,40 @@
|
|||||||
|
package net.tomatentum.marinara.wrapper.javacord.identifierconverter;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.javacord.api.interaction.SlashCommandInteraction;
|
||||||
|
import org.javacord.api.interaction.SlashCommandInteractionOption;
|
||||||
|
|
||||||
|
import net.tomatentum.marinara.interaction.InteractionType;
|
||||||
|
import net.tomatentum.marinara.interaction.ident.InteractionIdentifier;
|
||||||
|
import net.tomatentum.marinara.wrapper.IdentifierProvider;
|
||||||
|
|
||||||
|
public class SlashCommandIdentifierConverter implements IdentifierProvider.Converter<SlashCommandInteraction> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InteractionIdentifier convert(SlashCommandInteraction context) {
|
||||||
|
InteractionIdentifier last = InteractionIdentifier.builder()
|
||||||
|
.type(InteractionType.COMMAND)
|
||||||
|
.name(context.getCommandName())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
List<SlashCommandInteractionOption> options = context.getOptions();
|
||||||
|
if (!options.isEmpty()) {
|
||||||
|
last = InteractionIdentifier.builder()
|
||||||
|
.type(InteractionType.COMMAND)
|
||||||
|
.name(options.getFirst().getName())
|
||||||
|
.parent(last)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
List<SlashCommandInteractionOption> subOptions = context.getOptions().getFirst().getOptions();
|
||||||
|
if (!subOptions.isEmpty())
|
||||||
|
last = InteractionIdentifier.builder()
|
||||||
|
.type(InteractionType.COMMAND)
|
||||||
|
.name(subOptions.getFirst().getName())
|
||||||
|
.parent(last)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
return last;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -16,10 +16,10 @@ import net.tomatentum.marinara.Marinara;
|
|||||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||||
import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
|
import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
|
||||||
|
|
||||||
public class AutoCompleteTest {
|
class AutoCompleteTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAutocomplete() {
|
void testAutocomplete() {
|
||||||
|
|
||||||
SlashCommandInteractionOption optionMock = mock();
|
SlashCommandInteractionOption optionMock = mock();
|
||||||
AutocompleteInteraction autocompleteInteractionMock = mock();
|
AutocompleteInteraction autocompleteInteractionMock = mock();
|
||||||
@@ -33,7 +33,7 @@ public class AutoCompleteTest {
|
|||||||
|
|
||||||
LibraryWrapper wrapper = new JavacordWrapper(null); //null okay as we don't use the discord API in this test.
|
LibraryWrapper wrapper = new JavacordWrapper(null); //null okay as we don't use the discord API in this test.
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getRegistry().addInteractions(new TestAutocomplete());
|
marinara.getInteractionContainer().addAllMethods(new TestAutocomplete());
|
||||||
wrapper.handleInteraction(autocompleteInteractionMock);
|
wrapper.handleInteraction(autocompleteInteractionMock);
|
||||||
verify(autocompleteInteractionMock).respondWithChoices(any());
|
verify(autocompleteInteractionMock).respondWithChoices(any());
|
||||||
}
|
}
|
||||||
|
@@ -12,13 +12,13 @@ import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
|||||||
import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
|
import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
|
||||||
|
|
||||||
@TestInstance(Lifecycle.PER_CLASS)
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
public class ButtonTest {
|
class ButtonTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testButtonExecution() {
|
void testButtonExecution() {
|
||||||
LibraryWrapper wrapper = new JavacordWrapper(null); //null okay as we don't use the discord API in this test.
|
LibraryWrapper wrapper = new JavacordWrapper(null); //null okay as we don't use the discord API in this test.
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getRegistry().addInteractions(new TestButton());
|
marinara.getInteractionContainer().addAllMethods(new TestButton());
|
||||||
wrapper.handleInteraction(CommonMocks.getButtonInteractionMock("test"));
|
wrapper.handleInteraction(CommonMocks.getButtonInteractionMock("test"));
|
||||||
assertTrue(TestButton.didRun);
|
assertTrue(TestButton.didRun);
|
||||||
}
|
}
|
||||||
|
@@ -19,25 +19,25 @@ import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
|
|||||||
import net.tomatentum.marinara.wrapper.javacord.checks.PermissionCheck;
|
import net.tomatentum.marinara.wrapper.javacord.checks.PermissionCheck;
|
||||||
|
|
||||||
@TestInstance(Lifecycle.PER_CLASS)
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
public class InteractionCheckTest {
|
class InteractionCheckTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInteractionCheck() {
|
void testInteractionCheck() {
|
||||||
LibraryWrapper wrapper = new JavacordWrapper(null);
|
LibraryWrapper wrapper = new JavacordWrapper(null);
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getCheckRegistry().addCheck(new TestInteractionCheck());
|
marinara.getCheckContainer().addAllMethods(new TestInteractionCheck());
|
||||||
marinara.getRegistry().addInteractions(new TestButton());
|
marinara.getInteractionContainer().addAllMethods(new TestButton());
|
||||||
wrapper.handleInteraction(CommonMocks.getButtonInteractionMock("test"));
|
wrapper.handleInteraction(CommonMocks.getButtonInteractionMock("test"));
|
||||||
assertTrue(TestInteractionCheck.preExecuted);
|
assertTrue(TestInteractionCheck.preExecuted);
|
||||||
assertTrue(TestInteractionCheck.postExecuted);
|
assertTrue(TestInteractionCheck.postExecuted);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPermissionCheck() {
|
void testPermissionCheck() {
|
||||||
LibraryWrapper wrapper = new JavacordWrapper(null);
|
LibraryWrapper wrapper = new JavacordWrapper(null);
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getCheckRegistry().addCheck(new PermissionCheck());
|
marinara.getCheckContainer().addAllMethods(new PermissionCheck());
|
||||||
marinara.getRegistry().addInteractions(new TestButton());
|
marinara.getInteractionContainer().addAllMethods(new TestButton());
|
||||||
|
|
||||||
Server serverMock = mock();
|
Server serverMock = mock();
|
||||||
ButtonInteraction buttonInteractionMock = CommonMocks.getButtonInteractionMock("permissionCheck", serverMock);
|
ButtonInteraction buttonInteractionMock = CommonMocks.getButtonInteractionMock("permissionCheck", serverMock);
|
||||||
|
@@ -20,7 +20,7 @@ import net.tomatentum.marinara.Marinara;
|
|||||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||||
import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
|
import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
|
||||||
@TestInstance(Lifecycle.PER_CLASS)
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
public class SlashCommandTest {
|
class SlashCommandTest {
|
||||||
|
|
||||||
String DISCORD_TOKEN = System.getenv("DISCORD_TEST_TOKEN");
|
String DISCORD_TOKEN = System.getenv("DISCORD_TEST_TOKEN");
|
||||||
DiscordApi api;
|
DiscordApi api;
|
||||||
@@ -41,8 +41,8 @@ public class SlashCommandTest {
|
|||||||
@Test
|
@Test
|
||||||
void testSlashCommand() {
|
void testSlashCommand() {
|
||||||
Marinara marinara = Marinara.load(new JavacordWrapper(api));
|
Marinara marinara = Marinara.load(new JavacordWrapper(api));
|
||||||
marinara.getRegistry().addInteractions(new TestCommand());
|
marinara.getInteractionContainer().addAllMethods(new TestCommand());
|
||||||
marinara.getRegistry().registerCommands();
|
marinara.registerCommands();
|
||||||
System.out.println("Success!");
|
System.out.println("Success!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,7 +50,7 @@ public class SlashCommandTest {
|
|||||||
void testSlashCommandExecution() {
|
void testSlashCommandExecution() {
|
||||||
LibraryWrapper wrapper = new JavacordWrapper(api);
|
LibraryWrapper wrapper = new JavacordWrapper(api);
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getRegistry().addInteractions(new TestCommand());
|
marinara.getInteractionContainer().addAllMethods(new TestCommand());
|
||||||
|
|
||||||
SlashCommandInteractionOption optionMock = mock();
|
SlashCommandInteractionOption optionMock = mock();
|
||||||
SlashCommandInteraction interactionMock = mock();
|
SlashCommandInteraction interactionMock = mock();
|
||||||
|
@@ -5,18 +5,32 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
import org.javacord.api.interaction.AutocompleteInteraction;
|
import org.javacord.api.interaction.AutocompleteInteraction;
|
||||||
|
import org.javacord.api.interaction.SlashCommandInteraction;
|
||||||
|
|
||||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||||
import net.tomatentum.marinara.interaction.annotation.AutoComplete;
|
import net.tomatentum.marinara.interaction.annotation.AutoComplete;
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
||||||
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
||||||
|
import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType;
|
||||||
|
|
||||||
public class TestAutocomplete implements InteractionHandler {
|
public class TestAutocomplete implements InteractionHandler {
|
||||||
|
|
||||||
@SlashCommand(name = "test")
|
@SlashCommand(
|
||||||
@AutoComplete
|
name = "test",
|
||||||
|
options = @SlashCommandOption(
|
||||||
|
name = "foo",
|
||||||
|
type = SlashCommandOptionType.STRING,
|
||||||
|
autocompletes = @AutoComplete("testAuto")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
public void exec(SlashCommandInteraction context) {
|
||||||
|
//only here for command definition
|
||||||
|
}
|
||||||
|
|
||||||
|
@AutoComplete("testAuto")
|
||||||
public void autocomplete(AutocompleteInteraction context, String value) {
|
public void autocomplete(AutocompleteInteraction context, String value) {
|
||||||
System.out.println("Success!");
|
System.out.println("Success!");
|
||||||
assertEquals(value, "test");
|
assertEquals("test", value);
|
||||||
context.respondWithChoices(Collections.emptyList());
|
context.respondWithChoices(Collections.emptyList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -16,8 +16,8 @@ import net.tomatentum.marinara.wrapper.javacord.checks.PermissionCheck.HasPermis
|
|||||||
|
|
||||||
public class TestButton implements InteractionHandler {
|
public class TestButton implements InteractionHandler {
|
||||||
|
|
||||||
|
|
||||||
public static boolean didRun = false;
|
public static boolean didRun = false;
|
||||||
|
|
||||||
@Button("test")
|
@Button("test")
|
||||||
@TestCheck
|
@TestCheck
|
||||||
public void exec(ButtonInteraction interaction, TextChannel channel, Message message, User member, Server server) {
|
public void exec(ButtonInteraction interaction, TextChannel channel, Message message, User member, Server server) {
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
package net.tomatentum.marinara.test.javacord;
|
package net.tomatentum.marinara.test.javacord;
|
||||||
|
|
||||||
import net.tomatentum.marinara.interaction.commands.ChoiceValueProvider;
|
import net.tomatentum.marinara.interaction.commands.choice.ChoiceValueProvider;
|
||||||
|
|
||||||
public enum TestChoiceEnum implements ChoiceValueProvider<String> {
|
public enum TestChoiceEnum implements ChoiceValueProvider<String> {
|
||||||
TestValue("testValue"),
|
TestValue("testValue"),
|
||||||
|
@@ -5,11 +5,13 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||||||
import org.javacord.api.interaction.SlashCommandInteraction;
|
import org.javacord.api.interaction.SlashCommandInteraction;
|
||||||
|
|
||||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||||
|
import net.tomatentum.marinara.interaction.commands.annotation.CommandChoices;
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand;
|
||||||
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOption;
|
||||||
import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType;
|
import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType;
|
||||||
|
|
||||||
public class TestCommand implements InteractionHandler {
|
public class TestCommand implements InteractionHandler {
|
||||||
|
|
||||||
@SlashCommand(
|
@SlashCommand(
|
||||||
name = "test",
|
name = "test",
|
||||||
description = "testingen",
|
description = "testingen",
|
||||||
@@ -21,12 +23,13 @@ public class TestCommand implements InteractionHandler {
|
|||||||
name = "foo",
|
name = "foo",
|
||||||
description = "foo bar is very fooby",
|
description = "foo bar is very fooby",
|
||||||
type = SlashCommandOptionType.STRING,
|
type = SlashCommandOptionType.STRING,
|
||||||
choiceEnum = TestChoiceEnum.class
|
choices = @CommandChoices(cenum = TestChoiceEnum.class)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
public void exec(SlashCommandInteraction interaction, String test) {
|
public void exec(SlashCommandInteraction interaction, String test) {
|
||||||
assertEquals(test, "test");
|
assertEquals("test", test);
|
||||||
System.out.println("Success!");
|
System.out.println("Success!");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -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
|
Reference in New Issue
Block a user