提问者:小点点

无法读取未定义的属性“name”,我需要一些帮助



const fs = require('fs');

function loadCommands(client) {
    fs.readdir('commands/', (err, files) => {        

        if (err) console.log(err);
    
        const jsfile = files.filter(f => f.split('.').pop() === 'js');
        if (jsfile.length <= 0) {
            return console.log('Bot Couldn\'t Find Commands in commands Folder.');
        }
    
        jsfile.forEach((f, i) => {
            const pull = require(`../commands/${f}`);
            client.commands.set(pull.config.name, pull);
            pull.config.aliases.forEach(alias => {
                bot.aliases.set(alias, pull.config.name);
            });
        });
    });
}

module.exports = {
    loadCommands
}

我不知道为什么错误在这里它看起来很好的代码...

在client.commands.set(pull.config.name,pull)中发现错误;和bot.aliases.set(alias,pull.config.name)中;未定义的显示。

我需要一些帮助这里,我正在尝试为我的机器人做一个自定义前缀,我真的需要这段代码!

代码中的错误


共1个答案

匿名用户

../commands/whateverfile.js中的一个文件不导出config或导出config但不在其上放置.name属性。你没有给我们看那些文件,所以很难猜出问题所在。

您可以尝试这个来找出哪个文件是坏的。

        jsfile.forEach((f, i) => {
            const pull = require(`../commands/${f}`);
            if (!pull || !pull.config || !pull.config.name) {
                throw new Error (`bogus require file ${f}`)
            }
            client.commands.set(pull.config.name, pull);
            pull.config.aliases.forEach(alias => {
                bot.aliases.set(alias, pull.config.name);
            });
        });