From dc3a38526adea6b815e91ed37323026a9bc90504 Mon Sep 17 00:00:00 2001 From: arthur Date: Tue, 28 May 2024 19:24:37 +0200 Subject: [PATCH] corrige bug et debus refonte top --- commands/casino/top.js | 130 ++++++++++-------------------------- events/interactionCreate.js | 1 + events/messageCreateLVL.js | 4 +- 3 files changed, 39 insertions(+), 96 deletions(-) diff --git a/commands/casino/top.js b/commands/casino/top.js index 5837dd1..d865afb 100644 --- a/commands/casino/top.js +++ b/commands/casino/top.js @@ -6,105 +6,47 @@ module.exports = { aliases: ['leaderboard', 'lb'], description: 'Affiche le top 10 des membres du serveur', emote: '🏆', - utilisation: '[pocket|bank|reputation]', + utilisation: '[coins|pocket|bank|reputation|niveau]', permission: 0, async execute(message, args, client) { - let type = 'bank'; - let emote = ':bank:'; - if (args[0] ==='pocket') { - type = args[0] - emote = ':moneybag:'; - } else if (args[0] === 'reputation') { - type = args[0] - emote = ':star2:'; - } else if (args[0] && args[0] !== 'bank') { - const embed = new EmbedBuilder() - .setTitle('Erreur') - .setDescription('❌ Veuillez spĂ©cifier un type de leaderboard valide: `pocket`, `bank` ou `reputation`') - .setColor(await embedColor(message.author.id, message.guild.id)) - .setTimestamp() - .setFooter({ text: `DemandĂ© par ${message.author.tag}`, iconURL: message.author.displayAvatarURL() }); - - return message.reply({ embeds: [embed], allowedMentions: { repliedUser: false } }) - } - - const [embed, row] = await leaderboard(message, type); - const replyMessage = await message.reply({ embeds: [embed], components: [row], allowedMentions: { repliedUser: false } }); - - client.on('interactionCreate', async (interaction) => { - if (!interaction.isStringSelectMenu()) return; - - if (interaction.customId === 'topLeaderboard') { - const selected = interaction.values[0]; - - await interaction.deferUpdate(); - - const [embed, row] = await leaderboard(message, selected); - replyMessage.edit({ embeds: [embed], components: [row], allowedMentions: { repliedUser: false } }); - interaction.followUp({ content: `Leaderboard mis Ă  jour en fonction de \`${selected}\``, ephemeral: true, allowedMentions: { repliedUser: false } }); - } - }); - }, -}; - - -async function leaderboard(message, type) { - const topUsers = await new Promise((resolve, reject) => { - db.all(`SELECT * FROM users WHERE guildId = ? ORDER BY ${type} DESC LIMIT 10`, [message.guild.id], (err, rows) => { - if (err) reject(err); - resolve(rows); - }); - }); - - let emote = ':bank:'; - if (type === 'pocket') { - emote = ':moneybag:'; - } else if (type === 'reputation') { - emote = ':star2:'; - } - - const description = topUsers.map((user, index) => { - let place; - if (index === 0) { - place = ':first_place:'; - } else if (index === 1) { - place = ':second_place:'; - } else if (index === 2) { - place = ':third_place:'; + if (args[0] === 'coins' || args[0] === 'coin' || args[0] === 'global' || args[0] === 'money') { + args[0] = 'coins' + } else if (args[0] === 'pocket' || args[0] === 'cash' || args[0] === 'wallet' || args[0] === 'porte-monnaie' || args[0] === 'poche') { + args[0] = 'pocket' + } else if (args[0] === 'bank' || args[0] === 'banque' || args[0] === 'coffre' || args[0] === 'coffre-fort' || args[0] === 'bk') { + args[0] = 'bank' + } else if (args[0] === 'reputation' || args[0] === 'rep' || args[0] === 'rĂ©putation' || args[0] === 'reput') { + args[0] = 'reputation' + } else if (args[0] === 'niveau' || args[0] === 'lvl' || args[0] === 'level' || args[0] === 'lvl') { + args[0] = 'niveau' + } else if (!args[0]) { + args[0] = 'coins' } else { - place = `${index + 1}.`; + args[0] = 'coins' + message.reply({ embeds: [ + new EmbedBuilder() + .setTitle('Argument invalide') + .setDescription(`Vous pouvez utiliser les arguments suivants: \`coins\`, \`pocket\`, \`bank\`, \`reputation\`, \`niveau\` ou ne rien mettre pour afficher le top 10 des membres du serveur.`) + .setColor(await embedColor(message.author.id, message.guild.id)) + .setTimestamp() + .setFooter({ text: `DemandĂ© par ${message.author.tag}`, iconURL: message.author.displayAvatarURL() }) + ], allowedMentions: { repliedUser: false } }) } - return `${place} <@${user.userId}> - \`${user[type]}\` ${emote}`; - }); - - const embed = new EmbedBuilder() - .setAuthor({ name: `Top 10 des Membres de ${message.guild.name} en ${type}`, iconURL: message.guild.iconURL() }) - .setDescription(description.join('\n\n')) - .setColor(await embedColor(message.author.id, message.guild.id)) - .setTimestamp() - .setFooter({ text: `DemandĂ© par ${message.author.tag}`, iconURL: message.author.displayAvatarURL() }); - - const select = new StringSelectMenuBuilder() - .setCustomId('topLeaderboard') - .setPlaceholder('Choisissez un type de leaderboard') - .addOptions( - new StringSelectMenuOptionBuilder() - .setLabel('Pocket') - .setDescription('Affiche le top en fonction de leur argent de poche') - .setValue('pocket'), - new StringSelectMenuOptionBuilder() - .setLabel('Bank') - .setDescription('Affiche le top en fonction de leur argent en banque') - .setValue('bank'), - new StringSelectMenuOptionBuilder() - .setLabel('Reputation') - .setDescription('Affiche le top en fonction de leur rĂ©putation') - .setValue('reputation') - ); - const row = new ActionRowBuilder() - .addComponents(select); + let data; + if (args[0] === 'coins') { + data = db.all(`SELECT userId, pocket+bank AS coins FROM users WHERE guildId = ? ORDER BY coins DESC LIMIT 10`, [message.guild.id]); + } else if (args[0] === 'pocket') { + data = db.all(message.guild.id, 'SELECT userId, pocket AS coins FROM users WHERE guildId = ? ORDER BY coins DESC LIMIT 10', [message.guild.id]); + } else if (args[0] === 'bank') { + data = db.all(message.guild.id, 'SELECT userId, bank AS coins FROM users WHERE guildId = ? ORDER BY coins DESC LIMIT 10', [message.guild.id]); + } else if (args[0] === 'reputation') { + data = db.all(message.guild.id, 'SELECT userId, reputation AS coins FROM users WHERE guildId = ? ORDER BY coins DESC LIMIT 10', [message.guild.id]); + } else if (args[0] === 'niveau') { + data = db.all(message.guild.id, 'SELECT userId, lvl AS coins FROM users WHERE guildId = ? ORDER BY coins DESC LIMIT 10', [message.guild.id]); + } - return [embed, row]; + console.log(data) + }, } \ No newline at end of file diff --git a/events/interactionCreate.js b/events/interactionCreate.js index a6f438f..5b08b20 100644 --- a/events/interactionCreate.js +++ b/events/interactionCreate.js @@ -1,5 +1,6 @@ const { Events } = require('discord.js'); const db = require('../fonctions/database.js'); +const embedColor = require('../fonctions/embedColor.js'); module.exports = { name: Events.InteractionCreate, diff --git a/events/messageCreateLVL.js b/events/messageCreateLVL.js index 42f7ec2..cb7452d 100644 --- a/events/messageCreateLVL.js +++ b/events/messageCreateLVL.js @@ -7,10 +7,10 @@ module.exports = { if (message.author.bot) return; if (message.content.length < 50) { - db.run(`UPDATE users SET xp = xp + 1, WHERE guildId = ? AND userId = ?`, [message.guild.id, message.author.id]); + db.run(`UPDATE users SET xp = xp + 1 WHERE guildId = ? AND userId = ?`, [message.guild.id, message.author.id]); } if (message.content.length >= 50) { - db.run(`UPDATE users SET xp = xp + 3, WHERE guildId = ? AND userId = ?`, [message.guild.id, message.author.id]); + db.run(`UPDATE users SET xp = xp + 3 WHERE guildId = ? AND userId = ?`, [message.guild.id, message.author.id]); } const user = await new Promise((resolve, reject) => {