我的 Mineflayer 机器人不回复我的消息

时间:2021-07-15 08:30:45

标签: javascript mineflayer

我正在使用 MineFlayer JS 制作一个响应用户消息的基本机器人。这是源代码。我遇到的问题是,当我在聊天中键入 /msg bot test 时,它没有响应我的聊天消息。

// Importing the necessary modules
const mineflayer = require('mineflayer')

// Options for the bot
const options = {
    host: 'localhost',
    port: 53115,
    username: 'bot'
}

// Creating the actual bot
const bot = mineflayer.createBot(options)

// Creating a function to say "Hey I am a bot." in minecraft chat and logs "I spawned." in console
function typeHiInChat() {
    bot.chat("Hey! I am a bot.")
    console.log('I spawned.')
}

// Creating a function to say "I have been kicked from the server!" open it getting kicked
function onKick() {
    console.log('I have been kicked from the server!')
}

// Setting up a listener that listens for the 'spawn' event
bot.once('spawn', typeHiInChat)

// Setting up a listener that listens for the 'kick' event
bot.once('kicked', onKick)

bot.on('message', (message, jsonMSG) => {
    msg = JSON.stringify(jsonMSG)
    if (msg == "test") {
        bot.chat("This Works!")
    }
})

当我在 Minecraft 聊天中输入 /msg test 时,它不响应我的聊天消息。它确实说“嘿!我是机器人。”在聊天中,但不是实际的事情。

1 个答案:

答案 0 :(得分:0)

我建议使用 'chat' 事件,因为看起来您没有正确使用 'message' 事件,其中第一个参数是 jsonMSG,第二个参数是位置。看看这里 https://github.com/PrismarineJS/mineflayer/blob/master/docs/api.md#events

基于此,这是一个应该可行的示例

bot.on('chat', (username, message) => {
  if (username === bot.username) return
  if(message === 'test') bot.chat("This Works!") 
  
})