I know there are many questions already answered about this but no matter what I try it doesn't work like I want it to.
I am trying to make a sort of leaderboard on my discord bot and that part works fine, but if you're familiar with the game Minecraft you know that every user has an UUID, and I have the UUID of the players in my data, I need the ASYNC/AWAIT to convert de UUID to the player's username BEFORE continuing with the code. However ASYNC/AWAIT simply does not work in a forEach loop.
Here's the code I have:
const { MessageEmbed } = require("discord.js");
const mcapi = require('mcapi');
const { blue_light, red_light } = require(`../../colours.json`);
const eventogxp = require(`../../models/eventogxpModel`)
run: async (client, message, args) => {
var data = await eventogxp.find({
__v: 0
}).sort([[
'quantiagxp', 'descending'
]])
const generateEmbed = start => {
const current = data.slice(start, start + 10)
leaderboard = ""
var i = start+1;
current.forEach(async g => {
var ign = await mcapi.uuidToUsername(g.uuid) //This converts the UUID to UserName
leaderboard += "`" + `#${i}` + "` " + "`" + `${ign}` + "`" + ` - **${g.quantiagxp}**` + `\n`
i += 1
});
const embed = new MessageEmbed()
.setTitle(`EVENTOGXP TOP ${start + 1}-${start + current.length} de ${data.length}`)
.setDescription(`${leaderboard}`)
.setColor(blue_light)
return embed
}
const author = message.author
message.channel.send(generateEmbed(0)).then(message => {
if (data.length <= 10) return
message.react('➡️')
const collector = message.createReactionCollector(
(reaction, user) => ['⬅️', '➡️'].includes(reaction.emoji.name) && user.id === author.id,
{time: 6000000}
)
let currentIndex = 0
collector.on('collect', reaction => {
message.reactions.removeAll().then(async () => {
reaction.emoji.name === '⬅️' ? currentIndex -= 10 : currentIndex += 10
message.edit(generateEmbed(currentIndex))
if (currentIndex !== 0) await message.react('⬅️')
if (currentIndex + 10 < data.length) message.react('➡️')
})
})
})
}
This is the result that I get: object Promise error
The expected output would be the player's in-game-name (username) instead of the object Promise.