File size: 1,065 Bytes
3a3b605
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""تحميل ملفات المحتوى (سطر = رد) واختيار عشوائي مع إعادة تحميل تلقائي عند تغيير الملف."""
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