3
This commit is contained in:
parent
0483cd9991
commit
232b8e3dc9
86
src/commands/Community/portista.js
Normal file
86
src/commands/Community/portista.js
Normal file
@ -0,0 +1,86 @@
|
||||
const { SlashCommandBuilder } = require('discord.js');
|
||||
const conn = require('../../../database/db');
|
||||
console.error("--------------------- spacer ---------------------");
|
||||
async function getChoicesFromDatabase() {
|
||||
const query = `SELECT name, value FROM products`;
|
||||
let rows;
|
||||
|
||||
try {
|
||||
// Assuming conn is an object with an async method like `query` or `execute`
|
||||
rows = await conn.execute(query); // Using await for async operation
|
||||
} catch (err) {
|
||||
console.error('Erro ao consultar o banco de dados:', err);
|
||||
return [];
|
||||
}
|
||||
|
||||
if (!Array.isArray(rows)) {
|
||||
console.error("rows is not an array:", rows);
|
||||
return []; // Return empty array if data format is incorrect
|
||||
}
|
||||
|
||||
const choices = rows
|
||||
.filter(row => row && row.name && row.value) // Filtra valores inválidos
|
||||
.map(row => {
|
||||
const name = String(row.name).slice(0, 100);
|
||||
const value = String(row.value).slice(0, 100);
|
||||
|
||||
// Filtra qualquer valor que contenha descrições como "VARCHAR(11)" ou "NOT NULL"
|
||||
if (name.match(/VARCHAR\(\d+\)/) || value.match(/VARCHAR\(\d+\)/)) {
|
||||
console.warn('⚠️ Valor inválido detectado:', row);
|
||||
return null; // Ignora esse item
|
||||
}
|
||||
|
||||
return { name, value };
|
||||
})
|
||||
.filter(choice => choice !== null); // Remove os itens filtrados
|
||||
|
||||
console.log(choices);
|
||||
return choices;
|
||||
}
|
||||
|
||||
|
||||
async function createAddCommand() {
|
||||
// Obtém as opções do banco de dados de forma assíncrona
|
||||
const choices = await getChoicesFromDatabase();
|
||||
|
||||
// Configura o comando /add com as opções carregadas
|
||||
const data = new SlashCommandBuilder()
|
||||
.setName('add')
|
||||
.setDescription('Adiciona o servidor à base de dados.')
|
||||
.addStringOption(option =>
|
||||
option
|
||||
.setName('tipo_sanguinio1')
|
||||
.setDescription('Escolha uma opção do banco de dados')
|
||||
.setRequired(true)
|
||||
.addChoices(...choices) // Passa as opções carregadas para o comando
|
||||
);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
data: createAddCommand(),
|
||||
|
||||
async execute(interaction, client) {
|
||||
const guildId = interaction.guild.id;
|
||||
const guildownerId = interaction.guild.ownerId;
|
||||
|
||||
await interaction.deferReply();
|
||||
|
||||
try {
|
||||
const checkQuery = `SELECT * FROM Guilds WHERE guildId = ?`;
|
||||
const [checkRows] = await conn.execute(checkQuery, [guildId]);
|
||||
|
||||
if (checkRows.length > 0) {
|
||||
await interaction.editReply('❌ Este guildId já está registrado.');
|
||||
} else {
|
||||
const insertQuery = `INSERT INTO Guilds (guildId, guildownerId) VALUES (?, ?)`;
|
||||
await conn.execute(insertQuery, [guildId, guildownerId]);
|
||||
await interaction.editReply('✅ GuildId registrado com sucesso!');
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
await interaction.editReply('❌ Ocorreu um erro inesperado.');
|
||||
}
|
||||
}
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user