Skip to content

Commit

Permalink
Added which button was pressed to BaseHelper.canInteract() for better…
Browse files Browse the repository at this point in the history
… control over click permission.
  • Loading branch information
ygimenez committed May 31, 2024
1 parent 8465512 commit 43e7b63
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/github/ygimenez/method/Pages.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/github/ygimenez/model/Paginator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}
}
33 changes: 27 additions & 6 deletions src/main/java/com/github/ygimenez/model/helper/BaseHelper.java
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -28,13 +31,13 @@ public abstract class BaseHelper<Helper extends BaseHelper<Helper, T>, T> {

private boolean cancellable = true;
private long time = 0;
private Predicate<User> canInteract = null;
private BiPredicate<User, Button> 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<Helper> subClass, T buttons, boolean useButtons) {
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -123,6 +132,18 @@ public boolean canInteract(User user) {
* @return The {@link Helper} instance for chaining convenience.
*/
public Helper setCanInteract(@Nullable Predicate<User> 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<User, Button> canInteract) {
this.canInteract = canInteract;
return subClass.cast(this);
}
Expand Down

0 comments on commit 43e7b63

Please sign in to comment.