1
0
Fork 0

fix pagination

master
eirk 2021-08-22 21:49:00 -04:00
parent 4c2690d7fd
commit 87b320f2fb
1 changed files with 15 additions and 7 deletions

View File

@ -3,27 +3,34 @@ import { Message, MessageEmbed, EmojiResolvable, User, MessageReaction } from 'd
export default async function PaginationEmbed(message: Message, pages: MessageEmbed[], options?: {leftArrow?: EmojiResolvable, rightArrow?: EmojiResolvable, timeout?: number }) {
// eslint-disable-next-line no-param-reassign
options = {
leftArrow: options.leftArrow ?? '⬅️',
rightArrow: options.rightArrow ?? '➡️',
timeout: options.timeout ?? 120000,
leftArrow: options?.leftArrow ?? '⬅️',
rightArrow: options?.rightArrow ?? '➡️',
timeout: options?.timeout ?? 120000,
};
let pageNumber = 0;
let pageNumber: number = 0;
const paginationMessage = await message.channel.send({ content: `Page ${pageNumber + 1} of ${pages.length}`, embeds: [pages[pageNumber]] });
await paginationMessage.react(options.leftArrow);
await paginationMessage.react(options.rightArrow);
const filter = (reaction: MessageReaction, user: User) => [options.leftArrow, options.rightArrow].includes(reaction.emoji.name) && !user.bot;
const filter = (reaction: MessageReaction, user: User) => {
if ([options.leftArrow, options.rightArrow].includes(reaction.emoji.name)
&& !user.bot
&& user.id === message.author.id) {
return true;
}
return false;
};
const reactionCollector = paginationMessage.createReactionCollector({
filter,
time: options.timeout,
dispose: true,
});
reactionCollector.on('collect', (reaction) => {
reaction.users.remove(message.author);
reactionCollector.on('collect', (reaction, user) => {
reaction.users.remove(user);
if (reaction.emoji.name === options.leftArrow) {
if (pageNumber > 0) {
pageNumber -= 1;
@ -39,6 +46,7 @@ export default async function PaginationEmbed(message: Message, pages: MessageEm
}
paginationMessage.edit({ content: `Page ${pageNumber + 1} / ${pages.length}`, embeds: [pages[pageNumber]] });
});
reactionCollector.on('end', () => {
if (!paginationMessage.deleted) {
paginationMessage.reactions.removeAll();