Compare commits
3 Commits
42c67150aa
...
9aaed5d8f2
| Author | SHA1 | Date | |
|---|---|---|---|
| 9aaed5d8f2 | |||
| fe0a06bdfc | |||
| 1936684a8c |
@ -1,114 +1,42 @@
|
||||
// comandos/Community/meta.js
|
||||
const { ModalBuilder, TextInputBuilder, TextInputStyle, ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder, SlashCommandBuilder } = require('discord.js');
|
||||
const conn = require('../../../database/db'); // Ajusta o caminho
|
||||
|
||||
module.exports = {
|
||||
// src/commands/community/meta.js
|
||||
const {
|
||||
SlashCommandBuilder,
|
||||
ModalBuilder,
|
||||
TextInputBuilder,
|
||||
TextInputStyle,
|
||||
ActionRowBuilder,
|
||||
} = require('discord.js');
|
||||
|
||||
module.exports = {
|
||||
data: new SlashCommandBuilder()
|
||||
.setName('meta')
|
||||
.setDescription('Exibe as informações do membro que enviou o comando.'),
|
||||
|
||||
.setName('meta')
|
||||
.setDescription('Verifica ou atualiza as tuas metas na base de dados.'),
|
||||
|
||||
async execute(interaction) {
|
||||
await interaction.deferReply({ ephemeral: true });
|
||||
|
||||
try {
|
||||
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',
|
||||
[interaction.user.id]
|
||||
);
|
||||
|
||||
if (!rows.length) {
|
||||
return await interaction.editReply('Nenhum membro encontrado.');
|
||||
}
|
||||
|
||||
const embed = new EmbedBuilder().setColor("#00b0f4");
|
||||
const row = rows[0]; // Já que é apenas um usuário, podemos pegar o primeiro da lista
|
||||
let metas = {};
|
||||
|
||||
try {
|
||||
if (row.meta) {
|
||||
metas = JSON.parse(row.meta);
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('JSON mal formatado para usuário:', row.discord_username);
|
||||
}
|
||||
|
||||
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!' });
|
||||
}
|
||||
const modal = new ModalBuilder()
|
||||
.setCustomId('definirMeta')
|
||||
.setTitle('Definir metas semanais');
|
||||
|
||||
const carvaoInput = new TextInputBuilder()
|
||||
.setCustomId('carvao')
|
||||
.setLabel('Meta de carvão')
|
||||
.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);
|
||||
|
||||
await interaction.showModal(modal);
|
||||
},
|
||||
|
||||
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');
|
||||
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