@baipiaodajun/mcbots 1.2.3 → 1.2.5

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 +2 -1
  2. package/package.json +1 -1
  3. package/server.js +66 -26
package/README.md CHANGED
@@ -62,7 +62,8 @@ docker run -d \
62
62
  - 新版本引入了 **热更新功能:`SERVER_JSON`**
63
63
  - 目前该功能处于 **实验状态**,可能会导致一些 **意料之外的问题**
64
64
  - 如果需要稳定使用,推荐继续使用 **1.1 版本**
65
-
65
+ - **1.2.4** 优化机器人断开逻辑,避免幽灵机器人出现。
66
+ - **1.2.5** 根据群友建议加入服务备注,以便能够区分是哪一家服务器,因为一些服务器没有域名。
66
67
  ---
67
68
 
68
69
  ### 页面更新
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@baipiaodajun/mcbots",
3
- "version": "1.2.3",
3
+ "version": "1.2.5",
4
4
  "description": "Minecraft bot and status dashboard for multi-server management",
5
5
  "main": "server.js",
6
6
  "scripts": {
package/server.js CHANGED
@@ -42,6 +42,7 @@ if (fs.existsSync(CONFIG_FILE)) {
42
42
  SERVERS.length = 0;
43
43
  SERVERS.push(...fileConfig);
44
44
  console.log(`从 ${CONFIG_FILE} 加载服务器配置成功,数量: ${SERVERS.length} 项`);
45
+ debugPrint(`配置为:${JSON.stringify(SERVERS, null, 2)}`);
45
46
  loaded = true;
46
47
  } else {
47
48
  console.warn('配置文件内容不是数组,已忽略');
@@ -206,11 +207,12 @@ class HotUpdateAuth {
206
207
  const hotUpdateAuth =new HotUpdateAuth();
207
208
  // Minecraft机器人管理器
208
209
  class MinecraftBotManager {
209
- constructor(host, port, minBots, maxBots, version) {
210
+ constructor(host, port, minBots, maxBots, version,note) {
210
211
  this.host = host;
211
212
  this.port = port;
212
213
  this.minBots = minBots;
213
214
  this.maxBots = maxBots;
215
+ this.note=note;
214
216
  this.version = version || "1.20.1";
215
217
  this.currentBots = 0;
216
218
  this.activeBots = new Map();
@@ -233,6 +235,7 @@ class MinecraftBotManager {
233
235
  port: port,
234
236
  minBots: minBots,
235
237
  maxBots: maxBots,
238
+ note:note,
236
239
  currentBots: 0,
237
240
  activeBots: [],
238
241
  lastUpdate: Date.now(),
@@ -401,6 +404,12 @@ class MinecraftBotManager {
401
404
 
402
405
  // 设置机器人事件
403
406
  setupBotEvents(bot, botName) {
407
+ const triggerDisconnect = (reason) => {
408
+ // 检查机器人是否还在活跃列表中,避免重复调用
409
+ if (this.activeBots.has(botName)) {
410
+ this.handleBotDisconnect(botName);
411
+ }
412
+ };
404
413
  bot.on('login', () => {
405
414
  console.log(`[${this.host}:${this.port}] 机器人 ${botName} 登录成功`);
406
415
  if (this.timeout) {
@@ -423,25 +432,9 @@ class MinecraftBotManager {
423
432
  debugPrint(`[${this.host}:${this.port}] ${botName} 收到消息: ${text}`);
424
433
  });
425
434
 
426
- bot.on('error', (error) => {
427
- console.log(`[${this.host}:${this.port}] 机器人 ${botName} 错误:`, error.message);
428
- // if (this.activeBots.has(botName) && this.failedAttempts <= SERVER_CONFIG.maxFailedAttempts) {
429
- this.handleBotDisconnect(botName);
430
- // } else {
431
- // this.handleBotFailure(botName);
432
- // }
433
- });
434
-
435
- bot.on('end', (reason) => {
436
- console.log(`[${this.host}:${this.port}] 机器人 ${botName} 断开连接:`, reason);
437
- // console.log(`[${this.host}:${this.port}] ${botName}:失败次数${this.failedAttempts}`);
438
- this.handleBotDisconnect(botName);
439
- });
440
-
441
- bot.on('kicked', (reason) => {
442
- console.log(`[${this.host}:${this.port}] 机器人 ${botName} 被踢出:`, reason);
443
- this.handleBotDisconnect(botName);
444
- });
435
+ bot.on('error', (err) => triggerDisconnect(`错误: ${err.message}`));
436
+ bot.on('end', (reason) => triggerDisconnect(`断开: ${reason}`));
437
+ bot.on('kicked', (reason) => triggerDisconnect(`被踢: ${reason}`));
445
438
  }
446
439
 
447
440
  // 设置机器人行为
@@ -474,12 +467,34 @@ class MinecraftBotManager {
474
467
  }
475
468
 
476
469
  handleBotDisconnect(botName) {
477
- if (this.activeBots.has(botName)) {
470
+ // 获取当前的机器人实例
471
+ const bot = this.activeBots.get(botName);
472
+
473
+ if (bot) {
478
474
  debugPrint(`[${this.host}:${this.port}] 从活跃列表中移除机器人: ${botName}`);
475
+
476
+ // --- 核心修复:物理销毁机器人对象 ---
477
+ try {
478
+ // 1. 移除所有事件监听器,防止它继续打印日志或触发回调
479
+ bot.removeAllListeners();
480
+
481
+ // 2. 尝试优雅退出
482
+ if (typeof bot.quit === 'function') bot.quit();
483
+
484
+ // 3. 强制销毁底层 socket 确保连接彻底断开
485
+ if (bot._client && bot._client.socket) {
486
+ bot._client.socket.destroy();
487
+ }
488
+ } catch (err) {
489
+ debugPrint(`[${this.host}:${this.port}] 销毁幽灵机器人 ${botName} 时出错: ${err.message}`);
490
+ }
491
+ // ----------------------------------
492
+
479
493
  this.activeBots.delete(botName);
480
494
  this.currentBots = Math.max(0, this.currentBots - 1);
481
495
  this.updateStatus();
482
496
  this.pendingReconnect.add(botName);
497
+
483
498
  debugPrint(`[${this.host}:${this.port}] 当前活跃机器人数量: ${this.currentBots}, 目标: ${this.minBots}`);
484
499
 
485
500
  setTimeout(() => {
@@ -813,14 +828,28 @@ class StatusServer {
813
828
  .server-header {
814
829
  display: flex;
815
830
  justify-content: space-between;
816
- align-items: center;
817
831
  margin-bottom: 15px;
832
+ gap: 8px;
833
+ }
834
+ .server-note {
835
+ font-size: 0.85rem;
836
+ font-weight: 600;
837
+ display: inline-block;
838
+ padding: 6px 12px;
839
+ background: #e6f0ff;
840
+ color: #1e40af;
841
+ border-radius: 20px;
842
+ border: 1px solid #93c5fd;
843
+ flex-shrink: 0;
844
+ white-space: nowrap;
845
+ margin-right: 8px;
818
846
  }
819
-
820
847
  .server-title {
821
848
  font-size: 1.2rem;
822
849
  font-weight: 600;
823
850
  color: #2d3748;
851
+ min-width: 0;
852
+ flex: 1;
824
853
  }
825
854
 
826
855
  .status-badge {
@@ -829,6 +858,7 @@ class StatusServer {
829
858
  font-size: 0.85rem;
830
859
  font-weight: 600;
831
860
  text-transform: uppercase;
861
+ flex-shrink: 0;
832
862
  }
833
863
 
834
864
  .status-healthy {
@@ -1018,9 +1048,9 @@ class StatusServer {
1018
1048
  <div class="server-card ${server.status}">
1019
1049
  <div class="server-header">
1020
1050
  <div class="server-title">${server.host}:${server.port}</div>
1051
+ ${server.note ? `<div class="server-note">${server.note}</div>` : "" }
1021
1052
  <div class="status-badge ${statusClass}">${statusText}</div>
1022
1053
  </div>
1023
-
1024
1054
  <div class="server-info">
1025
1055
  <div class="info-item">
1026
1056
  <div class="info-label">机器人数量</div>
@@ -1572,7 +1602,8 @@ async function initialize() {
1572
1602
  server.port,
1573
1603
  server.minBots,
1574
1604
  server.maxBots,
1575
- server.version
1605
+ server.version,
1606
+ server.note ?? ""
1576
1607
  )
1577
1608
  );
1578
1609
 
@@ -1627,6 +1658,14 @@ async function hotUpdateServers() {
1627
1658
  // 完全一致:保留,并从 newServerMap 删除(避免重复创建)
1628
1659
  newServerMap.delete(key);
1629
1660
  // 状态保持最新(可选:刷新 lastUpdate)
1661
+
1662
+ //备注处理,不重建机器人,只是更新备注
1663
+ if (manager.note !== newConfig.note) {
1664
+ manager.note = newConfig.note;
1665
+ const status = globalServerStatus.servers.get(key);
1666
+ if (status) status.note = newConfig.note;
1667
+ }
1668
+
1630
1669
  const status = globalServerStatus.servers.get(key);
1631
1670
  if (status) status.lastUpdate = Date.now();
1632
1671
  } else {
@@ -1663,7 +1702,8 @@ async function hotUpdateServers() {
1663
1702
  config.port,
1664
1703
  config.minBots,
1665
1704
  config.maxBots,
1666
- config.version
1705
+ config.version,
1706
+ config.note
1667
1707
  );
1668
1708
  newManager.startMonitoring();
1669
1709
  botManagers.push(newManager);