arisu/src/commands/Community/testeadd.js
2025-04-04 20:36:41 +01:00

93 lines
3.3 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { InteractionContextType, PermissionFlagsBits, SlashCommandBuilder, MessageFlags } = require('discord.js');
const conn = require('../../../database/db');
module.exports = {
// Função que busca as opções no banco
data: new SlashCommandBuilder()
.setName('add')
.setDescription('Adiciona o servidor à base de dados.')
.addStringOption(option =>
option.setName('nome')
.setDescription('Arisu - Escolhe uma opção')
.setRequired(true)
.addChoices(
{ name: 'Bxio', value: '0' },
{ name: 'TeixLo', value: '0' },
{ name: 'Mike', value: '1' },
{ name: 'Totem', value: '3' }
)
)
.addStringOption(option =>
option.setName('tipo_sanguinio1')
.setDescription('Escolha uma opção do banco de dados')
.setRequired(true)
.addChoices(
{ name: 'Bxio', value: '0' },
{ name: 'TeixLo', value: '0' },
{ name: 'Mike', value: '1' },
{ name: 'Totem', value: '3' }
)
),
async execute(interaction, client) {
const guildId = interaction.guild.id;
const guildName = interaction.guild.name;
const guildownerId = interaction.guild.ownerId;
console.log(`guildId: ${guildId}`);
console.log(`guildName: ${guildName}`);
console.log(`guildownerId: ${guildownerId}`);
//console.log(choices);
// Responde imediatamente para evitar timeout
await interaction.deferReply(); // público por padrão
try {
// Be very careful with this approach! This is vulnerable to SQL injection.
const query = `SELECT * FROM Guilds WHERE guildId = '${guildId}'`;
const [rowsS, fields] = await conn.execute(query);
//console.log(' Primeira tentativa de acessar à base de dados.');
console.log("resultado db:", rowsS); // Log the result
} catch (err) {
interaction.reply('❌ Ocorreu um erro inesperado.');
}
try {
// Verificar se o guildId já existe na base de dados
const checkQuery = `SELECT * FROM Guilds WHERE guildId = '${guildId}'`;
const [checkRows] = await conn.execute(checkQuery);
//console.log(checkRows); // Log the result
if (checkRows.length > 0) {
// Já existe um guildId na tabela, você pode retornar uma resposta indicando a duplicação
interaction.editReply('❌ Este guildId já está registrado.');
//console.log(checkRows.length); // Log the result
} else {
// O guildId não existe, podemos prosseguir com a inserção ou outra ação
const insertQuery = `INSERT INTO Guilds (guildId,guildownerId) VALUES ('${guildId}','${guildownerId}')`;
await conn.execute(insertQuery);
interaction.editReply('✅ GuildId registrado com sucesso!');
}
} catch (err) {
interaction.editReply('❌ Ocorreu um erro inesperado.');
console.error(err); // Logar o erro para ajudar no diagnóstico
}
}
};