diff --git a/commands/Economy/leaderboard.js b/commands/Economy/leaderboard.js index 1c145ef0..ba3b6805 100644 --- a/commands/Economy/leaderboard.js +++ b/commands/Economy/leaderboard.js @@ -1,14 +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); - }); - - return await Promise.all(allPromises); -}; - class Leaderboard extends BaseCommand { /** * @@ -60,7 +52,7 @@ class Leaderboard extends BaseCommand { const membersLeaderboard = [], membersData = await client.membersData.find({ guildID: interaction.guildId }).lean(); - await asyncForEach(membersData, async member => { + await client.functions.asyncForEach(membersData, async member => { membersLeaderboard.push({ id: member.id, money: member.money + member.bankSold, @@ -103,7 +95,7 @@ class Leaderboard extends BaseCommand { const membersLeaderboard = [], membersData = await client.membersData.find({ guildID: interaction.guildId }).lean(); - await asyncForEach(membersData, async member => { + await client.functions.asyncForEach(membersData, async member => { membersLeaderboard.push({ id: member.id, level: member.level, @@ -157,7 +149,7 @@ class Leaderboard extends BaseCommand { const usersLeaderboard = [], usersData = await client.usersData.find({ rep: { $gt: 0 } }).lean(); - await asyncForEach(usersData, async user => { + await client.functions.asyncForEach(usersData, async user => { usersLeaderboard.push({ id: user.id, rep: user.rep, diff --git a/commands/Economy/money.js b/commands/Economy/money.js index 42af4969..b4532103 100644 --- a/commands/Economy/money.js +++ b/commands/Economy/money.js @@ -1,14 +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); - }); - - return await Promise.all(allPromises); -}; - class Money extends BaseCommand { /** * @@ -53,7 +45,7 @@ class Money extends BaseCommand { const guilds = client.guilds.cache.filter(g => g.members.cache.find(m => m.id === member.id)); let globalMoney = 0; - await asyncForEach(guilds, async guild => { + await client.functions.asyncForEach(guilds, async guild => { const data = await client.findOrCreateMember({ id: member.id, guildId: guild.id, diff --git a/commands/Economy/profile.js b/commands/Economy/profile.js index d8ba96a6..d0726611 100644 --- a/commands/Economy/profile.js +++ b/commands/Economy/profile.js @@ -1,14 +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); - }); - - return await Promise.all(allPromises); -}; - class Profile extends BaseCommand { /** * @@ -58,7 +50,7 @@ class Profile extends BaseCommand { const guilds = client.guilds.cache.filter(g => g.members.cache.find(m => m.id === member.id)); let globalMoney = 0; - await asyncForEach(guilds, async guild => { + await client.functions.asyncForEach(guilds, async guild => { const data = await client.findOrCreateMember({ id: member.id, guildId: guild.id, diff --git a/helpers/functions.js b/helpers/functions.js index 6118d2c5..9e834672 100644 --- a/helpers/functions.js +++ b/helpers/functions.js @@ -14,6 +14,20 @@ module.exports = { if (channel) return (await channel.createInvite()).url || "No channels found or missing permissions"; }, + /** + * Calls a callback for each element in collection async + * @param {Array} collection + * @param {Function} callback + * @returns {Promise} + */ + async asyncForEach (collection, callback) { + const allPromises = collection.map(async key => { + await callback(key); + }); + + return await Promise.all(allPromises); + }, + /** * Sort array by key * @param {Array} array Array to sort