UNPKG

@sheweny/paginator

Version:

This module is a prototype of discord embeds paginator using discord.js V13 and array-paginator module.

89 lines (88 loc) 3.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EmbedPaginator = void 0; const array_paginator_1 = require("array-paginator"); const discord_js_1 = require("discord.js"); class EmbedPaginator { firstAndLast; channel; pager; summoner; message; constructor(channel, pages = [], options) { this.firstAndLast = options.firstAndLast ?? true; this.channel = channel; this.pager = new array_paginator_1.Paginator(pages, 1); this.summoner = options.summoner; this.message = null; this.init(); } async init() { if (this.pager.total < 2) throw new Error("A Pagination Embed must contain at least 2 pages"); this.message = await this.channel.send({ embeds: this.pager.first(), components: [this.getComponents()], }); this.listenReactions(); } getComponents() { const row = new discord_js_1.ActionRowBuilder(); if (this.firstAndLast) row.addComponents(new discord_js_1.ButtonBuilder() .setLabel("First") .setCustomId("first") .setStyle(discord_js_1.ButtonStyle.Secondary) .setDisabled(this.pager.hasFirst() ? false : true)); row.addComponents(new discord_js_1.ButtonBuilder() .setLabel("Previous") .setCustomId("previous") .setStyle(discord_js_1.ButtonStyle.Secondary) .setDisabled(this.pager.hasPrevious() ? false : true)); row.addComponents(new discord_js_1.ButtonBuilder() .setLabel("Next") .setCustomId("next") .setStyle(discord_js_1.ButtonStyle.Secondary) .setDisabled(this.pager.hasNext() ? false : true)); if (this.firstAndLast) row.addComponents(new discord_js_1.ButtonBuilder() .setLabel("Last") .setCustomId("last") .setStyle(discord_js_1.ButtonStyle.Secondary) .setDisabled(this.pager.hasLast() ? false : true)); return row; } listenReactions() { const filter = (i) => i.message.id === this.message.id && i.user.id === this.summoner; const collector = this.channel.createMessageComponentCollector({ filter: filter, }); collector.on("collect", async (i) => { switch (i.customId) { case "first": if (this.pager.first()) this.changePage(this.pager.first(), i); break; case "previous": if (this.pager.hasPrevious()) this.changePage(this.pager.previous(), i); break; case "next": if (this.pager.hasNext()) this.changePage(this.pager.next(), i); break; case "last": if (this.pager.last()) this.changePage(this.pager.last(), i); break; } }); } changePage(page, interaction) { interaction.update({ embeds: page.embeds, components: [this.getComponents(), ...page.components], }); } } exports.EmbedPaginator = EmbedPaginator;