From 2e5908a312db9afec0d4086d7ea2170daea68f5b Mon Sep 17 00:00:00 2001 From: vb2007 Date: Sun, 28 Jul 2024 19:11:54 +0200 Subject: [PATCH] made the /leaderboard command work --- commands/economy/leaderboard.js | 44 ++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/commands/economy/leaderboard.js b/commands/economy/leaderboard.js index 7d0d53b..20bb4dd 100644 --- a/commands/economy/leaderboard.js +++ b/commands/economy/leaderboard.js @@ -9,32 +9,42 @@ module.exports = { .addIntegerOption(option => option .setName("amount") - .setDescription("The amount of top users to display.") + .setDescription("The amount of top users to display (MAX 100).") .setRequired(false) ) .setDMPermission(false), async execute(interaction) { + var amount = interaction.options.getInteger("amount") || 10; + if(!interaction.inGuild()) { var replyContent = "You can only check a member's balance in a server."; } + else if (amount > 100){ + var replyContent = "Cannot display more than 100 users.\nPlease try again with a smaller amount."; + } else { - - var embedReply = new EmbedBuilder({ - color: 0x5F0FD6, - title: "Server leaderboard.", - description: replyContent, - timestamp: new Date().toISOString(), - footer: { - text: `Requested by: ${interaction.user.username}`, - icon_url: interaction.user.displayAvatarURL({ dynamic: true }) - } - }); - - await interaction.reply({ embeds: [embedReply] }); + var query = await db.query("SELECT userName, balance FROM economy ORDER BY balance DESC LIMIT ?", [amount]); - //logging - const response = JSON.stringify(embedReply.toJSON()); - await logToFileAndDatabase(interaction, response); + var replyContent = query.map((user, index) => + `${index + 1}. ${user.userName} : ${user.balance}$ :moneybag:` + ).join("\n"); } + + var embedReply = new EmbedBuilder({ + color: 0x5F0FD6, + title: `Server leaderboard: TOP ${amount} users.`, + description: replyContent, + timestamp: new Date().toISOString(), + footer: { + text: `Requested by: ${interaction.user.username}`, + icon_url: interaction.user.displayAvatarURL({ dynamic: true }) + } + }); + + await interaction.reply({ embeds: [embedReply] }); + + //logging + const response = JSON.stringify(embedReply.toJSON()); + await logToFileAndDatabase(interaction, response); } } \ No newline at end of file