Suvarii commited on
Commit
bd2cadc
·
verified ·
1 Parent(s): fe1e16c

Update bot.js

Browse files
Files changed (1) hide show
  1. bot.js +98 -24
bot.js CHANGED
@@ -11,10 +11,14 @@ const logger = loggers.logger;
11
 
12
  // --- AYARLAR (İstersen buradan değiştir) ---
13
  const HOME_ARRIVAL_DELAY_MS = 5000; // /home sonrası bekleme (ışınlanma tamamlanması için)
14
- const POST_ACTION_DELAY_MS = 1000; // besleme/işlem sonrası bekleme, sonra /is home atılacak
 
15
  const PERIODIC_CHECK_INTERVAL_MS = 5 * 60 * 1000; // 5 dakika
16
  // ------------------------------------------------
17
 
 
 
 
18
  function createBot() {
19
  const bot = mineflayer.createBot({
20
  username: config["bot-account"]["username"],
@@ -57,10 +61,13 @@ function createBot() {
57
  return t.toLowerCase();
58
  }
59
 
 
 
 
60
  function isBesleyiciName(name) {
61
  if (!name) return false;
62
  const norm = normalizeForMatch(name);
63
- return norm.includes("besleyici") || norm.includes("ʙᴇsʟᴇʏiᴄi");
64
  }
65
 
66
  function safeGet(obj, pathArray) {
@@ -236,6 +243,35 @@ function createBot() {
236
  }
237
  }
238
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239
  // --- HOME kontrol mekanizması ---
240
  let isPerformingHomeCheck = false; // aynı anda birden fazla çalışmasın
241
  let pendingHomeScan = false; // sadece bizim tetiklediğimiz /home sonrası tarama yap
@@ -256,19 +292,18 @@ function createBot() {
256
  // 2) Bekle (ışınlanma için)
257
  await new Promise(r => setTimeout(r, HOME_ARRIVAL_DELAY_MS));
258
 
259
- // 3) Eğer pendingHomeScan false olduysa (başka bir işlem iptal etti) çık
260
  if (!pendingHomeScan) {
261
  isPerformingHomeCheck = false;
262
  return;
263
  }
264
 
265
- // 4) Tarama ve besleme mantığı (kısa ve sessiz)
266
  const arr = Object.values(bot.entities);
267
  if (!arr || arr.length === 0) {
268
  logger.info(`[${config["bot-account"]["username"]}] /home sonrası: entity bulunamadı.`);
269
  } else {
270
- // Hızlı tespit: displayName veya metadata yolu
271
- const targetNorm = normalizeForMatch("besleyici");
272
  let found = null;
273
  let foundPath = null;
274
 
@@ -318,22 +353,32 @@ function createBot() {
318
 
319
  if (found) {
320
  const readable = getEntityReadableName(found);
321
- logger.info(`[${config["bot-account"]["username"]}] Besleyici bulundu id:${found.id} path:${foundPath} — aktive ediliyor.`);
322
- // activateEntityAt dene; windowOpen event'i besleme ve chat bildirimini halledecek
 
323
  const activated = await safeActivateEntity(found, { maxRetries: 4, retryDelay: 300, requireLook: true, maxDistance: 7 });
324
  if (!activated) {
325
- logger.warn(`[${config["bot-account"]["username"]}] Besleyici aktive edilemedi (id:${found.id}).`);
 
 
 
 
 
 
 
 
 
 
 
326
  }
327
  } else {
328
- logger.info(`[${config["bot-account"]["username"]}] /home sonrası tarama: besleyici minyon bulunamadı.`);
329
  }
330
  }
331
 
332
- // 5) Kısa bekleme, sonra /is home ile adaya dön
333
- await new Promise(r => setTimeout(r, POST_ACTION_DELAY_MS));
334
  try {
335
- // Bu komutla adaya dönüyoruz; dönüşte tarama yapmaması için pendingHomeScan false yapıyoruz
336
- pendingHomeScan = false;
337
  bot.chat("/is home");
338
  } catch (e) {
339
  logger.warn("performHomeCheck: /is home gönderilemedi: " + (e && e.message ? e.message : e));
@@ -380,19 +425,15 @@ function createBot() {
380
  logger.info(`[${config["bot-account"]["username"]}] Skyblock'a ışınlanma komutu kullanıldı.`);
381
  }, 5000);
382
 
383
- // Burada /home komutu yerine performHomeCheck çağırıyoruz; böylece kontrol + dönüş mantığı çalışır
384
  setTimeout(async () => {
385
- // İlk girişte home'a gidip kontrol et, sonra adaya dön
386
  performHomeCheck().catch(() => {});
387
- // Periyodik kontrolleri başlat
388
  startPeriodicHomeChecks();
389
  }, 10000);
390
  } else {
391
- // Eğer auto-auth kapalıysa yine periyodik kontrolleri başlatmak isteyebilirsin
392
  startPeriodicHomeChecks();
393
  }
394
 
395
- // Diğer orijinal modüller
396
  if (config.utils["chat-messages"].enabled) {
397
  logger.info("Started chat-messages module");
398
  let messages = config.utils["chat-messages"]["messages"];
@@ -456,11 +497,22 @@ function createBot() {
456
  }
457
  });
458
 
459
- // windowOpen: minyon paneli ve besleme mantığı (chat bildirimleri korunuyor)
460
  bot.on("windowOpen", function (window) {
461
  try {
462
- if (window.title && window.title.includes("ʙᴇsʟᴇʏiᴄi ᴍiɴʏᴏɴ ᴘᴀɴᴇʟi")) {
463
- const beefSlots = window.slots.filter(xx => xx?.name && xx?.name == "cooked_beef");
 
 
 
 
 
 
 
 
 
 
 
464
  if (beefSlots.length > 0) {
465
  const beefSlot = beefSlots[0];
466
  let minyoncani = 9999;
@@ -484,14 +536,36 @@ function createBot() {
484
  bot.closeWindow(window);
485
  }
486
  } else {
487
- bot.closeWindow(window);
488
  }
 
 
 
 
 
 
 
 
489
  } else {
490
- bot.closeWindow(window);
 
 
 
 
 
 
 
491
  }
492
  } catch (err) {
493
  logger.error("windowOpen işlenirken hata: " + (err && err.stack ? err.stack : err));
494
  try { bot.closeWindow(window); } catch (e) {}
 
 
 
 
 
 
 
495
  }
496
  });
497
 
 
11
 
12
  // --- AYARLAR (İstersen buradan değiştir) ---
13
  const HOME_ARRIVAL_DELAY_MS = 5000; // /home sonrası bekleme (ışınlanma tamamlanması için)
14
+ const POST_ACTION_DELAY_MS = 400; // besleme/işlem sonrası kısa bekleme (panel kapandıktan sonra dönmeden önce)
15
+ const WINDOW_OPEN_TIMEOUT_MS = 2500; // panel açılmasını bekleme süresi (ms)
16
  const PERIODIC_CHECK_INTERVAL_MS = 5 * 60 * 1000; // 5 dakika
17
  // ------------------------------------------------
18
 
19
+ // Besleyici adı ayarı: settings.json içinde "utils": { "besleyici-name": "Besleyici x" } şeklinde tanımla
20
+ const BESLEYICI_NAME_CONFIG = (config.utils && config.utils["besleyici-name"]) ? config.utils["besleyici-name"] : "Besleyici";
21
+
22
  function createBot() {
23
  const bot = mineflayer.createBot({
24
  username: config["bot-account"]["username"],
 
61
  return t.toLowerCase();
62
  }
63
 
64
+ // normalize edilmiş hedef adı (ayar üzerinden değiştirilebilir)
65
+ const TARGET_NAME_NORM = normalizeForMatch(BESLEYICI_NAME_CONFIG);
66
+
67
  function isBesleyiciName(name) {
68
  if (!name) return false;
69
  const norm = normalizeForMatch(name);
70
+ return norm.includes(TARGET_NAME_NORM);
71
  }
72
 
73
  function safeGet(obj, pathArray) {
 
243
  }
244
  }
245
 
246
+ // --- windowOpen bekleyici mekanizması ---
247
+ // windowOpen event'i geldiğinde bu resolver'lar çağrılacak
248
+ let windowOpenResolvers = [];
249
+ function waitForWindowOpenOrTimeout(timeoutMs) {
250
+ return new Promise((resolve) => {
251
+ // Eğer zaten bir window açıksa hemen resolve et (güvenlik)
252
+ try {
253
+ if (bot.currentWindow && bot.currentWindow.type) {
254
+ resolve({ opened: true });
255
+ return;
256
+ }
257
+ } catch (e) {}
258
+
259
+ const timer = setTimeout(() => {
260
+ // timeout: panel açılmadı
261
+ const idx = windowOpenResolvers.indexOf(resolver);
262
+ if (idx !== -1) windowOpenResolvers.splice(idx, 1);
263
+ resolve({ opened: false, timedOut: true });
264
+ }, timeoutMs);
265
+
266
+ function resolver() {
267
+ clearTimeout(timer);
268
+ resolve({ opened: true, timedOut: false });
269
+ }
270
+
271
+ windowOpenResolvers.push(resolver);
272
+ });
273
+ }
274
+
275
  // --- HOME kontrol mekanizması ---
276
  let isPerformingHomeCheck = false; // aynı anda birden fazla çalışmasın
277
  let pendingHomeScan = false; // sadece bizim tetiklediğimiz /home sonrası tarama yap
 
292
  // 2) Bekle (ışınlanma için)
293
  await new Promise(r => setTimeout(r, HOME_ARRIVAL_DELAY_MS));
294
 
 
295
  if (!pendingHomeScan) {
296
  isPerformingHomeCheck = false;
297
  return;
298
  }
299
 
300
+ // 3) Tarama ve besleme mantığı (kısa ve sessiz)
301
  const arr = Object.values(bot.entities);
302
  if (!arr || arr.length === 0) {
303
  logger.info(`[${config["bot-account"]["username"]}] /home sonrası: entity bulunamadı.`);
304
  } else {
305
+ // Hedef adı ayardan geliyor
306
+ const targetNorm = TARGET_NAME_NORM;
307
  let found = null;
308
  let foundPath = null;
309
 
 
353
 
354
  if (found) {
355
  const readable = getEntityReadableName(found);
356
+ logger.info(`[${config["bot-account"]["username"]}] '${BESLEYICI_NAME_CONFIG}' bulundu id:${found.id} path:${foundPath} — aktive ediliyor.`);
357
+
358
+ // 4) Activate et ve panel açılmasını bekle (veya timeout)
359
  const activated = await safeActivateEntity(found, { maxRetries: 4, retryDelay: 300, requireLook: true, maxDistance: 7 });
360
  if (!activated) {
361
+ logger.warn(`[${config["bot-account"]["username"]}] '${BESLEYICI_NAME_CONFIG}' aktive edilemedi (id:${found.id}).`);
362
+ } else {
363
+ // Activate başarılıysa panel açılmasını bekle; windowOpen handler paneli işleyip kapattıktan sonra resolver'lar çağrılacak
364
+ const waitResult = await waitForWindowOpenOrTimeout(WINDOW_OPEN_TIMEOUT_MS);
365
+ if (waitResult.opened) {
366
+ // windowOpen handler'ı paneli işleyip kapattıktan sonra buraya gelecek
367
+ await new Promise(r => setTimeout(r, POST_ACTION_DELAY_MS));
368
+ } else {
369
+ // timeout oldu; panel açılmadı veya açılma gecikti
370
+ logger.warn(`[${config["bot-account"]["username"]}] Panel açılmadı veya timeout oldu; devam ediliyor.`);
371
+ await new Promise(r => setTimeout(r, POST_ACTION_DELAY_MS));
372
+ }
373
  }
374
  } else {
375
+ logger.info(`[${config["bot-account"]["username"]}] /home sonrası tarama: '${BESLEYICI_NAME_CONFIG}' minyon bulunamadı.`);
376
  }
377
  }
378
 
379
+ // 5) Dönüş: /is home (dönüşte tarama yapmaması için pendingHomeScan false yapıyoruz)
380
+ pendingHomeScan = false;
381
  try {
 
 
382
  bot.chat("/is home");
383
  } catch (e) {
384
  logger.warn("performHomeCheck: /is home gönderilemedi: " + (e && e.message ? e.message : e));
 
425
  logger.info(`[${config["bot-account"]["username"]}] Skyblock'a ışınlanma komutu kullanıldı.`);
426
  }, 5000);
427
 
428
+ // İlk girişte home'a gidip kontrol et, sonra adaya dön
429
  setTimeout(async () => {
 
430
  performHomeCheck().catch(() => {});
 
431
  startPeriodicHomeChecks();
432
  }, 10000);
433
  } else {
 
434
  startPeriodicHomeChecks();
435
  }
436
 
 
437
  if (config.utils["chat-messages"].enabled) {
438
  logger.info("Started chat-messages module");
439
  let messages = config.utils["chat-messages"]["messages"];
 
497
  }
498
  });
