Discord.js Pager
Install
npm i @fyleto/dpager
yarn add @fyleto/dpager
Use
const { Pager } = require("@fyleto/dpager");
import { Pager } from "@fyleto/dpager";
Class that defines the page handler.
Constructor
title
(optional): Default title to use for pages that do not have it's own title
const { Pager } = require("@fyleto/dpager");
const pages = new Pager("shop");
Methods
addPage
Add a singular page.
T
: Content of the page (string)T
(overload) : Content, title and others for the page's embed (object)
const pages = new Pager();
pages.addPage("what's up");
pages.addPage({
title: "Page title",
content: "Page content",
});
addPages
Add multiple pages at once
...T
: Content of the page (string)...T
(overload) : Content, title and others for the page's embed (object)
const pages = new Pager();
pages.addPages("what's up", "my name's ko!");
pages.addPages(
{
title: "Page title",
content: "Page content",
},
{
title: "goofy",
content: "mhm",
}
);
removePage
Remove one page by it's index
index
: Page index (0-indexed, so page 1 is 0)
pages.removePage(1);
addDynamicPages
Dynamically pages from an array of strings
array
: Array of stringsmaxContentperPage
: Amount of content on a page before it goes to the next pageseprator
(optional): Seprator to seprate content on one page
pages.addDynamicPages(["hi", "im", "dy", "namic!", "cool?"], 2, "\n");
pages.addPages("hi\nim", "dy\nnamic!", "cool?");
config
Change the buttons. (You cannot edit the customIds and it's recommended you don't or else this won't work)
i
: Object of new buttons (Should look something like this):
const {ButtonStyle} = require("discord.js")
let buttons = {
nextPage: {},
prevPage: {},
prevMaxPage: {},
nextMaxPage: {}
}
{
label: "Button label",
style: ButtonStyle.Success,
emoji: "Button emoji"
}
pages.config(i)
currentPage
Returns an object with the current page's embed, components and raw contents if you do not want it's embed
customId
(optional): When given the customId, it will go to the page based on the id (So if you pass "nextPage"
into it, It will change the index and return the object for the next page. and etc)
pages.currentPage();
pages.currentPage("nextPage");
pages.currentPage();
pages.currentPage("prevPage");
returns
{
embed:
components:
raw: {
title:
content:
}
}
Properties
pages
Array of the pages.
pages.pages;
index
Current page index. (0-indexed)
pages.index;
Example
const { Pager } = require("@fyleto/dpager");
const { ComponentType, ButtonStyle } = require("discord.js");
const pager = new Pager();
pager.addDynamicPages(
[
"This page content",
"is seprated",
"wow",
"isnt that cool?",
"no",
"it's not cool :pensive:",
"Yes you nerd.",
],
2
);
pager.config({
nextPage: {
style: ButtonStyle.Primary,
},
prevPage: {
style: ButtonStyle.Primary,
},
nextMaxPage: {
style: ButtonStyle.Danger,
},
prevMaxPage: {
style: ButtonStyle.Danger,
},
});
let initPage = await pager.currentPage();
let index = `Page ${pager.index + 1}/${pager.pages.length}`;
await interaction.reply({
content: index,
embeds: [initPage.embed],
components: initPage.components,
});
const reply = await interaction.fetchReply();
const collector = reply.createMessageComponentCollector({
componentType: ComponentType.Button,
time: 15_000,
});
collector.on("collect", async (i) => {
let page = await pager.currentPage(i.customId);
let index = `Page ${pager.index + 1}/${pager.pages.length}`;
i.update({
content: index,
embeds: [page.embed],
components: page.components,
});
});
Do not mind my chromebook's bad quality