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.");
}
},
};