簡體   English   中英

無法讀取未定義的屬性“名稱”,我需要一些幫助

[英]Cannot read property 'name' of undefined, i need some help here


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); 顯示未定義。

我需要一些幫助,我正在嘗試為我的機器人制作一個自定義前綴,我真的需要這個代碼!

代碼中的錯誤

../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);
            });
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM