Compare commits
No commits in common. "9aaed5d8f224717f34859a02ed3d285099970d9c" and "42c67150aaa788f1d2c0d37a4ede6f7df7e4bdd6" have entirely different histories.
9aaed5d8f2
...
42c67150aa
@ -1,42 +1,114 @@
|
|||||||
// src/commands/community/meta.js
|
// comandos/Community/meta.js
|
||||||
const {
|
const { ModalBuilder, TextInputBuilder, TextInputStyle, ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder, SlashCommandBuilder } = require('discord.js');
|
||||||
SlashCommandBuilder,
|
const conn = require('../../../database/db'); // Ajusta o caminho
|
||||||
ModalBuilder,
|
|
||||||
TextInputBuilder,
|
module.exports = {
|
||||||
TextInputStyle,
|
|
||||||
ActionRowBuilder,
|
|
||||||
} = require('discord.js');
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
.setName('meta')
|
.setName('meta')
|
||||||
.setDescription('Verifica ou atualiza as tuas metas na base de dados.'),
|
.setDescription('Exibe as informações do membro que enviou o comando.'),
|
||||||
|
|
||||||
async execute(interaction) {
|
async execute(interaction) {
|
||||||
const modal = new ModalBuilder()
|
await interaction.deferReply({ ephemeral: true });
|
||||||
.setCustomId('definirMeta')
|
|
||||||
.setTitle('Definir metas semanais');
|
try {
|
||||||
|
const [rows] = await conn.execute(
|
||||||
const carvaoInput = new TextInputBuilder()
|
'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',
|
||||||
.setCustomId('carvao')
|
[interaction.user.id]
|
||||||
.setLabel('Meta de carvão')
|
);
|
||||||
.setStyle(TextInputStyle.Short)
|
|
||||||
.setPlaceholder('Ex: 1000')
|
if (!rows.length) {
|
||||||
.setRequired(true);
|
return await interaction.editReply('Nenhum membro encontrado.');
|
||||||
|
}
|
||||||
const enxofreInput = new TextInputBuilder()
|
|
||||||
.setCustomId('enxofre')
|
const embed = new EmbedBuilder().setColor("#00b0f4");
|
||||||
.setLabel('Meta de enxofre')
|
const row = rows[0]; // Já que é apenas um usuário, podemos pegar o primeiro da lista
|
||||||
.setStyle(TextInputStyle.Short)
|
let metas = {};
|
||||||
.setPlaceholder('Ex: 800')
|
|
||||||
.setRequired(true);
|
try {
|
||||||
|
if (row.meta) {
|
||||||
const firstRow = new ActionRowBuilder().addComponents(carvaoInput);
|
metas = JSON.parse(row.meta);
|
||||||
const secondRow = new ActionRowBuilder().addComponents(enxofreInput);
|
}
|
||||||
|
} catch (err) {
|
||||||
modal.addComponents(firstRow, secondRow);
|
console.warn('JSON mal formatado para usuário:', row.discord_username);
|
||||||
|
}
|
||||||
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,42 +1,62 @@
|
|||||||
// events/interactionCreate.js
|
const { ModalBuilder, TextInputBuilder, TextInputStyle, ActionRowBuilder } = require('discord.js');
|
||||||
const conn = require('../../../database/db');
|
const conn = require('../../../database/db'); // Ajusta o caminho
|
||||||
|
|
||||||
module.exports = {
|
module.exports = (client) => {
|
||||||
name: 'interactionCreate',
|
client.on('interactionCreate', async interaction => {
|
||||||
async execute(interaction, client) {
|
if (interaction.isChatInputCommand()) {
|
||||||
// Deixa os comandos slash serem tratados normalmente
|
const command = client.commands.get(interaction.commandName);
|
||||||
if (interaction.isCommand()) return;
|
if (command) await command.execute(interaction);
|
||||||
|
|
||||||
// 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