forked from engineering/cloudservices
fix pagination
parent
4c2690d7fd
commit
87b320f2fb
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue