我正在使用MineFlayer JS制作一个基本的机器人来响应用户的消息。这是源代码。我遇到的问题是,当我在聊天中输入/msg机器人测试时,它不会响应我的聊天消息。
// 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!")
}
})当我在我的“我的世界”聊天中输入/msg测试时,它不会响应我的聊天消息。它确实会说“嘿!我是个机器人。”在聊天中,但不是真正的事情。
发布于 2021-07-16 07:05:16
我建议使用'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!")
})
https://stackoverflow.com/questions/68390422
复制相似问题