Compare commits

..

6 Commits

Author SHA1 Message Date
9f87f47b1f
Add Javacord test 2024-11-01 16:07:18 +01:00
9d81522429
switch to user create wrapper instances 2024-11-01 16:07:10 +01:00
7888819f6e
rework class invariant checks and cast issues 2024-11-01 16:06:52 +01:00
3d5201329b
Add missing initialisation and nonNull filter 2024-11-01 16:05:33 +01:00
4b835187b5
remove unused dependencies 2024-11-01 16:05:04 +01:00
3778f45cf3
fix wrong logic 2024-11-01 15:30:50 +01:00
7 changed files with 71 additions and 32 deletions

@ -2,16 +2,12 @@
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format # https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format
[versions] [versions]
commons-math3 = "3.6.1"
guava = "33.0.0-jre"
junit-jupiter = "5.10.2" junit-jupiter = "5.10.2"
log4j = "2.24.1" log4j = "2.24.1"
javacord = "3.8.0" javacord = "3.8.0"
geantyref = "2.0.0" geantyref = "2.0.0"
[libraries] [libraries]
commons-math3 = { module = "org.apache.commons:commons-math3", version.ref = "commons-math3" }
guava = { module = "com.google.guava:guava", version.ref = "guava" }
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-core", version.ref = "log4j"} log4j = { module = "org.apache.logging.log4j:log4j-core", version.ref = "log4j"}
javacord = { module = "org.javacord:javacord", version.ref = "javacord"} javacord = { module = "org.javacord:javacord", version.ref = "javacord"}

