我找不到有关此问题的任何文档或任何内容。我正在使用VSC和discord.JS,并且正在制作排行榜系统。目标是当用户输入!top时,将弹出一个排行榜(嵌入式)排行榜,其中前10位的钱最多。这在重新加载后(仅在某些情况下)第一次工作,但之后.sort不t排行榜和排行榜的统计数据在其他地方被重新排列,而$ 0则排在第一。这是我的代码:
if (msg.content.startsWith("!top")) {
let moneyC = [];
let embedT = new Discord.MessageEmbed();
let membersCurrent = 0;
msg.guild.members.cache.forEach(element => {
money.fetchBal(element.id).then((i) => {
membersCurrent++;
moneyC.push({ name: element.user.username, moneyT: i.money });
if (membersCurrent >= msg.guild.memberCount) {
moneyC.sort((a, b) => b.money - a.money);
for (i = 0; i < 10; i++) {
embedT.addField("---", moneyC[i].name + " = " + moneyC[i].moneyT);
}
}
if (membersCurrent == msg.guild.memberCount) {
membersCurrent++;
sendEmbed();
}
})
});
function sendEmbed() {
console.log(moneyC);
embedT.setDescription("here are the top 10 people with the highest balance!")
msg.channel.send(embedT);
moneyC = [];
}
}
这是控制台输出和第一次和第二次的不和谐输出:
控制台:
{ name: 'TheBigCringeMaster', moneyT: 100 },
{ name: 'PogchampInRealLife', moneyT: 0 },
{ name: 'bluestone', moneyT: 0 },
{ name: 'Birdie_YT', moneyT: 0 },
{ name: 'iDopeyScope', moneyT: 0 },
{ name: 'Lewcyる', moneyT: 0 },
{ name: 'Aretimis', moneyT: 0 },
{ name: 'Cam S', moneyT: 0 },
{ name: 'IAmABoomer', moneyT: 0 },
{ name: '$HOO!ER_SavgE', moneyT: 0 },
{ name: 'AwokenYt', moneyT: 0 },
{ name: 'Wingyman2019', moneyT: 0 },
{ name: 'I LOVE EINAR - owo', moneyT: 0 },
{ name: 'Lilly', moneyT: 0 },
{ name: 'Reaction Roles', moneyT: 0 },
{ name: 'boobieman123', moneyT: 0 },
{ name: 'Jamelfarm', moneyT: 0 }
(这些是我的一些不和谐成员)
这是不和谐的输出,@ TheBigCringeMaster是我,总是有最多的钱:
这里是图像= img
问题是您要切换moneyT
和money
,
moneyC.sort((a, b) => b.money - a.money);
=> moneyC.sort((a, b) => b.moneyT - a.moneyT);
如显示:
moneyC.push({ name: element.user.username, moneyT: i.money })