npm

pagination.djs
TypeScript icon, indicating that this package has built-in type declarations

4.0.17 • Public • Published

pagination.djs

A simple and lightweight module to paginate discord embeds.

GitHub npm npm

Read docs here: pagination.djs

Features

  • Written In Typescript
  • Offers CJS and ESM builds
  • Full TypeScript & JavaScript support
  • Simple and easy to use

Installation

Note V4 is for discord.js v14.x. For discord.js v13.x, Use v3.

pagination.djs depends on the following packages. Be sure to install these along with this package!

You can use the following command to install this package, or replace npm install with your package manager of choice.

npm install pagination.djs discord.js

Uses

Example shows how to use it with any application command but it's valid for message commands as well. You just need to pass the message in place of interaction.

Basic examples

Paginate through descriptions

const { Pagination } = require('pagination.djs');
const pagination = new Pagination(interaction);

const descriptions = ['This is a description.', 'This is a second description.'];
pagination.setDescriptions(descriptions);
pagination.render();

Paginate through images

const { Pagination } = require('pagination.djs');
const pagination = new Pagination(interaction);

const images = ['1st image link', '2nd image link'];
pagination.setImages(images);
pagination.render();

Paginate through Fields

const { Pagination } = require('pagination.djs');
const pagination = new Pagination(interaction);

pagination.setFields([
	{
		name: 'First',
		value: 'First'
	},
	{
		name: 'Second',
		value: 'Second'
	},
	{
		name: 'Third',
		value: 'Third'
	}
]);
pagination.paginateFields();
pagination.render();

Warning You need to add paginateFields() in order to paginate through fields

Paginate through all

You can paginate through descriptions, images, fields all at the same time

const { Pagination } = require('pagination.djs');

const descriptions = ['This is a description.', 'This is a second description.'];
const images = ['1st image link', '2nd image link'];
const pagination = new Pagination(interaction)
	.setDescriptions(descriptions)
	.setImages(images)
	.setFields([
		{
			name: 'First',
			value: 'First'
		},
		{
			name: 'Second',
			value: 'Second'
		},
		{
			name: 'Third',
			value: 'Third'
		}
	])
	.paginateFields(true);
pagination.render();

Paginate through multiple embeds

Warning If you use this then all the embed methods (setTitle(), ...) and other pagination methods (setImages(), ...) will be ignored

Paginate through multiple embeds

const { Pagination } = require('pagination.djs');
const { EmbedBuilder } = require('discord.js');
const pagination = new Pagination(interaction);

const embeds = [];

for (let i = 0; i <= 5; i++) {
	const newEmbed = new EmbedBuilder().setTitle(`Embed ${i + 1}`);
	embeds.push(newEmbed);
}

pagination.setEmbeds(embeds);
// or if you want to set a common change in all embeds, you can do it by adding a cb.
pagination.setEmbeds(embeds, (embed, index, array) => {
	return embed.setFooter({ text: `Page: ${index + 1}/${array.length}` });
});
pagination.render();

Customize embed

The pagination class extends the discord.js EmbedBuilder class. So you can directly use the embed methods.

const { Pagination } = require('pagination.djs');
const pagination = new Pagination(interaction);

pagination.setTitle('Pagination');
pagination.setDescription('This is a description.');

pagination.setColor('#00ff00');
pagination.setFooter('Pagination');
pagination.setTimestamp();

pagination.addFields([
	{
		name: 'First',
		value: 'First'
	},
	{
		name: 'Second',
		value: 'Second'
	},
	{
		name: 'Third',
		value: 'Third'
	}
]);
pagination.paginateFields(true);
pagination.render();

Customization

You can customize the behavior of the pagination by passing the following options:

const { Pagination } = require('pagination.djs');
const pagination = new Pagination(interaction, {
	firstEmoji: '⏮', // First button emoji
	prevEmoji: '◀️', // Previous button emoji
	nextEmoji: '▶️', // Next button emoji
	lastEmoji: '⏭', // Last button emoji
	limit: 5, // number of entries per page
	idle: 30000, // idle time in ms before the pagination closes
	ephemeral: false, // ephemeral reply
	prevDescription: '',
	postDescription: '',
	attachments: [new AttachmentBuilder()], // attachments you want to pass with the embed
	buttonStyle: ButtonStyle.Secondary, // button style
	loop: false // loop through the pages
});

Note: All the options are optional. If you don't pass any option, the default values will be used.

You can set the options with setOptions() method also

pagination.setOptions(option);

By default embed will show page number and total pages in footer as

Pages: pageNumber/totalPages

