Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

306
Vistas
slashCommands.push(slashCommand.data.toJSON()); | TypeError: Cannot read properties of undefined (reading 'toJSON')

I started the bot as usual, in the handler or other file from the slash command I didn't change anything. anyone know how to group? I need it now, and I can't find anything on the internet. I started doing the bot at 1 p.m., now when I am writing this it is 10:13 p.m. and the problem appeared only around 9:30 p.m. I don't know what to write next but they keep telling me so have a nice day / night: D

const fs = require('fs')
const { REST } = require('@discordjs/rest')
const { Routes } = require('discord-api-types/v9')

const token = process.env['token']; //get the token in .env
const guild = process.env['guild']; //get the guild ID in .env
const application_id = process.env['application_id']; //get the application ID in .env

module.exports = (client) => {

    const slashCommands = []; //make a variable

    fs.readdirSync('./slashCommands/').forEach(dir => {
        const slashCommandFiles = fs.readdirSync(`./slashCommands/${dir}/`).filter(file => file.endsWith('.js'));

        for (const file of slashCommandFiles) {
            const slashCommand =require(`../slashCommands/${dir}/${file}`);
            slashCommands.push(slashCommand.data.toJSON());
            if(slashCommand.data.name) { //if the slash command file has a name
                client.slashCommands.set(slashCommand.data.name, slashCommand)
                console.log(file, '- Success') //check if the file load and log in console
            } else {
                console.log(file, '- Error') //if the file doesn't have command name, log it error in console
            }
        }
    });
    
    const rest = new REST({ version: '9' }).setToken(token);

    (async () => {
        try{
            console.log('Start registering application slash commands...')

            await rest.put(
                guild
                ? Routes.applicationGuildCommands(application_id, guild) //registered the slash command in guild
                : Routes.applicationCommands(application_id), //registered the slash command globally
                {
                    body: slashCommands,
                }
            );

            console.log('Successfully registered application slash commands.')
        } catch (err) {
            console.log(err);
        }
    })();

};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

One of the files located in this directory is missing the data object: require(../slashCommands/${dir}/${file});

A fix that will stop the error, but will also render the file useless would be skipping over the file that is missing data or data.toJSON()

for (const file of slashCommandFiles) {
    const slashCommand = require(`../slashCommands/${dir}/${file}`)
    if (!slashCommand.data || !slashCommand.data.toJSON()) continue
    // Carry on with code after here
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda