@baipiaodajun/mcbots 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +10 -1
  2. package/package.json +1 -1
  3. package/server.js +34 -10
package/README.md CHANGED
@@ -7,7 +7,16 @@
7
7
  ```
8
8
  process.env.SERVERS_JSON='[{"host":"mc-yy.io","port":25565,"minBots":1,"maxBots":3,"version":"1.20.1"},{"host":"mc-xx.io","port":25565,"minBots":1,"maxBots":3,"version":"1.20.1"}]';
9
9
 
10
- const mcbot = require('@baipiaodajun/mcbots');
10
+ const { initialize, shutdown } = require('@baipiaodajun/mcbots');
11
+
12
+ initialize().then(() => {
13
+ console.log('mcbots start successed');
14
+ }).catch(err => {
15
+ console.error('mcbots start fail:', err);
16
+ });
17
+
18
+ process.on('SIGINT', shutdown);
19
+ process.on('SIGTERM', shutdown);
11
20
  ```
12
21
 
13
22
  再新建一个package.json,内容如下:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@baipiaodajun/mcbots",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Minecraft bot and status dashboard for multi-server management",
5
5
  "main": "server.js",
6
6
  "scripts": {
package/server.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const mineflayer = require('mineflayer');
2
2
  const express = require('express');
3
3
  const PORT = process.env.SERVER_PORT || process.env.PORT || 3000 ;
4
+ const CHAT = process.env.CHAT || false
4
5
  // Minecraft服务器配置
5
6
  const SERVERS = [
6
7
  {
@@ -31,17 +32,17 @@ if (process && process.env && process.env.SERVERS_JSON) {
31
32
 
32
33
  // 配置常量
33
34
  const BOT_CONFIG = {
34
- reconnectDelay: 2000,
35
- maxReconnectAttempts: 3,
35
+ reconnectDelay: 5000,
36
+ maxReconnectAttempts: 5,
36
37
  healthCheckInterval: 60000,
37
38
  viewDistance: 4,
38
- chat: 'enabled'
39
+ chat: CHAT
39
40
  };
40
41
 
41
42
  const SERVER_CONFIG = {
42
43
  statusCheckInterval: 30000,
43
- maxFailedAttempts: 3,
44
- resetTimeout: 300000
44
+ maxFailedAttempts: 5,
45
+ resetTimeout: 180000
45
46
  };
46
47
 
47
48
  // 全局状态存储
@@ -166,7 +167,20 @@ class MinecraftBotManager {
166
167
 
167
168
  bot.on('error', (error) => {
168
169
  console.log(`[${this.host}:${this.port}] 机器人 ${botName} 错误:`, error.message);
169
- this.handleBotDisconnect(botName);
170
+
171
+ // 重要修复:在错误事件中也处理断开连接
172
+ if (error.message.includes('timed out') || error.message.includes('keepAlive')) {
173
+ console.log(`[${this.host}:${this.port}] 检测到超时错误,强制断开机器人: ${botName}`);
174
+ this.handleBotDisconnect(botName);
175
+ // 强制结束连接
176
+ try {
177
+ bot.end();
178
+ } catch (e) {
179
+ // 忽略结束时的错误
180
+ }
181
+ } else {
182
+ this.handleBotFailure();
183
+ }
170
184
  });
171
185
 
172
186
  bot.on('end', (reason) => {
@@ -199,7 +213,7 @@ class MinecraftBotManager {
199
213
  }, 5000);
200
214
 
201
215
  // 随机聊天(如果启用)
202
- if (BOT_CONFIG.chat === 'enabled' && Math.random() < 0.1) {
216
+ if (BOT_CONFIG.chat && Math.random() < 0.1) {
203
217
  setInterval(() => {
204
218
  const messages = ['Hello!', 'Nice server!', 'What\'s up?', 'Good game!'];
205
219
  const randomMessage = messages[Math.floor(Math.random() * messages.length)];
@@ -208,17 +222,23 @@ class MinecraftBotManager {
208
222
  }
209
223
  }
210
224
 
211
- // 处理机器人断开连接
225
+ // 处理机器人断开连接 - 增强版本
212
226
  handleBotDisconnect(botName) {
213
227
  if (this.activeBots.has(botName)) {
228
+ console.log(`[${this.host}:${this.port}] 从活跃列表中移除机器人: ${botName}`);
214
229
  this.activeBots.delete(botName);
215
230
  this.currentBots = Math.max(0, this.currentBots - 1);
216
231
  this.updateStatus();
217
232
 
218
- // 自动重连
233
+ // 记录断开连接时间,用于调试
234
+ console.log(`[${this.host}:${this.port}] 当前活跃机器人数量: ${this.currentBots}, 目标: ${this.minBots}`);
235
+
236
+ // 延迟重连,避免频繁重连
219
237
  setTimeout(() => {
220
238
  this.maintainBots();
221
239
  }, BOT_CONFIG.reconnectDelay);
240
+ } else {
241
+ console.log(`[${this.host}:${this.port}] 机器人 ${botName} 不在活跃列表中,无需处理`);
222
242
  }
223
243
  }
224
244
 
@@ -241,10 +261,12 @@ class MinecraftBotManager {
241
261
  }, BOT_CONFIG.reconnectDelay);
242
262
  }
243
263
 
244
- // 维护机器人数目
264
+ // 维护机器人数目 - 增强版本
245
265
  maintainBots() {
246
266
  const neededBots = this.minBots - this.currentBots;
247
267
 
268
+ // console.log(`[${this.host}:${this.port}] 当前机器人: ${this.currentBots}, 需要: ${neededBots}, 失败次数: ${this.failedAttempts}`);
269
+
248
270
  if (neededBots > 0 && this.failedAttempts < SERVER_CONFIG.maxFailedAttempts) {
249
271
  console.log(`[${this.host}:${this.port}] 需要启动 ${neededBots} 个机器人`);
250
272
 
@@ -253,6 +275,8 @@ class MinecraftBotManager {
253
275
  this.createBot();
254
276
  }, i * 2000); // 每隔2秒启动一个
255
277
  }
278
+ } else if (neededBots > 0) {
279
+ console.log(`[${this.host}:${this.port}] 由于失败次数过多,暂停创建新机器人`);
256
280
  }
257
281
 
258
282
  this.updateStatus();