Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages

name: Node.js Package

on:
release:
types: [created]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
- run: npm ci
- run: npm test

publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
35 changes: 14 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,21 @@

<div align="center">
<p>
<a href="https://npmjs.com/package/discordjs-button-pagination
/"><img src="https://nodei.co/npm/discordjs-button-pagination.png?downloads=true&stars=true" alt="NPM info" /></a>
<a href="https://www.npmjs.com/package/discordjs-button-pagination-v2/"><img src="https://nodei.co/npm/discordjs-button-pagination-v2.png?downloads=true&stars=true" alt="NPM info" /></a>
</p>
</div>

# discordjs-button-pagination
# discordjs-button-pagination-v2
A simple package to paginate discord embeds via discord buttons introduced in [discord.js v13](https://github.com/discordjs/discord.js/tree/master).

# Versions

## `discordjs-button-pagination@interaction` [Default]
## `discordjs-button-pagination-v2@interaction` [Default]
for slash command interaction.

## `discordjs-button-pagination@msg`
for message command.
# Installation
For `message` event
* `npm install discordjs-button-pagination@msg`

For `interaction` event
* `npm install discordjs-button-pagination@interaction`

## Default command: `npm install discordjs-button-pagination` will install the **`interaction`** version
### Default command: `npm install discordjs-button-pagination-v2`

# Requirements
Node.js 16.6.1 or newer is required along with Discord.js 13.0.0 or newer.
Expand All @@ -34,28 +26,28 @@ Node.js 16.6.1 or newer is required along with Discord.js 13.0.0 or newer.
__Basic Bot Example__
```js
// Import the discordjs-button-pagination package
const paginationEmbed = require('discordjs-button-pagination');
const paginationEmbed = require('discordjs-button-pagination-v2');

// Use MessageEmbed to make pages
// Keep in mind that Embeds should't have their footers set since the pagination method sets page info there
const { MessageEmbed , MessageButton} = require('discord.js');
const embed1 = new MessageEmbed()
const { EmbedBuilder , ButtonBuilder, ButtonStyle} = require('discord.js');
const embed1 = new EmbedBuilder()
.setTitle('First Page')
.setDescription('This is the first page');

const embed2 = new MessageEmbed()
const embed2 = new EmbedBuilder()
.setTitle('Second Page')
.setDescription('This is the second page');

const button1 = new MessageButton()
const button1 = new ButtonBuilder()
.setCustomId('previousbtn')
.setLabel('Previous')
.setStyle('DANGER');
.setStyle(ButtonStyle.Danger);

const button2 = new MessageButton()
const button2 = new ButtonBuilder()
.setCustomId('nextbtn')
.setLabel('Next')
.setStyle('SUCCESS');
.setStyle(ButtonStyle.Success);

// Create an array of embeds
pages = [
Expand All @@ -81,6 +73,7 @@ paginationEmbed(interaction, pages, buttonList, timeout);

# Note
This will not work with buttons whose style is set as 'LINK' as they do not trigger an interaction event. The buttons will auto disable once the the collector ends after the timeout.
You must use the following custom ids for it to work. "previousbtn" and "nextbtn"
## The collector timer resets after receiving a button interaction.

# Preview
Expand All @@ -103,5 +96,5 @@ Disabled Buttons after collector end



### For any issues or suggestions, kindly open an issue/pull request on the [**GitHub Repository**](https://github.com/ryzyx/discordjs-button-pagination)
### For any issues or suggestions, kindly open an issue/pull request on the [**GitHub Repository**](https://github.com/chillingcone426/discordjs-button-pagination)

26 changes: 11 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
const {
MessageActionRow,
Message,
MessageEmbed,
MessageButton,
} = require("discord.js");
const { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder, ComponentType } = require('discord.js');

/**
* Creates a pagination embed
* @param {Interaction} interaction
* @param {MessageEmbed[]} pages
* @param {MessageButton[]} buttonList
* @param {EmbedBuilder[]} pages
* @param {ButtonBuilder[]} buttonList
* @param {number} timeout
* @returns
*/
Expand All @@ -29,7 +24,7 @@ const paginationEmbed = async (

let page = 0;

const row = new MessageActionRow().addComponents(buttonList);
const row = new ActionRowBuilder().addComponents(buttonList);

//has the interaction already been deferred? If not, defer the reply.
if (interaction.deferred == false) {
Expand All @@ -42,21 +37,22 @@ const paginationEmbed = async (
fetchReply: true,
});

const filter = (i) =>
const filter = (i) => {
i.deferUpdate();
i.customId === buttonList[0].customId ||
i.customId === buttonList[1].customId;

};
const collector = await curPage.createMessageComponentCollector({
filter,
time: timeout,
componentType: ComponentType.Button,
});

collector.on("collect", async (i) => {
switch (i.customId) {
case buttonList[0].customId:
case "previousbtn":
page = page > 0 ? --page : pages.length - 1;
break;
case buttonList[1].customId:
case "nextbtn":
page = page + 1 < pages.length ? ++page : 0;
break;
default:
Expand All @@ -72,7 +68,7 @@ const paginationEmbed = async (

collector.on("end", (_, reason) => {
if (reason !== "messageDelete") {
const disabledRow = new MessageActionRow().addComponents(
const disabledRow = new ActionRowBuilder().addComponents(
buttonList[0].setDisabled(true),
buttonList[1].setDisabled(true)
);
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "discordjs-button-pagination",
"version": "3.0.1",
"description": "A simple package for pagination using buttons introduced in discord.js v13.",
"name": "discordjs-button-pagination-v2",
"version": "4.0.2",
"description": "A simple package for pagination using buttons introduced in discord.js v13 that was updates to work with discord.js v14.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ryzyx/discordjs-button-pagination.git"
"url": "git+https://github.com/chillingcone426/discordjs-button-pagination.git"
},
"keywords": [
"Discord",
Expand All @@ -19,13 +19,13 @@
"discordjs",
"discord.js"
],
"author": "Ryzyx",
"author": "thebeston123",
"license": "MIT",
"bugs": {
"url": "https://github.com/ryzyx/discordjs-button-pagination/issues"
"url": "https://github.com/chillingcone426/discordjs-button-pagination/issues"
},
"homepage": "https://github.com/ryzyx/discordjs-button-pagination#readme",
"homepage": "https://github.com/chillingcone426/discordjs-button-pagination#readme",
"dependencies": {
"discord.js": "^13.0.1"
"discord.js": "^14.11.0"
}
}