Embeds pagination not working

const { EmbedBuilder, PermissionsBitField } = require("discord.js");
const Server = require("../../Events/Schemas/tierCreate");
const { formatDueDate } = require('../../Events/Functions/utilityFunctions')

async function createPage(users, currentPage, itemsPerPage, grhighServerData, message) {
const start = (currentPage - 1) * itemsPerPage;
const end = start + itemsPerPage;
const pageUsers = users.slice(start, end);

const embed = new EmbedBuilder()
.setTitle(`${grhighServerData.embedName} List`)
.setColor("#0099ff");

pageUsers.forEach(async (user) => {
const formattedDueDate = formatDueDate(user.dueDate);
const guild = message.guild;
const member = await guild.members.fetch(user.userId);
const displayName = member.displayName;
// Add more user information as needed
embed.addFields({
name: `${displayName} - ${user.userId}`,
value: `Total Donations: ⏣ ${user.payment.toLocaleString("en-US")} - Due Date: ${formattedDueDate}`,
inline: false,
});
});

return embed;
}

module.exports = {
data: {
name: "list",
description: "Lists all the users in the grhigh tier.",
options: [],
},
async execute(message, grhighServerData, embedName) {
if (message.author.bot) {
return;
}

if (message.author.id === '756795184263462922') {
//bot owner
} else {
// Check for required permissions (KICK_MEMBERS in this case)
if (!message.member.permissions.has(PermissionsBitField.Flags.KickMembers)) {
message.channel.send("You don't have permission to use this command.");
return;
}
}

const serverId = grhighServerData.serverId;

try {
// Query the server collection to retrieve users in the "grhigh" tier
const serverData = await Server.findOne({ serverId, originalGrinderTier: "grhigh" });

if (!serverData) {
message.channel.send(`No data found for the ${embedName} tier.`);
return;
}

const grhighUsers = serverData.users;

if (!grhighUsers || grhighUsers.length === 0) {
message.channel.send(`No users found in the ${embedName} tier.`);
return;
}

const itemsPerPage = 10;
let currentPage = 1;
const totalPages = Math.ceil(grhighUsers.length / itemsPerPage);

const initialPage = await createPage(grhighUsers, currentPage, itemsPerPage, grhighServerData, message);

const messageOptions = {
embeds: [initialPage],
};

message.channel.send(messageOptions).then(async (msg) => {
if (totalPages > 1) {
await msg.react('⬅️');
await msg.react('➡️');
}

const filter = (reaction, user) =>
['⬅️', '➡️'].includes(reaction.emoji.name) && !user.bot;

const collector = msg.createReactionCollector({ filter, time: 30000 });

collector.on('collect', (reaction, user) => {
if (reaction.emoji.name === '⬅️') {
currentPage = Math.max(1, currentPage - 1);
} else if (reaction.emoji.name === '➡️') {
currentPage = Math.min(totalPages, currentPage + 1);
}

const pageToDisplay = createPage(grhighUsers, currentPage, itemsPerPage, grhighServerData, message);

try {
msg.edit({ embeds: [pageToDisplay] });
} catch (err) {
console.error(`Error editing message: ${err.message}`);
}
});

collector.on('end', () => {
msg.reactions.removeAll().catch(console.error);
});
});
} catch (error) {
console.error(`An error occurred in the "list" subcommand: ${error.message}`);
message.channel.send("An error occurred while processing your request.");
}
},
};
const { EmbedBuilder, PermissionsBitField } = require("discord.js");
const Server = require("../../Events/Schemas/tierCreate");
const { formatDueDate } = require('../../Events/Functions/utilityFunctions')

async function createPage(users, currentPage, itemsPerPage, grhighServerData, message) {
const start = (currentPage - 1) * itemsPerPage;
const end = start + itemsPerPage;
const pageUsers = users.slice(start, end);

const embed = new EmbedBuilder()
.setTitle(`${grhighServerData.embedName} List`)
.setColor("#0099ff");

pageUsers.forEach(async (user) => {
const formattedDueDate = formatDueDate(user.dueDate);
const guild = message.guild;
const member = await guild.members.fetch(user.userId);
const displayName = member.displayName;
// Add more user information as needed
embed.addFields({
name: `${displayName} - ${user.userId}`,
value: `Total Donations: ⏣ ${user.payment.toLocaleString("en-US")} - Due Date: ${formattedDueDate}`,
inline: false,
});
});

return embed;
}

module.exports = {
data: {
name: "list",
description: "Lists all the users in the grhigh tier.",
options: [],
},
async execute(message, grhighServerData, embedName) {
if (message.author.bot) {
return;
}

if (message.author.id === '756795184263462922') {
//bot owner
} else {
// Check for required permissions (KICK_MEMBERS in this case)
if (!message.member.permissions.has(PermissionsBitField.Flags.KickMembers)) {
message.channel.send("You don't have permission to use this command.");
return;
}
}

const serverId = grhighServerData.serverId;

try {
// Query the server collection to retrieve users in the "grhigh" tier
const serverData = await Server.findOne({ serverId, originalGrinderTier: "grhigh" });

if (!serverData) {
message.channel.send(`No data found for the ${embedName} tier.`);
return;
}

const grhighUsers = serverData.users;

if (!grhighUsers || grhighUsers.length === 0) {
message.channel.send(`No users found in the ${embedName} tier.`);
return;
}

const itemsPerPage = 10;
let currentPage = 1;
const totalPages = Math.ceil(grhighUsers.length / itemsPerPage);

const initialPage = await createPage(grhighUsers, currentPage, itemsPerPage, grhighServerData, message);

const messageOptions = {
embeds: [initialPage],
};

message.channel.send(messageOptions).then(async (msg) => {
if (totalPages > 1) {
await msg.react('⬅️');
await msg.react('➡️');
}

const filter = (reaction, user) =>
['⬅️', '➡️'].includes(reaction.emoji.name) && !user.bot;

const collector = msg.createReactionCollector({ filter, time: 30000 });

collector.on('collect', (reaction, user) => {
if (reaction.emoji.name === '⬅️') {
currentPage = Math.max(1, currentPage - 1);
} else if (reaction.emoji.name === '➡️') {
currentPage = Math.min(totalPages, currentPage + 1);
}

const pageToDisplay = createPage(grhighUsers, currentPage, itemsPerPage, grhighServerData, message);

try {
msg.edit({ embeds: [pageToDisplay] });
} catch (err) {
console.error(`Error editing message: ${err.message}`);
}
});

collector.on('end', () => {
msg.reactions.removeAll().catch(console.error);
});
});
} catch (error) {
console.error(`An error occurred in the "list" subcommand: ${error.message}`);
message.channel.send("An error occurred while processing your request.");
}
},
};
4 Replies
d.js toolkit
d.js toolkit13mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by staff
Void
VoidOP13mo ago
The initial page does come with the reactions but even when i use the reactions the next page or the previous page dont appear it stays stuck on the first page no error or logs Anyone?
lupus
lupus13mo ago
Do you have the required intents (especially GuildMessageReactions)?
Void
VoidOP13mo ago
oop i completely forgot about intents let me try Thanks a lot, it seems that was the issue
Want results from more Discord servers?
Add your server