简体   繁体   中英

discord bot error TypeError: Cannot read property 'name' of undefined

hi i was doing a discord bot.i almost ended but when i went to run the bot i got the error on the tittle.

the code:

fs.readdir("./commands/", (err, files) => {

    if(err) console.log(err)

    let jsfile = files.filter(f => f.split(".").pop() === "js") 
    if(jsfile.length <= 0) {
         return console.log("[LOGS] Couldn't Find Commands!");
    }

    jsfile.forEach((f, i) => {
        let pull = require(`./commands/${f}`);
        bot.commands.set(pull.config.name, pull);  
        pull.config.aliases.forEach(alias => {
            bot.aliases.set(alias, pull.config.name)
        });
    });
});

The error:

TypeError: Cannot read property 'name' of undefined
at C:\Users\ayman\Desktop\test\index.js:28:38
at Array.forEach (<anonymous>)
at C:\Users\ayman\Desktop\test\index.js:26:12
at FSReqCallback.oncomplete (fs.js:158:23)

pls if someone could leave me here the correct code or form.

The error is most likely coming from one of your files missing a config export.
To spot which file might be wrong, you could use this code instead of your own:

// Inside jsfile.forEach( ..
let pull = require(`./commands/${f}`);
if (!pull.config || !pull.config.name) return console.log(`ERROR > ${f} does not have a config or config.name !`);

bot.commands.set(pull.config.name, pull);  
pull.config.aliases.forEach(alias => {
    bot.aliases.set(alias, pull.config.name)
});

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