Update server.js
Browse files
server.js
CHANGED
|
@@ -5,6 +5,7 @@ const mineflayer = require('mineflayer');
|
|
| 5 |
const fetch = require('node-fetch');
|
| 6 |
const { parse } = require('csv-parse/sync');
|
| 7 |
const path = require('path');
|
|
|
|
| 8 |
|
| 9 |
const app = express();
|
| 10 |
const server = http.createServer(app);
|
|
@@ -18,6 +19,23 @@ const SHEET_URL = `https://docs.google.com/spreadsheets/d/${SHEET_ID}/export?for
|
|
| 18 |
const bots = new Map();
|
| 19 |
const serverBotMap = new Map(); // Track one bot per server
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
class BotManager {
|
| 22 |
constructor(botName, ip, port, version) {
|
| 23 |
this.botName = botName;
|
|
@@ -31,22 +49,28 @@ class BotManager {
|
|
| 31 |
this.lastReconnectTime = null;
|
| 32 |
this.isManualDisconnect = false;
|
| 33 |
this.startTime = Date.now();
|
| 34 |
-
this.
|
|
|
|
|
|
|
| 35 |
}
|
| 36 |
|
| 37 |
async connect() {
|
| 38 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
// Check if server already has a bot
|
| 40 |
-
const serverKey = `${this.ip}:${this.port}`;
|
| 41 |
const existingBot = serverBotMap.get(serverKey);
|
| 42 |
if (existingBot && existingBot !== this.botName) {
|
| 43 |
this.status = 'Server already has a bot';
|
|
|
|
| 44 |
return false;
|
| 45 |
}
|
| 46 |
|
| 47 |
this.status = 'Connecting...';
|
| 48 |
this.bot = mineflayer.createBot({
|
| 49 |
-
host: this.ip,
|
| 50 |
port: parseInt(this.port),
|
| 51 |
username: this.botName,
|
| 52 |
auth: 'offline',
|
|
@@ -55,13 +79,15 @@ class BotManager {
|
|
| 55 |
checkTimeoutInterval: 30000
|
| 56 |
});
|
| 57 |
|
| 58 |
-
// Register this bot for the server
|
| 59 |
serverBotMap.set(serverKey, this.botName);
|
|
|
|
| 60 |
|
| 61 |
this.bot.once('spawn', () => {
|
| 62 |
this.status = 'Connected';
|
|
|
|
| 63 |
this.disconnectTime = null;
|
| 64 |
-
console.log(`Bot ${this.botName} spawned on ${this.ip}:${this.port}`);
|
| 65 |
|
| 66 |
// Start AFK behavior
|
| 67 |
this.startAFK();
|
|
@@ -91,20 +117,33 @@ class BotManager {
|
|
| 91 |
} catch (error) {
|
| 92 |
console.error(`Failed to connect bot ${this.botName}:`, error);
|
| 93 |
this.status = 'Connection Failed';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
return false;
|
| 95 |
}
|
| 96 |
}
|
| 97 |
|
| 98 |
handleDisconnect() {
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
serverBotMap.
|
|
|
|
|
|
|
|
|
|
| 102 |
}
|
| 103 |
|
| 104 |
if (this.status !== 'Dead') {
|
| 105 |
this.status = 'Disconnected';
|
| 106 |
}
|
| 107 |
this.disconnectTime = Date.now();
|
|
|
|
| 108 |
this.bot = null;
|
| 109 |
|
| 110 |
console.log(`Bot ${this.botName} disconnected from ${this.ip}:${this.port}`);
|
|
@@ -168,18 +207,20 @@ class BotManager {
|
|
| 168 |
const timeElapsed = Date.now() - this.lastReconnectTime;
|
| 169 |
const hourInMs = 60 * 60 * 1000;
|
| 170 |
const timeRemaining = Math.max(0, hourInMs - timeElapsed);
|
| 171 |
-
return Math.ceil(timeRemaining / 1000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
}
|
| 173 |
|
| 174 |
getInfo() {
|
| 175 |
-
const uptime = this.status === 'Connected' ?
|
| 176 |
-
Math.floor((Date.now() - this.startTime) / 1000) : 0;
|
| 177 |
-
|
| 178 |
return {
|
| 179 |
botName: this.botName,
|
| 180 |
status: this.status,
|
| 181 |
deathCount: this.deathCount,
|
| 182 |
-
|
| 183 |
canReconnect: this.canReconnect(),
|
| 184 |
disconnectTime: this.disconnectTime,
|
| 185 |
timeUntilReconnect: this.getTimeUntilReconnect(),
|
|
@@ -230,7 +271,7 @@ async function updateBots() {
|
|
| 230 |
if (!bots.has(botName)) {
|
| 231 |
const botManager = new BotManager(botName, ip, port, version);
|
| 232 |
bots.set(botName, botManager);
|
| 233 |
-
|
| 234 |
} else {
|
| 235 |
// Mark existing bot as still in sheet
|
| 236 |
bots.get(botName).inSheet = true;
|
|
@@ -240,10 +281,13 @@ async function updateBots() {
|
|
| 240 |
// Remove bots that are no longer in the sheet
|
| 241 |
for (const [botName, botManager] of bots.entries()) {
|
| 242 |
if (!botManager.inSheet) {
|
|
|
|
| 243 |
botManager.disconnect();
|
| 244 |
bots.delete(botName);
|
| 245 |
}
|
| 246 |
}
|
|
|
|
|
|
|
| 247 |
}
|
| 248 |
|
| 249 |
// Socket.IO events
|
|
|
|
| 5 |
const fetch = require('node-fetch');
|
| 6 |
const { parse } = require('csv-parse/sync');
|
| 7 |
const path = require('path');
|
| 8 |
+
const dns = require('dns').promises;
|
| 9 |
|
| 10 |
const app = express();
|
| 11 |
const server = http.createServer(app);
|
|
|
|
| 19 |
const bots = new Map();
|
| 20 |
const serverBotMap = new Map(); // Track one bot per server
|
| 21 |
|
| 22 |
+
// Function to resolve domain to IP
|
| 23 |
+
async function resolveToIP(hostname) {
|
| 24 |
+
try {
|
| 25 |
+
// Check if it's already an IP address
|
| 26 |
+
if (/^(\d{1,3}\.){3}\d{1,3}$/.test(hostname)) {
|
| 27 |
+
return hostname;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
// Resolve domain to IP
|
| 31 |
+
const addresses = await dns.resolve4(hostname);
|
| 32 |
+
return addresses[0]; // Return first IP
|
| 33 |
+
} catch (error) {
|
| 34 |
+
console.error(`Failed to resolve ${hostname}:`, error.message);
|
| 35 |
+
return hostname; // Return original if resolution fails
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
class BotManager {
|
| 40 |
constructor(botName, ip, port, version) {
|
| 41 |
this.botName = botName;
|
|
|
|
| 49 |
this.lastReconnectTime = null;
|
| 50 |
this.isManualDisconnect = false;
|
| 51 |
this.startTime = Date.now();
|
| 52 |
+
this.connectedTime = null;
|
| 53 |
+
this.inSheet = true;
|
| 54 |
+
this.resolvedIP = null;
|
| 55 |
}
|
| 56 |
|
| 57 |
async connect() {
|
| 58 |
try {
|
| 59 |
+
// Resolve domain to IP for comparison
|
| 60 |
+
this.resolvedIP = await resolveToIP(this.ip);
|
| 61 |
+
const serverKey = `${this.resolvedIP}:${this.port}`;
|
| 62 |
+
|
| 63 |
// Check if server already has a bot
|
|
|
|
| 64 |
const existingBot = serverBotMap.get(serverKey);
|
| 65 |
if (existingBot && existingBot !== this.botName) {
|
| 66 |
this.status = 'Server already has a bot';
|
| 67 |
+
console.log(`Bot ${this.botName} blocked: Server ${serverKey} already has bot ${existingBot}`);
|
| 68 |
return false;
|
| 69 |
}
|
| 70 |
|
| 71 |
this.status = 'Connecting...';
|
| 72 |
this.bot = mineflayer.createBot({
|
| 73 |
+
host: this.ip, // Use original hostname/IP for connection
|
| 74 |
port: parseInt(this.port),
|
| 75 |
username: this.botName,
|
| 76 |
auth: 'offline',
|
|
|
|
| 79 |
checkTimeoutInterval: 30000
|
| 80 |
});
|
| 81 |
|
| 82 |
+
// Register this bot for the server using resolved IP
|
| 83 |
serverBotMap.set(serverKey, this.botName);
|
| 84 |
+
console.log(`Registering bot ${this.botName} for server ${serverKey}`);
|
| 85 |
|
| 86 |
this.bot.once('spawn', () => {
|
| 87 |
this.status = 'Connected';
|
| 88 |
+
this.connectedTime = Date.now();
|
| 89 |
this.disconnectTime = null;
|
| 90 |
+
console.log(`Bot ${this.botName} spawned on ${this.ip}:${this.port} (resolved: ${this.resolvedIP})`);
|
| 91 |
|
| 92 |
// Start AFK behavior
|
| 93 |
this.startAFK();
|
|
|
|
| 117 |
} catch (error) {
|
| 118 |
console.error(`Failed to connect bot ${this.botName}:`, error);
|
| 119 |
this.status = 'Connection Failed';
|
| 120 |
+
|
| 121 |
+
// Clean up server registration if connection failed
|
| 122 |
+
if (this.resolvedIP) {
|
| 123 |
+
const serverKey = `${this.resolvedIP}:${this.port}`;
|
| 124 |
+
if (serverBotMap.get(serverKey) === this.botName) {
|
| 125 |
+
serverBotMap.delete(serverKey);
|
| 126 |
+
}
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
return false;
|
| 130 |
}
|
| 131 |
}
|
| 132 |
|
| 133 |
handleDisconnect() {
|
| 134 |
+
if (this.resolvedIP) {
|
| 135 |
+
const serverKey = `${this.resolvedIP}:${this.port}`;
|
| 136 |
+
if (serverBotMap.get(serverKey) === this.botName) {
|
| 137 |
+
serverBotMap.delete(serverKey);
|
| 138 |
+
console.log(`Unregistering bot ${this.botName} from server ${serverKey}`);
|
| 139 |
+
}
|
| 140 |
}
|
| 141 |
|
| 142 |
if (this.status !== 'Dead') {
|
| 143 |
this.status = 'Disconnected';
|
| 144 |
}
|
| 145 |
this.disconnectTime = Date.now();
|
| 146 |
+
this.connectedTime = null;
|
| 147 |
this.bot = null;
|
| 148 |
|
| 149 |
console.log(`Bot ${this.botName} disconnected from ${this.ip}:${this.port}`);
|
|
|
|
| 207 |
const timeElapsed = Date.now() - this.lastReconnectTime;
|
| 208 |
const hourInMs = 60 * 60 * 1000;
|
| 209 |
const timeRemaining = Math.max(0, hourInMs - timeElapsed);
|
| 210 |
+
return Math.ceil(timeRemaining / 1000);
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
getConnectedDuration() {
|
| 214 |
+
if (!this.connectedTime || this.status !== 'Connected') return 0;
|
| 215 |
+
return Math.floor((Date.now() - this.connectedTime) / 1000);
|
| 216 |
}
|
| 217 |
|
| 218 |
getInfo() {
|
|
|
|
|
|
|
|
|
|
| 219 |
return {
|
| 220 |
botName: this.botName,
|
| 221 |
status: this.status,
|
| 222 |
deathCount: this.deathCount,
|
| 223 |
+
connectedDuration: this.getConnectedDuration(),
|
| 224 |
canReconnect: this.canReconnect(),
|
| 225 |
disconnectTime: this.disconnectTime,
|
| 226 |
timeUntilReconnect: this.getTimeUntilReconnect(),
|
|
|
|
| 271 |
if (!bots.has(botName)) {
|
| 272 |
const botManager = new BotManager(botName, ip, port, version);
|
| 273 |
bots.set(botName, botManager);
|
| 274 |
+
console.log(`Added new bot from sheet: ${botName} for ${ip}:${port}`);
|
| 275 |
} else {
|
| 276 |
// Mark existing bot as still in sheet
|
| 277 |
bots.get(botName).inSheet = true;
|
|
|
|
| 281 |
// Remove bots that are no longer in the sheet
|
| 282 |
for (const [botName, botManager] of bots.entries()) {
|
| 283 |
if (!botManager.inSheet) {
|
| 284 |
+
console.log(`Removing bot ${botName} - no longer in sheet`);
|
| 285 |
botManager.disconnect();
|
| 286 |
bots.delete(botName);
|
| 287 |
}
|
| 288 |
}
|
| 289 |
+
|
| 290 |
+
console.log(`Total bots: ${bots.size}, Active servers: ${serverBotMap.size}`);
|
| 291 |
}
|
| 292 |
|
| 293 |
// Socket.IO events
|