@ -1,24 +1,13 @@
package net.tomatentum.marinara; package net.tomatentum.marinara;
import java.lang.reflect.Constructor;
import net.tomatentum.marinara.registry.InteractionRegistry; import net.tomatentum.marinara.registry.InteractionRegistry;
import net.tomatentum.marinara.wrapper.LibraryWrapper; import net.tomatentum.marinara.wrapper.LibraryWrapper;
public class Marinara { public class Marinara {
public static <T extends LibraryWrapper> Marinara load(Class<T> clazz) { public static <T extends LibraryWrapper> Marinara load(LibraryWrapper wrapper) {
try {
Constructor<T> ctor = clazz.getConstructor();
ctor.setAccessible(true);
T wrapper = ctor.newInstance();
InteractionRegistry registry = new InteractionRegistry(wrapper); InteractionRegistry registry = new InteractionRegistry(wrapper);
return new Marinara(registry); return new Marinara(registry);
}catch (Exception ex) {
System.err.println(ex);
System.exit(100);
return null;
}
} }
private InteractionRegistry registry; private InteractionRegistry registry;

@ -12,7 +12,7 @@ import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup;
public class SlashCommandDefinition { public class SlashCommandDefinition {
private List<ExecutableSlashCommandDefinition> executableDefinitons; private List<ExecutableSlashCommandDefinition> executableDefinitons;
private SlashCommand slashCommand; private SlashCommand slashCommand;
private boolean isRootCommand = false; private boolean isRootCommand;
public SlashCommandDefinition(SlashCommand applicationCommand) { public SlashCommandDefinition(SlashCommand applicationCommand) {
this.executableDefinitons = new ArrayList<>(); this.executableDefinitons = new ArrayList<>();
@ -23,16 +23,14 @@ public class SlashCommandDefinition {
if (def.applicationCommand() != null) { if (def.applicationCommand() != null) {
if (slashCommand == null) if (slashCommand == null)
this.slashCommand = def.applicationCommand(); this.slashCommand = def.applicationCommand();
if (!this.slashCommand.equals(def.applicationCommand())) if (!this.slashCommand.name().equals(def.applicationCommand().name()))
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"); 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 (isRootCommand) {
if (!def.isRootCommand()) if (executableDefinitons.isEmpty())
throw new IllegalArgumentException(def + ": cannot have subcommands and rootcommand definitions together"); this.isRootCommand = def.isRootCommand();
long subCommandAmount = executableDefinitons.stream()
.filter((x) -> !x.isRootCommand()) if ((isRootCommand && !def.isRootCommand()) || (!isRootCommand && def.isRootCommand())) {
.count();
if (subCommandAmount > 0)
throw new IllegalArgumentException(def + ": cannot have subcommands and rootcommand definitions together"); throw new IllegalArgumentException(def + ": cannot have subcommands and rootcommand definitions together");
} }
@ -53,7 +51,7 @@ public class SlashCommandDefinition {
subCommandGroupMap.put(x.name(), x); subCommandGroupMap.put(x.name(), x);
}); });
return (SubCommandGroup[]) subCommandGroupMap.values().toArray(); return subCommandGroupMap.values().toArray(new SubCommandGroup[0]);
} }
public SubCommand[] getSubCommands(String groupName) { public SubCommand[] getSubCommands(String groupName) {
@ -76,7 +74,7 @@ public class SlashCommandDefinition {
subCommandMap.put(x.name(), x); subCommandMap.put(x.name(), x);
}); });
return (SubCommand[]) subCommandMap.values().toArray(); return subCommandMap.values().toArray(new SubCommand[0]);
} }
public SlashCommand getFullSlashCommand() { public SlashCommand getFullSlashCommand() {

@ -3,6 +3,7 @@ package net.tomatentum.marinara.registry;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import net.tomatentum.marinara.interaction.InteractionHandler; import net.tomatentum.marinara.interaction.InteractionHandler;
@ -18,6 +19,7 @@ public class InteractionRegistry {
private LibraryWrapper wrapper; private LibraryWrapper wrapper;
public InteractionRegistry(LibraryWrapper wrapper) { public InteractionRegistry(LibraryWrapper wrapper) {
this.interactionMethods = new ArrayList<>();
this.wrapper = wrapper; this.wrapper = wrapper;
wrapper.subscribeInteractions(this::handle); wrapper.subscribeInteractions(this::handle);
} }
@ -31,6 +33,7 @@ public class InteractionRegistry {
public void registerCommands() { public void registerCommands() {
List<SlashCommandDefinition> defs = new ArrayList<>(); List<SlashCommandDefinition> defs = new ArrayList<>();
List<ExecutableSlashCommandDefinition> execDefs = interactionMethods.stream() List<ExecutableSlashCommandDefinition> execDefs = interactionMethods.stream()
.filter(Objects::nonNull)
.filter((x) -> x.getClass().isAssignableFrom(SlashCommandInteractionMethod.class)) .filter((x) -> x.getClass().isAssignableFrom(SlashCommandInteractionMethod.class))
.map((x) -> ((SlashCommandInteractionMethod)x).getCommandDefinition()) .map((x) -> ((SlashCommandInteractionMethod)x).getCommandDefinition())
.toList(); .toList();

@ -30,8 +30,8 @@ public final class ReflectionUtil {
if (!isAnnotationPresent(method, SlashCommand.class)) if (!isAnnotationPresent(method, SlashCommand.class))
throw new RuntimeException(method.getName() + ": Missing ApplicationCommand Annotation on either Class or Method"); throw new RuntimeException(method.getName() + ": Missing ApplicationCommand Annotation on either Class or Method");
if (!(method.isAnnotationPresent(SubCommand.class) && if ((method.isAnnotationPresent(SubCommand.class) &&
isAnnotationPresent(method, SlashCommand.class))) { !isAnnotationPresent(method, SlashCommand.class))) {
throw new RuntimeException(method.getName() + ": Missing ApplicationCommand Annotation on either Method or Class"); throw new RuntimeException(method.getName() + ": Missing ApplicationCommand Annotation on either Method or Class");
} }
} }

@ -0,0 +1,25 @@
package net.tomatentum.marinara.test;
import org.javacord.api.DiscordApi;
import org.javacord.api.DiscordApiBuilder;
import org.junit.jupiter.api.Test;
import net.tomatentum.marinara.Marinara;
import net.tomatentum.marinare.wrapper.javacord.JavacordWrapper;
public class SlashCommandTest {
String DISCORD_TOKEN = System.getenv("DISCORD_TEST_TOKEN");
@Test
public void testSlashCommand() {
DiscordApi api = new DiscordApiBuilder()
.setToken(DISCORD_TOKEN)
.login().join();
Marinara marinara = Marinara.load(new JavacordWrapper(api));
marinara.getRegistry().addInteractions(new TestCommand());
marinara.getRegistry().registerCommands();
System.out.println("done");
}
}

@ -0,0 +1,28 @@
package net.tomatentum.marinara.test;
import org.javacord.api.interaction.SlashCommandInteraction;
import net.tomatentum.marinara.interaction.InteractionHandler;
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 TestCommand implements InteractionHandler {
@SlashCommand(
name = "test",
description = "testingen",
serverIds = {
1037753048602255440L
},
options = {
@SlashCommandOption(
name = "pommes",
description = "mit Fett",
type = SlashCommandOptionType.MENTIONABLE
)
}
)
public void exec(SlashCommandInteraction interaction) {
System.out.println("Success!");
}
}