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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { createClient } from 'bedrock-protocol'
|
| 2 |
+
import { createServer } from 'http'
|
| 3 |
+
|
| 4 |
+
const SERVER_HOST = 'Super7arbcraftnews.aternos.me'
|
| 5 |
+
const SERVER_PORT = 57611
|
| 6 |
+
const BOT_USERNAME = '24hour'
|
| 7 |
+
const RECONNECT_DELAY_MS = 15_000
|
| 8 |
+
|
| 9 |
+
let reconnectTimer = null
|
| 10 |
+
let isShuttingDown = false
|
| 11 |
+
let currentClient = null
|
| 12 |
+
let reconnectCount = 0
|
| 13 |
+
let status = 'connecting'
|
| 14 |
+
let lastError = null
|
| 15 |
+
let connectedAt = null
|
| 16 |
+
|
| 17 |
+
function log(msg, extra = {}) {
|
| 18 |
+
const time = new Date().toISOString()
|
| 19 |
+
console.log(JSON.stringify({ time, msg, ...extra }))
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
function connect() {
|
| 23 |
+
if (isShuttingDown) return
|
| 24 |
+
|
| 25 |
+
reconnectCount++
|
| 26 |
+
status = 'connecting'
|
| 27 |
+
log('🔄 البوت يحاول الاتصال...', {
|
| 28 |
+
host: SERVER_HOST,
|
| 29 |
+
port: SERVER_PORT,
|
| 30 |
+
username: BOT_USERNAME,
|
| 31 |
+
attempt: reconnectCount
|
| 32 |
+
})
|
| 33 |
+
|
| 34 |
+
let client
|
| 35 |
+
try {
|
| 36 |
+
client = createClient({
|
| 37 |
+
host: SERVER_HOST,
|
| 38 |
+
port: SERVER_PORT,
|
| 39 |
+
username: BOT_USERNAME,
|
| 40 |
+
offline: true,
|
| 41 |
+
skipPing: false,
|
| 42 |
+
connectTimeout: 30_000,
|
| 43 |
+
})
|
| 44 |
+
} catch (err) {
|
| 45 |
+
log('❌ فشل إنشاء الاتصال', { error: err.message })
|
| 46 |
+
lastError = err.message
|
| 47 |
+
status = 'error'
|
| 48 |
+
scheduleReconnect()
|
| 49 |
+
return
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
currentClient = client
|
| 53 |
+
|
| 54 |
+
client.on('error', (err) => {
|
| 55 |
+
log('⚠️ خطأ — سيتم إعادة المحاولة', { error: err.message })
|
| 56 |
+
lastError = err.message
|
| 57 |
+
status = 'error'
|
| 58 |
+
currentClient = null
|
| 59 |
+
scheduleReconnect()
|
| 60 |
+
})
|
| 61 |
+
|
| 62 |
+
client.on('spawn', () => {
|
| 63 |
+
reconnectCount = 0
|
| 64 |
+
status = 'connected'
|
| 65 |
+
connectedAt = new Date().toISOString()
|
| 66 |
+
lastError = null
|
| 67 |
+
log('✅ البوت دخل السيرفر بنجاح!', { username: BOT_USERNAME })
|
| 68 |
+
})
|
| 69 |
+
|
| 70 |
+
client.on('text', (packet) => {
|
| 71 |
+
log('📩 رسالة', { from: packet.source_name, msg: packet.message })
|
| 72 |
+
})
|
| 73 |
+
|
| 74 |
+
client.on('disconnect', (reason) => {
|
| 75 |
+
log('📴 انفصل — سيتم إعادة الاتصال', { reason: reason?.message ?? 'unknown' })
|
| 76 |
+
status = 'disconnected'
|
| 77 |
+
currentClient = null
|
| 78 |
+
scheduleReconnect()
|
| 79 |
+
})
|
| 80 |
+
|
| 81 |
+
client.on('close', () => {
|
| 82 |
+
log('🔌 الاتصال أُغلق — سيتم إعادة الاتصال')
|
| 83 |
+
status = 'disconnected'
|
| 84 |
+
currentClient = null
|
| 85 |
+
scheduleReconnect()
|
| 86 |
+
})
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
function scheduleReconnect() {
|
| 90 |
+
if (isShuttingDown) return
|
| 91 |
+
if (reconnectTimer) return
|
| 92 |
+
|
| 93 |
+
log(`⏳ إعادة الاتصال خلال ${RECONNECT_DELAY_MS / 1000} ثوانٍ...`)
|
| 94 |
+
reconnectTimer = setTimeout(() => {
|
| 95 |
+
reconnectTimer = null
|
| 96 |
+
connect()
|
| 97 |
+
}, RECONNECT_DELAY_MS)
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
process.on('uncaughtException', (err) => {
|
| 101 |
+
if (err.message?.includes('timed out') || err.message?.includes('ETIMEDOUT')) {
|
| 102 |
+
log('⏰ انتهت مهلة الاتصال — سيتم إعادة المحاولة', { error: err.message })
|
| 103 |
+
status = 'timeout'
|
| 104 |
+
lastError = err.message
|
| 105 |
+
currentClient = null
|
| 106 |
+
scheduleReconnect()
|
| 107 |
+
} else {
|
| 108 |
+
log('💥 خطأ غير معالَج', { error: err.message, stack: err.stack })
|
| 109 |
+
}
|
| 110 |
+
})
|
| 111 |
+
|
| 112 |
+
process.on('unhandledRejection', (reason) => {
|
| 113 |
+
log('🚫 promise مرفوضة', { reason: String(reason) })
|
| 114 |
+
})
|
| 115 |
+
|
| 116 |
+
const httpServer = createServer((req, res) => {
|
| 117 |
+
const uptime = Math.floor(process.uptime())
|
| 118 |
+
const html = `
|
| 119 |
+
<!DOCTYPE html>
|
| 120 |
+
<html dir="rtl" lang="ar">
|
| 121 |
+
<head>
|
| 122 |
+
<meta charset="UTF-8">
|
| 123 |
+
<meta http-equiv="refresh" content="10">
|
| 124 |
+
<title>بوت 24hour - لوحة التحكم</title>
|
| 125 |
+
<style>
|
| 126 |
+
body { font-family: Arial, sans-serif; background: #0f0f0f; color: #e0e0e0; display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; }
|
| 127 |
+
.card { background: #1a1a2e; border-radius: 16px; padding: 40px; max-width: 500px; width: 90%; box-shadow: 0 8px 32px rgba(0,0,0,0.4); }
|
| 128 |
+
h1 { color: #00d4ff; margin: 0 0 24px; font-size: 1.8rem; }
|
| 129 |
+
.status { display: inline-block; padding: 6px 16px; border-radius: 20px; font-weight: bold; margin-bottom: 24px; }
|
| 130 |
+
.connected { background: #0d4f0d; color: #4cff4c; }
|
| 131 |
+
.connecting { background: #4f3d00; color: #ffcc00; }
|
| 132 |
+
.disconnected, .error, .timeout { background: #4f0d0d; color: #ff4c4c; }
|
| 133 |
+
.info { background: #111128; border-radius: 8px; padding: 16px; margin-bottom: 16px; }
|
| 134 |
+
.info p { margin: 6px 0; font-size: 0.95rem; }
|
| 135 |
+
.label { color: #888; }
|
| 136 |
+
.value { color: #fff; font-weight: bold; }
|
| 137 |
+
.footer { color: #555; font-size: 0.8rem; margin-top: 24px; text-align: center; }
|
| 138 |
+
</style>
|
| 139 |
+
</head>
|
| 140 |
+
<body>
|
| 141 |
+
<div class="card">
|
| 142 |
+
<h1>🤖 بوت Minecraft 24hour</h1>
|
| 143 |
+
<div class="status ${status}">${
|
| 144 |
+
status === 'connected' ? '✅ متصل' :
|
| 145 |
+
status === 'connecting' ? '⏳ يتصل...' :
|
| 146 |
+
status === 'timeout' ? '⏰ انتهت المهلة' :
|
| 147 |
+
status === 'disconnected' ? '📴 منفصل' : '❌ خطأ'
|
| 148 |
+
}</div>
|
| 149 |
+
<div class="info">
|
| 150 |
+
<p><span class="label">السيرفر: </span><span class="value">${SERVER_HOST}:${SERVER_PORT}</span></p>
|
| 151 |
+
<p><span class="label">اسم البوت: </span><span class="value">${BOT_USERNAME}</span></p>
|
| 152 |
+
<p><span class="label">عدد محاولات الاتصال: </span><span class="value">${reconnectCount}</span></p>
|
| 153 |
+
<p><span class="label">وقت التشغيل: </span><span class="value">${Math.floor(uptime / 3600)}س ${Math.floor((uptime % 3600) / 60)}د ${uptime % 60}ث</span></p>
|
| 154 |
+
${connectedAt ? `<p><span class="label">آخر اتصال: </span><span class="value">${connectedAt}</span></p>` : ''}
|
| 155 |
+
${lastError ? `<p><span class="label">آخر خطأ: </span><span class="value" style="color:#ff6b6b">${lastError}</span></p>` : ''}
|
| 156 |
+
</div>
|
| 157 |
+
<div class="footer">يتم تحديث الصفحة كل 10 ثوانٍ تلقائياً</div>
|
| 158 |
+
</div>
|
| 159 |
+
</body>
|
| 160 |
+
</html>`
|
| 161 |
+
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' })
|
| 162 |
+
res.end(html)
|
| 163 |
+
})
|
| 164 |
+
|
| 165 |
+
httpServer.listen(7860, '0.0.0.0', () => {
|
| 166 |
+
log('🌐 لوحة التحكم تعمل على المنفذ 7860')
|
| 167 |
+
connect()
|
| 168 |
+
})
|