I created a leaderboard that sorts users from richest to poorest in discord.js and mongoose. Some users aren’t appearing on the leaderboard when they are supposed to be on it. This guy has trillions but isn’t on it
Here is my code for my leaderboard
const { Message, DiscordAPIError } = require('discord.js')
const Client = require('../bot')
const stats = require('./stats')
const mongoose = require('mongoose')
const Discord = require('discord.js')
const commaNumber = require('comma-number')
module.exports = {
name: `lb`,
aliases:['leaderboard'],
/**
* @param {Client} client
* @param {Message} message
* @param {String[]} args
*/
run: async(client, message, args) => {
const users = await client.schema.find()
const Userid = users.User
const lb = users
.slice(0)
.sort(({ Bobux: a }, { Bobux: b }) => b - a)
.map(
({ User, Bobux }, pos) => `${pos + 1}. <@${User}> - ${commaNumber(Bobux)} Bobux`,
);
const newnew = lb.slice(0, 15)
const embed = new Discord.MessageEmbed()
.setTitle('Global Leaderboard For Most Bobux - Top 15')
.setDescription(newnew)
.setColor('RANDOM')
message.channel.send(embed)
},
timeout: 5000
}
Here is the schema code. It’s in the main bot.js file
this.schema = this.mongoose.model(
`economy`,
new this.mongoose.Schema({
User: String,
Bobux: Number,
})
);
const self = this
this.economy = {
async getBal(User) {
return await self.schema.findOne({
User
}).then((d) => d ? d.Bobux : 0)
},
async addBal(User, Bobux) {
return await self.schema.findOne({ User }, async(err, data) => {
if(err) throw err
if(data) {
data.Bobux += Number(Bobux)
} else {
data = new self.schema({ User, Bobux })
}
data.save()
})
},
async subBal(User, Bobux) {
return await self.schema.findOne({ User }, async(err, data) => {
if(err) throw err
if(data) {
data.Bobux -= Number(Bobux)
} else {
data = new self.schema({ User, Bobux })
}
data.save()
})
}
I am not sure why users are not appearing on the leaderboard
Sorry for the long post
Cause you mention and your client ( discord application ) is not caching that user in your server yet so they don't display with you only. Behaviour is same with other client if they don't cache that user yet I would prefer you fetch guildMember and display their nickname or username with tag in leaderboard ( not mention) for well display