JaBa/commands/Economy/leaderboard.js

174 lines
4.5 KiB
JavaScript
Raw Normal View History

const Command = require("../../base/Command"),
Discord = require("discord.js");
2022-01-04 02:18:28 +05:00
class Leaderboard extends Command {
constructor(client) {
super(client, {
name: "leaderboard",
dirname: __dirname,
enabled: true,
guildOnly: true,
aliases: ["lb"],
memberPermissions: [],
botPermissions: ["SEND_MESSAGES", "EMBED_LINKS"],
nsfw: false,
ownerOnly: false,
cooldown: 2000
});
}
async run(message, args, data) {
const isOnlyOnMobile = (message.member.presence.clientStatus ? JSON.stringify(Object.keys(message.member.presence.clientStatus)) === JSON.stringify(["mobile"]) : false);
2022-01-04 02:18:28 +05:00
const type = args[0];
2022-01-06 20:31:57 +05:00
if (!type || !["credits", "level", "rep"].includes(type)) return message.error("economy/leaderboard:MISSING_TYPE");
2022-01-04 02:18:28 +05:00
if (type === "credits") {
const members = await this.client.membersData.find({
guildID: message.guild.id
}).lean(),
membersLeaderboard = members.map((m) => {
return {
id: m.id,
money: m.money + m.bankSold
2022-01-04 02:18:28 +05:00
};
}).sort((a, b) => b.money - a.money);
2022-01-04 02:18:28 +05:00
if (membersLeaderboard.length > 20) membersLeaderboard.length = 20;
2022-01-06 20:31:57 +05:00
let userNames = "";
let money = "";
for (let i = 0; i < membersLeaderboard.length; i++) {
const data = membersLeaderboard[i];
const user = (await this.client.users.fetch(data.id)).tag;
userNames += `**${i + 1}**. ${user}\n`;
money += `${data.money}\n`;
}
const embed = new Discord.MessageEmbed()
.setAuthor({
name: message.translate("economy/leaderboard:TABLE", {
name: message.guild.name
}),
iconURL: message.guild.iconURL({
dynamic: true
})
})
.setColor(data.config.embed.color)
.addFields({
name: message.translate("economy/leaderboard:TOP"),
value: userNames,
inline: true
}, {
2022-01-13 20:01:39 +05:00
name: message.translate("economy/leaderboard:CREDITS"),
value: money,
inline: true
});
message.reply({
embeds: [embed]
});
2022-01-04 02:18:28 +05:00
} else if (type === "level") {
const members = await this.client.membersData.find({
guildID: message.guild.id
}).lean(),
membersLeaderboard = members.map((m) => {
return {
id: m.id,
level: m.level,
xp: m.exp
2022-01-04 02:18:28 +05:00
};
}).sort((a, b) => b.level - a.level);
2022-01-04 02:18:28 +05:00
if (membersLeaderboard.length > 20) membersLeaderboard.length = 20;
2022-01-06 20:31:57 +05:00
let userNames = "";
let level = "";
let xp = "";
for (let i = 0; i < membersLeaderboard.length; i++) {
const data = membersLeaderboard[i];
const user = (await this.client.users.fetch(data.id)).tag;
userNames += `**${i + 1}**. ${user}\n`;
level += `${data.level}\n`;
xp += `${data.xp} / ${5 * (data.level * data.level) + 80 * data.level + 100}\n`;
}
const embed = new Discord.MessageEmbed()
.setAuthor({
name: message.translate("economy/leaderboard:TABLE", {
name: message.guild.name
}),
iconURL: message.guild.iconURL({
dynamic: true
})
})
.setColor(data.config.embed.color)
.addFields({
name: message.translate("economy/leaderboard:TOP"),
value: userNames,
inline: true
}, {
name: message.translate("economy/leaderboard:LEVEL"),
value: level,
inline: true
}, {
name: message.translate("economy/leaderboard:XP"),
value: xp,
inline: true
});
message.reply({
embeds: [embed]
});
2022-01-04 02:18:28 +05:00
} else if (type === "rep") {
const users = await this.client.usersData.find({
2022-01-06 20:31:57 +05:00
rep: { $gt: 0 }
}).lean(),
2022-01-04 02:18:28 +05:00
usersLeaderboard = users.map((u) => {
return {
id: u.id,
rep: u.rep
2022-01-04 02:18:28 +05:00
};
}).sort((a, b) => b.rep - a.rep);
2022-01-04 02:18:28 +05:00
if (usersLeaderboard.length > 20) usersLeaderboard.length = 20;
2022-01-06 20:31:57 +05:00
let userNames = "";
let rep = "";
for (let i = 0; i < usersLeaderboard.length; i++) {
const data = usersLeaderboard[i];
const user = (await this.client.users.fetch(data.id)).tag;
userNames += `**${i + 1}**. ${user}\n`;
rep += `${data.rep}\n`;
}
const embed = new Discord.MessageEmbed()
.setAuthor({
name: message.translate("economy/leaderboard:TABLE", {
name: message.guild.name
}),
iconURL: message.guild.iconURL({
dynamic: true
})
})
.setColor(data.config.embed.color)
.addFields({
name: message.translate("economy/leaderboard:TOP"),
value: userNames,
inline: true
}, {
name: message.translate("economy/leaderboard:REP"),
value: rep,
inline: true
});
message.reply({
embeds: [embed]
});
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
if (isOnlyOnMobile) message.sendT("economy/leaderboard:MOBILE");
}
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
module.exports = Leaderboard;