Compare commits

...

4 Commits

Author SHA1 Message Date
11fd16fa77
Add Command Execution test 2024-11-01 17:38:02 +01:00
bbeb58e5e4
fix isAssignableFrom order 2024-11-01 17:37:49 +01:00
e39ac2d67e
fix random nulls in list 2024-11-01 17:37:36 +01:00
0f337696c1
change to selective catching 2024-11-01 17:36:12 +01:00
7 changed files with 291 additions and 15 deletions

@ -1,5 +1,6 @@
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.security.InvalidParameterException;
import java.util.ArrayList; import java.util.ArrayList;
@ -52,8 +53,8 @@ public abstract class InteractionMethod {
} }
method.setAccessible(true); method.setAccessible(true);
try { try {
method.invoke(handler, parameters); method.invoke(handler, parameters.toArray());
}catch (Exception ex) { }catch (IllegalAccessException | InvocationTargetException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
} }

@ -3,7 +3,6 @@ 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;
@ -26,14 +25,15 @@ public class InteractionRegistry {
public void addInteractions(InteractionHandler interactionHandler) { public void addInteractions(InteractionHandler interactionHandler) {
for (Method method : interactionHandler.getClass().getMethods()) { for (Method method : interactionHandler.getClass().getMethods()) {
interactionMethods.add(InteractionMethod.create(method, interactionHandler, wrapper)); InteractionMethod iMethod = InteractionMethod.create(method, interactionHandler, wrapper);
if (iMethod != null)
this.interactionMethods.add(iMethod);
} }
} }
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();

@ -58,7 +58,7 @@ public class JavacordWrapper extends LibraryWrapper {
@Override @Override
public InteractionType getInteractionType(Class<?> clazz) { public InteractionType getInteractionType(Class<?> clazz) {
if (clazz.isAssignableFrom(ApplicationCommandInteraction.class)) if (ApplicationCommandInteraction.class.isAssignableFrom(clazz))
return InteractionType.COMMAND; return InteractionType.COMMAND;
return null; return null;

@ -2,24 +2,52 @@ package net.tomatentum.marinara.test;
import org.javacord.api.DiscordApi; import org.javacord.api.DiscordApi;
import org.javacord.api.DiscordApiBuilder; import org.javacord.api.DiscordApiBuilder;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import net.tomatentum.marinara.Marinara; import net.tomatentum.marinara.Marinara;
import net.tomatentum.marinara.test.mocks.SlashCommandInteractionMock;
import net.tomatentum.marinara.wrapper.LibraryWrapper;
import net.tomatentum.marinare.wrapper.javacord.JavacordWrapper; import net.tomatentum.marinare.wrapper.javacord.JavacordWrapper;
@TestInstance(Lifecycle.PER_CLASS)
public class SlashCommandTest { public class SlashCommandTest {
String DISCORD_TOKEN = System.getenv("DISCORD_TEST_TOKEN"); String DISCORD_TOKEN = System.getenv("DISCORD_TEST_TOKEN");
DiscordApi api;
@Test @BeforeAll
public void testSlashCommand() { void setUp() {
DiscordApi api = new DiscordApiBuilder() api = new DiscordApiBuilder()
.setToken(DISCORD_TOKEN) .setToken(DISCORD_TOKEN)
.login().join(); .login().join();
}
@AfterAll
void tearDown() {
api.disconnect();
api = null;
}
@Test
void testSlashCommand() {
Marinara marinara = Marinara.load(new JavacordWrapper(api)); Marinara marinara = Marinara.load(new JavacordWrapper(api));
marinara.getRegistry().addInteractions(new TestCommand()); marinara.getRegistry().addInteractions(new TestCommand());
marinara.getRegistry().registerCommands(); marinara.getRegistry().registerCommands();
System.out.println("done"); System.out.println("Success!");
} }
@Test
void testSlashCommandExecution() {
LibraryWrapper wrapper = new JavacordWrapper(api);
Marinara marinara = Marinara.load(wrapper);
marinara.getRegistry().addInteractions(new TestCommand());
wrapper.handleInteraction(new SlashCommandInteractionMock());
}
} }

@ -1,5 +1,7 @@
package net.tomatentum.marinara.test; package net.tomatentum.marinara.test;
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;
@ -16,13 +18,14 @@ public class TestCommand implements InteractionHandler {
}, },
options = { options = {
@SlashCommandOption( @SlashCommandOption(
name = "pommes", name = "foo",
description = "mit Fett", description = "foo bar is very fooby",
type = SlashCommandOptionType.MENTIONABLE type = SlashCommandOptionType.STRING
) )
} }
) )
public void exec(SlashCommandInteraction interaction) { public void exec(SlashCommandInteraction interaction, String test) {
assertEquals(test, "test");
System.out.println("Success!"); System.out.println("Success!");
} }
} }

