fix pagination

merge-requests/5/head
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 }) { export default async function PaginationEmbed(message: Message, pages: MessageEmbed[], options?: {leftArrow?: EmojiResolvable, rightArrow?: EmojiResolvable, timeout?: number }) {
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign
options = { options = {
leftArrow: options.leftArrow ?? '⬅️', leftArrow: options?.leftArrow ?? '⬅️',
rightArrow: options.rightArrow ?? '➡️', rightArrow: options?.rightArrow ?? '➡️',
timeout: options.timeout ?? 120000, 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]] }); 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.leftArrow);
await paginationMessage.react(options.rightArrow); 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({ const reactionCollector = paginationMessage.createReactionCollector({
filter, filter,
time: options.timeout, time: options.timeout,
dispose: true, dispose: true,
}); });
reactionCollector.on('collect', (reaction) => { reactionCollector.on('collect', (reaction, user) => {
reaction.users.remove(message.author); reaction.users.remove(user);
if (reaction.emoji.name === options.leftArrow) { if (reaction.emoji.name === options.leftArrow) {
if (pageNumber > 0) { if (pageNumber > 0) {
pageNumber -= 1; 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]] }); paginationMessage.edit({ content: `Page ${pageNumber + 1} / ${pages.length}`, embeds: [pages[pageNumber]] });
}); });
reactionCollector.on('end', () => { reactionCollector.on('end', () => {
if (!paginationMessage.deleted) { if (!paginationMessage.deleted) {
paginationMessage.reactions.removeAll(); paginationMessage.reactions.removeAll();