diff --git a/src/commands/Community/meta.js b/src/commands/Community/meta.js index 8c2cc7f..4449f2a 100644 --- a/src/commands/Community/meta.js +++ b/src/commands/Community/meta.js @@ -1,9 +1,10 @@ +// src/commands/community/meta.js const { SlashCommandBuilder, ActionRowBuilder, StringSelectMenuBuilder, - ButtonBuilder, // Adicionando o ButtonBuilder para o botão - EmbedBuilder, // Para o embed de exibição de usuários + ButtonBuilder, + EmbedBuilder, } = require('discord.js'); const conn = require('../../../database/db'); @@ -12,12 +13,9 @@ const { .setName('meta') .setDescription('Escolhe um recurso para editar a meta.'), - - async execute(interaction) { - await interaction.deferReply({ ephemeral: true }); - - // Parte adicional que recupera e exibe informações de usuários e suas metas + await interaction.deferReply({ ephemeral: true }); + try { const [usersRows] = await conn.execute('SELECT u.id, u.discord_id, u.discord_username, u.nig, u.meta, c.name AS nomedocargo FROM users u JOIN choices c ON u.cargo = c.value ORDER BY c.id DESC'); const embed = new EmbedBuilder().setColor("#00b0f4"); @@ -56,77 +54,75 @@ const { console.error('Erro ao buscar usuários:', error); await interaction.editReply({ content: '❌ Erro ao carregar informações dos usuários.' }); } - - - - try { - const userId = interaction.user.id; - const [rows] = await conn.query('SELECT meta FROM users WHERE discord_id = ?', [userId]); - if (!rows.length || !rows[0].meta) { - return interaction.editReply({ content: '❌ Nenhuma meta encontrada.', ephemeral: true }); - } - - let metas = {}; try { - metas = JSON.parse(rows[0].meta); - } catch (err) { - console.error('Erro ao analisar JSON de metas:', err); - return interaction.editReply({ content: '❌ Erro ao ler as metas do utilizador.', ephemeral: true }); - } + const userId = interaction.user.id; + const [rows] = await conn.query('SELECT meta FROM users WHERE discord_id = ?', [userId]); - const chaves = Object.keys(metas); - if (chaves.length === 0) { - return interaction.editReply({ content: '❌ Nenhuma meta definida ainda.', ephemeral: true }); - } - - const options = chaves.map(key => ({ - label: key, - value: key, - })); - - const select = new StringSelectMenuBuilder() - .setCustomId('selecionarRecurso') - .setPlaceholder('Escolhe o recurso...') - .addOptions(options); - - const rowSelect = new ActionRowBuilder().addComponents(select); - const rowButton = new ActionRowBuilder().addComponents( - new ButtonBuilder() - .setCustomId('mostrarDropdown') - .setLabel('Escolher Recurso') - .setStyle(1) // Usando o valor numérico para PRIMARY (1) - ); - - await interaction.editReply({ - content: '🛠️ Clica no botão para escolher o recurso:', - components: [rowButton], - ephemeral: true, - }); - - // Espera pelo clique no botão - const filter = (i) => i.customId === 'mostrarDropdown' && i.user.id === interaction.user.id; - const collector = interaction.channel.createMessageComponentCollector({ filter, time: 15000 }); - - collector.on('collect', async (i) => { - if (i.customId === 'mostrarDropdown') { - await i.update({ - content: '🛠️ Agora escolhe o recurso:', - components: [rowSelect], // Exibe o menu de seleção após clicar no botão - }); + if (!rows.length || !rows[0].meta) { + return interaction.editReply({ content: '❌ Nenhuma meta encontrada.', ephemeral: true }); } - }); - collector.on('end', (collected) => { - if (collected.size === 0) { - interaction.editReply({ content: '❌ O tempo para escolher o recurso expirou.' }); + let metas = {}; + try { + metas = JSON.parse(rows[0].meta); + } catch (err) { + console.error('Erro ao analisar JSON de metas:', err); + return interaction.editReply({ content: '❌ Erro ao ler as metas do utilizador.', ephemeral: true }); } - }); + + const chaves = Object.keys(metas); + if (chaves.length === 0) { + return interaction.editReply({ content: '❌ Nenhuma meta definida ainda.', ephemeral: true }); + } + + const options = chaves.map(key => ({ + label: key, + value: key, + })); + + const select = new StringSelectMenuBuilder() + .setCustomId('selecionarRecurso') + .setPlaceholder('Escolhe o recurso...') + .addOptions(options); + + const rowSelect = new ActionRowBuilder().addComponents(select); + const rowButton = new ActionRowBuilder().addComponents( + new ButtonBuilder() + .setCustomId('mostrarDropdown') + .setLabel('Escolher Recurso') + .setStyle(1) + ); + + await interaction.editReply({ + content: '🛠️ Clica no botão para escolher o recurso:', + components: [rowButton], + ephemeral: true, + }); + + // Espera pelo clique no botão + const filter = (i) => i.customId === 'mostrarDropdown' && i.user.id === interaction.user.id; + const collector = interaction.channel.createMessageComponentCollector({ filter, time: 15000 }); + + collector.on('collect', async (i) => { + if (i.customId === 'mostrarDropdown') { + await i.update({ + content: '🛠️ Agora escolhe o recurso:', + components: [rowSelect], + }); + } + }); + + collector.on('end', (collected) => { + if (collected.size === 0) { + interaction.editReply({ content: '❌ O tempo para escolher o recurso expirou.' }); + } + }); + } catch (error) { console.error('Erro ao buscar usuários:', error); await interaction.editReply({ content: '❌ Erro ao carregar informações dos usuários.' }); } - }, }; \ No newline at end of file diff --git a/src/events/handlers/meta.js b/src/events/handlers/meta.js index 5ddaa63..960727a 100644 --- a/src/events/handlers/meta.js +++ b/src/events/handlers/meta.js @@ -111,9 +111,9 @@ const { } const atual = metas[recurso]?.atual ?? 0; - + const novoValor = atual + novoAlvo; // Somar ao valor atual - + metas[recurso] = { alvo: novoAlvo, atual: novoValor, // Não alterar o valor atual ao atualizar o alvo