如何通过第三方输入文件让Python编写的Mineflayer Bot退出服务器?
实现输入quit命令让Mineflayer Bot退出服务器
注意:你贴出的Bot代码是Node.js语法(使用了require),但你提到是用Python编写,这里存在环境混淆,下面分两种场景给出解决方案:
场景1:使用Node.js的Mineflayer(匹配你贴的代码)
1. 调整Bot代码,确保实例可被调用
修改Bot代码,确保可以在外部访问到Bot实例:
const mineflayer = require('mineflayer') const bot = mineflayer.createBot({ host: host, username: username, version: false }) bot.on('login', () => { console.log("spawn") }) module.exports = bot
2. 编写命令输入处理逻辑
用Node.js的readline模块处理输入,判断quit命令并触发Bot退出:
const readline = require('readline') const bot = require('./your-bot-file.js') // 替换为你的Bot代码文件名 const rl = readline.createInterface({ input: process.stdin, output: process.stdout }) const handleInput = () => { rl.question('', cmd => { if (cmd.trim().toLowerCase() === 'quit') { bot.quit('User requested quit') rl.close() process.exit(0) } handleInput() // 继续等待下一条命令 }) } handleInput()
场景2:使用Python的Mineflayer库
如果你确实想用Python实现,先确保安装了Python版Mineflayer(比如py-mineflayer),然后按以下方式修改:
1. Python版Bot代码
from mineflayer import createBot bot = createBot( host=host, username=username, version=False ) @bot.on('login') def logined(*args): print("spawn")
2. Python版输入循环
直接修改你的while循环,判断输入并调用Bot退出方法:
import sys while True: cmd = input().strip().lower() if cmd == 'quit': bot.quit() sys.exit()
内容的提问来源于stack exchange,提问作者PyChel
相关产品推荐
相关产品推荐

