This commit is contained in:
parent
e444007de0
commit
6de4d17726
46
src/commands/Community/meta.js
Normal file
46
src/commands/Community/meta.js
Normal file
@ -0,0 +1,46 @@
|
||||
const { SlashCommandBuilder, ButtonBuilder, ButtonStyle, ActionRowBuilder, EmbedBuilder } = require('discord.js');
|
||||
const conn = require('../database/db');
|
||||
|
||||
module.exports = {
|
||||
data: new SlashCommandBuilder()
|
||||
.setName('metas')
|
||||
.setDescription('Mostra metas dos usuários'),
|
||||
async execute(interaction) {
|
||||
const [rows] = await conn.execute(`SELECT u.id, u.discord_id, u.discord_username, u.nig, u.meta, c.name AS nomedocargo FROM users uJOIN choices c ON u.cargo = c.value ORDER BY c.id DESC`);
|
||||
|
||||
const embed = new EmbedBuilder().setColor("#00b0f4");
|
||||
|
||||
rows.forEach(row => {
|
||||
let metas = {};
|
||||
try {
|
||||
metas = JSON.parse(row.meta);
|
||||
} catch {
|
||||
console.warn('Meta mal formatada:', row.discord_username);
|
||||
}
|
||||
|
||||
let metasText = '';
|
||||
for (const [key, data] of Object.entries(metas)) {
|
||||
if (typeof data === 'object' && 'alvo' in data && 'atual' in data) {
|
||||
metasText += `* *${key}:* \`${data.atual} / ${data.alvo}\`\n`;
|
||||
} else {
|
||||
metasText += `* *${key}:* \`formato inválido\`\n`;
|
||||
}
|
||||
}
|
||||
|
||||
embed.addFields({
|
||||
name: '',
|
||||
value: `\n\n> **${row.nig}** \n > <@${row.discord_id}>\n **Cargo:** \`${row.nomedocargo}\` \n**Meta Semanal:** \n${metasText}\n\n`,
|
||||
inline: true
|
||||
});
|
||||
});
|
||||
|
||||
const botao = new ButtonBuilder()
|
||||
.setCustomId('editar_metas')
|
||||
.setLabel('Editar Minhas Metas')
|
||||
.setStyle(ButtonStyle.Primary);
|
||||
|
||||
const row = new ActionRowBuilder().addComponents(botao);
|
||||
|
||||
await interaction.reply({ embeds: [embed], components: [row], ephemeral: true });
|
||||
}
|
||||
};
|
||||
62
src/events/handlers/meta.js
Normal file
62
src/events/handlers/meta.js
Normal file
@ -0,0 +1,62 @@
|
||||
const { ModalBuilder, TextInputBuilder, TextInputStyle, ActionRowBuilder } = require('discord.js');
|
||||
const conn = require('../database/db');
|
||||
|
||||
module.exports = (client) => {
|
||||
client.on('interactionCreate', async interaction => {
|
||||
if (interaction.isChatInputCommand()) {
|
||||
const command = client.commands.get(interaction.commandName);
|
||||
if (command) await command.execute(interaction);
|
||||
}
|
||||
|
||||
// Botão
|
||||
if (interaction.isButton() && interaction.customId === 'editar_metas') {
|
||||
const modal = new ModalBuilder()
|
||||
.setCustomId('modal_editar_metas')
|
||||
.setTitle('Editar Metas');
|
||||
|
||||
const carvao = new TextInputBuilder()
|
||||
.setCustomId('carvao')
|
||||
.setLabel('Meta de carvão')
|
||||
.setStyle(TextInputStyle.Short)
|
||||
.setRequired(false);
|
||||
|
||||
const enxofre = new TextInputBuilder()
|
||||
.setCustomId('enxofre')
|
||||
.setLabel('Meta de enxofre')
|
||||
.setStyle(TextInputStyle.Short)
|
||||
.setRequired(false);
|
||||
|
||||
const row1 = new ActionRowBuilder().addComponents(carvao);
|
||||
const row2 = new ActionRowBuilder().addComponents(enxofre);
|
||||
|
||||
modal.addComponents(row1, row2);
|
||||
await interaction.showModal(modal);
|
||||
}
|
||||
|
||||
// Modal
|
||||
if (interaction.isModalSubmit() && interaction.customId === 'modal_editar_metas') {
|
||||
const novoCarvao = interaction.fields.getTextInputValue('carvao');
|
||||
const novoEnxofre = interaction.fields.getTextInputValue('enxofre');
|
||||
|
||||
const [result] = await conn.execute('SELECT meta FROM users WHERE discord_id = ?', [interaction.user.id]);
|
||||
|
||||
let metas = {};
|
||||
try {
|
||||
metas = JSON.parse(result[0]?.meta || '{}');
|
||||
} catch {}
|
||||
|
||||
if (!metas.carvao) metas.carvao = { atual: 0, alvo: 0 };
|
||||
if (!metas.enxofre) metas.enxofre = { atual: 0, alvo: 0 };
|
||||
|
||||
if (!isNaN(parseInt(novoCarvao))) metas.carvao.alvo = parseInt(novoCarvao);
|
||||
if (!isNaN(parseInt(novoEnxofre))) metas.enxofre.alvo = parseInt(novoEnxofre);
|
||||
|
||||
await conn.execute('UPDATE users SET meta = ? WHERE discord_id = ?', [
|
||||
JSON.stringify(metas),
|
||||
interaction.user.id
|
||||
]);
|
||||
|
||||
await interaction.reply({ content: '✅ Metas atualizadas com sucesso!', ephemeral: true });
|
||||
}
|
||||
});
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user