我正在制作一个不和谐的机器人,但无法进行故障排除。它给了我一些错误,但我不知道它们是什么意思

时间:2019-02-04 18:59:04

标签: javascript node.js bots discord discord.js

我正在设置一个不和谐的机器人。它将成为具有某些命令的通信机器人。我的机器人现在无法打开,因为它给了我以下错误代码

E:\FLRP Communication\package.js:20
bot.command.set(props.help.name, props);
                ^

TypeError: Cannot read property 'set' of undefined
    at jsfile.forEach (E:\FLRP Communication\package.js:20:17)
    at Array.forEach (<anonymous>)
    at fs.readdir (E:\FLRP Communication\package.js:17:10)
    at FSReqWrap.oncomplete (fs.js:141:20)

我正在使用教学视频,但我所做的一切都和他一样。我不知道该怎么办

const Discord = require("discord.js");
const Config = require("./config.json");
const Token = require("./token.json");
const fs = require("fs");

const bot = new Discord.Client({disableEveryone: true});
bot.commands = new Discord.Collection();

fs.readdir("./commands/", (err, files) =>{
  if(err) console.log(err);
  let jsfile = files.filter(f => f.split(".").pop() === "js")
  if(jsfile.length <= 0){
    console.log("could not find command.");
    return;
  }

  jsfile.forEach((f, i) =>{
    let props = require(`./commands/${f}`);
    console.log(`${f} loaded`);
    bot.command.set(props.help.name, props);
  });
});

bot.on("ready", async () =>{
  console.log(`${bot.user.username} is online! Its running on
${bot.guilds.size} servers`);
  bot.user.setActivity("Work in progress", {type: "PLAYING"});
});

bot.on("message", async message =>{
  if(message.author.bot) return;
  if(message.channel.type === "dm") return;
  let prefix = Config.prefix;
  let msgArray = message.content.split(" ");
  let cmd = msgArray[0];
  let args = msgArray.slice(1);
  let cmdFile = bot.commands.get(cmd.slice(prefix.length));
  if(cmdFile) cmdFile.run(bot, message, args);
});

bot.login(Token.token);

我希望该机器人首先打开(但它不会打开)。在此之后可能会出现更多错误,但此时仅显示1

预先感谢

1 个答案:

答案 0 :(得分:0)

您正在使用上面的bot.commands,然后使用:

bot.command.set(props.help.name, props);

将其更改为:

bot.commands.set(props.help.name, props);