Compare commits
5 Commits
4332592dfa
...
29bb7e667e
Author | SHA1 | Date | |
---|---|---|---|
29bb7e667e | |||
94da2a0e3c | |||
7f47130461 | |||
83a3efd4b8 | |||
aefd8a51a0 |
@ -14,7 +14,7 @@ public record AppliedCheck(InteractionCheck<?> check, Annotation annotation) {
|
||||
.filter(x -> x.getName().equals("preExec"))
|
||||
.filter(x -> !x.isBridge())
|
||||
.toArray(s -> new Method[s]);
|
||||
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.getClass());
|
||||
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.annotationType());
|
||||
method.setAccessible(true);
|
||||
try {
|
||||
return (boolean) method.invoke(check, context, annotation);
|
||||
@ -29,7 +29,7 @@ public record AppliedCheck(InteractionCheck<?> check, Annotation annotation) {
|
||||
.filter(x -> x.getName().equals("postExec"))
|
||||
.filter(x -> !x.isBridge())
|
||||
.toArray(s -> new Method[s]);
|
||||
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.getClass());
|
||||
Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.annotationType());
|
||||
method.setAccessible(true);
|
||||
try {
|
||||
method.invoke(check, context, annotation);
|
||||
|
@ -54,7 +54,7 @@ public class InteractionRegistry {
|
||||
public void handle(Object context) {
|
||||
interactionMethods.forEach((m) -> {
|
||||
InteractionType type = marinara.getWrapper().getInteractionType(context.getClass());
|
||||
if (m.getType().equals(type))
|
||||
if (m.getType().equals(type) && m.canRun(context))
|
||||
m.run(context);
|
||||
});
|
||||
}
|
||||
|
@ -23,6 +23,10 @@ public final class ReflectionUtil {
|
||||
}
|
||||
|
||||
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.");
|
||||
}
|
||||
@ -77,7 +81,7 @@ public final class ReflectionUtil {
|
||||
Class<?> currMostSpecific = null;
|
||||
for (Class<?> currClass : classes) {
|
||||
int currCastDepth = getCastDepth(base, currClass);
|
||||
if (currCastDepth < min) {
|
||||
if (currCastDepth <= min) {
|
||||
min = currCastDepth;
|
||||
currMostSpecific = currClass;
|
||||
}
|
||||
|
6
wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/checks/PermissionCheck.java
6
wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/checks/PermissionCheck.java
@ -1,5 +1,9 @@
|
||||
package net.tomatentum.marinara.wrapper.javacord.checks;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.javacord.api.entity.permission.PermissionType;
|
||||
@ -10,6 +14,8 @@ import net.tomatentum.marinara.checks.InteractionCheck;
|
||||
|
||||
public class PermissionCheck implements InteractionCheck<PermissionCheck.HasPermission> {
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public static @interface HasPermission {
|
||||
public PermissionType[] value();
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ public class ButtonTest {
|
||||
LibraryWrapper wrapper = new JavacordWrapper(new DiscordApiMock()); //null okay as we don't use the discord API in this test.
|
||||
Marinara marinara = Marinara.load(wrapper);
|
||||
marinara.getRegistry().addInteractions(new TestButton());
|
||||
wrapper.handleInteraction(new ButtonInteractionMock());
|
||||
wrapper.handleInteraction(new ButtonInteractionMock("test"));
|
||||
assertTrue(TestButton.didRun);
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,9 @@
|
||||
package net.tomatentum.marinara.test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.javacord.api.entity.permission.PermissionType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
@ -9,8 +11,10 @@ import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
import net.tomatentum.marinara.Marinara;
|
||||
import net.tomatentum.marinara.test.mocks.ButtonInteractionMock;
|
||||
import net.tomatentum.marinara.test.mocks.DiscordApiMock;
|
||||
import net.tomatentum.marinara.test.mocks.ServerMock;
|
||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||
import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
|
||||
import net.tomatentum.marinara.wrapper.javacord.checks.PermissionCheck;
|
||||
|
||||
@TestInstance(Lifecycle.PER_CLASS)
|
||||
public class InteractionCheckTest {
|
||||
@ -21,9 +25,23 @@ public class InteractionCheckTest {
|
||||
Marinara marinara = Marinara.load(wrapper);
|
||||
marinara.getCheckRegistry().addCheck(new TestInteractionCheck());
|
||||
marinara.getRegistry().addInteractions(new TestButton());
|
||||
wrapper.handleInteraction(new ButtonInteractionMock());
|
||||
wrapper.handleInteraction(new ButtonInteractionMock("test"));
|
||||
assertTrue(TestInteractionCheck.preExecuted);
|
||||
assertTrue(TestInteractionCheck.postExecuted);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPermissionCheck() {
|
||||
LibraryWrapper wrapper = new JavacordWrapper(new DiscordApiMock());
|
||||
Marinara marinara = Marinara.load(wrapper);
|
||||
marinara.getCheckRegistry().addCheck(new PermissionCheck());
|
||||
marinara.getRegistry().addInteractions(new TestButton());
|
||||
wrapper.handleInteraction(new ButtonInteractionMock("permissionCheck"));
|
||||
assertTrue(TestButton.didPermRun);
|
||||
TestButton.didPermRun = false;
|
||||
ServerMock.TESTPERMISSION = PermissionType.ATTACH_FILE;
|
||||
wrapper.handleInteraction(new ButtonInteractionMock("permissionCheck"));
|
||||
assertFalse(TestButton.didPermRun);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import org.javacord.api.entity.channel.TextChannel;
|
||||
import org.javacord.api.entity.message.Message;
|
||||
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.ButtonInteraction;
|
||||
@ -11,6 +12,7 @@ import org.javacord.api.interaction.ButtonInteraction;
|
||||
import net.tomatentum.marinara.interaction.InteractionHandler;
|
||||
import net.tomatentum.marinara.interaction.annotation.Button;
|
||||
import net.tomatentum.marinara.test.TestInteractionCheck.TestCheck;
|
||||
import net.tomatentum.marinara.wrapper.javacord.checks.PermissionCheck.HasPermission;
|
||||
|
||||
public class TestButton implements InteractionHandler {
|
||||
|
||||
@ -27,5 +29,14 @@ public class TestButton implements InteractionHandler {
|
||||
didRun = true;
|
||||
System.out.println("Success!");
|
||||
}
|
||||
|
||||
public static boolean didPermRun = false;
|
||||
|
||||
@Button("permissionCheck")
|
||||
@HasPermission({PermissionType.ADMINISTRATOR})
|
||||
public void exec(ButtonInteraction interaction) {
|
||||
didPermRun = true;
|
||||
System.out.println("It worked!");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -23,6 +23,12 @@ import org.javacord.api.interaction.callback.InteractionOriginalResponseUpdater;
|
||||
|
||||
public class ButtonInteractionMock implements ButtonInteraction {
|
||||
|
||||
private String customId;
|
||||
|
||||
public ButtonInteractionMock(String customId) {
|
||||
this.customId = customId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Message getMessage() {
|
||||
return new MessageMock();
|
||||
@ -30,7 +36,7 @@ public class ButtonInteractionMock implements ButtonInteraction {
|
||||
|
||||
@Override
|
||||
public String getCustomId() {
|
||||
return "test";
|
||||
return this.customId;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,5 +1,7 @@
|
||||
package net.tomatentum.marinara.test.mocks;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Collection;
|
||||
import java.util.EnumSet;
|
||||
@ -27,6 +29,7 @@ import org.javacord.api.entity.channel.ServerTextChannel;
|
||||
import org.javacord.api.entity.channel.ServerThreadChannel;
|
||||
import org.javacord.api.entity.channel.ServerVoiceChannel;
|
||||
import org.javacord.api.entity.emoji.KnownCustomEmoji;
|
||||
import org.javacord.api.entity.permission.PermissionType;
|
||||
import org.javacord.api.entity.permission.Role;
|
||||
import org.javacord.api.entity.server.ActiveThreads;
|
||||
import org.javacord.api.entity.server.Ban;
|
||||
@ -2259,5 +2262,12 @@ public class ServerMock implements Server {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'getSystemChannelFlags'");
|
||||
}
|
||||
public static PermissionType TESTPERMISSION = PermissionType.ADMINISTRATOR;
|
||||
@Override
|
||||
public boolean hasPermissions(User user, PermissionType... type) {
|
||||
assertNotNull(user);
|
||||
assertNotNull(type);
|
||||
return TESTPERMISSION.equals(type[0]);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user