This commit is contained in:
parent
1936684a8c
commit
fe0a06bdfc
@ -1,42 +1,57 @@
|
||||
// events/interactionCreate.js
|
||||
const conn = require('../../../database/db');
|
||||
const { InteractionContextType, PermissionFlagsBits, SlashCommandBuilder, MessageFlags } = require('discord.js');
|
||||
const conn = require('../../../database/db'); // Ajusta o caminho
|
||||
|
||||
|
||||
module.exports = {
|
||||
name: 'interactionCreate',
|
||||
async execute(interaction, client) {
|
||||
// Deixa os comandos slash serem tratados normalmente
|
||||
if (interaction.isCommand()) return;
|
||||
name: 'interactionCreate',
|
||||
async execute(interaction, client) {
|
||||
|
||||
// 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 userId = interaction.user.id;
|
||||
|
||||
const metas = {
|
||||
carvao: Number(carvao),
|
||||
enxofre: Number(enxofre),
|
||||
};
|
||||
if (interaction.isAutocomplete()) {
|
||||
const focusedValue = interaction.options.getFocused(true);
|
||||
|
||||
if (interaction.commandName === 'addmembro') {
|
||||
console.error(focusedValue);
|
||||
if (focusedValue.name == 'cargo'){
|
||||
|
||||
try {
|
||||
const [rows] = await conn.execute(
|
||||
'SELECT name,value FROM choices LIMIT 25',
|
||||
[`%${focusedValue}%`]
|
||||
);
|
||||
|
||||
try {
|
||||
await conn.query(
|
||||
'UPDATE users SET meta = ? WHERE discord_id = ?',
|
||||
[JSON.stringify(metas), userId]
|
||||
);
|
||||
const suggestions = rows.map(row => ({
|
||||
name: row.name,
|
||||
value: row.value,
|
||||
}));
|
||||
|
||||
await interaction.reply({
|
||||
content: '✅ Metas atualizadas com sucesso!',
|
||||
ephemeral: true,
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Erro ao atualizar metas:', err);
|
||||
await interaction.reply({
|
||||
content: '❌ Ocorreu um erro ao salvar as metas.',
|
||||
ephemeral: true,
|
||||
});
|
||||
await interaction.respond(suggestions);
|
||||
} catch (err) {
|
||||
console.error('Erro ao buscar sugestões:', err);
|
||||
await interaction.respond([]);
|
||||
}
|
||||
}
|
||||
if (focusedValue.name == 'raça'){
|
||||
try {
|
||||
const [rows] = await conn.execute(
|
||||
'SELECT name,value FROM choices LIMIT 25',
|
||||
[`%${focusedValue}%`]
|
||||
);
|
||||
|
||||
const suggestions = rows.map(row => ({
|
||||
name: row.name,
|
||||
value: row.value,
|
||||
}));
|
||||
|
||||
await interaction.respond(suggestions);
|
||||
} catch (err) {
|
||||
console.error('Erro ao buscar sugestões:', err);
|
||||
await interaction.respond([]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
@ -1,62 +1,42 @@
|
||||
const { ModalBuilder, TextInputBuilder, TextInputStyle, ActionRowBuilder } = require('discord.js');
|
||||
const conn = require('../../../database/db'); // Ajusta o caminho
|
||||
// events/interactionCreate.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);
|
||||
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 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]
|
||||
);
|
||||
|
||||
await interaction.reply({
|
||||
content: '✅ Metas atualizadas com sucesso!',
|
||||
ephemeral: true,
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Erro ao atualizar metas:', err);
|
||||
await interaction.reply({
|
||||
content: '❌ Ocorreu um erro ao salvar as metas.',
|
||||
ephemeral: true,
|
||||
});
|
||||
}
|
||||
|
||||
// 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