Is it possible to prevent the leaderboard from resetting every time I restart my client? You can see an example here: https://i.stack.imgur.com/2nEPw.png
Please disregard the "undefined" error, I will correct it.
In the current setup, the leaderboard only displays users who have interacted with the bot after its restart. Once they use a command, their ranking is reflected on the leaderboard as shown in this image:
https://i.stack.imgur.com/5oiMe.png
This is the code I am using:
let Data = await UserModel.find({});
let Members = [];
for (let obj of Data) {
if (
message.guild?.members.cache.map((member) => member.id).includes(obj.ID)
)
Members.push(obj);
}
const Embed = new MessageEmbed()
.setTitle(`${message.guild?.name}'s Leaderboard`)
.setFooter({
text: `You aren't ranked yet!`,
iconURL: message.author.displayAvatarURL({ dynamic: true }),
});
Members = Members.sort(function (b, a) {
return a.Money - b.Money;
});
Members = Members.filter(function BigEnough(value) {
return value.Money > 0;
});
let pos = 0;
for (let obj of Members) {
pos++;
if (obj.ID === message.author.id) {
Embed.setFooter({
text: `You are ranked ${pos} in the leaderboard!`,
iconURL: message.author.displayAvatarURL({ dynamic: true }),
});
}
}
Members = Members.slice(0, 10);
let desc: any;
for (let i = 0; i < Members.length; i++) {
let User = message.guild?.members.cache.get(Members[i].ID);
if (!User) return;
let Bal = Members[i].Money;
desc += `${i + 1}. ${User.user.tag} - $${Math.round(Bal)}\n`;
}
Embed.setDescription(desc);
Embed.setColor("RED");
message.reply({ embeds: [Embed] });
Currently, the code seems to be pulling cached user data only. Is there a way to fetch all the users in the server?