Mineflayer Bot在Authme服务器注册后因登录超时被踢,求解决方案
Mineflayer Bot无法在AuthMe服务器完成注册问题求助
我搭建了一台AuthMe服务器,需要用Mineflayer编写Bot实现挂机Farming功能。我从YouTube、Google、ChatGPT查找参考代码后自行编写了Bot注册逻辑,但Bot总是因登录超时被服务器踢出,求帮忙解决。
当前代码文件
Index.js
const mineflayer = require('mineflayer'); const Movements = require('mineflayer-pathfinder').Movements; const pathfinder = require('mineflayer-pathfinder').pathfinder; const { GoalBlock } = require('mineflayer-pathfinder').goals; const config = require('./settings.json'); const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Bot is arrived') }); app.listen(8000, () => { console.log('server started'); }); function createBot() { const bot = mineflayer.createBot({ username: config['bot-account']['username'], password: config['bot-account']['password'], auth: config['bot-account']['type'], host: config.server.ip, port: config.server.port, version: config.server.version, }); bot.loadPlugin(pathfinder); bot.settings.colorsEnabled = false; bot.once('spawn', () => { console.log('\x1b[33m[AfkBot] Bot joined to the server', '\x1b[0m'); if (config.utils['auto-auth'].enabled) { console.log('[INFO] Started auto-auth module'); var password = config.utils['auto-auth'].password; setTimeout(() => { bot.chat(`/register ${password} ${password}`); bot.chat(`/login ${password}`); }, 500); console.log(`[Auth] Authentification commands executed.`); } if (config.utils['chat-messages'].enabled) { console.log('[INFO] Started chat-messages module'); var messages = config.utils['chat-messages']['messages']; if (config.utils['chat-messages'].repeat) { var delay = config.utils['chat-messages']['repeat-delay']; let i = 0; let msg_timer = setInterval(() => { bot.chat(`${messages[i]}`); if (i + 1 == messages.length) { i = 0; } else i++; }, delay * 1000); } else { messages.forEach((msg) => { bot.chat(msg); }); } } const pos = config.position; if (config.position.enabled) { console.log( `\x1b[32m[Afk Bot] Starting moving to target location (${pos.x}, ${pos.y}, ${pos.z})\x1b[0m` ); const mcData = require('minecraft-data')(bot.version); const defaultMove = new Movements(bot, mcData); bot.pathfinder.setMovements(defaultMove); bot.pathfinder.setGoal(new GoalBlock(pos.x, pos.y, pos.z)); } if (config.utils['anti-afk'].enabled) { bot.setControlState('jump', true); if (config.utils['anti-afk'].sneak) { bot.setControlState('sneak', true); } } }); bot.on('chat', (username, message) => { if (config.utils['chat-log']) { console.log(`[ChatLog] <${username}> ${message}`); } }); bot.on('goal_reached', () => { console.log( `\x1b[32m[AfkBot] Bot arrived to target location. ${bot.entity.position}\x1b[0m` ); }); bot.on('death', () => { console.log( `\x1b[33m[AfkBot] Bot has been died and was respawned ${bot.entity.position}`, '\x1b[0m' ); }); if (config.utils['auto-reconnect']) { bot.on('end', () => { setTimeout(() => { createBot(); }, config.utils['auto-recconect-delay']); }); } bot.on('kicked', (reason) => console.log( '\x1b[33m', `[AfkBot] Bot was kicked from the server. Reason: \n${reason}`, '\x1b[0m' ) ); bot.on('error', (err) => console.log(`\x1b[31m[ERROR] ${err.message}`, '\x1b[0m') ); } createBot()
settings.json
{ "bot-account": { "username": "User123", "password": "", "type": "mojang" }, "server": { "ip": "################", "port": 39092, "version": "1.16.5" }, "utils": { "auto-auth": { "enabled": true, "password": "Mypassword" }, "chat-messages": { "enabled": true, "repeat": true, "repeat-delay": 10, "messages": [ "Hi", "Pro User here" ] }, "anti-afk": { "enabled": false, "sneak": true }, "auto-reconnect": true, "auto-reconnect-delay": 5000 }, "position": { "enabled": true, "x": 100, "y": 64, "z": 200 } }
问题分析与解决代码
核心问题
- 原代码在
spawn事件触发后才执行注册命令,但AuthMe服务器会在玩家连接后立即要求验证,spawn触发时机可能晚于服务器超时时间 - 固定500ms延迟不可靠,不同服务器加载速度不同,容易错过验证窗口
- 同时发送
/register和/login逻辑错误,应该根据服务器提示判断执行对应命令 - 配置文件存在拼写错误:代码中
auto-recconect-delay与配置里的auto-reconnect-delay不匹配
修改后的关键代码
替换原spawn事件逻辑
bot.once('spawn', () => { console.log('\x1b[33m[AfkBot] Bot已连接服务器\x1b[0m'); }); // 单独处理AuthMe验证逻辑 if (config.utils['auto-auth'].enabled) { console.log('[INFO] 启动自动验证模块'); const password = config.utils['auto-auth'].password; const authListener = (username, message) => { // 匹配AuthMe注册提示(根据服务器实际提示调整关键词) if (message.toLowerCase().includes('register')) { bot.chat(`/register ${password} ${password}`); console.log('[Auth] 发送注册命令'); } // 匹配AuthMe登录提示 else if (message.toLowerCase().includes('login')) { bot.chat(`/login ${password}`); console.log('[Auth] 发送登录命令'); } // 匹配验证成功提示 else if (message.toLowerCase().includes('successfully') || message.toLowerCase().includes('logged in')) { console.log('[Auth] 验证成功'); bot.removeListener('chat', authListener); // 验证完成后执行后续逻辑 initPostAuthLogic(bot); } }; bot.on('chat', authListener); } // 验证成功后执行的后续逻辑(移动、聊天等) function initPostAuthLogic(bot) { const mcData = require('minecraft-data')(bot.version); const defaultMove = new Movements(bot, mcData); if (config.utils['chat-messages'].enabled) { console.log('[INFO] 启动聊天消息模块'); const messages = config.utils['chat-messages']['messages']; if (config.utils['chat-messages'].repeat) { const delay = config.utils['chat-messages']['repeat-delay']; let i = 0; setInterval(() => { bot.chat(messages[i]); i = (i + 1) % messages.length; }, delay * 1000); } else { messages.forEach(msg => bot.chat(msg)); } } const pos = config.position; if (config.position.enabled) { console.log(`\x1b[32m[Afk Bot] 开始移动至目标位置 (${pos.x}, ${pos.y}, ${pos.z})\x1b[0m`); bot.pathfinder.setMovements(defaultMove); bot.pathfinder.setGoal(new GoalBlock(pos.x, pos.y, pos.z)); } if (config.utils['anti-afk'].enabled) { bot.setControlState('jump', true); if (config.utils['anti-afk'].sneak) { bot.setControlState('sneak', true); } } }
修正重连延迟拼写错误
if (config.utils['auto-reconnect']) { bot.on('end', () => { setTimeout(() => { createBot(); }, config.utils['auto-reconnect-delay']); // 修正拼写错误 }); }
配置文件调整(如果是离线服务器)
将bot-account中的type改为offline,因为离线账号不需要Mojang验证:
"bot-account": { "username": "User123", "password": "", "type": "offline" }
注意事项
- 确保代码中匹配的关键词与AuthMe服务器的实际提示一致(如果是中文服务器,需将关键词改为中文,比如
'请注册'、'请登录') - 离线服务器必须设置
auth: 'offline',否则无法正常连接 - 移除固定延迟后,通过监听服务器消息触发验证命令,能精准匹配AuthMe的验证窗口,避免超时被踢
内容的提问来源于stack exchange,提问作者Ammu Bhai
相关产品推荐
相关产品推荐

