2022-08-09 23:48:33 +05:00
|
|
|
const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");
|
|
|
|
const BaseCommand = require("../../base/BaseCommand");
|
|
|
|
|
2022-10-02 21:44:53 +05:00
|
|
|
const asyncForEach = async (collection, callback) => {
|
|
|
|
const allPromises = collection.map(async key => {
|
|
|
|
await callback(key);
|
|
|
|
});
|
|
|
|
|
|
|
|
return await Promise.all(allPromises);
|
|
|
|
};
|
|
|
|
|
2022-08-09 23:48:33 +05:00
|
|
|
class Leaderboard extends BaseCommand {
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {import("../base/JaBa")} client
|
|
|
|
*/
|
|
|
|
constructor(client) {
|
|
|
|
super({
|
|
|
|
command: new SlashCommandBuilder()
|
|
|
|
.setName("leaderboard")
|
|
|
|
.setDescription(client.translate("economy/leaderboard:DESCRIPTION"))
|
2022-10-13 00:05:36 +05:00
|
|
|
.setDMPermission(false)
|
2022-08-09 23:48:33 +05:00
|
|
|
.addStringOption(option => option.setName("type")
|
|
|
|
.setDescription(client.translate("owner/debug:TYPE"))
|
|
|
|
.setRequired(true)
|
|
|
|
.addChoices(
|
|
|
|
{ name: client.translate("economy/leaderboard:LEVEL"), value: "level" },
|
|
|
|
{ name: client.translate("economy/leaderboard:MONEY"), value: "money" },
|
2022-12-15 21:02:38 +05:00
|
|
|
{ name: client.translate("economy/leaderboard:REP"), value: "rep" },
|
2022-08-09 23:48:33 +05:00
|
|
|
)),
|
|
|
|
aliases: [],
|
|
|
|
dirname: __dirname,
|
2022-12-15 21:02:38 +05:00
|
|
|
ownerOnly: false,
|
2022-08-09 23:48:33 +05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {import("../../base/JaBa")} client
|
|
|
|
*/
|
|
|
|
async onLoad() {
|
|
|
|
//...
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {import("../../base/JaBa")} client
|
|
|
|
* @param {import("discord.js").ChatInputCommandInteraction} interaction
|
|
|
|
* @param {Object} data
|
|
|
|
*/
|
|
|
|
async execute(client, interaction) {
|
|
|
|
await interaction.deferReply();
|
2022-10-02 21:44:53 +05:00
|
|
|
|
2022-08-09 23:48:33 +05:00
|
|
|
const type = interaction.options.getString("type");
|
|
|
|
const isOnMobile = JSON.stringify(Object.keys(interaction.member.presence.clientStatus)) === JSON.stringify(["mobile"]);
|
|
|
|
if (isOnMobile) interaction.followUp({
|
|
|
|
content: interaction.translate("economy/leaderboard:MOBILE"),
|
2022-12-15 21:02:38 +05:00
|
|
|
ephemeral: true,
|
2022-08-09 23:48:33 +05:00
|
|
|
});
|
|
|
|
|
|
|
|
if (type === "money") {
|
2022-10-02 21:44:53 +05:00
|
|
|
const membersLeaderboard = [],
|
|
|
|
membersData = await client.membersData.find({ guildID: interaction.guildId }).lean();
|
|
|
|
|
|
|
|
await asyncForEach(membersData, async member => {
|
|
|
|
membersLeaderboard.push({
|
|
|
|
id: member.id,
|
2022-12-15 21:02:38 +05:00
|
|
|
money: member.money + member.bankSold,
|
2022-10-02 21:44:53 +05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
membersLeaderboard.sort((a, b) => b.money - a.money);
|
2022-08-09 23:48:33 +05:00
|
|
|
if (membersLeaderboard.length > 20) membersLeaderboard.length = 20;
|
|
|
|
|
|
|
|
let userNames = "";
|
|
|
|
let money = "";
|
|
|
|
for (let i = 0; i < membersLeaderboard.length; i++) {
|
|
|
|
const data = membersLeaderboard[i];
|
|
|
|
|
2022-10-02 23:40:05 +05:00
|
|
|
userNames += `**${i + 1}**. <@${data.id}>\n`;
|
2022-08-09 23:48:33 +05:00
|
|
|
money += `${data.money}\n`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const embed = new EmbedBuilder()
|
|
|
|
.setAuthor({
|
|
|
|
name: interaction.translate("economy/leaderboard:TABLE", {
|
2022-12-15 21:02:38 +05:00
|
|
|
name: interaction.guild.name,
|
2022-08-09 23:48:33 +05:00
|
|
|
}),
|
2022-12-15 21:02:38 +05:00
|
|
|
iconURL: interaction.guild.iconURL(),
|
2022-08-09 23:48:33 +05:00
|
|
|
})
|
|
|
|
.setColor(client.config.embed.color)
|
|
|
|
.addFields({
|
2022-10-02 23:40:05 +05:00
|
|
|
name: interaction.translate("common:USER"),
|
2022-08-09 23:48:33 +05:00
|
|
|
value: userNames,
|
2022-12-15 21:02:38 +05:00
|
|
|
inline: true,
|
2022-08-09 23:48:33 +05:00
|
|
|
}, {
|
|
|
|
name: interaction.translate("common:CREDITS"),
|
|
|
|
value: money,
|
2022-12-15 21:02:38 +05:00
|
|
|
inline: true,
|
2022-08-09 23:48:33 +05:00
|
|
|
});
|
|
|
|
|
|
|
|
interaction.editReply({
|
2022-12-15 21:02:38 +05:00
|
|
|
embeds: [embed],
|
2022-08-09 23:48:33 +05:00
|
|
|
});
|
|
|
|
} else if (type === "level") {
|
2022-10-02 21:44:53 +05:00
|
|
|
const membersLeaderboard = [],
|
|
|
|
membersData = await client.membersData.find({ guildID: interaction.guildId }).lean();
|
|
|
|
|
|
|
|
await asyncForEach(membersData, async member => {
|
|
|
|
membersLeaderboard.push({
|
|
|
|
id: member.id,
|
|
|
|
level: member.level,
|
2022-12-15 21:02:38 +05:00
|
|
|
xp: member.exp,
|
2022-08-09 23:48:33 +05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
membersLeaderboard.sort((a, b) => b.level - a.level);
|
|
|
|
if (membersLeaderboard.length > 20) membersLeaderboard.length = 20;
|
|
|
|
|
|
|
|
const userNames = [];
|
|
|
|
const level = [];
|
|
|
|
const xp = [];
|
|
|
|
for (let i = 0; i < membersLeaderboard.length; i++) {
|
|
|
|
const data = membersLeaderboard[i];
|
|
|
|
|
2022-10-02 23:40:05 +05:00
|
|
|
userNames.push(`**${i + 1}**. <@${data.id}>`);
|
2022-08-09 23:48:33 +05:00
|
|
|
level.push(`${data.level}`);
|
|
|
|
xp.push(`${data.xp} / ${5 * (data.level * data.level) + 80 * data.level + 100}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const embed = new EmbedBuilder()
|
|
|
|
.setAuthor({
|
|
|
|
name: interaction.translate("economy/leaderboard:TABLE", {
|
2022-12-15 21:02:38 +05:00
|
|
|
name: interaction.guild.name,
|
2022-08-09 23:48:33 +05:00
|
|
|
}),
|
2022-12-15 21:02:38 +05:00
|
|
|
iconURL: interaction.guild.iconURL(),
|
2022-08-09 23:48:33 +05:00
|
|
|
})
|
|
|
|
.setColor(client.config.embed.color)
|
|
|
|
.addFields([
|
|
|
|
{
|
2022-10-02 23:40:05 +05:00
|
|
|
name: interaction.translate("common:USER"),
|
2022-08-09 23:48:33 +05:00
|
|
|
value: userNames.join("\n"),
|
2022-12-15 21:02:38 +05:00
|
|
|
inline: true,
|
2022-08-09 23:48:33 +05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: interaction.translate("common:LEVEL"),
|
|
|
|
value: level.join("\n"),
|
2022-12-15 21:02:38 +05:00
|
|
|
inline: true,
|
2022-08-09 23:48:33 +05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: interaction.translate("common:XP"),
|
|
|
|
value: xp.join("\n"),
|
2022-12-15 21:02:38 +05:00
|
|
|
inline: true,
|
|
|
|
},
|
2022-08-09 23:48:33 +05:00
|
|
|
]);
|
|
|
|
|
|
|
|
interaction.editReply({
|
2022-12-15 21:02:38 +05:00
|
|
|
embeds: [embed],
|
2022-08-09 23:48:33 +05:00
|
|
|
});
|
|
|
|
} else if (type === "rep") {
|
2022-10-02 21:44:53 +05:00
|
|
|
const usersLeaderboard = [],
|
|
|
|
usersData = await client.usersData.find({ rep: { $gt: 0 } }).lean();
|
|
|
|
|
|
|
|
await asyncForEach(usersData, async user => {
|
|
|
|
usersLeaderboard.push({
|
|
|
|
id: user.id,
|
2022-12-15 21:02:38 +05:00
|
|
|
rep: user.rep,
|
2022-10-02 21:44:53 +05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
usersLeaderboard.sort((a, b) => b.rep - a.rep);
|
2022-08-09 23:48:33 +05:00
|
|
|
if (usersLeaderboard.length > 20) usersLeaderboard.length = 20;
|
|
|
|
|
|
|
|
let userNames = "";
|
|
|
|
let rep = "";
|
|
|
|
for (let i = 0; i < usersLeaderboard.length; i++) {
|
|
|
|
const data = usersLeaderboard[i];
|
|
|
|
|
2022-10-02 23:40:05 +05:00
|
|
|
userNames += `**${i + 1}**. <@${data.id}>\n`;
|
2022-08-09 23:48:33 +05:00
|
|
|
rep += `${data.rep}\n`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const embed = new EmbedBuilder()
|
|
|
|
.setAuthor({
|
|
|
|
name: interaction.translate("economy/leaderboard:TABLE", {
|
2022-12-15 21:02:38 +05:00
|
|
|
name: interaction.guild.name,
|
2022-08-09 23:48:33 +05:00
|
|
|
}),
|
2022-12-15 21:02:38 +05:00
|
|
|
iconURL: interaction.guild.iconURL(),
|
2022-08-09 23:48:33 +05:00
|
|
|
})
|
|
|
|
.setColor(client.config.embed.color)
|
|
|
|
.addFields({
|
2022-10-02 23:40:05 +05:00
|
|
|
name: interaction.translate("common:USER"),
|
2022-08-09 23:48:33 +05:00
|
|
|
value: userNames,
|
2022-12-15 21:02:38 +05:00
|
|
|
inline: true,
|
2022-08-09 23:48:33 +05:00
|
|
|
}, {
|
|
|
|
name: interaction.translate("common:REP"),
|
|
|
|
value: rep,
|
2022-12-15 21:02:38 +05:00
|
|
|
inline: true,
|
2022-08-09 23:48:33 +05:00
|
|
|
});
|
|
|
|
|
|
|
|
interaction.editReply({
|
2022-12-15 21:02:38 +05:00
|
|
|
embeds: [embed],
|
2022-08-09 23:48:33 +05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Leaderboard;
|