Spaces:
Sleeping
Sleeping
| import { createClient } from 'bedrock-protocol' | |
| import { createServer } from 'http' | |
| const SERVER_HOST = 'Super7arbcraftnews.aternos.me' | |
| const SERVER_PORT = 57611 | |
| const BOT_USERNAME = '24hour' | |
| const RECONNECT_DELAY_MS = 15_000 | |
| let reconnectTimer = null | |
| let isShuttingDown = false | |
| let currentClient = null | |
| let reconnectCount = 0 | |
| let status = 'connecting' | |
| let lastError = null | |
| let connectedAt = null | |
| function log(msg, extra = {}) { | |
| const time = new Date().toISOString() | |
| console.log(JSON.stringify({ time, msg, ...extra })) | |
| } | |
| function connect() { | |
| if (isShuttingDown) return | |
| reconnectCount++ | |
| status = 'connecting' | |
| log('🔄 البوت يحاول الاتصال...', { | |
| host: SERVER_HOST, | |
| port: SERVER_PORT, | |
| username: BOT_USERNAME, | |
| attempt: reconnectCount | |
| }) | |
| let client | |
| try { | |
| client = createClient({ | |
| host: SERVER_HOST, | |
| port: SERVER_PORT, | |
| username: BOT_USERNAME, | |
| offline: true, | |
| skipPing: false, | |
| connectTimeout: 30_000, | |
| }) | |
| } catch (err) { | |
| log('❌ فشل إنشاء الاتصال', { error: err.message }) | |
| lastError = err.message | |
| status = 'error' | |
| scheduleReconnect() | |
| return | |
| } | |
| currentClient = client | |
| client.on('error', (err) => { | |
| log('⚠️ خطأ — سيتم إعادة المحاولة', { error: err.message }) | |
| lastError = err.message | |
| status = 'error' | |
| currentClient = null | |
| scheduleReconnect() | |
| }) | |
| client.on('spawn', () => { | |
| reconnectCount = 0 | |
| status = 'connected' | |
| connectedAt = new Date().toISOString() | |
| lastError = null | |
| log('✅ البوت دخل السيرفر بنجاح!', { username: BOT_USERNAME }) | |
| }) | |
| client.on('text', (packet) => { | |
| log('📩 رسالة', { from: packet.source_name, msg: packet.message }) | |
| }) | |
| client.on('disconnect', (reason) => { | |
| log('📴 انفصل — سيتم إعادة الاتصال', { reason: reason?.message ?? 'unknown' }) | |
| status = 'disconnected' | |
| currentClient = null | |
| scheduleReconnect() | |
| }) | |
| client.on('close', () => { | |
| log('🔌 الاتصال أُغلق — سيتم إعادة الاتصال') | |
| status = 'disconnected' | |
| currentClient = null | |
| scheduleReconnect() | |
| }) | |
| } | |
| function scheduleReconnect() { | |
| if (isShuttingDown) return | |
| if (reconnectTimer) return | |
| log(`⏳ إعادة الاتصال خلال ${RECONNECT_DELAY_MS / 1000} ثوانٍ...`) | |
| reconnectTimer = setTimeout(() => { | |
| reconnectTimer = null | |
| connect() | |
| }, RECONNECT_DELAY_MS) | |
| } | |
| process.on('uncaughtException', (err) => { | |
| if (err.message?.includes('timed out') || err.message?.includes('ETIMEDOUT')) { | |
| log('⏰ انتهت مهلة الاتصال — سيتم إعادة المحاولة', { error: err.message }) | |
| status = 'timeout' | |
| lastError = err.message | |
| currentClient = null | |
| scheduleReconnect() | |
| } else { | |
| log('💥 خطأ غير معالَج', { error: err.message, stack: err.stack }) | |
| } | |
| }) | |
| process.on('unhandledRejection', (reason) => { | |
| log('🚫 promise مرفوضة', { reason: String(reason) }) | |
| }) | |
| const httpServer = createServer((req, res) => { | |
| const uptime = Math.floor(process.uptime()) | |
| const html = ` | |
| <!DOCTYPE html> | |
| <html dir="rtl" lang="ar"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta http-equiv="refresh" content="10"> | |
| <title>بوت 24hour - لوحة التحكم</title> | |
| <style> | |
| body { font-family: Arial, sans-serif; background: #0f0f0f; color: #e0e0e0; display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; } | |
| .card { background: #1a1a2e; border-radius: 16px; padding: 40px; max-width: 500px; width: 90%; box-shadow: 0 8px 32px rgba(0,0,0,0.4); } | |
| h1 { color: #00d4ff; margin: 0 0 24px; font-size: 1.8rem; } | |
| .status { display: inline-block; padding: 6px 16px; border-radius: 20px; font-weight: bold; margin-bottom: 24px; } | |
| .connected { background: #0d4f0d; color: #4cff4c; } | |
| .connecting { background: #4f3d00; color: #ffcc00; } | |
| .disconnected, .error, .timeout { background: #4f0d0d; color: #ff4c4c; } | |
| .info { background: #111128; border-radius: 8px; padding: 16px; margin-bottom: 16px; } | |
| .info p { margin: 6px 0; font-size: 0.95rem; } | |
| .label { color: #888; } | |
| .value { color: #fff; font-weight: bold; } | |
| .footer { color: #555; font-size: 0.8rem; margin-top: 24px; text-align: center; } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="card"> | |
| <h1>🤖 بوت Minecraft 24hour</h1> | |
| <div class="status ${status}">${ | |
| status === 'connected' ? '✅ متصل' : | |
| status === 'connecting' ? '⏳ يتصل...' : | |
| status === 'timeout' ? '⏰ انتهت المهلة' : | |
| status === 'disconnected' ? '📴 منفصل' : '❌ خطأ' | |
| }</div> | |
| <div class="info"> | |
| <p><span class="label">السيرفر: </span><span class="value">${SERVER_HOST}:${SERVER_PORT}</span></p> | |
| <p><span class="label">اسم البوت: </span><span class="value">${BOT_USERNAME}</span></p> | |
| <p><span class="label">عدد محاولات الاتصال: </span><span class="value">${reconnectCount}</span></p> | |
| <p><span class="label">وقت التشغيل: </span><span class="value">${Math.floor(uptime / 3600)}س ${Math.floor((uptime % 3600) / 60)}د ${uptime % 60}ث</span></p> | |
| ${connectedAt ? `<p><span class="label">آخر اتصال: </span><span class="value">${connectedAt}</span></p>` : ''} | |
| ${lastError ? `<p><span class="label">آخر خطأ: </span><span class="value" style="color:#ff6b6b">${lastError}</span></p>` : ''} | |
| </div> | |
| <div class="footer">يتم تحديث الصفحة كل 10 ثوانٍ تلقائياً</div> | |
| </div> | |
| </body> | |
| </html>` | |
| res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' }) | |
| res.end(html) | |
| }) | |
| httpServer.listen(7860, '0.0.0.0', () => { | |
| log('🌐 لوحة التحكم تعمل على المنفذ 7860') | |
| connect() | |
| }) | |