Suvarii commited on
Commit
fb8a6d2
·
verified ·
1 Parent(s): 151efa7

Update bot.js

Browse files
Files changed (1) hide show
  1. bot.js +280 -217
bot.js CHANGED
@@ -1,7 +1,7 @@
1
  const mineflayer = require("mineflayer");
2
  const Movements = require("mineflayer-pathfinder").Movements;
3
  const pathfinder = require("mineflayer-pathfinder").pathfinder;
4
- const { GoalBlock, GoalXZ, GoalNear } = require("mineflayer-pathfinder").goals;
5
 
6
  const config = require("./settings.json");
7
 
@@ -9,240 +9,303 @@ const loggers = require("./logging.js");
9
  const logger = loggers.logger;
10
 
11
  function createBot() {
12
- const bot = mineflayer.createBot({
13
- username: config["bot-account"]["username"],
14
- auth: config["bot-account"]["type"],
15
- host: config.server.ip,
16
- port: config.server.port,
17
- version: config.server.version,
18
- });
19
-
20
- bot.loadPlugin(pathfinder);
21
-
22
- bot.once("spawn", async() => {
23
- const defaultMove = new Movements(bot);
24
- bot.pathfinder.setMovements(defaultMove);
25
- bot.settings.colorsEnabled = false;
26
- logger.info("Bot sunucuya giriş yaptı.");
27
-
28
- if (config.utils["auto-auth"].enabled) {
29
-
30
- let password = config.utils["auto-auth"].password;
31
- setTimeout(() => {
32
- bot.chat(`/login ${password}`);
33
- logger.info(`[${config["bot-account"]["username"]}] Login komutu kullanıldı.`);
34
- }, 500);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- setTimeout(async () => {
 
 
 
 
 
37
  bot.chat(`/skyblock`);
38
- logger.info(`[${config["bot-account"]["username"]}] Skyblock'a ışınlanma komutu kullanıldı.`);
39
- }, 5000);
40
- setTimeout(async () => {
41
- bot.chat(`/home`);
42
- logger.info(`[${config["bot-account"]["username"]}] Home konumuna ışınlanma komutu kullanıldı.`);
43
- }, 10000);
44
- setInterval(async () => {
45
  bot.chat(`/home`);
46
- setInterval(async () => {
47
- if(Object.keys(bot.entities).filter(ff => JSON.stringify(bot.entities[ff]).includes("Besleyici ")).length > 0){
48
- bot.activateEntityAt(bot.entities[Object.keys(bot.entities).filter(ff => JSON.stringify(bot.entities[ff]).includes("Besleyici "))[0]], bot.entities[Object.keys(bot.entities).filter(ff => JSON.stringify(bot.entities[ff]).includes("Besleyici "))[0]].position)
49
- setInterval(async () => {
50
- bot.chat(`/is home`);
51
- }, 5000);
52
- }else{
53
- setTimeout(async () => {
54
- bot.chat(`/skyblock`);
55
- logger.info(`[${config["bot-account"]["username"]}] Bölgede besleyici minyon bulunamadı.Skyblock'a ışınlanma komutu kullanıldı.`);
56
- }, 5000);
57
- setTimeout(async () => {
58
- bot.chat(`/home`);
59
- logger.info(`[${config["bot-account"]["username"]}] Bölgede besleyici minyon bulunamadı.Home konumuna ışınlanma komutu kullanıldı.`);
60
- }, 10000);
61
- setInterval(async () => {
62
-
63
- if(Object.keys(bot.entities).filter(ff => JSON.stringify(bot.entities[ff]).includes("Besleyici ")).length > 0){
64
- bot.activateEntityAt(bot.entities[Object.keys(bot.entities).filter(ff => JSON.stringify(bot.entities[ff]).includes("Besleyici "))[0]], bot.entities[Object.keys(bot.entities).filter(ff => JSON.stringify(bot.entities[ff]).includes("Besleyici "))[0]].position)
65
- };
66
  }, 13000);
67
- setInterval(async () => {
68
- bot.chat(`/is home`);
69
- }, 16000);
70
- };
71
- }, 2000);
72
- }, 300000);
73
 
74
- }
 
 
 
 
 
75
 
76
- if (config.utils["chat-messages"].enabled) {
77
- logger.info("Started chat-messages module");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
- let messages = config.utils["chat-messages"]["messages"];
80
 
81
- if (config.utils["chat-messages"].repeat) {
82
- let delay = config.utils["chat-messages"]["repeat-delay"];
83
- let i = 0;
 
 
 
84
 
85
- setInterval(() => {
86
- bot.chat(`${messages[i]}`);
 
 
87
 
88
- if (i + 1 === messages.length) {
89
- i = 0;
90
- } else i++;
91
- }, delay * 1000);
92
- } else {
93
- messages.forEach((msg) => {
94
- bot.chat(msg);
95
- });
96
- }
97
  }
98
 
99
- const pos = config.position;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
- if (config.position.enabled) {
102
- logger.info(
103
- `Starting moving to target location (${pos.x}, ${pos.y}, ${pos.z})`,
104
- );
105
- bot.pathfinder.setGoal(new GoalBlock(pos.x, pos.y, pos.z));
106
  }
107
 
108
- if (config.utils["anti-afk"].enabled) {
109
- if (config.utils["anti-afk"].sneak) {
110
- bot.setControlState("sneak", true);
111
- }
112
-
113
- if (config.utils["anti-afk"].jump) {
114
- bot.setControlState("jump", true);
115
- }
116
-
117
- if (config.utils["anti-afk"]["hit"].enabled) {
118
- let delay = config.utils["anti-afk"]["hit"]["delay"];
119
- let attackMobs = config.utils["anti-afk"]["hit"]["attack-mobs"];
120
-
121
- setInterval(() => {
122
- if (attackMobs) {
123
- let entity = bot.nearestEntity(
124
- (e) =>
125
- e.type !== "object" &&
126
- e.type !== "player" &&
127
- e.type !== "global" &&
128
- e.type !== "orb" &&
129
- e.type !== "other",
130
- );
131
-
132
- if (entity) {
133
- bot.attack(entity);
134
- return;
135
- }
136
- }
137
-
138
- bot.swingArm("right", true);
139
- }, delay);
140
- }
141
-
142
- if (config.utils["anti-afk"].rotate) {
143
- setInterval(() => {
144
- bot.look(bot.entity.yaw + 1, bot.entity.pitch, true);
145
- }, 100);
146
- }
147
-
148
- if (config.utils["anti-afk"]["circle-walk"].enabled) {
149
- let radius = config.utils["anti-afk"]["circle-walk"]["radius"];
150
- setInterval(() => {
151
- circleWalk(bot, radius);
152
- }, 30000);
153
- }
154
  }
155
- });
156
-
157
- bot.on('windowOpen', function (window) {
158
- if(window.title.includes("ʙᴇsʟᴇʏiᴄi ᴍiɴʏᴏɴ ᴘᴀɴᴇʟi")){
159
- //Besleyici minyon paneli
160
- if(window.slots.filter(xx => xx?.name && xx?.name == "cooked_beef").length > 0){
161
- var minyoncani = Number((JSON.parse(window.slots.filter(xx => xx?.name && xx?.name == "cooked_beef")[0]?.nbt?.value?.display?.value?.Lore?.value?.value[1])?.extra[0].text).split("/")[0]);
162
- if(minyoncani <= 2){
163
- bot.clickWindow(36,0,0);
 
 
 
 
 
 
 
164
  logger.info(`[${config["bot-account"]["username"]}] Minyon canı ${minyoncani} olduğu için besleyici minyon beslendi.`);
165
  bot.closeWindow(window);
166
- }else{
167
- logger.info(`[${config["bot-account"]["username"]}] Minyon canı ${minyoncani} olduğu için beslemeye gerek duyulmadı.`);
168
- bot.closeWindow(window);
169
- }
170
- }else{
171
  bot.closeWindow(window);
172
- }
173
- }else{
174
- bot.closeWindow(window);
 
 
 
175
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
  });
177
-
178
- bot.on("chat", (username, message) => {
179
- if (config.utils["chat-log"]) {
180
- logger.info(`<${username}> ${message}`);
181
- }
182
- });
183
-
184
- bot.on("goal_reached", () => {
185
- if (config.position.enabled) {
186
- logger.info(`Bot arrived to target location. ${bot.entity.position}`);
187
- }
188
- });
189
-
190
- bot.on("death", () => {
191
- logger.warn(
192
- `Bot has been died and was respawned at ${bot.entity.position}`,
193
- );
194
- });
195
-
196
- if (config.utils["auto-reconnect"]) {
197
- bot.on("end", () => {
198
- setTimeout(() => {
199
- createBot();
200
- }, config.utils["auto-reconnect-delay"]);
201
- });
202
- }
203
-
204
- bot.on("kicked", (reason) => {
205
- let reasonText = JSON.parse(reason).text;
206
- if (reasonText === "") {
207
- reasonText = JSON.parse(reason).extra[0].text;
208
- }
209
- try{
210
- reasonText = reasonText?.replace(/§./g, "");
211
- logger.warn(`Bot sunucudan atıldı.Sebep: ${reasonText}`);
212
- }catch{
213
- logger.warn(`Bot sunucudan atıldı.Sebep: Bulunamadı.`);
214
- }
215
-
216
- });
217
-
218
- bot.on("error", (err) => logger.error(`${err.message}`));
219
  }
220
 
221
-
222
  function circleWalk(bot, radius) {
223
- // Make bot walk in square with center in bot's wthout stopping
224
- return new Promise(() => {
225
- const pos = bot.entity.position;
226
- const x = pos.x;
227
- const y = pos.y;
228
- const z = pos.z;
229
-
230
- const points = [
231
- [x + radius, y, z],
232
- [x, y, z + radius],
233
- [x - radius, y, z],
234
- [x, y, z - radius],
235
- ];
236
-
237
- let i = 0;
238
- setTimeout(() => {
239
- if (i === points.length) i = 0;
240
- const defaultMove = new Movements(bot);
241
- bot.pathfinder.setMovements(defaultMove);
242
- bot.pathfinder.setGoal(new GoalXZ(points[i][0], points[i][2]));
243
- i++;
244
- }, 1000);
245
- });
246
  }
247
 
248
  createBot();
@@ -254,12 +317,12 @@ const port = process.env.PORT || 7860; //buraya karışmayın.
254
  app.get("/", (req, res) => res.send("Pinglendi")); //değiştirebilirsiniz.
255
 
256
  app.listen(
257
- port,
258
- () =>
259
- console.log(`Bot bu adres üzerinde çalışıyor: http://localhost:${port}`), //port
260
  );
261
 
262
  process.on('uncaughtException', function (err) {
263
- console.error("Hata aldım!");
264
- console.log(err)
265
- })
 
1
  const mineflayer = require("mineflayer");
2
  const Movements = require("mineflayer-pathfinder").Movements;
3
  const pathfinder = require("mineflayer-pathfinder").pathfinder;
4
+ const { GoalBlock, GoalXZ } = require("mineflayer-pathfinder").goals;
5
 
6
  const config = require("./settings.json");
7
 
 
9
  const logger = loggers.logger;
10
 
11
  function createBot() {
12
+ const bot = mineflayer.createBot({
13
+ username: config["bot-account"]["username"],
14
+ auth: config["bot-account"]["type"],
15
+ host: config.server.ip,
16
+ port: config.server.port,
17
+ version: config.server.version,
18
+ });
19
+
20
+ bot.loadPlugin(pathfinder);
21
+
22
+ // Güvenli "Besleyici" tespiti için yardımcı fonksiyon
23
+ function isBesleyiciEntity(e) {
24
+ try {
25
+ if (!e) return false;
26
+ // displayName objesi varsa toString ile al
27
+ const display = e.displayName && typeof e.displayName.toString === "function"
28
+ ? e.displayName.toString()
29
+ : null;
30
+ const name = e.name || display || e.username || "";
31
+ return typeof name === "string" && name.includes("Besleyici ");
32
+ } catch (err) {
33
+ return false;
34
+ }
35
+ }
36
+
37
+ // Tek bir tekrar eden görev: her 5 dakika / 2s gibi orijinal mantığı koruyarak
38
+ let besleyiciCheckInterval = null;
39
+ let periodicIsHomeInterval = null;
40
+
41
+ bot.once("spawn", async () => {
42
+ const defaultMove = new Movements(bot);
43
+ bot.pathfinder.setMovements(defaultMove);
44
+ bot.settings.colorsEnabled = false;
45
+ logger.info("Bot sunucuya giriş yaptı.");
46
+
47
+ if (config.utils["auto-auth"].enabled) {
48
+ let password = config.utils["auto-auth"].password;
49
+ setTimeout(() => {
50
+ bot.chat(`/login ${password}`);
51
+ logger.info(`[${config["bot-account"]["username"]}] Login komutu kullanıldı.`);
52
+ }, 500);
53
+
54
+ setTimeout(async () => {
55
+ bot.chat(`/skyblock`);
56
+ logger.info(`[${config["bot-account"]["username"]}] Skyblock'a ışınlanma komutu kullanıldı.`);
57
+ }, 5000);
58
+
59
+ setTimeout(async () => {
60
+ bot.chat(`/home`);
61
+ logger.info(`[${config["bot-account"]["username"]}] Home konumuna ışınlanma komutu kullanıldı.`);
62
+ }, 10000);
63
+
64
+ // Besleyici arama ve işlem mantığını sadeleştiren fonksiyon
65
+ async function tryFindAndActivateBesleyici() {
66
+ try {
67
+ const besleyici = Object.values(bot.entities).find(isBesleyiciEntity);
68
+ if (besleyici) {
69
+ // activateEntityAt çağrısını orijinal kullandığın şekilde bırakıyorum
70
+ bot.activateEntityAt(besleyici, besleyici.position);
71
+ logger.info(`[${config["bot-account"]["username"]}] Besleyici bulundu ve aktive edildi.`);
72
+ // /is home komutunu düzenli at
73
+ if (!periodicIsHomeInterval) {
74
+ periodicIsHomeInterval = setInterval(() => {
75
+ bot.chat(`/is home`);
76
+ }, 5000);
77
+ }
78
+ return true;
79
+ } else {
80
+ return false;
81
+ }
82
+ } catch (err) {
83
+ logger.error(`Besleyici arama sırasında hata: ${err.message}`);
84
+ return false;
85
+ }
86
+ }
87
 
88
+ // İlk kontrol: her 5 dakika bir (300000 ms) çalışacak şekilde ayarlandı (orijinal mantık)
89
+ besleyiciCheckInterval = setInterval(async () => {
90
+ const found = await tryFindAndActivateBesleyici();
91
+ if (!found) {
92
+ // Besleyici yoksa önce skyblock sonra home komutlarını sırayla at
93
+ setTimeout(() => {
94
  bot.chat(`/skyblock`);
95
+ logger.info(`[${config["bot-account"]["username"]}] Bölgede besleyici minyon bulunamadı. Skyblock'a ışınlanma komutu kullanıldı.`);
96
+ }, 5000);
97
+
98
+ setTimeout(() => {
 
 
 
99
  bot.chat(`/home`);
100
+ logger.info(`[${config["bot-account"]["username"]}] Bölgede besleyici minyon bulunamadı. Home konumuna ışınlanma komutu kullanıldı.`);
101
+ }, 10000);
102
+
103
+ // 13s aralıkla tekrar kontrol et (orijinal kodda vardı)
104
+ setTimeout(() => {
105
+ const innerCheck = setInterval(async () => {
106
+ const found2 = await tryFindAndActivateBesleyici();
107
+ if (found2) {
108
+ clearInterval(innerCheck);
109
+ }
 
 
 
 
 
 
 
 
 
 
110
  }, 13000);
111
+ // Eğer uzun süre bulunamazsa innerCheck sonsuza gitmesin istersen burada bir timeout ekleyebilirsin.
112
+ }, 12000);
 
 
 
 
113
 
114
+ // /is home komutunu 16s sonra tekrar at (orijinal mantık)
115
+ setTimeout(() => {
116
+ bot.chat(`/is home`);
117
+ }, 16000);
118
+ }
119
+ }, 300000); // 5 dakika
120
 
121
+ // İlk küçük bekleme ile hemen bir deneme yap (orijinal kodda 2s içinde kontrol vardı)
122
+ setTimeout(() => {
123
+ tryFindAndActivateBesleyici();
124
+ }, 2000);
125
+ }
126
+
127
+ if (config.utils["chat-messages"].enabled) {
128
+ logger.info("Started chat-messages module");
129
+
130
+ let messages = config.utils["chat-messages"]["messages"];
131
+
132
+ if (config.utils["chat-messages"].repeat) {
133
+ let delay = config.utils["chat-messages"]["repeat-delay"];
134
+ let i = 0;
135
+
136
+ setInterval(() => {
137
+ bot.chat(`${messages[i]}`);
138
+
139
+ if (i + 1 === messages.length) {
140
+ i = 0;
141
+ } else i++;
142
+ }, delay * 1000);
143
+ } else {
144
+ messages.forEach((msg) => {
145
+ bot.chat(msg);
146
+ });
147
+ }
148
+ }
149
 
150
+ const pos = config.position;
151
 
152
+ if (config.position.enabled) {
153
+ logger.info(
154
+ `Starting moving to target location (${pos.x}, ${pos.y}, ${pos.z})`,
155
+ );
156
+ bot.pathfinder.setGoal(new GoalBlock(pos.x, pos.y, pos.z));
157
+ }
158
 
159
+ if (config.utils["anti-afk"].enabled) {
160
+ if (config.utils["anti-afk"].sneak) {
161
+ bot.setControlState("sneak", true);
162
+ }
163
 
164
+ if (config.utils["anti-afk"].jump) {
165
+ bot.setControlState("jump", true);
 
 
 
 
 
 
 
166
  }
167
 
168
+ if (config.utils["anti-afk"]["hit"].enabled) {
169
+ let delay = config.utils["anti-afk"]["hit"]["delay"];
170
+ let attackMobs = config.utils["anti-afk"]["hit"]["attack-mobs"];
171
+
172
+ setInterval(() => {
173
+ if (attackMobs) {
174
+ let entity = bot.nearestEntity(
175
+ (e) =>
176
+ e.type !== "object" &&
177
+ e.type !== "player" &&
178
+ e.type !== "global" &&
179
+ e.type !== "orb" &&
180
+ e.type !== "other",
181
+ );
182
+
183
+ if (entity) {
184
+ bot.attack(entity);
185
+ return;
186
+ }
187
+ }
188
+
189
+ bot.swingArm("right", true);
190
+ }, delay);
191
+ }
192
 
193
+ if (config.utils["anti-afk"].rotate) {
194
+ setInterval(() => {
195
+ bot.look(bot.entity.yaw + 1, bot.entity.pitch, true);
196
+ }, 100);
 
197
  }
198
 
199
+ if (config.utils["anti-afk"]["circle-walk"].enabled) {
200
+ let radius = config.utils["anti-afk"]["circle-walk"]["radius"];
201
+ setInterval(() => {
202
+ circleWalk(bot, radius);
203
+ }, 30000);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
  }
205
+ }
206
+ });
207
+
208
+ bot.on('windowOpen', function (window) {
209
+ try {
210
+ if (window.title && window.title.includes("ʙᴇsʟᴇʏiᴄi ᴍiɴʏᴏɴ ᴘᴀɴᴇʟi")) {
211
+ // Besleyici minyon paneli
212
+ const cooked = window.slots.filter(xx => xx?.name && xx?.name == "cooked_beef");
213
+ if (cooked.length > 0) {
214
+ // Orijinal nbt parsing mantığını korudum
215
+ const loreRaw = cooked[0]?.nbt?.value?.display?.value?.Lore?.value?.value[1];
216
+ const parsed = loreRaw ? JSON.parse(loreRaw) : null;
217
+ const extraText = parsed?.extra?.[0]?.text;
218
+ const minyoncani = Number((extraText || "").split("/")[0]);
219
+ if (!isNaN(minyoncani) && minyoncani <= 2) {
220
+ bot.clickWindow(36, 0, 0);
221
  logger.info(`[${config["bot-account"]["username"]}] Minyon canı ${minyoncani} olduğu için besleyici minyon beslendi.`);
222
  bot.closeWindow(window);
223
+ } else {
224
+ logger.info(`[${config["bot-account"]["username"]}] Minyon canı ${minyoncani} olduğu için beslemeye gerek duyulmadı.`);
 
 
 
225
  bot.closeWindow(window);
226
+ }
227
+ } else {
228
+ bot.closeWindow(window);
229
+ }
230
+ } else {
231
+ bot.closeWindow(window);
232
  }
233
+ } catch (err) {
234
+ logger.error(`windowOpen işlenirken hata: ${err.message}`);
235
+ try { bot.closeWindow(window); } catch {}
236
+ }
237
+ });
238
+
239
+ bot.on("chat", (username, message) => {
240
+ if (config.utils["chat-log"]) {
241
+ logger.info(`<${username}> ${message}`);
242
+ }
243
+ });
244
+
245
+ bot.on("goal_reached", () => {
246
+ if (config.position.enabled) {
247
+ logger.info(`Bot arrived to target location. ${bot.entity.position}`);
248
+ }
249
+ });
250
+
251
+ bot.on("death", () => {
252
+ logger.warn(
253
+ `Bot has been died and was respawned at ${bot.entity.position}`,
254
+ );
255
+ });
256
+
257
+ if (config.utils["auto-reconnect"]) {
258
+ bot.on("end", () => {
259
+ // Temizleme: interval'leri kapat
260
+ try {
261
+ if (besleyiciCheckInterval) clearInterval(besleyiciCheckInterval);
262
+ if (periodicIsHomeInterval) clearInterval(periodicIsHomeInterval);
263
+ } catch (e) {}
264
+ setTimeout(() => {
265
+ createBot();
266
+ }, config.utils["auto-reconnect-delay"]);
267
  });
268
+ }
269
+
270
+ bot.on("kicked", (reason) => {
271
+ let reasonText = "";
272
+ try {
273
+ const parsed = JSON.parse(reason);
274
+ reasonText = parsed.text || parsed.extra?.[0]?.text || "";
275
+ reasonText = reasonText?.replace(/§./g, "");
276
+ logger.warn(`Bot sunucudan atıldı. Sebep: ${reasonText}`);
277
+ } catch {
278
+ logger.warn(`Bot sunucudan atıldı. Sebep: Bulunamadı.`);
279
+ }
280
+ });
281
+
282
+ bot.on("error", (err) => logger.error(`${err.message}`));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283
  }
284
 
 
285
  function circleWalk(bot, radius) {
286
+ // Make bot walk in square with center in bot's current position without stopping
287
+ return new Promise(() => {
288
+ const pos = bot.entity.position;
289
+ const x = pos.x;
290
+ const y = pos.y;
291
+ const z = pos.z;
292
+
293
+ const points = [
294
+ [x + radius, y, z],
295
+ [x, y, z + radius],
296
+ [x - radius, y, z],
297
+ [x, y, z - radius],
298
+ ];
299
+
300
+ let i = 0;
301
+ setTimeout(() => {
302
+ if (i === points.length) i = 0;
303
+ const defaultMove = new Movements(bot);
304
+ bot.pathfinder.setMovements(defaultMove);
305
+ bot.pathfinder.setGoal(new GoalXZ(points[i][0], points[i][2]));
306
+ i++;
307
+ }, 1000);
308
+ });
309
  }
310
 
311
  createBot();
 
317
  app.get("/", (req, res) => res.send("Pinglendi")); //değiştirebilirsiniz.
318
 
319
  app.listen(
320
+ port,
321
+ () =>
322
+ console.log(`Bot bu adres üzerinde çalışıyor: http://localhost:${port}`), //port
323
  );
324
 
325
  process.on('uncaughtException', function (err) {
326
+ console.error("Hata aldım!");
327
+ console.log(err);
328
+ });