diff --git a/src/main/java/com/github/ygimenez/method/Pages.java b/src/main/java/com/github/ygimenez/method/Pages.java index 145330a..68981c9 100644 --- a/src/main/java/com/github/ygimenez/method/Pages.java +++ b/src/main/java/com/github/ygimenez/method/Pages.java @@ -597,7 +597,7 @@ public static ActionReference paginate(@NotNull Message msg, @NotNull PaginateHe public void acceptThrows(@NotNull User u, @NotNull PaginationEventWrapper wrapper) { Message msg = wrapper.retrieveMessage(); - if (helper.canInteract(u)) { + if (helper.canInteract(u, wrapper)) { if (u.isBot() || msg == null || !wrapper.getMessageId().equals(msg.getId())) return; Emote emt = NONE; @@ -868,7 +868,7 @@ public static ActionReference categorize(@NotNull Message msg, @NotNull Categori public void acceptThrows(@NotNull User u, @NotNull PaginationEventWrapper wrapper) { Message m = wrapper.retrieveMessage(); - if (helper.canInteract(u)) { + if (helper.canInteract(u, wrapper)) { if (u.isBot() || m == null || !wrapper.getMessageId().equals(msg.getId())) return; Emoji emoji = null; @@ -1178,7 +1178,7 @@ public static ActionReference buttonize(@NotNull Message msg, @NotNull Buttonize public void acceptThrows(@NotNull User u, @NotNull PaginationEventWrapper wrapper) { Message m = wrapper.retrieveMessage(); - if (helper.canInteract(u)) { + if (helper.canInteract(u, wrapper)) { if (u.isBot() || m == null || !wrapper.getMessageId().equals(msg.getId())) return; Emoji emoji = null; @@ -1518,7 +1518,7 @@ public static ActionReference lazyPaginate(@NotNull Message msg, @NotNull LazyPa public void acceptThrows(@NotNull User u, @NotNull PaginationEventWrapper wrapper) { Message msg = wrapper.retrieveMessage(); - if (helper.canInteract(u)) { + if (helper.canInteract(u, wrapper)) { if (u.isBot() || msg == null || !wrapper.getMessageId().equals(msg.getId())) return; Emote emt = NONE; diff --git a/src/main/java/com/github/ygimenez/model/Paginator.java b/src/main/java/com/github/ygimenez/model/Paginator.java index 4134cbc..07771ac 100644 --- a/src/main/java/com/github/ygimenez/model/Paginator.java +++ b/src/main/java/com/github/ygimenez/model/Paginator.java @@ -206,7 +206,7 @@ public Logger getLogger() { */ public void log(LogLevel level, String msg, Throwable t) { if (PUtilsConfig.getLogLevel().compareTo(level) >= 0) { - logger.error("[" + level.name().replace("_", " ") + "] " + msg, t); + logger.error("[{}] {}", level.name().replace("_", " "), msg, t); } } @@ -220,7 +220,7 @@ public void log(LogLevel level, String msg, Throwable t) { */ public void log(LogLevel level, String msg) { if (PUtilsConfig.getLogLevel().compareTo(level) >= 0) { - logger.error("[" + level.name().replace("_", " ") + "] " + msg); + logger.error("[{}] {}", level.name().replace("_", " "), msg); } } } diff --git a/src/main/java/com/github/ygimenez/model/helper/BaseHelper.java b/src/main/java/com/github/ygimenez/model/helper/BaseHelper.java index 444803b..bd2ae64 100644 --- a/src/main/java/com/github/ygimenez/model/helper/BaseHelper.java +++ b/src/main/java/com/github/ygimenez/model/helper/BaseHelper.java @@ -1,16 +1,19 @@ package com.github.ygimenez.model.helper; import com.github.ygimenez.method.Pages; +import com.github.ygimenez.model.PaginationEventWrapper; import com.github.ygimenez.type.Emote; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.interactions.components.Component; import net.dv8tion.jda.api.interactions.components.LayoutComponent; +import net.dv8tion.jda.api.interactions.components.buttons.Button; import net.dv8tion.jda.api.utils.messages.MessageRequest; import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.concurrent.TimeUnit; +import java.util.function.BiPredicate; import java.util.function.Predicate; /** @@ -28,13 +31,13 @@ public abstract class BaseHelper, T> { private boolean cancellable = true; private long time = 0; - private Predicate canInteract = null; + private BiPredicate canInteract = null; /** * Constructor for {@link BaseHelper}. * - * @param subClass A class that extends {@link BaseHelper}. - * @param buttons The collection used to store pages. + * @param subClass A class that extends {@link BaseHelper}. + * @param buttons The collection used to store pages. * @param useButtons Whether to use interaction buttons or not. */ protected BaseHelper(Class subClass, T buttons, boolean useButtons) { @@ -109,11 +112,17 @@ public Helper setTimeout(int time, TimeUnit unit) { /** * Checks whether the supplied {@link User} can interact with the event. * - * @param user The {@link User} to check. + * @param user The {@link User} to check. + * @param wrapper The click event. * @return Whether the suppied user can interact with the event. */ - public boolean canInteract(User user) { - return canInteract == null || canInteract.test(user); + public boolean canInteract(User user, PaginationEventWrapper wrapper) { + Button btn = null; + if (wrapper.getContent() instanceof Button) { + btn = (Button) wrapper.getContent(); + } + + return canInteract == null || canInteract.test(user, btn); } /** @@ -123,6 +132,18 @@ public boolean canInteract(User user) { * @return The {@link Helper} instance for chaining convenience. */ public Helper setCanInteract(@Nullable Predicate canInteract) { + this.canInteract = canInteract == null ? null : (u, b) -> canInteract.test(u); + return subClass.cast(this); + } + + /** + * Set the condition used to check if a given user can interact with the event buttons. + * + * @param canInteract A {@link BiPredicate} for checking if a given user can interact with the buttons, also giving + * which {@link Button} was pressed (will be null if using reaction buttons). (default: null). + * @return The {@link Helper} instance for chaining convenience. + */ + public Helper setCanInteract(@Nullable BiPredicate canInteract) { this.canInteract = canInteract; return subClass.cast(this); }