Compare commits
3 Commits
42c67150aa
...
9aaed5d8f2
| Author | SHA1 | Date | |
|---|---|---|---|
| 9aaed5d8f2 | |||
| fe0a06bdfc | |||
| 1936684a8c |
@ -1,114 +1,42 @@
|
|||||||
// comandos/Community/meta.js
|
// src/commands/community/meta.js
|
||||||
const { ModalBuilder, TextInputBuilder, TextInputStyle, ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder, SlashCommandBuilder } = require('discord.js');
|
const {
|
||||||
const conn = require('../../../database/db'); // Ajusta o caminho
|
SlashCommandBuilder,
|
||||||
|
ModalBuilder,
|
||||||
module.exports = {
|
TextInputBuilder,
|
||||||
|
TextInputStyle,
|
||||||
|
ActionRowBuilder,
|
||||||
|
} = require('discord.js');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
.setName('meta')
|
.setName('meta')
|
||||||
.setDescription('Exibe as informações do membro que enviou o comando.'),
|
.setDescription('Verifica ou atualiza as tuas metas na base de dados.'),
|
||||||
|
|
||||||
async execute(interaction) {
|
async execute(interaction) {
|
||||||
await interaction.deferReply({ ephemeral: true });
|
const modal = new ModalBuilder()
|
||||||
|
.setCustomId('definirMeta')
|
||||||
try {
|
.setTitle('Definir metas semanais');
|
||||||
const [rows] = await conn.execute(
|
|
||||||
'SELECT u.id, u.discord_id, u.discord_username, u.nig, u.meta, c.name AS nomedocargo FROM users u JOIN choices c ON u.cargo = c.value WHERE u.discord_id = ? ORDER BY c.id DESC',
|
const carvaoInput = new TextInputBuilder()
|
||||||
[interaction.user.id]
|
.setCustomId('carvao')
|
||||||
);
|
.setLabel('Meta de carvão')
|
||||||
|
.setStyle(TextInputStyle.Short)
|
||||||
if (!rows.length) {
|
.setPlaceholder('Ex: 1000')
|
||||||
return await interaction.editReply('Nenhum membro encontrado.');
|
.setRequired(true);
|
||||||
}
|
|
||||||
|
const enxofreInput = new TextInputBuilder()
|
||||||
const embed = new EmbedBuilder().setColor("#00b0f4");
|
.setCustomId('enxofre')
|
||||||
const row = rows[0]; // Já que é apenas um usuário, podemos pegar o primeiro da lista
|
.setLabel('Meta de enxofre')
|
||||||
let metas = {};
|
.setStyle(TextInputStyle.Short)
|
||||||
|
.setPlaceholder('Ex: 800')
|
||||||
try {
|
.setRequired(true);
|
||||||
if (row.meta) {
|
|
||||||
metas = JSON.parse(row.meta);
|
const firstRow = new ActionRowBuilder().addComponents(carvaoInput);
|
||||||
}
|
const secondRow = new ActionRowBuilder().addComponents(enxofreInput);
|
||||||
} catch (err) {
|
|
||||||
console.warn('JSON mal formatado para usuário:', row.discord_username);
|
modal.addComponents(firstRow, secondRow);
|
||||||
}
|
|
||||||
|
await interaction.showModal(modal);
|
||||||
let metasText = '';
|
|
||||||
for (const [nomejson, dados] of Object.entries(metas)) {
|
|
||||||
if (typeof dados === 'object' && dados !== null && 'alvo' in dados && 'atual' in dados) {
|
|
||||||
metasText += `* *${nomejson}:* \`${dados.atual} / ${dados.alvo}\`\n`;
|
|
||||||
} else {
|
|
||||||
metasText += `* *${nomejson}:* \`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 button = new ActionRowBuilder().addComponents(
|
|
||||||
new ButtonBuilder()
|
|
||||||
.setCustomId('edit_meta')
|
|
||||||
.setLabel('Editar Meta')
|
|
||||||
.setStyle(ButtonStyle.Primary)
|
|
||||||
);
|
|
||||||
|
|
||||||
await interaction.editReply({ embeds: [embed], components: [button] });
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Erro ao acessar o banco de dados:', error);
|
|
||||||
await interaction.editReply({ content: 'Erro ao carregar os dados do membro!' });
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
};
|
||||||
async handleButtonClick(interaction) {
|
|
||||||
if (interaction.customId === 'edit_meta') {
|
|
||||||
const modal = new ModalBuilder()
|
|
||||||
.setCustomId('meta_modal')
|
|
||||||
.setTitle('Editar Meta');
|
|
||||||
|
|
||||||
modal.addComponents(
|
|
||||||
new ActionRowBuilder().addComponents(
|
|
||||||
new TextInputBuilder()
|
|
||||||
.setCustomId('new_atual')
|
|
||||||
.setLabel('Novo valor de Atual')
|
|
||||||
.setStyle(TextInputStyle.Short)
|
|
||||||
.setPlaceholder('Insira o novo valor...')
|
|
||||||
.setRequired(true)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
await interaction.showModal(modal);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
async handleModalSubmit(interaction) {
|
|
||||||
const newAtual = interaction.fields.getTextInputValue('new_atual');
|
|
||||||
|
|
||||||
try {
|
|
||||||
const [rows] = await conn.execute(
|
|
||||||
'SELECT u.id, u.meta FROM users u WHERE u.discord_id = ?',
|
|
||||||
[interaction.user.id]
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!rows.length) {
|
|
||||||
return await interaction.reply({ content: 'Usuário não encontrado', ephemeral: true });
|
|
||||||
}
|
|
||||||
|
|
||||||
let metas = JSON.parse(rows[0].meta);
|
|
||||||
|
|
||||||
metas['carvao'].atual = newAtual;
|
|
||||||
|
|
||||||
await conn.execute(
|
|
||||||
'UPDATE users SET meta = ? WHERE discord_id = ?',
|
|
||||||
[JSON.stringify(metas), interaction.user.id]
|
|
||||||
);
|
|
||||||
|
|
||||||
await interaction.reply({ content: 'Meta atualizada com sucesso!', ephemeral: true });
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Erro ao atualizar a meta no banco de dados:', error);
|
|
||||||
await interaction.reply({ content: 'Erro ao atualizar a meta!', ephemeral: true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@ -1,62 +1,42 @@
|
|||||||
const { ModalBuilder, TextInputBuilder, TextInputStyle, ActionRowBuilder } = require('discord.js');
|
// events/interactionCreate.js
|
||||||
const conn = require('../../../database/db'); // Ajusta o caminho
|
const conn = require('../../../database/db');
|
||||||
|
|
||||||
module.exports = (client) => {
|
module.exports = {
|
||||||
client.on('interactionCreate', async interaction => {
|
name: 'interactionCreate',
|
||||||
if (interaction.isChatInputCommand()) {
|
async execute(interaction, client) {
|
||||||
const command = client.commands.get(interaction.commandName);
|
// Deixa os comandos slash serem tratados normalmente
|
||||||
if (command) await command.execute(interaction);
|
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