Download sleeping.js from Saedmh/Saed: direct link, hf CLI and curl.
- Browser
- Download file 5.29 kB
-
https://huggingface.co/spaces/Saedmh/Saed/resolve/main/sleeping.js
- Command line
-
hf download hf://spaces/Saedmh/Saed/sleeping.js
-
curl -L -o sleeping.js https://huggingface.co/spaces/Saedmh/Saed/resolve/main/sleeping.js
5.29 kB
| ; | |
| /** | |
| * Sleeping System — نظام النوم والراحة | |
| * ينام بالليل، يدور على سرير، يستيقظ بالصبح | |
| */ | |
| const { EventEmitter } = require('events'); | |
| const sleep = ms => new Promise(r => setTimeout(r, Math.max(0, ms))); | |
| const rand = (a, b) => Math.floor(Math.random() * (b - a + 1)) + a; | |
| const chance = pct => Math.random() * 100 < pct; | |
| const BED_BLOCKS = [ | |
| 'white_bed','orange_bed','magenta_bed','light_blue_bed', | |
| 'yellow_bed','lime_bed','pink_bed','gray_bed', | |
| 'light_gray_bed','cyan_bed','purple_bed','blue_bed', | |
| 'brown_bed','green_bed','red_bed','black_bed', | |
| ]; | |
| class SleepingSystem extends EventEmitter { | |
| constructor(bot, config = {}) { | |
| super(); | |
| this.bot = bot; | |
| this.config = { | |
| nightCheckInterval: 5000, | |
| sleepHour: 13000, | |
| wakeHour: 1000, | |
| ...config, | |
| }; | |
| this._active = false; | |
| this._timers = []; | |
| this._sleeping = false; | |
| this._slept = 0; | |
| this._lastSleep = null; | |
| this._log('INIT', 'نظام النوم جاهز'); | |
| } | |
| _log(tag, msg) { | |
| const ts = new Date().toISOString().replace('T', ' ').slice(0, 19); | |
| console.log(`[${ts}] [SLEEP] ${tag} ${msg}`); | |
| } | |
| start() { | |
| if (this._active) return; | |
| this._active = true; | |
| this._log('START', 'بدء نظام النوم'); | |
| this._addTimer(() => this._checkTime(), this.config.nightCheckInterval); | |
| } | |
| stop() { | |
| this._active = false; | |
| this._sleeping = false; | |
| this._timers.forEach(t => { try { clearInterval(t); clearTimeout(t); } catch(_) {} }); | |
| this._timers = []; | |
| this._log('STOP', 'إيقاف نظام النوم'); | |
| } | |
| _addTimer(fn, interval) { | |
| const t = setInterval(() => { if (this._active) fn(); }, interval); | |
| this._timers.push(t); | |
| } | |
| // ═══ فحص الوقت ═══ | |
| _checkTime() { | |
| if (!this.bot?.entity || this._sleeping) return; | |
| const time = this.bot.time?.age; | |
| if (time === undefined) return; | |
| const dayTime = time % 24000; | |
| const isNight = dayTime > this.config.sleepHour || dayTime < this.config.wakeHour; | |
| if (isNight && !this._sleeping) { | |
| this._log('🌙', 'الليل — يدور على سرير'); | |
| this._trySleep(); | |
| } | |
| } | |
| // ═══ محاولة النوم ═══ | |
| async _trySleep() { | |
| if (this._sleeping || !this.bot?.entity) return; | |
| // دور على سرير قريب | |
| const bed = this._findBed(); | |
| if (!bed) { | |
| this._log('⚠️', 'لا سرير متاح — ينام على الأرض'); | |
| this._sleepOnGround(); | |
| return; | |
| } | |
| try { | |
| // امشي للسرير | |
| await this._walkTo(bed.position); | |
| // نام | |
| await this.bot.sleep(bed); | |
| this._sleeping = true; | |
| this._slept++; | |
| this._lastSleep = Date.now(); | |
| this._log('😴', `نام (إجمالي: ${this._slept})`); | |
| this.emit('sleeping'); | |
| // استنى لحد ما الصبح | |
| await this._waitForMorning(); | |
| } catch(e) { | |
| this._log('❌', `ما قدر ينام: ${e.message}`); | |
| this._sleepOnGround(); | |
| } | |
| } | |
| // ═══ دور على سرير ═══ | |
| _findBed() { | |
| if (!this.bot) return null; | |
| const bedBlock = this.bot.findBlock({ | |
| matching: (block) => BED_BLOCKS.includes(block.name), | |
| maxDistance: 16, | |
| count: 1, | |
| }); | |
| return bedBlock; | |
| } | |
| // ═══ نوم على الأرض ═══ | |
| _sleepOnGround() { | |
| if (!this.bot?.entity) return; | |
| this._sleeping = true; | |
| this._slept++; | |
| this._lastSleep = Date.now(); | |
| this._log('😴', 'نام على الأرض'); | |
| this.emit('sleeping'); | |
| // نام لمدة 7 ثواني (بديل للنوم الحقيقي) | |
| setTimeout(() => { | |
| this._sleeping = false; | |
| this._log('☀️', 'استيقظ'); | |
| this.emit('wakeup'); | |
| }, 7000); | |
| } | |
| // ═══ انتظار الصبح ═══ | |
| async _waitForMorning() { | |
| while (this._active && this._sleeping && this.bot?.entity) { | |
| const time = this.bot.time?.age; | |
| if (time === undefined) break; | |
| const dayTime = time % 24000; | |
| if (dayTime >= this.config.wakeHour && dayTime < this.config.sleepHour) { | |
| this._sleeping = false; | |
| this._log('☀️', 'استيقظ'); | |
| this.emit('wakeup'); | |
| break; | |
| } | |
| await sleep(1000); | |
| } | |
| } | |
| // ═══ استيقظ ═══ | |
| async wakeUp() { | |
| if (!this._sleeping || !this.bot) return; | |
| try { | |
| await this.bot.wakeUp(); | |
| this._sleeping = false; | |
| this._log('☀️', 'استيقظ يدوياً'); | |
| this.emit('wakeup'); | |
| } catch(_) {} | |
| } | |
| async _walkTo(position) { | |
| if (!this.bot?.entity) return; | |
| const yaw = Math.atan2( | |
| position.z - this.bot.entity.position.z, | |
| position.x - this.bot.entity.position.x | |
| ); | |
| this.bot.look(yaw, 0, false); | |
| this.bot.setControlState('forward', true); | |
| await sleep(rand(300, 800)); | |
| this.bot.setControlState('forward', false); | |
| } | |
| getStats() { | |
| return { | |
| slept: this._slept, | |
| lastSleep: this._lastSleep ? new Date(this._lastSleep).toISOString() : null, | |
| sleeping: this._sleeping, | |
| active: this._active, | |
| }; | |
| } | |
| destroy() { | |
| this.stop(); | |
| this.removeAllListeners(); | |
| } | |
| } | |
| module.exports = { SleepingSystem, BED_BLOCKS }; | |