JaBa/commands/Economy/leaderboard.js

188 lines
4.9 KiB
JavaScript
Raw Normal View History

const { SlashCommandBuilder } = require("discord.js");
2022-08-09 23:48:33 +05:00
const BaseCommand = require("../../base/BaseCommand");
class Leaderboard extends BaseCommand {
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../base/Client")} client
2022-08-09 23:48:33 +05:00
*/
constructor(client) {
super({
command: new SlashCommandBuilder()
.setName("leaderboard")
.setDescription(client.translate("economy/leaderboard:DESCRIPTION"))
.setDescriptionLocalizations({
2023-07-05 00:58:06 +05:00
uk: client.translate("economy/leaderboard:DESCRIPTION", null, "uk-UA"),
ru: client.translate("economy/leaderboard:DESCRIPTION", null, "ru-RU"),
})
.setDMPermission(false)
2023-07-05 00:58:06 +05:00
.addStringOption(option =>
option
.setName("type")
.setDescription(client.translate("owner/debug:TYPE"))
.setDescriptionLocalizations({
uk: client.translate("owner/debug:TYPE", null, "uk-UA"),
ru: client.translate("owner/debug:TYPE", null, "ru-RU"),
})
.setRequired(true)
.setChoices({ name: client.translate("common:LEVEL"), value: "level" }, { name: client.translate("common:MONEY"), value: "money" }, { name: client.translate("common:REP"), value: "rep" }),
),
2022-08-09 23:48:33 +05:00
dirname: __dirname,
ownerOnly: false,
2022-08-09 23:48:33 +05:00
});
}
2022-08-09 23:48:33 +05:00
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../../base/Client")} client
2022-08-09 23:48:33 +05:00
* @param {import("discord.js").ChatInputCommandInteraction} interaction
* @param {Object} data
*/
async execute(client, interaction) {
await interaction.deferReply();
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"]);
2023-07-05 00:58:06 +05:00
if (isOnMobile)
interaction.followUp({
content: interaction.translate("economy/leaderboard:MOBILE"),
ephemeral: true,
});
2022-08-09 23:48:33 +05:00
const embed = client.embed({
author: {
name: interaction.translate("economy/leaderboard:TABLE", {
name: interaction.guild.name,
}),
iconURL: interaction.guild.iconURL(),
},
});
2022-08-09 23:48:33 +05:00
if (type === "money") {
const membersLeaderboard = [],
membersData = await client.membersData.find({ guildID: interaction.guildId }).lean();
2023-11-05 16:03:23 +05:00
await client.functions.asyncForEach(membersData, member => {
membersLeaderboard.push({
id: member.id,
money: member.money + member.bankSold,
});
});
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 = "",
money = "";
2022-08-09 23:48:33 +05:00
for (let i = 0; i < membersLeaderboard.length; i++) {
const data = membersLeaderboard[i];
userNames += `**${i + 1}**. <@${data.id}>\n`;
2022-08-09 23:48:33 +05:00
money += `${data.money}\n`;
}
embed.fields = [
{
name: interaction.translate("common:USER"),
value: userNames,
inline: true,
},
{
name: interaction.translate("common:CREDITS"),
value: money,
inline: true,
},
];
2022-08-09 23:48:33 +05:00
interaction.editReply({
embeds: [embed],
2022-08-09 23:48:33 +05:00
});
} else if (type === "level") {
const membersLeaderboard = [],
membersData = await client.membersData.find({ guildID: interaction.guildId }).lean();
2023-11-05 16:03:23 +05:00
await client.functions.asyncForEach(membersData, async member => {
membersLeaderboard.push({
id: member.id,
level: member.level,
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 = [],
level = [],
xp = [];
2022-08-09 23:48:33 +05:00
for (let i = 0; i < membersLeaderboard.length; i++) {
const data = membersLeaderboard[i];
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}`);
}
embed.fields = [
{
name: interaction.translate("common:USER"),
value: userNames.join("\n"),
inline: true,
},
{
name: interaction.translate("common:LEVEL"),
value: level.join("\n"),
inline: true,
},
{
name: interaction.translate("common:XP"),
value: xp.join("\n"),
inline: true,
},
];
2022-08-09 23:48:33 +05:00
interaction.editReply({
embeds: [embed],
2022-08-09 23:48:33 +05:00
});
} else if (type === "rep") {
const usersLeaderboard = [],
usersData = await client.usersData.find({ rep: { $gt: 0 } }).lean();
2023-11-05 16:03:23 +05:00
await client.functions.asyncForEach(usersData, async user => {
usersLeaderboard.push({
id: user.id,
rep: user.rep,
});
});
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 = "",
rep = "";
2022-08-09 23:48:33 +05:00
for (let i = 0; i < usersLeaderboard.length; i++) {
const data = usersLeaderboard[i];
userNames += `**${i + 1}**. <@${data.id}>\n`;
2022-08-09 23:48:33 +05:00
rep += `${data.rep}\n`;
}
embed.fields = [
{
name: interaction.translate("common:USER"),
value: userNames,
inline: true,
},
{
name: interaction.translate("common:REP"),
value: rep,
inline: true,
},
];
2022-08-09 23:48:33 +05:00
interaction.editReply({
embeds: [embed],
2022-08-09 23:48:33 +05:00
});
}
}
}
2023-07-05 00:58:06 +05:00
module.exports = Leaderboard;