This commit is contained in:
parent
9aaed5d8f2
commit
489a62b026
@ -10,31 +10,29 @@ const {
|
||||
module.exports = {
|
||||
data: new SlashCommandBuilder()
|
||||
.setName('meta')
|
||||
.setDescription('Verifica ou atualiza as tuas metas na base de dados.'),
|
||||
.setDescription('Atualiza a meta de um recurso específico.')
|
||||
.addStringOption(option =>
|
||||
option.setName('recurso')
|
||||
.setDescription('O nome do recurso (ex: enxofre, ferro)')
|
||||
.setRequired(true)
|
||||
),
|
||||
|
||||
async execute(interaction) {
|
||||
const modal = new ModalBuilder()
|
||||
.setCustomId('definirMeta')
|
||||
.setTitle('Definir metas semanais');
|
||||
const recurso = interaction.options.getString('recurso');
|
||||
|
||||
const carvaoInput = new TextInputBuilder()
|
||||
.setCustomId('carvao')
|
||||
.setLabel('Meta de carvão')
|
||||
const modal = new ModalBuilder()
|
||||
.setCustomId(`definirMeta-${recurso}`) // inclui o nome no ID
|
||||
.setTitle(`Definir meta para: ${recurso}`);
|
||||
|
||||
const alvoInput = new TextInputBuilder()
|
||||
.setCustomId('alvo')
|
||||
.setLabel(`Nova meta para ${recurso}`)
|
||||
.setStyle(TextInputStyle.Short)
|
||||
.setPlaceholder('Ex: 1000')
|
||||
.setRequired(true);
|
||||
|
||||
const enxofreInput = new TextInputBuilder()
|
||||
.setCustomId('enxofre')
|
||||
.setLabel('Meta de enxofre')
|
||||
.setStyle(TextInputStyle.Short)
|
||||
.setPlaceholder('Ex: 800')
|
||||
.setRequired(true);
|
||||
|
||||
const firstRow = new ActionRowBuilder().addComponents(carvaoInput);
|
||||
const secondRow = new ActionRowBuilder().addComponents(enxofreInput);
|
||||
|
||||
modal.addComponents(firstRow, secondRow);
|
||||
const row = new ActionRowBuilder().addComponents(alvoInput);
|
||||
modal.addComponents(row);
|
||||
|
||||
await interaction.showModal(modal);
|
||||
},
|
||||
|
||||
@ -4,35 +4,44 @@ const conn = require('../../../database/db');
|
||||
module.exports = {
|
||||
name: 'interactionCreate',
|
||||
async execute(interaction, client) {
|
||||
// Deixa os comandos slash serem tratados normalmente
|
||||
if (interaction.isCommand()) return;
|
||||
|
||||
// Trata submissões de modal
|
||||
if (interaction.isModalSubmit()) {
|
||||
if (interaction.customId === 'definirMeta') {
|
||||
const carvao = interaction.fields.getTextInputValue('carvao');
|
||||
const enxofre = interaction.fields.getTextInputValue('enxofre');
|
||||
const [prefix, recurso] = interaction.customId.split('-');
|
||||
|
||||
if (prefix === 'definirMeta' && recurso) {
|
||||
const novoAlvo = Number(interaction.fields.getTextInputValue('alvo'));
|
||||
const userId = interaction.user.id;
|
||||
|
||||
const metas = {
|
||||
carvao: Number(carvao),
|
||||
enxofre: Number(enxofre),
|
||||
};
|
||||
|
||||
try {
|
||||
await conn.query(
|
||||
'UPDATE users SET meta = ? WHERE discord_id = ?',
|
||||
[JSON.stringify(metas), userId]
|
||||
);
|
||||
const [rows] = await conn.query('SELECT meta FROM users WHERE discord_id = ?', [userId]);
|
||||
|
||||
let metas = {};
|
||||
|
||||
if (rows.length > 0 && rows[0].meta) {
|
||||
metas = JSON.parse(rows[0].meta);
|
||||
}
|
||||
|
||||
// Atualiza só o recurso selecionado
|
||||
const atual = metas[recurso]?.atual ?? 0;
|
||||
metas[recurso] = {
|
||||
alvo: novoAlvo,
|
||||
atual: atual
|
||||
};
|
||||
|
||||
await conn.query('UPDATE users SET meta = ? WHERE discord_id = ?', [
|
||||
JSON.stringify(metas),
|
||||
userId,
|
||||
]);
|
||||
|
||||
await interaction.reply({
|
||||
content: '✅ Metas atualizadas com sucesso!',
|
||||
content: `✅ Meta de **${recurso}** atualizada para **${novoAlvo}** (atual: ${atual})`,
|
||||
ephemeral: true,
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Erro ao atualizar metas:', err);
|
||||
await interaction.reply({
|
||||
content: '❌ Ocorreu um erro ao salvar as metas.',
|
||||
content: '❌ Erro ao salvar a meta.',
|
||||
ephemeral: true,
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user