Skip to content

Instantly share code, notes, and snippets.

@Asgarrrr
Created May 30, 2021 03:10
Show Gist options
  • Save Asgarrrr/ebc168470c56532d13e04fbf9d4ad2ca to your computer and use it in GitHub Desktop.
Save Asgarrrr/ebc168470c56532d13e04fbf9d4ad2ca to your computer and use it in GitHub Desktop.
// —— This corresponds to the message that was sent by the user to trigger the command
async run(message) {
// —— At the beginning, we are on the first page
let position = 0;
// —— Array of all our pages, a page is an object (an embed)
const pages = [{
title: "This is page 1",
description: "This is an example of a pagination system",
color: "#fcba03"
}, {
title: "Wow, a second page?",
description: "Hi, imagine, all you can do with as many pages as you want ?",
color: "#6f18f2"
}, {
title: "And one more for the road ^^",
description: "There is no page limit, and each page is an object, corresponding to the content of an [embed](https://discord.js.org/#/docs/main/stable/class/MessageEmbed), ",
color: "#f2184b"
}];
// —— Creation of a grey botton, without text, just an emoji, with the ID "previous"
const previous = new MessageButton().setLabel("").setStyle("gray").setEmoji("◀️").setID("previous");
// —— Creation of a grey botton, without text, just an emoji, with the ID "next"
const next = new MessageButton().setLabel("").setStyle("gray").setEmoji("▶️").setID("next");
// —— Checks according to the position if one (or more) buttons should be disabled
function checkPos() {
previous.setDisabled(position === 0 ? true : false);
next.setDisabled(position === Object.keys(pages).length - 1 ? true : false);
};
checkPos();
const pagination = await message.channel.send({
embed: pages[position],
buttons: [previous, next]
});
// —— Creation of a collector, it will react when a user presses a button on the targeted message
const collector = pagination.createButtonCollector(
// —— Only the actions of the person who initiated the command will be taken in consideration.
(button) => button.clicker.user.id === message.author.id,
// —— Collector for 10 minutes
{
time: 600000
});
// —— When a user who has passed the filter uses a button
collector.on("collect", async (button) => {
// —— If the button whose ID is "previous" and the user is not on the first page
if(button.id === "previous" && position > 0)
--position;
// —— If the button whose ID is "next" and the user is not on the last page
if(button.id === "next" && position < Object.keys(pages).length - 1)
++position;
// —— Let's reverify the buttons, to disable them according to the position
checkPos();
// —— Editing the content, and the buttons, according to the page
await pagination.edit({
embed: pages[position],
buttons: [previous, next]
});
// —— Confirms to the "interaction" that it has worked
await button.defer();
});
// —— At the end, the interaction with the buttons is not allowed anymore, we will disable them
collector.on("end", async (collected) => {
// —— Deactivation of the buttons
previous.setDisabled(true);
next.setDisabled(true);
// —— Editing the message, we don't need to change the embeds, just the buttons
await pagination.edit({
buttons: [previous, next]
});
});
}
@Galaxy-Coding
Copy link

Nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment