简体   繁体   中英

"TypeError: Cannot read properties of undefined (reading 'name')"

I tried to do a bot and it worked pretty well but now there is this error in my "deploy-commands" file, yet I don't think I touched anything in this file

const fs = require('fs');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { clientId, guildId, token } = require('./config.json');

const commands = [];
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    commands.push(command.data.toJSON());
}

const rest = new REST({version: '9'}).setToken(token);

(async () => {
    try {
        await rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands});
        console.log('Les command ont étés enregistrées !')
    } catch (error) {
        console.error(error);
    }
})();

What's this error please and how I can solve it?

You shall declare command outside the for loop

const command = require(`./commands/${file}`)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM