@baipiaodajun/mcbots 1.0.7 → 1.0.9
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.
- package/README.md +1 -1
- package/package.json +1 -1
- package/server.js +26 -22
package/README.md
CHANGED
|
@@ -54,7 +54,7 @@ docker run -d \
|
|
|
54
54
|
--name mcbot \
|
|
55
55
|
-e 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"}]' \
|
|
56
56
|
-p 3000:3000 \
|
|
57
|
-
mingli2038/
|
|
57
|
+
mingli2038/mcbot:latest
|
|
58
58
|
```
|
|
59
59
|
## 来源
|
|
60
60
|
这个东西不是我的原创,思路来自Tweek白嫖群的[这种事可以花点钱]用户给的镜像[ghcr.io/oprmg/mcbot:latest](https://ghcr.io/oprmg/mcbot:latest),我通过AI重构其实现并加入自己的想法从而重新发布出来。
|
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -117,6 +117,8 @@ class MinecraftBotManager {
|
|
|
117
117
|
this.timeout= null;
|
|
118
118
|
this.reachable=false;
|
|
119
119
|
this.lastReachable = null;
|
|
120
|
+
this.resetTimer=null;
|
|
121
|
+
this.notice=true;
|
|
120
122
|
// 机器人名称池
|
|
121
123
|
this.botNames = Array.from({ length: 20 }, () => generateUsername());
|
|
122
124
|
|
|
@@ -151,6 +153,7 @@ class MinecraftBotManager {
|
|
|
151
153
|
console.log(`[${this.host}:${this.port}] MC服务器恢复可达`);
|
|
152
154
|
}
|
|
153
155
|
this.lastReachable = this.reachable;
|
|
156
|
+
this.notice=true;
|
|
154
157
|
}
|
|
155
158
|
}
|
|
156
159
|
// 创建Minecraft机器人
|
|
@@ -224,21 +227,17 @@ class MinecraftBotManager {
|
|
|
224
227
|
|
|
225
228
|
bot.on('error', (error) => {
|
|
226
229
|
console.log(`[${this.host}:${this.port}] 机器人 ${botName} 错误:`, error.message);
|
|
227
|
-
if (this.activeBots.has(botName) && this.failedAttempts <= SERVER_CONFIG.maxFailedAttempts) {
|
|
230
|
+
// if (this.activeBots.has(botName) && this.failedAttempts <= SERVER_CONFIG.maxFailedAttempts) {
|
|
228
231
|
this.handleBotDisconnect(botName);
|
|
229
|
-
} else {
|
|
230
|
-
|
|
231
|
-
}
|
|
232
|
+
// } else {
|
|
233
|
+
// this.handleBotFailure(botName);
|
|
234
|
+
// }
|
|
232
235
|
});
|
|
233
236
|
|
|
234
237
|
bot.on('end', (reason) => {
|
|
235
238
|
console.log(`[${this.host}:${this.port}] 机器人 ${botName} 断开连接:`, reason);
|
|
236
239
|
console.log(`[${this.host}:${this.port}] ${botName}:失败次数${this.failedAttempts}`);
|
|
237
|
-
|
|
238
|
-
this.handleBotDisconnect(botName);
|
|
239
|
-
} else {
|
|
240
|
-
this.handleBotFailure(botName);
|
|
241
|
-
}
|
|
240
|
+
this.handleBotDisconnect(botName);
|
|
242
241
|
});
|
|
243
242
|
|
|
244
243
|
bot.on('kicked', (reason) => {
|
|
@@ -308,33 +307,28 @@ class MinecraftBotManager {
|
|
|
308
307
|
console.log(`[${this.host}:${this.port}] 失败次数过多,${SERVER_CONFIG.resetTimeout / 1000}秒后再次尝试`);
|
|
309
308
|
setTimeout(() => {
|
|
310
309
|
this.failedAttempts = 0;
|
|
311
|
-
|
|
312
|
-
this.reconnect(botName);
|
|
313
|
-
}
|
|
314
|
-
else{
|
|
315
|
-
this.maintainBots();
|
|
316
|
-
}
|
|
310
|
+
this.reconnect(botName);
|
|
317
311
|
}, SERVER_CONFIG.resetTimeout);
|
|
318
312
|
return;
|
|
319
313
|
}
|
|
320
|
-
|
|
321
314
|
setTimeout(() => {
|
|
322
|
-
this.
|
|
315
|
+
this.reconnect(botName);
|
|
323
316
|
}, BOT_CONFIG.reconnectDelay);
|
|
324
317
|
}
|
|
325
318
|
|
|
326
319
|
// 维护机器人数目 - 增强版本
|
|
327
|
-
maintainBots(
|
|
320
|
+
maintainBots() {
|
|
328
321
|
const neededBots = this.minBots - this.currentBots;
|
|
329
322
|
|
|
330
323
|
// console.log(`[${this.host}:${this.port}] 当前机器人: ${this.currentBots}, 需要: ${neededBots}, 失败次数: ${this.failedAttempts}`);
|
|
331
|
-
|
|
332
324
|
if (neededBots > 0 && this.failedAttempts < SERVER_CONFIG.maxFailedAttempts) {
|
|
333
|
-
|
|
334
|
-
|
|
325
|
+
if (this.notice) {
|
|
326
|
+
console.log(`[${this.host}:${this.port}] 需要启动 ${neededBots} 个机器人`);
|
|
327
|
+
this.notice=false;
|
|
328
|
+
}
|
|
335
329
|
for (let i = 0; i < neededBots; i++) {
|
|
336
330
|
setTimeout(() => {
|
|
337
|
-
this.createBot(
|
|
331
|
+
this.createBot();
|
|
338
332
|
}, i * 8000); // 每隔8秒启动一个
|
|
339
333
|
}
|
|
340
334
|
}
|
|
@@ -351,6 +345,16 @@ class MinecraftBotManager {
|
|
|
351
345
|
this.createBot(botName);
|
|
352
346
|
}, 5000);
|
|
353
347
|
} else{
|
|
348
|
+
// 如果还没有设置过 resetTimer,就设置一次
|
|
349
|
+
if (!this.resetTimer) {
|
|
350
|
+
this.resetTimer = setTimeout(() => {
|
|
351
|
+
this.failedAttempts = 0;
|
|
352
|
+
this.resetTimer = null; // 清理标记,允许下次再设置
|
|
353
|
+
console.log(
|
|
354
|
+
`[${this.host}:${this.port}] 失败次数已重置,可以重新尝试创建机器人`
|
|
355
|
+
);
|
|
356
|
+
}, SERVER_CONFIG.resetTimeout);
|
|
357
|
+
}
|
|
354
358
|
console.log(`[${this.host}:${this.port}] 由于失败次数过多,暂停创建新机器人`);
|
|
355
359
|
}
|
|
356
360
|
this.updateStatus();
|