Suvarii commited on
Commit
36e8b2f
·
verified ·
1 Parent(s): 4655574

Update bot.js

Browse files
Files changed (1) hide show
  1. bot.js +105 -12
bot.js CHANGED
@@ -278,6 +278,100 @@ function createBot() {
278
  });
279
  }
280
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
281
  // --- HOME kontrol mekanizması ---
282
  let isPerformingHomeCheck = false; // aynı anda birden fazla çalışmasın
283
  let pendingHomeScan = false; // sadece bizim tetiklediğimiz /home sonrası tarama yap
@@ -507,7 +601,6 @@ function createBot() {
507
  // windowClose listener: panel kapandığında bekleyenleri çözer
508
  bot.on("windowClose", (window) => {
509
  try {
510
- // temizle ve resolver'ları çağır
511
  while (windowOpenResolvers.length > 0) {
512
  const r = windowOpenResolvers.shift();
513
  try { r(); } catch (e) {}
@@ -530,7 +623,7 @@ function createBot() {
530
 
531
  logger.info(`[${config["bot-account"]["username"]}] windowOpen başlığı: ${titleStr}`);
532
 
533
- // Panel başlığı kontrolü (esnek)
534
  const panelMatch = (titleStr && (titleStr.toLowerCase().includes("besleyici") || titleStr.includes("ʙᴇsʟᴇʏiᴄi")));
535
  if (!panelMatch) {
536
  try { bot.closeWindow(window); } catch (e) {}
@@ -548,20 +641,20 @@ function createBot() {
548
 
549
  if (beefSlots.length > 0) {
550
  const beefSlot = beefSlots[0];
551
- let minyoncani = 9999;
552
- let loreText = "";
553
  try {
554
- const lore = beefSlot?.nbt?.value?.display?.value?.Lore?.value?.value[1];
555
- loreText = lore ? (JSON.parse(lore)?.extra?.[0]?.text ?? lore?.toString?.() ?? "") : "";
556
- minyoncani = Number((loreText).split("/")[0]);
557
- if (Number.isNaN(minyoncani)) minyoncani = Number(String(loreText).replace(/\D/g, "")) || minyoncani;
558
- } catch (err) {
559
- logger.warn("Minyon canı okunurken hata: " + (err && err.message ? err.message : err));
560
  }
561
 
562
- logger.info(`[${config["bot-account"]["username"]}] windowOpen loreText: ${loreText}`);
 
563
 
564
- if (minyoncani <= 2) {
 
 
565
  bot.clickWindow(36, 0, 0);
566
  logger.info(`[${config["bot-account"]["username"]}] Minyon canı ${minyoncani} olduğu için besleyici minyon beslendi.`);
567
  logger.info(`Minyon canı ${minyoncani} olduğu için besleyici ile beslendi.`);
 
278
  });
279
  }
280
 
281
+ // --- Güvenli minyon canı çıkarma yardımcı fonksiyonu ---
282
+ function extractMinyonCanFromSlot(slot) {
283
+ try {
284
+ if (!slot) return null;
285
+ const nbt = slot.nbt;
286
+ if (!nbt) return null;
287
+
288
+ // Olası lore yolları
289
+ const possibleLorePaths = [
290
+ ["value", "display", "value", "Lore", "value"],
291
+ ["value", "display", "value", "Lore"],
292
+ ["value", "display", "value"],
293
+ ];
294
+
295
+ let loreCandidate = null;
296
+
297
+ for (const path of possibleLorePaths) {
298
+ let cur = nbt;
299
+ let ok = true;
300
+ for (const p of path) {
301
+ if (cur == null) { ok = false; break; }
302
+ cur = cur[p];
303
+ }
304
+ if (!ok) continue;
305
+
306
+ if (Array.isArray(cur) && cur.length > 0) {
307
+ const tryIndices = [1, cur.length - 1, 0];
308
+ for (const idx of tryIndices) {
309
+ const item = cur[idx];
310
+ if (!item) continue;
311
+ if (typeof item === "string") { loreCandidate = item; break; }
312
+ if (item && typeof item.value === "string") { loreCandidate = item.value; break; }
313
+ try {
314
+ const s = item.toString();
315
+ if (s) { loreCandidate = s; break; }
316
+ } catch (e) {}
317
+ }
318
+ } else if (typeof cur === "string") {
319
+ loreCandidate = cur;
320
+ } else if (cur && typeof cur.toString === "function") {
321
+ loreCandidate = cur.toString();
322
+ }
323
+ if (loreCandidate) break;
324
+ }
325
+
326
+ if (!loreCandidate) {
327
+ try {
328
+ loreCandidate = util.inspect(nbt, { depth: 4 });
329
+ } catch (e) {
330
+ loreCandidate = String(nbt);
331
+ }
332
+ }
333
+
334
+ if (!loreCandidate) return null;
335
+
336
+ // JSON ise parse et
337
+ let plainText = loreCandidate;
338
+ try {
339
+ const maybe = JSON.parse(loreCandidate);
340
+ if (maybe && typeof maybe === "object") {
341
+ if (maybe.extra && Array.isArray(maybe.extra) && maybe.extra.length > 0) {
342
+ if (typeof maybe.extra[0].text === "string") plainText = maybe.extra[0].text;
343
+ else plainText = JSON.stringify(maybe.extra);
344
+ } else if (typeof maybe.text === "string") {
345
+ plainText = maybe.text;
346
+ } else {
347
+ plainText = JSON.stringify(maybe);
348
+ }
349
+ }
350
+ } catch (e) {
351
+ // JSON değilse olduğu gibi kullan
352
+ }
353
+
354
+ // X/Y formatı ara
355
+ const xyMatch = plainText.match(/(\d{1,4})\s*\/\s*(\d{1,4})/);
356
+ if (xyMatch) {
357
+ const x = Number(xyMatch[1]);
358
+ if (!Number.isNaN(x)) return x;
359
+ }
360
+
361
+ // İlk sayı
362
+ const numMatch = plainText.match(/(\d{1,4})/);
363
+ if (numMatch) {
364
+ const n = Number(numMatch[1]);
365
+ if (!Number.isNaN(n)) return n;
366
+ }
367
+
368
+ return null;
369
+ } catch (err) {
370
+ logger.warn("extractMinyonCanFromSlot hata: " + (err && err.message ? err.message : err));
371
+ return null;
372
+ }
373
+ }
374
+
375
  // --- HOME kontrol mekanizması ---
376
  let isPerformingHomeCheck = false; // aynı anda birden fazla çalışmasın
377
  let pendingHomeScan = false; // sadece bizim tetiklediğimiz /home sonrası tarama yap
 
601
  // windowClose listener: panel kapandığında bekleyenleri çözer
602
  bot.on("windowClose", (window) => {
603
  try {
 
604
  while (windowOpenResolvers.length > 0) {
605
  const r = windowOpenResolvers.shift();
606
  try { r(); } catch (e) {}
 
623
 
624
  logger.info(`[${config["bot-account"]["username"]}] windowOpen başlığı: ${titleStr}`);
625
 
626
+ // Esnek panel eşlemesi (küçük harfe çevirerek "besleyici" kelimesini arar)
627
  const panelMatch = (titleStr && (titleStr.toLowerCase().includes("besleyici") || titleStr.includes("ʙᴇsʟᴇʏiᴄi")));
628
  if (!panelMatch) {
629
  try { bot.closeWindow(window); } catch (e) {}
 
641
 
642
  if (beefSlots.length > 0) {
643
  const beefSlot = beefSlots[0];
644
+
645
+ // Detaylı inspect logu: yapıyı görmemiz için çok önemli
646
  try {
647
+ logger.info(`[${config["bot-account"]["username"]}] beefSlot inspect: ${util.inspect(beefSlot, { depth: 4 })}`);
648
+ } catch (e) {
649
+ logger.info(`[${config["bot-account"]["username"]}] beefSlot inspect alınamadı.`);
 
 
 
650
  }
651
 
652
+ // Güvenli ekstraksiyon
653
+ const minyoncani = extractMinyonCanFromSlot(beefSlot);
654
 
655
+ if (minyoncani == null) {
656
+ logger.info(`[${config["bot-account"]["username"]}] Minyon canı okunamadı (null).`);
657
+ } else if (minyoncani <= 2) {
658
  bot.clickWindow(36, 0, 0);
659
  logger.info(`[${config["bot-account"]["username"]}] Minyon canı ${minyoncani} olduğu için besleyici minyon beslendi.`);
660
  logger.info(`Minyon canı ${minyoncani} olduğu için besleyici ile beslendi.`);