You can change it by setting pagination.setFooter("my footer") and you can pass {pageNumber} and {totalPages} which will be replaced with the respective value.

Set emojis

set button emojis with setEmojis() method. You can set any custom or default emojis.

pagination.setEmojis({
	firstEmoji: '⏮',
	prevEmoji: '◀️',
	nextEmoji: '▶️',
	lastEmoji: '⏭'
});

Customize button

Customize label, emoji or style of button using setButtonAppearance() method

pagination.setButtonAppearance({
	first: {
		label: 'First',
		emoji: '⏮',
		style: ButtonStyle.PRIMARY
	},
	prev: {
		label: 'Prev',
		emoji: '◀️',
		style: ButtonStyle.SECONDARY
	},
	next: {
		label: 'Next',
		emoji: '▶️',
		style: ButtonStyle.SUCCESS
	},
	last: {
		label: 'Last',
		emoji: '⏭',
		style: ButtonStyle.DANGER
	}
});

Change button styles

Change all the button style

pagination.setButtonStyle(ButtonStyle.Secondary);

Add or remove buttons

Pagination class have a property buttons which stores all the buttons with keys first, prev, next and last. If you want to add one more you can add it with any key.

pagination.buttons = { ...pagination.buttons, extra: new ButtonBuilder() };

If you want to remove some button then you can set the buttons to only these buttons.

pagination.buttons = { prev: new ButtonBuilder(), next: new ButtonBuilder() };

If you are adding an extra button then make sure it's key isn't first, prev, next, or last or it'll be used to paginate between different pages.

Add Action row

Add some action rows above or below the pagination button row

pagination.addActionRows([new ActionRowBuilder()], ExtraRowPosition.Below);

prevDescription and postDescription

Add a fixed prev descriptions or a post descriptions This can only be used when pagination through descriptions else it'll be ignored

const { Pagination } = require('pagination.djs');
const pagination = new Pagination(interaction).setPrevDescription('Previous').setPostDescription('Post').descriptions(['Array of descriptions']);

pagination.render();

Add multiple authorized users

By default interaction.user or member.author is the only authorized user. But you can set multiple authorized users with setAuthorizedUsers() method.

Note If you pass an empty array in setAuthorizedUsers() then everyone can use the buttons.

pagination.setAuthorizedUsers(['user1', 'user2']);
pagination.addAuthorizedUser('user1');
pagination.addAuthorizedUsers(['user3', 'user4']);

Send attachments

Send attachments along with the message You can pass attachments in the setOptions or using setAttachments(), addAttachmentBuilder() or addAttachments()

pagination.setAttachments([new AttachmentBuilder()]);

Other send options

By default render() will reply() to the interaction. But if the interaction is already replied or deferred then it'll editReply() instead. You can change the behavior farther more with the other send methods available. Available built-in methods are:

If you want to send it by yourself or send in a different channel then you can follow these steps:

const payloads = pagination.ready();
const message = await interaction.reply(payloads);
pagination.paginate(message);

Migration guide

If you are migrating from other lib where you use to set multiple embeds at the same time, then we also have a similar method called Pagination#setEmbeds, where you can pass your embeds and use render() method and pagination will take care of the rest.

Contributors ✨

Thanks goes to these wonderful people:

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
4.0.0-dev.4734ec7.00dev
4.0.17216latest
4.0.16-next.ef428d1.01next
3.6.51v3

Version History

VersionDownloads (Last 7 Days)Published
4.0.17216
4.0.1657
4.0.16-next.ef428d1.01
4.0.1511
4.0.149
4.0.131
4.0.125
4.0.111
4.0.10214
4.0.91
3.6.51
4.0.80
4.0.70
3.6.40
4.0.60
4.0.50
4.0.40
4.0.30
3.6.31
4.0.20
3.6.20
4.0.10
4.0.00
3.6.10
3.6.00
4.0.0-dev.4734ec7.00
3.5.30
3.5.21
3.5.00
3.5.10
3.4.50
3.4.40
3.4.30
3.4.20
3.4.10
3.4.00
3.3.00
3.2.20
3.2.10
3.1.11
3.1.00
3.0.10
3.0.00
2.2.50
2.2.40
2.2.30
2.2.20
2.2.10
2.1.00
2.0.20
2.0.10
2.0.00
1.5.00
1.4.00
1.3.10
1.3.00
1.2.40
1.2.30
1.2.20
1.2.10
1.1.00
1.0.10
1.0.00

Package Sidebar

Install

npm i pagination.djs

Weekly Downloads

528

Version

4.0.17

License

MIT

Unpacked Size

300 kB

Total Files

10

Last publish

Collaborators

  • imranbarbhuiya