Javascript discord.js 排行榜
Posted
技术标签:
【中文标题】Javascript discord.js 排行榜【英文标题】:Javascript discord.js leaderboard 【发布时间】:2021-09-03 06:22:20 【问题描述】:所以现在我正在尝试制作一个 XP 排行榜,但我被困住了。我能够将它放在将 XP 整理好的地方,但我不知道如何使用 XP (如用户名等)按顺序传递其他信息。老实说,我可能会朝着完全错误的方向前进。我现在的代码是这样的:
let obj =
'737903775691309127':
authorid: '737903775691309127',
username: 'Vibe Development',
serverID: '789259215868395552',
xp: 391,
level: 1,
message: 50,
avatarURL: 'https://cdn.discordapp.com/avatars/737903775691309127/91fa0efafb801dcbea4a669a67c6602e.webp'
,
'455139054464270345':
authorid: '455139054464270345',
username: 'Crispy Cream',
serverID: '789259215868395552',
xp: 753,
level: 2,
message: 99,
avatarURL: 'https://cdn.discordapp.com/avatars/455139054464270345/a_9ab406e25e688594f8316909f0c470b5.gif'
,
'738418475801772072':
authorid: '738418475801772072',
username: 'Starpower11',
serverID: '789259215868395552',
xp: 13,
level: 1,
message: 1,
avatarURL: 'https://cdn.discordapp.com/avatars/738418475801772072/a577da70ee54c036643490a6928b026e.webp'
,
'270904126974590976':
authorid: '270904126974590976',
username: 'Dank Memer',
serverID: '789259215868395552',
xp: 8,
level: 1,
message: 1,
avatarURL: 'https://cdn.discordapp.com/avatars/270904126974590976/d60c6bd5971f06776ba96497117f7f58.webp'
,
'838650739403390976':
authorid: '838650739403390976',
username: 'Vibe Testing',
serverID: '789259215868395552',
xp: 13,
level: 1,
message: 1,
avatarURL: 'https://cdn.discordapp.com/avatars/838650739403390976/930052efdecd2dd4ef7fd2a636949979.webp'
,
'432129282710700033':
authorid: '432129282710700033',
username: 'Aero',
serverID: '789259215868395552',
xp: 14,
level: 1,
message: 1,
avatarURL: 'https://cdn.discordapp.com/avatars/432129282710700033/8323250fe10eef177c35288e0b3b9bb8.webp'
,
'280497242714931202':
authorid: '280497242714931202',
username: 'Yui',
serverID: '789259215868395552',
xp: 3,
level: 1,
message: 1,
avatarURL: 'https://cdn.discordapp.com/avatars/280497242714931202/d1f617030b44cbc64cff2269da2c5e0c.webp'
,
'434556304661544960':
authorid: '434556304661544960',
username: 'Waifu',
serverID: '789259215868395552',
xp: 29,
level: 1,
message: 3,
avatarURL: 'https://cdn.discordapp.com/avatars/434556304661544960/43e86c35225c23d46ae7db54bb81b8f8.webp'
,
'632293976661164052':
authorid: '632293976661164052',
username: 'Paisley Park',
serverID: '789259215868395552',
xp: 13,
level: 1,
message: 2,
avatarURL: 'https://cdn.discordapp.com/avatars/632293976661164052/55146c900a44874dcb75d13df16ceb68.webp'
,
'368362411591204865':
authorid: '368362411591204865',
username: 'Melijn',
serverID: '789259215868395552',
xp: 8,
level: 1,
message: 2,
avatarURL: 'https://cdn.discordapp.com/avatars/368362411591204865/9326b331e0e42f185318bb305fdaa950.webp'
,
'716293342740348948':
authorid: '716293342740348948',
username: 'Pokedi',
serverID: '789259215868395552',
xp: 32,
level: 1,
message: 3,
avatarURL: 'https://cdn.discordapp.com/avatars/716293342740348948/2ef9cd6588c333a3299f694a23e4fa4d.webp'
;
const res = await Object.values(obj);
var levels = [];
for (let index = 0; index < res.length; index++)
levels.push(res[index].xp);
levels.sort((a, b) => b - a);
console.log(levels)
// [ 753, 391, 32, 29, 14, 13, 13, 13, 8, 8, 3 ]
对象是:
let obj =
'userid':
authorid: 'userid',
username: 'username',
serverID: 'server id',
xp: 391,
level: 1,
message: 50,
avatarURL: 'users avatar'
,
我也想得到的是返回的数组看起来像
[
authorid: '455139054464270345',
username: 'Crispy Cream',
serverID: '789259215868395552',
xp: 753,
level: 2,
message: 99,
avatarURL: 'https://cdn.discordapp.com/avatars/455139054464270345/a_9ab406e25e688594f8316909f0c470b5.gif'
,
authorid: '737903775691309127',
username: 'Vibe Development',
serverID: '789259215868395552',
xp: 391,
level: 1,
message: 50,
avatarURL: 'https://cdn.discordapp.com/avatars/737903775691309127/91fa0efafb801dcbea4a669a67c6602e.webp'
,
... and so on
]
希望这一切都说得通。谢谢!
【问题讨论】:
【参考方案1】:要获取按属性(在本例中为 xp
)排序的对象数组,而不仅仅是属性本身,您应该直接在 xp
属性上使用 Array.sort
,而无需先创建 levels
数组.比如:
const sortedObjects =
Object.values(obj) // get an array of the objects themselves
.sort((a, b) =>
b.xp - a.xp // sort by xp from greatest to least
);
或者只是:
const sortedObjects = Object.values(obj).sort((a, b) => b.xp - a.xp)
将返回按其xp
属性排序的对象数组。
【讨论】:
以上是关于Javascript discord.js 排行榜的主要内容,如果未能解决你的问题,请参考以下文章
排行榜命令中未定义的用户名 [ quick.db / discord.js ]
如何在 discord.js 中使用 JSON 数据库制作排行榜