Estoy aprendiendo (semi-exitosamente) cómo usar los nuevos comandos de barra en Discord.js, un módulo de node.js para interactuar con la API del bot de Discord. Por lo que puedo ver, esto es estrictamente un problema con el código base de Javascript, ¡y no se requiere conocimiento de Discord.js para ayudar a mi pequeño problema bastante molesto!
El código es para recuperar archivos de eventos y ejecutarlos. El problema es que cada vez que se ejecuta el archivo interactionCreate.js , el argumento de interaction parece no estar definido.
Si es necesario, la estructura del archivo es la siguiente:
project-folder/ ├── index.js ├── events/ └── interactionCreate.js Aquí está index.js :
const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js')); for (const file of eventFiles) { const event = require(`./events/${file}`); if (event.once) { client.once(event.name, (...args) => event.execute(...args)); } else { client.on(event.name, (...args) => event.execute(...args)); } } ... y aquí está interactionCreate.js :
module.exports = { name: 'interactionCreate', async execute(client, interaction) { if (!interaction.isCommand()) return; const command = client.commands.get(interaction.commandName); if (!command) return; try { await command.execute(interaction); } catch (error) { console.error(error); await interaction.reply({ content: 'There was an error while executing this command.', ephemeral: true }); } }, };No estoy completamente seguro de si esto es relevante, pero así es como se ve el código cuando no está en módulos separados. Por supuesto, la recuperación del archivo de eventos no es necesaria aquí.
index.js (before modularisation) :
client.on('interactionCreate', async interaction => { if (!interaction.isCommand()) return; const command = client.commands.get(interaction.commandName); if (!command) return; try { await command.execute(interaction); } catch (error) { console.error(error); await interaction.reply({ content: 'There was an error while executing this command.', ephemeral: true }); } });Nuevamente, no estoy seguro de si esto es necesario, pero este es uno de los mensajes de error que recibiría.
C:\path\to\files\project-folder\events\interactionCreate.js:4 if (!interaction.isCommand()) return; ^ TypeError: Cannot read properties of undefined (reading 'isCommand')No tengo mucha experiencia haciendo preguntas aquí, así que si necesitas algo más, ¡avísame! Con mucho gusto responderé a su respuesta o actualizaré la pregunta. ¡Muchas gracias!
Su declaración es así:
async execute(client, interaction) { //... } Y el evento interactionCreate solo proporciona 1 parámetro ( interaction ). Si todos sus eventos estuvieran en un orden de parámetro client, rest, of, the, args , este sería el enfoque:
if (event.once) { client.once(event.name, (...args) => event.execute(client, ...args)); } else { client.on(event.name, (...args) => event.execute(client, ...args)); }