63 lines
2.4 KiB
JavaScript
63 lines
2.4 KiB
JavaScript
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 });
|
|
}
|
|
});
|
|
};
|