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