TheShadowGamer / Invite-Manager

Invite manager is an open-source discord bot that allows you to track the invites of people who join your server.
MIT License
117 stars 61 forks source link

[Bug] Leaderboard rankings are all "1" #5

Closed voidpls closed 3 years ago

voidpls commented 3 years ago

https://i.imgur.com/ZuAn35N.png

The quantities and order are correct, but the numbers for the rankings are all wrong.

It seems like this is caused by the asynchronous code here:

let LB = [];
await all.forEach(async entry => {
    if(!entry.invites) return;
    LB.push(`${LB.length + 1}. ${(await (client.users.fetch(entry.discordUser))).tag} - ${entry.invites}`);
});

LB.length is 0 in each iteration, because the LB.push() from the previous iteration has not completed yet, due to each iteration not waiting for earlier promises to resolve.

let LB = all.filter(entry => entry.invites)
    .map(async (entry, i) => `${i + 1}. ${(await (client.users.fetch(entry.discordUser))).tag} - ${entry.invites}`)
LB = await Promise.all(LB)

Replacing the original snippet with this seems to fix it, but I haven't done enough testing to confidently PR it.

TheShadowGamer commented 3 years ago

I’m not using .map because sometimes there will be entries with 0 invites, and I don’t want those to show. I’ll look into a fix for this though, thanks.

voidpls commented 3 years ago

The .filter should remove any entries with 0 invites

TheShadowGamer commented 3 years ago

Ah ok.

TheShadowGamer commented 3 years ago

This has been fixed in v2.0.5!