【问题标题】:My Mineflayer bot doesn't respond to my message我的 Mineflayer 机器人没有回复我的消息
【发布时间】:2021-07-15 08:30:45
【问题描述】:

我正在使用 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 时,它没有响应我的聊天消息。它确实说“嘿!我是机器人。”在聊天中,但不是实际的东西。

【问题讨论】:

    标签: javascript mineflayer


    【解决方案1】:

    我建议使用“聊天”事件,因为您似乎没有正确使用“消息”事件,其中第一个参数是 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!") 
      
    })

    【讨论】:

      猜你喜欢
      • 2021-07-01
      • 1970-01-01
      • 2022-11-11
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-26
      • 2022-10-08
      相关资源
      最近更新 更多