首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的Mineflayer机器人不响应我的消息

我的Mineflayer机器人不响应我的消息
EN

Stack Overflow用户
提问于 2021-07-15 16:30:46
回答 1查看 292关注 0票数 0

我正在使用MineFlayer JS制作一个基本的机器人来响应用户的消息。这是源代码。我遇到的问题是,当我在聊天中输入/msg机器人测试时,它不会响应我的聊天消息。

代码语言:javascript
复制
// 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测试时,它不会响应我的聊天消息。它确实会说“嘿!我是个机器人。”在聊天中,但不是真正的事情。

EN

回答 1

Stack Overflow用户

发布于 2021-07-16 07:05:16

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

基于此,这是一个应该可以工作的示例

代码语言:javascript
复制
bot.on('chat', (username, message) => {
  if (username === bot.username) return
  if(message === 'test') bot.chat("This Works!") 
  
})

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68390422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档