"""تحميل ملفات المحتوى (سطر = رد) واختيار عشوائي مع إعادة تحميل تلقائي عند تغيير الملف.""" import random from config import DATA, GAME_FILES, TAFKIK_FILE _cache = {} # filename -> (mtime_ns, size, lines) def lines(filename): p = DATA / filename if not p.exists(): return [] st = p.stat() sig = (st.st_mtime_ns, st.st_size) cached = _cache.get(filename) if cached and cached[0] == sig: return cached[1] items = [l.strip() for l in p.read_text(encoding="utf-8", errors="ignore").splitlines() if l.strip()] _cache[filename] = (sig, items) return items def clear_cache(): _cache.clear() def pick(game): fn = GAME_FILES.get(game) if not fn: return None items = lines(fn) return random.choice(items) if items else None def custom_pick(chat_custom): return random.choice(chat_custom) if chat_custom else None def tafkik_word(): items = lines(TAFKIK_FILE) return random.choice(items) if items else None