【问题标题】:discord bot error TypeError: Cannot read property 'name' of undefined不和谐机器人错误类型错误:无法读取未定义的属性“名称”
【发布时间】:2020-10-28 02:35:16
【问题描述】:

嗨,我正在做一个不和谐的机器人。我几乎结束了,但是当我去运行机器人时,我得到了标题上的错误。

代码:

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

错误:

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)

如果有人能给我留下正确的代码或表格。

【问题讨论】:

  • 您确定为您的所有文件设置了pull.config.name 吗?可以分享./commands目录下的其中一个JS文件吗?
  • 我不知道怎么做。我刚开始编写机器人程序,所以我不知道很多事情。

标签: javascript node.js discord discord.js


【解决方案1】:

错误很可能来自您的某个文件缺少config 导出。
要找出可能是错误的文件,您可以使用此代码而不是您自己的代码:

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

【讨论】:

  • 感谢 Tenclea。我会试试看它是否可以。
  • 另外,我在 forEach(...
  • @KINGYT 您需要删除 forEach 中的代码并将其替换为我发送的内容:)
猜你喜欢
  • 2018-03-31
  • 1970-01-01
  • 2020-05-25
  • 2019-11-16
  • 1970-01-01
  • 2021-05-18
  • 2020-11-22
  • 1970-01-01
  • 2021-07-25
相关资源
最近更新 更多