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
[versions]
commons-math3 = "3.6.1"
guava = "33.0.0-jre"
junit-jupiter = "5.10.2"
log4j = "2.24.1"
javacord = "3.8.0"
geantyref = "2.0.0"
[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" }
log4j = { module = "org.apache.logging.log4j:log4j-core", version.ref = "log4j"}
javacord = { module = "org.javacord:javacord", version.ref = "javacord"}

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

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

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

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