@ -0,0 +1,147 @@
package net.tomatentum.marinara.test.mocks;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import org.javacord.api.DiscordApi;
import org.javacord.api.entity.channel.TextChannel;
import org.javacord.api.entity.message.component.HighLevelComponent;
import org.javacord.api.entity.permission.PermissionType;
import org.javacord.api.entity.server.Server;
import org.javacord.api.entity.user.User;
import org.javacord.api.interaction.DiscordLocale;
import org.javacord.api.interaction.InteractionType;
import org.javacord.api.interaction.SlashCommandInteraction;
import org.javacord.api.interaction.SlashCommandInteractionOption;
import org.javacord.api.interaction.callback.InteractionFollowupMessageBuilder;
import org.javacord.api.interaction.callback.InteractionImmediateResponseBuilder;
import org.javacord.api.interaction.callback.InteractionOriginalResponseUpdater;
public class SlashCommandInteractionMock implements SlashCommandInteraction{
@Override
public long getCommandId() {
throw new UnsupportedOperationException("Unimplemented method 'getCommandId'");
}
@Override
public String getCommandIdAsString() {
throw new UnsupportedOperationException("Unimplemented method 'getCommandIdAsString'");
}
@Override
public String getCommandName() {
return "test";
}
@Override
public Optional<Long> getRegisteredCommandServerId() {
throw new UnsupportedOperationException("Unimplemented method 'getRegisteredCommandServerId'");
}
@Override
public long getApplicationId() {
throw new UnsupportedOperationException("Unimplemented method 'getApplicationId'");
}
@Override
public InteractionType getType() {
return InteractionType.APPLICATION_COMMAND;
}
@Override
public InteractionImmediateResponseBuilder createImmediateResponder() {
throw new UnsupportedOperationException("Unimplemented method 'createImmediateResponder'");
}
@Override
public CompletableFuture<InteractionOriginalResponseUpdater> respondLater() {
throw new UnsupportedOperationException("Unimplemented method 'respondLater'");
}
@Override
public CompletableFuture<InteractionOriginalResponseUpdater> respondLater(boolean ephemeral) {
throw new UnsupportedOperationException("Unimplemented method 'respondLater'");
}
@Override
public CompletableFuture<Void> respondWithModal(String customId, String title,
List<HighLevelComponent> components) {
throw new UnsupportedOperationException("Unimplemented method 'respondWithModal'");
}
@Override
public InteractionFollowupMessageBuilder createFollowupMessageBuilder() {
throw new UnsupportedOperationException("Unimplemented method 'createFollowupMessageBuilder'");
}
@Override
public Optional<Server> getServer() {
throw new UnsupportedOperationException("Unimplemented method 'getServer'");
}
@Override
public Optional<TextChannel> getChannel() {
throw new UnsupportedOperationException("Unimplemented method 'getChannel'");
}
@Override
public User getUser() {
throw new UnsupportedOperationException("Unimplemented method 'getUser'");
}
@Override
public String getToken() {
throw new UnsupportedOperationException("Unimplemented method 'getToken'");
}
@Override
public int getVersion() {
throw new UnsupportedOperationException("Unimplemented method 'getVersion'");
}
@Override
public DiscordLocale getLocale() {
throw new UnsupportedOperationException("Unimplemented method 'getLocale'");
}
@Override
public Optional<DiscordLocale> getServerLocale() {
throw new UnsupportedOperationException("Unimplemented method 'getServerLocale'");
}
@Override
public Optional<EnumSet<PermissionType>> getBotPermissions() {
throw new UnsupportedOperationException("Unimplemented method 'getBotPermissions'");
}
@Override
public DiscordApi getApi() {
throw new UnsupportedOperationException("Unimplemented method 'getApi'");
}
@Override
public long getId() {
throw new UnsupportedOperationException("Unimplemented method 'getId'");
}
@Override
public List<SlashCommandInteractionOption> getOptions() {
return Arrays.asList(new SlashCommandInteractionOptionMock());
}
@Override
public List<SlashCommandInteractionOption> getArguments() {
return Arrays.asList(new SlashCommandInteractionOptionMock());
}
@Override
public String getFullCommandName() {
return "test";
}
}

@ -0,0 +1,97 @@
package net.tomatentum.marinara.test.mocks;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import org.javacord.api.entity.Attachment;
import org.javacord.api.entity.Mentionable;
import org.javacord.api.entity.channel.ServerChannel;
import org.javacord.api.entity.permission.Role;
import org.javacord.api.entity.user.User;
import org.javacord.api.interaction.SlashCommandInteractionOption;
public class SlashCommandInteractionOptionMock implements SlashCommandInteractionOption{
@Override
public List<SlashCommandInteractionOption> getArguments() {
return Collections.emptyList();
}
@Override
public String getName() {
return "foo";
}
@Override
public Optional<Boolean> isFocused() {
throw new UnsupportedOperationException("Unimplemented method 'isFocused'");
}
@Override
public Optional<String> getStringRepresentationValue() {
throw new UnsupportedOperationException("Unimplemented method 'getStringRepresentationValue'");
}
@Override
public Optional<String> getStringValue() {
return Optional.of("test");
}
@Override
public Optional<Long> getLongValue() {
throw new UnsupportedOperationException("Unimplemented method 'getLongValue'");
}
@Override
public Optional<Boolean> getBooleanValue() {
throw new UnsupportedOperationException("Unimplemented method 'getBooleanValue'");
}
@Override
public Optional<User> getUserValue() {
throw new UnsupportedOperationException("Unimplemented method 'getUserValue'");
}
@Override
public Optional<CompletableFuture<User>> requestUserValue() {
throw new UnsupportedOperationException("Unimplemented method 'requestUserValue'");
}
@Override
public Optional<ServerChannel> getChannelValue() {
throw new UnsupportedOperationException("Unimplemented method 'getChannelValue'");
}
@Override
public Optional<Attachment> getAttachmentValue() {
throw new UnsupportedOperationException("Unimplemented method 'getAttachmentValue'");
}
@Override
public Optional<Role> getRoleValue() {
throw new UnsupportedOperationException("Unimplemented method 'getRoleValue'");
}
@Override
public Optional<Mentionable> getMentionableValue() {
throw new UnsupportedOperationException("Unimplemented method 'getMentionableValue'");
}
@Override
public Optional<Double> getDecimalValue() {
throw new UnsupportedOperationException("Unimplemented method 'getDecimalValue'");
}
@Override
public Optional<CompletableFuture<Mentionable>> requestMentionableValue() {
throw new UnsupportedOperationException("Unimplemented method 'requestMentionableValue'");
}
@Override
public List<SlashCommandInteractionOption> getOptions() {
return Collections.emptyList();
}
}