diff --git a/TO REWRITE/leaderboard.js b/commands/Economy/leaderboard.js similarity index 80% rename from TO REWRITE/leaderboard.js rename to commands/Economy/leaderboard.js index 932efaad..2ca4995b 100644 --- a/TO REWRITE/leaderboard.js +++ b/commands/Economy/leaderboard.js @@ -1,6 +1,14 @@ const { SlashCommandBuilder, EmbedBuilder } = require("discord.js"); 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 { /** * @@ -40,6 +48,7 @@ class Leaderboard extends BaseCommand { */ async execute(client, interaction) { await interaction.deferReply(); + const type = interaction.options.getString("type"); const isOnMobile = JSON.stringify(Object.keys(interaction.member.presence.clientStatus)) === JSON.stringify(["mobile"]); if (isOnMobile) interaction.followUp({ @@ -48,15 +57,16 @@ class Leaderboard extends BaseCommand { }); if (type === "money") { - const members = await client.membersData.find({ - guildID: interaction.guildId - }).lean(), - membersLeaderboard = members.map(m => { - return { - id: m.id, - money: m.money + m.bankSold - }; - }).sort((a, b) => b.money - a.money); + const membersLeaderboard = [], + membersData = await client.membersData.find({ guildID: interaction.guildId }).lean(); + + await asyncForEach(membersData, async member => { + membersLeaderboard.push({ + id: member.id, + money: member.money + member.bankSold + }); + }); + membersLeaderboard.sort((a, b) => b.money - a.money); if (membersLeaderboard.length > 20) membersLeaderboard.length = 20; let userNames = ""; @@ -91,14 +101,14 @@ class Leaderboard extends BaseCommand { embeds: [embed] }); } else if (type === "level") { - const membersLeaderboard = []; - client.membersData.find({ - guildID: interaction.guildId - }).lean().then(async m => { - await membersLeaderboard.push({ - id: m.id, - level: m.level, - xp: m.exp + const membersLeaderboard = [], + membersData = await client.membersData.find({ guildID: interaction.guildId }).lean(); + + await asyncForEach(membersData, async member => { + membersLeaderboard.push({ + id: member.id, + level: member.level, + xp: member.exp }); }); membersLeaderboard.sort((a, b) => b.level - a.level); @@ -146,15 +156,16 @@ class Leaderboard extends BaseCommand { embeds: [embed] }); } else if (type === "rep") { - const users = await client.usersData.find({ - rep: { $gt: 0 } - }).lean(), - usersLeaderboard = users.map(u => { - return { - id: u.id, - rep: u.rep - }; - }).sort((a, b) => b.rep - a.rep); + const usersLeaderboard = [], + usersData = await client.usersData.find({ rep: { $gt: 0 } }).lean(); + + await asyncForEach(usersData, async user => { + usersLeaderboard.push({ + id: user.id, + rep: user.rep + }); + }); + usersLeaderboard.sort((a, b) => b.rep - a.rep); if (usersLeaderboard.length > 20) usersLeaderboard.length = 20; let userNames = ""; diff --git a/commands/Economy/money.js b/commands/Economy/money.js index 728ec1bd..de97d0d0 100644 --- a/commands/Economy/money.js +++ b/commands/Economy/money.js @@ -1,5 +1,6 @@ const { SlashCommandBuilder, EmbedBuilder } = require("discord.js"); const BaseCommand = require("../../base/BaseCommand"); + const asyncForEach = async (collection, callback) => { const allPromises = collection.map(async key => { await callback(key); diff --git a/commands/Economy/profile.js b/commands/Economy/profile.js index 392d5ee2..3dbb9e70 100644 --- a/commands/Economy/profile.js +++ b/commands/Economy/profile.js @@ -1,5 +1,6 @@ const { SlashCommandBuilder, EmbedBuilder } = require("discord.js"); const BaseCommand = require("../../base/BaseCommand"); + const asyncForEach = async (collection, callback) => { const allPromises = collection.map(async key => { await callback(key); diff --git a/package.json b/package.json index 98608e10..971149d5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jaba", - "version": "4.1.6", + "version": "4.1.7", "description": "My Discord Bot", "main": "index.js", "private": true,