This commit is contained in:
parent
a25ec95743
commit
20c67df574
@ -3,14 +3,15 @@ const conn = require('../../../database/db');
|
|||||||
|
|
||||||
console.error("--------------------- testeadd.js ---------------------");
|
console.error("--------------------- testeadd.js ---------------------");
|
||||||
|
|
||||||
// Define a função loadChoices antes de usá-la
|
|
||||||
|
// Função para carregar as escolhas do banco de dados
|
||||||
async function loadChoices() {
|
async function loadChoices() {
|
||||||
try {
|
try {
|
||||||
// Supondo que sua tabela de escolhas se chame "choices" e tenha colunas "name" e "value"
|
// Supondo que sua tabela se chame "choices" e possua as colunas "name" e "value"
|
||||||
const [rows] = await conn.execute('SELECT name, value FROM choices');
|
const [rows] = await conn.execute('SELECT name, value FROM choices');
|
||||||
return rows.map(row => ({
|
return rows.map(row => ({
|
||||||
name: row.name,
|
name: row.name,
|
||||||
value: row.value.toString() // converter para string se necessário
|
value: row.value.toString() // converte para string, se necessário
|
||||||
}));
|
}));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Erro ao carregar escolhas:", err);
|
console.error("Erro ao carregar escolhas:", err);
|
||||||
@ -18,47 +19,43 @@ async function loadChoices() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Carrega as escolhas antes de definir o comando
|
|
||||||
let dynamicChoices = [];
|
|
||||||
loadChoices()
|
|
||||||
.then(choices => {
|
|
||||||
dynamicChoices = choices;
|
|
||||||
console.log("Escolhas carregadas:", dynamicChoices);
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
console.error("Falha ao carregar escolhas:", err);
|
|
||||||
});
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
.setName('add')
|
.setName('add')
|
||||||
.setDescription('Adiciona o servidor à base de dados.')
|
.setDescription('Adiciona o servidor à base de dados.')
|
||||||
|
// Opção "nome" com autocomplete
|
||||||
.addStringOption(option =>
|
.addStringOption(option =>
|
||||||
option.setName('nome')
|
option.setName('nome')
|
||||||
.setDescription('Arisu - Escolhe uma opção')
|
.setDescription('Escolhe uma opção')
|
||||||
.setRequired(true)
|
.setRequired(true)
|
||||||
// Se as escolhas ainda não estiverem carregadas, defina um conjunto padrão para evitar erros.
|
.setAutocomplete(true)
|
||||||
|
|
||||||
.addChoices(...(dynamicChoices.length ? dynamicChoices : [
|
|
||||||
{ name: 'Opção Padrão 1', value: '0' },
|
|
||||||
{ name: 'Opção Padrão 2', value: '1' }
|
|
||||||
]))
|
|
||||||
)
|
)
|
||||||
|
// Opção "tipo_sanguinio1" com autocomplete
|
||||||
.addStringOption(option =>
|
.addStringOption(option =>
|
||||||
option.setName('tipo_sanguinio1')
|
option.setName('tipo_sanguinio1')
|
||||||
.setDescription('Escolha uma opção do banco de dados')
|
.setDescription('Escolha uma opção do banco de dados')
|
||||||
.setRequired(true)
|
.setRequired(true)
|
||||||
.addChoices(...(dynamicChoices.length ? dynamicChoices : [
|
.setAutocomplete(true)
|
||||||
{ name: 'Opção Padrão 1', value: '0' },
|
|
||||||
{ name: 'Opção Padrão 2', value: '1' }
|
|
||||||
]))
|
|
||||||
),
|
),
|
||||||
|
|
||||||
async execute(interaction, client) {
|
async execute(interaction, client) {
|
||||||
// Lógica do comando
|
// Trata a interação de autocomplete
|
||||||
|
if (interaction.isAutocomplete()) {
|
||||||
|
const focusedOption = interaction.options.getFocused(true);
|
||||||
|
try {
|
||||||
|
const choices = await loadChoices();
|
||||||
|
// Filtra as escolhas com base no que o usuário digitou
|
||||||
|
const filtered = choices.filter(choice =>
|
||||||
|
choice.name.toLowerCase().startsWith(focusedOption.value.toLowerCase())
|
||||||
|
);
|
||||||
|
return interaction.respond(filtered);
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Erro no autocomplete:", err);
|
||||||
|
return interaction.respond([]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execução normal do comando
|
||||||
const guildId = interaction.guild.id;
|
const guildId = interaction.guild.id;
|
||||||
const guildName = interaction.guild.name;
|
const guildName = interaction.guild.name;
|
||||||
const guildownerId = interaction.guild.ownerId;
|
const guildownerId = interaction.guild.ownerId;
|
||||||
@ -71,6 +68,7 @@ module.exports = {
|
|||||||
await interaction.deferReply();
|
await interaction.deferReply();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Consulta para verificar a existência do guildId
|
||||||
const query = `SELECT * FROM Guilds WHERE guildId = ?`;
|
const query = `SELECT * FROM Guilds WHERE guildId = ?`;
|
||||||
const [rows] = await conn.execute(query, [guildId]);
|
const [rows] = await conn.execute(query, [guildId]);
|
||||||
console.log('✅ Acessou a base de dados:', rows);
|
console.log('✅ Acessou a base de dados:', rows);
|
||||||
@ -80,11 +78,13 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Verifica se o guildId já está registrado
|
||||||
const checkQuery = `SELECT * FROM Guilds WHERE guildId = ?`;
|
const checkQuery = `SELECT * FROM Guilds WHERE guildId = ?`;
|
||||||
const [checkRows] = await conn.execute(checkQuery, [guildId]);
|
const [checkRows] = await conn.execute(checkQuery, [guildId]);
|
||||||
if (checkRows.length > 0) {
|
if (checkRows.length > 0) {
|
||||||
interaction.editReply('❌ Este guildId já está registrado.');
|
interaction.editReply('❌ Este guildId já está registrado.');
|
||||||
} else {
|
} else {
|
||||||
|
// Insere o novo guildId na tabela
|
||||||
const insertQuery = `INSERT INTO Guilds (guildId, guildownerId) VALUES (?, ?)`;
|
const insertQuery = `INSERT INTO Guilds (guildId, guildownerId) VALUES (?, ?)`;
|
||||||
await conn.execute(insertQuery, [guildId, guildownerId]);
|
await conn.execute(insertQuery, [guildId, guildownerId]);
|
||||||
interaction.editReply('✅ GuildId registrado com sucesso!');
|
interaction.editReply('✅ GuildId registrado com sucesso!');
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user