499
 
500
+ // Güvenli windowOpen handler: window.title artık her tipte olabilir
501
  bot.on("windowOpen", function (window) {
502
  try {
503
+ // window.title bazen ChatMessage veya obje olabilir; güvenli string'e çevir
504
+ let titleStr = "";
505
+ try {
506
+ if (typeof window.title === "string") titleStr = window.title;
507
+ else if (window.title && typeof window.title.toString === "function") titleStr = window.title.toString();
508
+ else titleStr = String(window.title || "");
509
+ } catch (e) {
510
+ titleStr = String(window.title || "");
511
+ }
512
+
513
+ // Panel başlığı kontrolü (güvenli includes)
514
+ if (titleStr && titleStr.includes && titleStr.includes("ʙᴇsʟᴇʏiᴄi ᴍiɴʏᴏɴ ᴘᴀɴᴇʟi")) {
515
+ const beefSlots = window.slots.filter(xx => xx?.name && xx?.name === "cooked_beef");
516
  if (beefSlots.length > 0) {
517
  const beefSlot = beefSlots[0];
518
  let minyoncani = 9999;
 
536
  bot.closeWindow(window);
537
  }
538
  } else {
539
+ try { bot.closeWindow(window); } catch (e) {}
540
  }
541
+
542
+ // Panel işlendi: bekleyen resolver'ları çağır
543
+ try {
544
+ while (windowOpenResolvers.length > 0) {
545
+ const r = windowOpenResolvers.shift();
546
+ try { r(); } catch (e) {}
547
+ }
548
+ } catch (e) {}
549
  } else {
550
+ // Başlık eşleşmiyorsa yine window'u kapat ve bekleyenleri temizle
551
+ try { bot.closeWindow(window); } catch (e) {}
552
+ try {
553
+ while (windowOpenResolvers.length > 0) {
554
+ const r = windowOpenResolvers.shift();
555
+ try { r(); } catch (e) {}
556
+ }
557
+ } catch (e) {}
558
  }
559
  } catch (err) {
560
  logger.error("windowOpen işlenirken hata: " + (err && err.stack ? err.stack : err));
561
  try { bot.closeWindow(window); } catch (e) {}
562
+ // Hata olsa bile bekleyenleri temizle
563
+ try {
564
+ while (windowOpenResolvers.length > 0) {
565
+ const r = windowOpenResolvers.shift();
566
+ try { r(); } catch (e) {}
567
+ }
568
+ } catch (e) {}
569
  }
570
  });
571