(4.1.7) Возвращена команда leaderboard

This commit is contained in:
JonnyBro 2022-10-02 21:44:53 +05:00
parent fe2e598c7b
commit 60ec94558a
4 changed files with 40 additions and 27 deletions

View file

@ -1,6 +1,14 @@
const { SlashCommandBuilder, EmbedBuilder } = require("discord.js"); const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");
const BaseCommand = require("../../base/BaseCommand"); const BaseCommand = require("../../base/BaseCommand");
const asyncForEach = async (collection, callback) => {
const allPromises = collection.map(async key => {
await callback(key);
});
return await Promise.all(allPromises);
};
class Leaderboard extends BaseCommand { class Leaderboard extends BaseCommand {
/** /**
* *
@ -40,6 +48,7 @@ class Leaderboard extends BaseCommand {
*/ */
async execute(client, interaction) { async execute(client, interaction) {
await interaction.deferReply(); await interaction.deferReply();
const type = interaction.options.getString("type"); const type = interaction.options.getString("type");
const isOnMobile = JSON.stringify(Object.keys(interaction.member.presence.clientStatus)) === JSON.stringify(["mobile"]); const isOnMobile = JSON.stringify(Object.keys(interaction.member.presence.clientStatus)) === JSON.stringify(["mobile"]);
if (isOnMobile) interaction.followUp({ if (isOnMobile) interaction.followUp({
@ -48,15 +57,16 @@ class Leaderboard extends BaseCommand {
}); });
if (type === "money") { if (type === "money") {
const members = await client.membersData.find({ const membersLeaderboard = [],
guildID: interaction.guildId membersData = await client.membersData.find({ guildID: interaction.guildId }).lean();
}).lean(),
membersLeaderboard = members.map(m => { await asyncForEach(membersData, async member => {
return { membersLeaderboard.push({
id: m.id, id: member.id,
money: m.money + m.bankSold money: member.money + member.bankSold
}; });
}).sort((a, b) => b.money - a.money); });
membersLeaderboard.sort((a, b) => b.money - a.money);
if (membersLeaderboard.length > 20) membersLeaderboard.length = 20; if (membersLeaderboard.length > 20) membersLeaderboard.length = 20;
let userNames = ""; let userNames = "";
@ -91,14 +101,14 @@ class Leaderboard extends BaseCommand {
embeds: [embed] embeds: [embed]
}); });
} else if (type === "level") { } else if (type === "level") {
const membersLeaderboard = []; const membersLeaderboard = [],
client.membersData.find({ membersData = await client.membersData.find({ guildID: interaction.guildId }).lean();
guildID: interaction.guildId
}).lean().then(async m => { await asyncForEach(membersData, async member => {
await membersLeaderboard.push({ membersLeaderboard.push({
id: m.id, id: member.id,
level: m.level, level: member.level,
xp: m.exp xp: member.exp
}); });
}); });
membersLeaderboard.sort((a, b) => b.level - a.level); membersLeaderboard.sort((a, b) => b.level - a.level);
@ -146,15 +156,16 @@ class Leaderboard extends BaseCommand {
embeds: [embed] embeds: [embed]
}); });
} else if (type === "rep") { } else if (type === "rep") {
const users = await client.usersData.find({ const usersLeaderboard = [],
rep: { $gt: 0 } usersData = await client.usersData.find({ rep: { $gt: 0 } }).lean();
}).lean(),
usersLeaderboard = users.map(u => { await asyncForEach(usersData, async user => {
return { usersLeaderboard.push({
id: u.id, id: user.id,
rep: u.rep rep: user.rep
}; });
}).sort((a, b) => b.rep - a.rep); });
usersLeaderboard.sort((a, b) => b.rep - a.rep);
if (usersLeaderboard.length > 20) usersLeaderboard.length = 20; if (usersLeaderboard.length > 20) usersLeaderboard.length = 20;
let userNames = ""; let userNames = "";

View file

@ -1,5 +1,6 @@
const { SlashCommandBuilder, EmbedBuilder } = require("discord.js"); const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");
const BaseCommand = require("../../base/BaseCommand"); const BaseCommand = require("../../base/BaseCommand");
const asyncForEach = async (collection, callback) => { const asyncForEach = async (collection, callback) => {
const allPromises = collection.map(async key => { const allPromises = collection.map(async key => {
await callback(key); await callback(key);

View file

@ -1,5 +1,6 @@
const { SlashCommandBuilder, EmbedBuilder } = require("discord.js"); const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");
const BaseCommand = require("../../base/BaseCommand"); const BaseCommand = require("../../base/BaseCommand");
const asyncForEach = async (collection, callback) => { const asyncForEach = async (collection, callback) => {
const allPromises = collection.map(async key => { const allPromises = collection.map(async key => {
await callback(key); await callback(key);

View file

@ -1,6 +1,6 @@
{ {
"name": "jaba", "name": "jaba",
"version": "4.1.6", "version": "4.1.7",
"description": "My Discord Bot", "description": "My Discord Bot",
"main": "index.js", "main": "index.js",
"private": true, "private": true,