File size: 10,798 Bytes
8bb61b2 3b2ae1c 8bb61b2 3b2ae1c 8bb61b2 6b93999 8bb61b2 3b2ae1c 8bb61b2 3b2ae1c 8bb61b2 3b2ae1c 8bb61b2 3b2ae1c 8bb61b2 3b2ae1c 8bb61b2 3b2ae1c 8bb61b2 3b2ae1c 8bb61b2 3b2ae1c 8bb61b2 6b93999 8bb61b2 6b93999 8bb61b2 3b2ae1c 8bb61b2 3b2ae1c 8bb61b2 6b93999 8bb61b2 3b2ae1c 8bb61b2 6b93999 8bb61b2 6b93999 8bb61b2 6b93999 8bb61b2 6b93999 8bb61b2 6b93999 8bb61b2 6b93999 3b2ae1c 6b93999 8bb61b2 3b2ae1c 8bb61b2 6b93999 8bb61b2 6b93999 8bb61b2 3b2ae1c 6b93999 8bb61b2 6b93999 3b2ae1c 8bb61b2 3b2ae1c 8bb61b2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const mineflayer = require('mineflayer');
const fetch = require('node-fetch');
const { parse } = require('csv-parse/sync');
const path = require('path');
const dns = require('dns').promises;
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
// Google Sheets configuration
const SHEET_ID = '109roJQr-Y4YCLTkCqaK6iwShC-Dr2Jb-hB0qE2phNqQ';
const SHEET_URL = `https://docs.google.com/spreadsheets/d/${SHEET_ID}/export?format=csv`;
// Bot management
const bots = new Map();
const serverBotMap = new Map(); // Track one bot per server
// Function to resolve domain to IP
async function resolveToIP(hostname) {
try {
// Check if it's already an IP address
if (/^(\d{1,3}\.){3}\d{1,3}$/.test(hostname)) {
return hostname;
}
// Resolve domain to IP
const addresses = await dns.resolve4(hostname);
return addresses[0]; // Return first IP
} catch (error) {
console.error(`Failed to resolve ${hostname}:`, error.message);
return hostname; // Return original if resolution fails
}
}
class BotManager {
constructor(botName, ip, port, version) {
this.botName = botName;
this.ip = ip;
this.port = port;
this.version = version || '1.20.1';
this.bot = null;
this.status = 'Disconnected';
this.deathCount = 0;
this.disconnectTime = null;
this.lastReconnectTime = null;
this.isManualDisconnect = false;
this.startTime = Date.now();
this.connectedTime = null;
this.inSheet = true;
this.resolvedIP = null;
}
async connect() {
try {
// Resolve domain to IP for comparison
this.resolvedIP = await resolveToIP(this.ip);
const serverKey = `${this.resolvedIP}:${this.port}`;
// Check if server already has a bot
const existingBot = serverBotMap.get(serverKey);
if (existingBot && existingBot !== this.botName) {
this.status = 'Server already has a bot';
console.log(`Bot ${this.botName} blocked: Server ${serverKey} already has bot ${existingBot}`);
return false;
}
this.status = 'Connecting...';
this.bot = mineflayer.createBot({
host: this.ip, // Use original hostname/IP for connection
port: parseInt(this.port),
username: this.botName,
auth: 'offline',
version: this.version,
hideErrors: true,
checkTimeoutInterval: 30000
});
// Register this bot for the server using resolved IP
serverBotMap.set(serverKey, this.botName);
console.log(`Registering bot ${this.botName} for server ${serverKey}`);
this.bot.once('spawn', () => {
this.status = 'Connected';
this.connectedTime = Date.now();
this.disconnectTime = null;
console.log(`Bot ${this.botName} spawned on ${this.ip}:${this.port} (resolved: ${this.resolvedIP})`);
// Start AFK behavior
this.startAFK();
});
this.bot.on('death', () => {
this.deathCount++;
this.status = 'Dead';
console.log(`Bot ${this.botName} died. Total deaths: ${this.deathCount}`);
this.handleDisconnect();
});
this.bot.on('kicked', (reason) => {
console.log(`Bot ${this.botName} was kicked: ${reason}`);
this.handleDisconnect();
});
this.bot.on('error', (err) => {
console.error(`Bot ${this.botName} error:`, err.message);
});
this.bot.on('end', () => {
this.handleDisconnect();
});
return true;
} catch (error) {
console.error(`Failed to connect bot ${this.botName}:`, error);
this.status = 'Connection Failed';
// Clean up server registration if connection failed
if (this.resolvedIP) {
const serverKey = `${this.resolvedIP}:${this.port}`;
if (serverBotMap.get(serverKey) === this.botName) {
serverBotMap.delete(serverKey);
}
}
return false;
}
}
handleDisconnect() {
if (this.resolvedIP) {
const serverKey = `${this.resolvedIP}:${this.port}`;
if (serverBotMap.get(serverKey) === this.botName) {
serverBotMap.delete(serverKey);
console.log(`Unregistering bot ${this.botName} from server ${serverKey}`);
}
}
if (this.status !== 'Dead') {
this.status = 'Disconnected';
}
this.disconnectTime = Date.now();
this.connectedTime = null;
this.bot = null;
console.log(`Bot ${this.botName} disconnected from ${this.ip}:${this.port}`);
}
disconnect() {
this.isManualDisconnect = true;
if (this.bot) {
this.bot.quit();
}
this.handleDisconnect();
}
startAFK() {
if (!this.bot) return;
// Simple AFK movement
let direction = 1;
const afkInterval = setInterval(() => {
if (!this.bot || this.status !== 'Connected') {
clearInterval(afkInterval);
return;
}
// Walk forward and backward
this.bot.setControlState('forward', direction > 0);
this.bot.setControlState('back', direction < 0);
setTimeout(() => {
if (this.bot) {
this.bot.clearControlStates();
}
}, 1000);
direction *= -1;
}, 5000);
}
canReconnect() {
if (this.status === 'Connected' || this.status === 'Connecting...') return false;
if (!this.inSheet) return false;
if (!this.lastReconnectTime) return true;
const hourAgo = Date.now() - (60 * 60 * 1000);
return this.lastReconnectTime < hourAgo;
}
async reconnect() {
if (!this.canReconnect()) {
return false;
}
this.lastReconnectTime = Date.now();
this.isManualDisconnect = false;
return await this.connect();
}
getTimeUntilReconnect() {
if (!this.lastReconnectTime) return 0;
const timeElapsed = Date.now() - this.lastReconnectTime;
const hourInMs = 60 * 60 * 1000;
const timeRemaining = Math.max(0, hourInMs - timeElapsed);
return Math.ceil(timeRemaining / 1000);
}
getConnectedDuration() {
if (!this.connectedTime || this.status !== 'Connected') return 0;
return Math.floor((Date.now() - this.connectedTime) / 1000);
}
getInfo() {
return {
botName: this.botName,
status: this.status,
deathCount: this.deathCount,
connectedDuration: this.getConnectedDuration(),
canReconnect: this.canReconnect(),
disconnectTime: this.disconnectTime,
timeUntilReconnect: this.getTimeUntilReconnect(),
inSheet: this.inSheet
};
}
}
// Fetch and parse Google Sheets data
async function fetchSheetData() {
try {
const response = await fetch(SHEET_URL);
const csvText = await response.text();
const records = parse(csvText, {
columns: true,
skip_empty_lines: true
});
return records;
} catch (error) {
console.error('Error fetching sheet data:', error);
return [];
}
}
// Update bots based on sheet data
async function updateBots() {
const sheetData = await fetchSheetData();
const activeBots = new Set();
// Mark all existing bots as not in sheet initially
for (const [botName, botManager] of bots.entries()) {
botManager.inSheet = false;
}
for (const row of sheetData) {
const botName = row['BOT NAME']?.trim();
const ip = row['IP']?.trim();
const port = row['PORT']?.trim();
const version = row['Version']?.trim() || '1.20.1';
if (!botName || !ip || !port) continue;
activeBots.add(botName);
// Add new bot if it doesn't exist
if (!bots.has(botName)) {
const botManager = new BotManager(botName, ip, port, version);
bots.set(botName, botManager);
console.log(`Added new bot from sheet: ${botName} for ${ip}:${port}`);
} else {
// Mark existing bot as still in sheet
bots.get(botName).inSheet = true;
}
}
// Remove bots that are no longer in the sheet
for (const [botName, botManager] of bots.entries()) {
if (!botManager.inSheet) {
console.log(`Removing bot ${botName} - no longer in sheet`);
botManager.disconnect();
bots.delete(botName);
}
}
console.log(`Total bots: ${bots.size}, Active servers: ${serverBotMap.size}`);
}
// Socket.IO events
io.on('connection', (socket) => {
console.log('Client connected');
// Send initial bot data
const sendBotData = () => {
const botData = Array.from(bots.values()).map(bot => bot.getInfo());
socket.emit('botUpdate', botData);
};
sendBotData();
const updateInterval = setInterval(sendBotData, 2000);
socket.on('reconnectBot', async (botName) => {
const botManager = bots.get(botName);
if (botManager) {
const success = await botManager.reconnect();
socket.emit('reconnectResult', { botName, success });
}
});
socket.on('refreshSheet', async () => {
await updateBots();
sendBotData();
});
socket.on('disconnect', () => {
clearInterval(updateInterval);
console.log('Client disconnected');
});
});
// Serve static files
app.use(express.static(__dirname));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// Start server
const PORT = process.env.PORT || 7860;
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
// Initial load and periodic updates
updateBots();
setInterval(updateBots, 30000); // Check sheet every 30 seconds |