Compare commits

..

No commits in common. "11fd16fa778077fee23d1ba0ffb345baa46102cc" and "9f87f47b1ff8c5f25340efb1d19b6064e0f0b1b0" have entirely different histories.

7 changed files with 16 additions and 292 deletions

@ -1,6 +1,5 @@
package net.tomatentum.marinara.interaction.methods;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.InvalidParameterException;
import java.util.ArrayList;
@ -53,8 +52,8 @@ public abstract class InteractionMethod {
}
method.setAccessible(true);
try {
method.invoke(handler, parameters.toArray());
}catch (IllegalAccessException | InvocationTargetException ex) {
method.invoke(handler, parameters);
}catch (Exception ex) {
throw new RuntimeException(ex);
}
}

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

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

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

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

@ -1,147 +0,0 @@
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";
}
}

@ -1,97 +0,0 @@
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();
}
}