我要实现的目标是在不和谐的bot上执行我的经济系统的排行榜命令。我将quick.db用于数据库。问题出在数组排序中,我需要平衡从最大变到最小。由于我是通过以下方式为用户存储数据的:bal_${message.author.id}_${message.guild.id}
我没有一个包含所有余额和用户ID的数组。每个用户都有自己的变量。
因此,按照我的编码方式,我制作了一个数组,其数据如下:bal + "$ " + mlist[i]
(mlist是所有用户的message.author.tag
的列表,而我是for中的计数器循环)。数据如下:
[ '10000$ MCUniversity#0859',
'0$ Deck Shop#1220',
'0$ BookBot#9900',
'-',
'-' ]
(如果列表中的空格为空,则设置负号(因为我希望显示前5位用户,所以为0-4)为空)
现在,我的问题是对列表进行排序。我不知道如何对其进行排序,以便仅查看数字而不是名称,并且对它进行正确排序(例如,我有10000 $,而我的朋友2 $,但自2以来他就在我的前面大于1)。
这是我的代码:
const Discord = require('discord.js')
const db = require('quick.db')
module.exports.run = async (client, message, args) => {
// Get the Guild and store it under the variable "list"
const list = client.guilds.get(message.guild.id);
const mlist = []
const ilist = []
// Iterate through the collection of GuildMembers from the Guild getting the username property of each member
list.members.forEach(member => mlist.push(member.user.tag));
list.members.forEach(member => ilist.push(member.user.id));
//mlist contains all the user's usernames and tags(e.g. MCUniversity#0859), ilist contains all ID's
console.log(mlist)
console.log(ilist)
let bl = []
for (let i = 0; i < ilist.length; i++) {
let bal = await db.fetch(`bal_${ilist[i]}_${message.guild.id}`)
if (bal == null) bal = 0;
console.log(mlist[i] + " " + bal)
bl.push(bal + "$ " + mlist[i])
}
bl.sort(); //function(a, b){return b-a}
bl.reverse();
if (bl[0] == undefined) bl[0] = "-"
if (bl[1] == undefined) bl[1] = "-"
if (bl[2] == undefined) bl[2] = "-"
if (bl[3] == undefined) bl[3] = "-"
if (bl[4] == undefined) bl[4] = "-"
console.log(bl)
let embed = new Discord.RichEmbed()
.setColor("GREEN")
.setAuthor(message.guild.name + " economy leaderboards.")
.setDescription(`**1.** ${bl[0]}\n**2.** ${bl[1]}\n**3.** ${bl[2]}\n**4.** ${bl[3]}\n**5.** ${bl[4]}\n`)
message.channel.send(embed)
}