Node.js 说函数不是函数

Node.js saying that a function is not a function

我的目标是使用 mineflayer 创建一个 minecraft 机器人用户。我安装了 node.js 并从终端安装了 mineflayer,然后创建了一个 require('mineflayer') 的变量。然后我用了

const mineflayer=require('mineflayer');
const bot=mineflayer.createBot({
    host:'minehut.gg', //only for test purposes
    username: 'MyUserName',
    password: 'myPWD'
});

const plankRecipe = bot.recipesFor(5)[0];

但是,在该代码段的最后一行,它抛出了以下内容: TypeError: bot.recipesFor is not a function 。为什么要这样做,我如何让它认识到它是一个函数?到目前为止,我唯一的猜测是它无法连接,只是没有告诉我它无法正确创建 bot 对象。

谢谢!

编辑: WebStorm 调试器说 bot 是一个 EventEmitter 对象,而不是预期的 Bot 对象。这有帮助吗?

我查看了官方文档,recipesFor 方法接受 4 个参数

你可以查看文档here

在 mineflayer 中,机器人在创建时没有任何方法(除了 EventEmitter 方法)。当您登录时,机器人会获得其方法。

这是有道理的,因为机器人如果不连接到游戏就不能真正做很多事情。甚至制作配方也是服务器端的。

正确的代码是:

const mineflayer = require("mineflayer"),
  bot = mineflayer.createBot({
    host: "example.com",
    username: "my_username",
    password: "my_password"
  });

bot.once("login", () => {
  const plankRecipe = bot.recipesFor(5)[0];
});

查看其中一个 examples

不幸的是,mineflayer 的 API 文档并不容易实现这一点。

错误 TypeError: bot.recipesFor is not a function 的意思正是它所说的:您尝试调用的 'function' 不是一个函数,这可能意味着它不存在或者是一个不存在的变量一个函数。