Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.github.ygimenez.method;
import com.github.ygimenez.exception.AlreadyActivatedException;
import com.github.ygimenez.exception.InvalidHandlerException;
import com.github.ygimenez.exception.InvalidStateException;
import com.github.ygimenez.listener.MessageHandler;
import com.github.ygimenez.model.*;
import com.github.ygimenez.model.helper.*;
import com.github.ygimenez.type.Emote;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.emoji.CustomEmoji;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.entities.emoji.EmojiUnion;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.message.react.GenericMessageReactionEvent;
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
import net.dv8tion.jda.api.interactions.InteractionHook;
import net.dv8tion.jda.api.interactions.components.ItemComponent;
import net.dv8tion.jda.api.interactions.components.LayoutComponent;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.requests.restaction.MessageEditAction;
import net.dv8tion.jda.api.sharding.ShardManager;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import static com.github.ygimenez.type.Emote.*;
/**
* The main class containing all pagination-related methods, including but not limited
* to {@link #paginate}, {@link #categorize}, {@link #buttonize} and {@link #lazyPaginate}.
*/
public abstract class Pages {
private static final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
private static final MessageHandler handler = new MessageHandler();
private static Paginator paginator;
private Pages() {
}
/**
* Set a {@link Paginator} object to handle incoming events. This is
* required only once unless you want to change which client is handling events.
*
* Before calling this method again, you must use {@link #deactivate()} to
* remove current {@link Paginator}, else this method will throw
* {@link AlreadyActivatedException}.
*
* @param paginator The {@link Paginator} object.
* @throws AlreadyActivatedException Thrown if there's a handler already set.
* @throws InvalidHandlerException Thrown if the handler isn't either a {@link JDA}
* or {@link ShardManager} object.
*/
public static void activate(@NotNull Paginator paginator) throws InvalidHandlerException {
if (isActivated()) throw new AlreadyActivatedException();
Object hand = paginator.getHandler();
if (hand instanceof JDA) {
((JDA) hand).addEventListener(handler);
} else if (hand instanceof ShardManager) {
((ShardManager) hand).addEventListener(handler);
} else {
throw new InvalidHandlerException();
}
Pages.paginator = paginator;
paginator.log(PUtilsConfig.LogLevel.LEVEL_2, "Pagination Utils activated successfully");
}
/**
* Removes current button handler, allowing another {@link #activate(Paginator)} call.
*
* Using this method without activating beforehand will do nothing.
*/
public static void deactivate() {
if (!isActivated()) return;
Object hand = paginator.getHandler();
if (hand instanceof JDA) {
((JDA) hand).removeEventListener(handler);
} else if (hand instanceof ShardManager) {
((ShardManager) hand).removeEventListener(handler);
}
paginator.log(PUtilsConfig.LogLevel.LEVEL_2, "Pagination Utils deactivated successfully");
paginator = null;
}
/**
* Checks whether this library has been activated or not.
*
* @return The activation state of this library.
*/
public static boolean isActivated() {
return paginator != null && paginator.getHandler() != null;
}
/**
* Retrieves the {@link Paginator} object used to activate this library.
*
* @return The current {@link Paginator} object.
*/
public static Paginator getPaginator() {
return paginator;
}
/**
* Retrieves the library's {@link MessageHandler} object.
*
* @return The {@link MessageHandler} object.
*/
public static MessageHandler getHandler() {
return handler;
}
/**
* Adds navigation buttons to the specified {@link Message}/{@link MessageEmbed}
* which will navigate through a given {@link List} of pages.
*
* @param msg The {@link Message} sent which will be paginated.
* @param pages The pages to be shown. The order of the {@link List} will
* define the order of the pages.
* @param useButtons Whether to use interaction {@link Button} or reactions. Will fallback to false if the supplied
* {@link Message} was not sent by the bot.
* @return An {@link ActionReference} pointing to this action. This is useful if you need to track whether an event
* is still being processed or was already removed (ie. garbage collected).
* @throws ErrorResponseException Thrown if the {@link Message} no longer exists
* or cannot be accessed when triggering a
* {@link GenericMessageReactionEvent}.
* @throws InsufficientPermissionException Thrown if this library cannot remove reactions
* due to lack of bot permission.
* @throws InvalidStateException Thrown if the library wasn't activated or the page list is empty.
*/
public static ActionReference paginate(@NotNull Message msg, @NotNull List pages, boolean useButtons) throws ErrorResponseException, InsufficientPermissionException {
return paginate(msg, pages, useButtons, 0, null, 0, false, null);
}
/**
* Adds navigation buttons to the specified {@link Message}/{@link MessageEmbed}
* which will navigate through a given {@link List} of pages. You can specify
* how long the listener will stay active before shutting down itself after a
* no-activity interval.
*
* @param msg The {@link Message} sent which will be paginated.
* @param pages The pages to be shown. The order of the {@link List} will
* define the order of the pages.
* @param useButtons Whether to use interaction {@link Button} or reactions. Will fallback to false if the supplied
* {@link Message} was not sent by the bot.
* @param time The time before the listener automatically stop listening
* for further events (recommended: 60).
* @param unit The time's {@link TimeUnit} (recommended:
* {@link TimeUnit#SECONDS}).
* @return An {@link ActionReference} pointing to this action. This is useful if you need to track whether an event
* is still being processed or was already removed (ie. garbage collected).
* @throws ErrorResponseException Thrown if the {@link Message} no longer exists
* or cannot be accessed when triggering a
* {@link GenericMessageReactionEvent}.
* @throws InsufficientPermissionException Thrown if this library cannot remove reactions
* due to lack of bot permission.
* @throws InvalidStateException Thrown if the library wasn't activated or the page list is empty.
*/
public static ActionReference paginate(@NotNull Message msg, @NotNull List pages, boolean useButtons, int time, TimeUnit unit) throws ErrorResponseException, InsufficientPermissionException {
return paginate(msg, pages, useButtons, time, unit, 0, false, null);
}
/**
* Adds navigation buttons to the specified {@link Message}/{@link MessageEmbed}
* which will navigate through a given {@link List} of pages.
*
* @param msg The {@link Message} sent which will be paginated.
* @param pages The pages to be shown. The order of the {@link List} will
* define the order of the pages.
* @param useButtons Whether to use interaction {@link Button} or reactions. Will fallback to false if the supplied
* {@link Message} was not sent by the bot.
* @param canInteract {@link Predicate} to determine whether the {@link User}
* that pressed the button can interact with it or not.
* @return An {@link ActionReference} pointing to this action. This is useful if you need to track whether an event
* is still being processed or was already removed (ie. garbage collected).
* @throws ErrorResponseException Thrown if the {@link Message} no longer exists
* or cannot be accessed when triggering a
* {@link GenericMessageReactionEvent}.
* @throws InsufficientPermissionException Thrown if this library cannot remove reactions
* due to lack of bot permission.
* @throws InvalidStateException Thrown if the library wasn't activated or the page list is empty.
*/
public static ActionReference paginate(@NotNull Message msg, @NotNull List pages, boolean useButtons, Predicate canInteract) throws ErrorResponseException, InsufficientPermissionException {
return paginate(msg, pages, useButtons, 0, null, 0, false, canInteract);
}
/**
* Adds navigation buttons to the specified {@link Message}/{@link MessageEmbed}
* which will navigate through a given {@link List} of pages. You can specify
* how long the listener will stay active before shutting down itself after a
* no-activity interval.
*
* @param msg The {@link Message} sent which will be paginated.
* @param pages The pages to be shown. The order of the {@link List} will
* define the order of the pages.
* @param useButtons Whether to use interaction {@link Button} or reactions. Will fallback to false if the supplied
* {@link Message} was not sent by the bot.
* @param time The time before the listener automatically stop listening
* for further events (recommended: 60).
* @param unit The time's {@link TimeUnit} (recommended:
* {@link TimeUnit#SECONDS}).
* @param canInteract {@link Predicate} to determine whether the {@link User}
* that pressed the button can interact with it or not.
* @return An {@link ActionReference} pointing to this action. This is useful if you need to track whether an event
* is still being processed or was already removed (ie. garbage collected).
* @throws ErrorResponseException Thrown if the {@link Message} no longer exists
* or cannot be accessed when triggering a
* {@link GenericMessageReactionEvent}.
* @throws InsufficientPermissionException Thrown if this library cannot remove reactions
* due to lack of bot permission.
* @throws InvalidStateException Thrown if the library wasn't activated or the page list is empty.
*/
public static ActionReference paginate(@NotNull Message msg, @NotNull List pages, boolean useButtons, int time, TimeUnit unit, Predicate canInteract) throws ErrorResponseException, InsufficientPermissionException {
return paginate(msg, pages, useButtons, time, unit, 0, false, canInteract);
}
/**
* Adds navigation buttons to the specified {@link Message}/{@link MessageEmbed}
* which will navigate through a given {@link List} of pages.
*
* @param msg The {@link Message} sent which will be paginated.
* @param pages The pages to be shown. The order of the {@link List} will
* define the order of the pages.
* @param useButtons Whether to use interaction {@link Button} or reactions. Will fallback to false if the supplied
* {@link Message} was not sent by the bot.
* @param fastForward Whether the {@link Emote#GOTO_FIRST} and {@link Emote#GOTO_LAST} buttons should be shown.
* @return An {@link ActionReference} pointing to this action. This is useful if you need to track whether an event
* is still being processed or was already removed (ie. garbage collected).
* @throws ErrorResponseException Thrown if the {@link Message} no longer exists
* or cannot be accessed when triggering a
* {@link GenericMessageReactionEvent}.
* @throws InsufficientPermissionException Thrown if this library cannot remove reactions
* due to lack of bot permission.
* @throws InvalidStateException Thrown if the library wasn't activated or the page list is empty.
*/
public static ActionReference paginate(@NotNull Message msg, @NotNull List pages, boolean useButtons, boolean fastForward) throws ErrorResponseException, InsufficientPermissionException {
return paginate(msg, pages, useButtons, 0, null, 0, fastForward, null);
}
/**
* Adds navigation buttons to the specified {@link Message}/{@link MessageEmbed}
* which will navigate through a given {@link List} of pages. You can specify
* how long the listener will stay active before shutting down itself after a
* no-activity interval.
*
* @param msg The {@link Message} sent which will be paginated.
* @param pages The pages to be shown. The order of the {@link List} will
* define the order of the pages.
* @param useButtons Whether to use interaction {@link Button} or reactions. Will fallback to false if the supplied
* {@link Message} was not sent by the bot.
* @param time The time before the listener automatically stop listening
* for further events (recommended: 60).
* @param unit The time's {@link TimeUnit} (recommended:
* {@link TimeUnit#SECONDS}).
* @param fastForward Whether the {@link Emote#GOTO_FIRST} and {@link Emote#GOTO_LAST} buttons should be shown.
* @return An {@link ActionReference} pointing to this action. This is useful if you need to track whether an event
* is still being processed or was already removed (ie. garbage collected).
* @throws ErrorResponseException Thrown if the {@link Message} no longer exists
* or cannot be accessed when triggering a
* {@link GenericMessageReactionEvent}.
* @throws InsufficientPermissionException Thrown if this library cannot remove reactions
* due to lack of bot permission.
* @throws InvalidStateException Thrown if the library wasn't activated or the page list is empty.
*/
public static ActionReference paginate(@NotNull Message msg, @NotNull List pages, boolean useButtons, int time, TimeUnit unit, boolean fastForward) throws ErrorResponseException, InsufficientPermissionException {
return paginate(msg, pages, useButtons, time, unit, 0, fastForward, null);
}
/**
* Adds navigation buttons to the specified {@link Message}/{@link MessageEmbed}
* which will navigate through a given {@link List} of pages.
*
* @param msg The {@link Message} sent which will be paginated.
* @param pages The pages to be shown. The order of the {@link List} will
* define the order of the pages.
* @param useButtons Whether to use interaction {@link Button} or reactions. Will fallback to false if the supplied
* {@link Message} was not sent by the bot.
* @param fastForward Whether the {@link Emote#GOTO_FIRST} and {@link Emote#GOTO_LAST} buttons should be shown.
* @param canInteract {@link Predicate} to determine whether the {@link User}
* that pressed the button can interact with it or not.
* @return An {@link ActionReference} pointing to this action. This is useful if you need to track whether an event
* is still being processed or was already removed (ie. garbage collected).
* @throws ErrorResponseException Thrown if the {@link Message} no longer exists
* or cannot be accessed when triggering a
* {@link GenericMessageReactionEvent}.
* @throws InsufficientPermissionException Thrown if this library cannot remove reactions
* due to lack of bot permission.
* @throws InvalidStateException Thrown if the library wasn't activated or the page list is empty.
*/
public static ActionReference paginate(@NotNull Message msg, @NotNull List pages, boolean useButtons, boolean fastForward, Predicate canInteract) throws ErrorResponseException, InsufficientPermissionException {
return paginate(msg, pages, useButtons, 0, null, 0, fastForward, canInteract);
}
/**
* Adds navigation buttons to the specified {@link Message}/{@link MessageEmbed}
* which will navigate through a given {@link List} of pages. You can specify
* how long the listener will stay active before shutting down itself after a
* no-activity interval.
*
* @param msg The {@link Message} sent which will be paginated.
* @param pages The pages to be shown. The order of the {@link List} will
* define the order of the pages.
* @param useButtons Whether to use interaction {@link Button} or reactions. Will fallback to false if the supplied
* {@link Message} was not sent by the bot.
* @param time The time before the listener automatically stop listening
* for further events (recommended: 60).
* @param unit The time's {@link TimeUnit} (recommended:
* {@link TimeUnit#SECONDS}).
* @param fastForward Whether the {@link Emote#GOTO_FIRST} and {@link Emote#GOTO_LAST} buttons should be shown.
* @param canInteract {@link Predicate} to determine whether the {@link User}
* that pressed the button can interact with it or not.
* @return An {@link ActionReference} pointing to this action. This is useful if you need to track whether an event
* is still being processed or was already removed (ie. garbage collected).
* @throws ErrorResponseException Thrown if the {@link Message} no longer exists
* or cannot be accessed when triggering a
* {@link GenericMessageReactionEvent}.
* @throws InsufficientPermissionException Thrown if this library cannot remove reactions
* due to lack of bot permission.
* @throws InvalidStateException Thrown if the library wasn't activated or the page list is empty.
*/
public static ActionReference paginate(@NotNull Message msg, @NotNull List pages, boolean useButtons, int time, TimeUnit unit, boolean fastForward, Predicate canInteract) throws ErrorResponseException, InsufficientPermissionException {
return paginate(msg, pages, useButtons, time, unit, 0, fastForward, canInteract);
}
/**
* Adds navigation buttons to the specified {@link Message}/{@link MessageEmbed}
* which will navigate through a given {@link List} of pages.
*
* @param msg The {@link Message} sent which will be paginated.
* @param pages The pages to be shown. The order of the {@link List} will
* define the order of the pages.
* @param useButtons Whether to use interaction {@link Button} or reactions. Will fallback to false if the supplied
* {@link Message} was not sent by the bot.
* @param skipAmount The amount of pages to be skipped when clicking {@link Emote#SKIP_BACKWARD}
* and {@link Emote#SKIP_FORWARD} buttons.
* @return An {@link ActionReference} pointing to this action. This is useful if you need to track whether an event
* is still being processed or was already removed (ie. garbage collected).
* @throws ErrorResponseException Thrown if the {@link Message} no longer exists
* or cannot be accessed when triggering a
* {@link GenericMessageReactionEvent}.
* @throws InsufficientPermissionException Thrown if this library cannot remove reactions
* due to lack of bot permission.
* @throws InvalidStateException Thrown if the library wasn't activated or the page list is empty.
*/
public static ActionReference paginate(@NotNull Message msg, @NotNull List pages, boolean useButtons, int skipAmount) throws ErrorResponseException, InsufficientPermissionException {
return paginate(msg, pages, useButtons, 0, null, skipAmount, false, null);
}
/**
* Adds navigation buttons to the specified {@link Message}/{@link MessageEmbed}
* which will navigate through a given {@link List} of pages. You can specify
* how long the listener will stay active before shutting down itself after a
* no-activity interval.
*
* @param msg The {@link Message} sent which will be paginated.
* @param pages The pages to be shown. The order of the {@link List} will
* define the order of the pages.
* @param useButtons Whether to use interaction {@link Button} or reactions. Will fallback to false if the supplied
* {@link Message} was not sent by the bot.
* @param time The time before the listener automatically stop listening
* for further events (recommended: 60).
* @param unit The time's {@link TimeUnit} (recommended:
* {@link TimeUnit#SECONDS}).
* @param skipAmount The amount of pages to be skipped when clicking {@link Emote#SKIP_BACKWARD}
* and {@link Emote#SKIP_FORWARD} buttons.
* @return An {@link ActionReference} pointing to this action. This is useful if you need to track whether an event
* is still being processed or was already removed (ie. garbage collected).
* @throws ErrorResponseException Thrown if the {@link Message} no longer exists
* or cannot be accessed when triggering a
* {@link GenericMessageReactionEvent}.
* @throws InsufficientPermissionException Thrown if this library cannot remove reactions
* due to lack of bot permission.
* @throws InvalidStateException Thrown if the library wasn't activated or the page list is empty.
*/
public static ActionReference paginate(@NotNull Message msg, @NotNull List pages, boolean useButtons, int time, TimeUnit unit, int skipAmount) throws ErrorResponseException, InsufficientPermissionException {
return paginate(msg, pages, useButtons, time, unit, skipAmount, false, null);
}
/**
* Adds navigation buttons to the specified {@link Message}/{@link MessageEmbed}
* which will navigate through a given {@link List} of pages.
*
* @param msg The {@link Message} sent which will be paginated.
* @param pages The pages to be shown. The order of the {@link List} will
* define the order of the pages.
* @param useButtons Whether to use interaction {@link Button} or reactions. Will fallback to false if the supplied
* {@link Message} was not sent by the bot.
* @param skipAmount The amount of pages to be skipped when clicking {@link Emote#SKIP_BACKWARD}
* and {@link Emote#SKIP_FORWARD} buttons.
* @param canInteract {@link Predicate} to determine whether the {@link User}
* that pressed the button can interact with it or not.
* @return An {@link ActionReference} pointing to this action. This is useful if you need to track whether an event
* is still being processed or was already removed (ie. garbage collected).
* @throws ErrorResponseException Thrown if the {@link Message} no longer exists
* or cannot be accessed when triggering a
* {@link GenericMessageReactionEvent}.
* @throws InsufficientPermissionException Thrown if this library cannot remove reactions
* due to lack of bot permission.
* @throws InvalidStateException Thrown if the library wasn't activated or the page list is empty.
*/
public static ActionReference paginate(@NotNull Message msg, @NotNull List pages, boolean useButtons, int skipAmount, Predicate canInteract) throws ErrorResponseException, InsufficientPermissionException {
return paginate(msg, pages, useButtons, 0, null, skipAmount, false, canInteract);
}
/**
* Adds navigation buttons to the specified {@link Message}/{@link MessageEmbed}
* which will navigate through a given {@link List} of pages. You can specify
* how long the listener will stay active before shutting down itself after a
* no-activity interval.
*
* @param msg The {@link Message} sent which will be paginated.
* @param pages The pages to be shown. The order of the {@link List} will
* define the order of the pages.
* @param useButtons Whether to use interaction {@link Button} or reactions. Will fallback to false if the supplied
* {@link Message} was not sent by the bot.
* @param time The time before the listener automatically stop listening
* for further events (recommended: 60).
* @param unit The time's {@link TimeUnit} (recommended:
* {@link TimeUnit#SECONDS}).
* @param skipAmount The amount of pages to be skipped when clicking {@link Emote#SKIP_BACKWARD}
* and {@link Emote#SKIP_FORWARD} buttons.
* @param canInteract {@link Predicate} to determine whether the {@link User}
* that pressed the button can interact with it or not.
* @return An {@link ActionReference} pointing to this action. This is useful if you need to track whether an event
* is still being processed or was already removed (ie. garbage collected).
* @throws ErrorResponseException Thrown if the {@link Message} no longer exists
* or cannot be accessed when triggering a
* {@link GenericMessageReactionEvent}.
* @throws InsufficientPermissionException Thrown if this library cannot remove reactions
* due to lack of bot permission.
* @throws InvalidStateException Thrown if the library wasn't activated or the page list is empty.
*/
public static ActionReference paginate(@NotNull Message msg, @NotNull List pages, boolean useButtons, int time, TimeUnit unit, int skipAmount, Predicate canInteract) throws ErrorResponseException, InsufficientPermissionException {
return paginate(msg, pages, useButtons, time, unit, skipAmount, false, canInteract);
}
/**
* Adds navigation buttons to the specified {@link Message}/{@link MessageEmbed}
* which will navigate through a given {@link List} of pages.
*
* @param msg The {@link Message} sent which will be paginated.
* @param pages The pages to be shown. The order of the {@link List} will
* define the order of the pages.
* @param useButtons Whether to use interaction {@link Button} or reactions. Will fallback to false if the supplied
* {@link Message} was not sent by the bot.
* @param skipAmount The amount of pages to be skipped when clicking {@link Emote#SKIP_BACKWARD}
* and {@link Emote#SKIP_FORWARD} buttons.
* @param fastForward Whether the {@link Emote#GOTO_FIRST} and {@link Emote#GOTO_LAST} buttons should be shown.
* @param canInteract {@link Predicate} to determine whether the {@link User}
* that pressed the button can interact with it or not.
* @return An {@link ActionReference} pointing to this action. This is useful if you need to track whether an event
* is still being processed or was already removed (ie. garbage collected).
* @throws ErrorResponseException Thrown if the {@link Message} no longer exists
* or cannot be accessed when triggering a
* {@link GenericMessageReactionEvent}.
* @throws InsufficientPermissionException Thrown if this library cannot remove reactions
* due to lack of bot permission.
* @throws InvalidStateException Thrown if the library wasn't activated or the page list is empty.
*/
public static ActionReference paginate(@NotNull Message msg, @NotNull List pages, boolean useButtons, int skipAmount, boolean fastForward, Predicate canInteract) throws ErrorResponseException, InsufficientPermissionException {
return paginate(msg, pages, useButtons, 0, null, skipAmount, fastForward, canInteract);
}
/**
* Adds navigation buttons to the specified {@link Message}/{@link MessageEmbed}
* which will navigate through a given {@link List} of pages. You can specify
* how long the listener will stay active before shutting down itself after a
* no-activity interval.
*
* @param msg The {@link Message} sent which will be paginated.
* @param pages The pages to be shown. The order of the {@link List} will
* define the order of the pages.
* @param useButtons Whether to use interaction {@link Button} or reactions. Will fallback to false if the supplied
* {@link Message} was not sent by the bot.
* @param time The time before the listener automatically stop listening
* for further events (recommended: 60).
* @param unit The time's {@link TimeUnit} (recommended:
* {@link TimeUnit#SECONDS}).
* @param skipAmount The amount of pages to be skipped when clicking {@link Emote#SKIP_BACKWARD}
* and {@link Emote#SKIP_FORWARD} buttons.
* @param fastForward Whether the {@link Emote#GOTO_FIRST} and {@link Emote#GOTO_LAST} buttons should be shown.
* @return An {@link ActionReference} pointing to this action. This is useful if you need to track whether an event
* is still being processed or was already removed (ie. garbage collected).
* @throws ErrorResponseException Thrown if the {@link Message} no longer exists
* or cannot be accessed when triggering a
* {@link GenericMessageReactionEvent}.
* @throws InsufficientPermissionException Thrown if this library cannot remove reactions
* due to lack of bot permission.
* @throws InvalidStateException Thrown if the library wasn't activated or the page list is empty.
*/
public static ActionReference paginate(@NotNull Message msg, @NotNull List pages, boolean useButtons, int time, TimeUnit unit, int skipAmount, boolean fastForward) throws ErrorResponseException, InsufficientPermissionException {
return paginate(msg, pages, useButtons, time, unit, skipAmount, fastForward, null);
}
/**
* Adds navigation buttons to the specified {@link Message}/{@link MessageEmbed}
* which will navigate through a given {@link List} of pages. You can specify
* how long the listener will stay active before shutting down itself after a
* no-activity interval.
*
* @param msg The {@link Message} sent which will be paginated.
* @param pages The pages to be shown. The order of the {@link List} will
* define the order of the pages.
* @param useButtons Whether to use interaction {@link Button} or reactions. Will fallback to false if the supplied
* {@link Message} was not sent by the bot.
* @param time The time before the listener automatically stop listening
* for further events (recommended: 60).
* @param unit The time's {@link TimeUnit} (recommended:
* {@link TimeUnit#SECONDS}).
* @param skipAmount The amount of pages to be skipped when clicking {@link Emote#SKIP_BACKWARD}
* and {@link Emote#SKIP_FORWARD} buttons.
* @param fastForward Whether the {@link Emote#GOTO_FIRST} and {@link Emote#GOTO_LAST} buttons should be shown.
* @param canInteract {@link Predicate} to determine whether the {@link User}
* that pressed the button can interact with it or not.
* @return An {@link ActionReference} pointing to this action. This is useful if you need to track whether an event
* is still being processed or was already removed (ie. garbage collected).
* @throws ErrorResponseException Thrown if the {@link Message} no longer exists
* or cannot be accessed when triggering a
* {@link GenericMessageReactionEvent}.
* @throws InsufficientPermissionException Thrown if this library cannot remove reactions
* due to lack of bot permission.
* @throws InvalidStateException Thrown if the library wasn't activated or the page list is empty.
*/
public static ActionReference paginate(@NotNull Message msg, @NotNull List pages, boolean useButtons, int time, TimeUnit unit, int skipAmount, boolean fastForward, Predicate canInteract) throws ErrorResponseException, InsufficientPermissionException {
return paginate(msg, new PaginateHelper(pages, useButtons)
.setTimeout(time, unit)
.setSkipAmount(skipAmount)
.setFastForward(fastForward)
.setCanInteract(canInteract)
);
}
/**
* Adds navigation buttons to the specified {@link Message}/{@link MessageEmbed}
* which will navigate through a given {@link List} of pages. This versions uses a helper class
* to aid customization and allow reusage of configurations.
*
* @param msg The {@link Message} sent which will be paginated.
* @param helper A {@link PaginateHelper} holding desired pagination settings.
* @return An {@link ActionReference} pointing to this action. This is useful if you need to track whether an event
* is still being processed or was already removed (ie. garbage collected).
* @throws ErrorResponseException Thrown if the {@link Message} no longer exists
* or cannot be accessed when triggering a
* {@link GenericMessageReactionEvent}.
* @throws InsufficientPermissionException Thrown if this library cannot remove reactions
* due to lack of bot permission.
* @throws InvalidStateException Thrown if the library wasn't activated or the page list is empty.
*/
public static ActionReference paginate(@NotNull Message msg, @NotNull PaginateHelper helper) throws ErrorResponseException, InsufficientPermissionException {
if (!isActivated() || helper.getContent().isEmpty()) throw new InvalidStateException();
boolean useBtns = helper.isUsingButtons() && msg.getAuthor().getId().equals(msg.getJDA().getSelfUser().getId());
List pgs = Collections.unmodifiableList(helper.getContent());
if (useBtns && helper.shouldUpdate(msg)) {
helper.apply(msg.editMessageComponents()).submit();
} else if (!useBtns) {
clearButtons(msg);
clearReactions(msg);
addReactions(msg, helper.getSkipAmount() > 1, helper.isFastForward());
}
return handler.addEvent(msg, new ThrowingBiConsumer<>() {
private final int maxP = pgs.size() - 1;
private int p = 0;
private ScheduledFuture> timeout;
private final Consumer success = s -> {
if (timeout != null) {
timeout.cancel(true);
}
handler.removeEvent(msg);
if (paginator.isDeleteOnCancel()) msg.delete().submit();
};
private final Function