Download core/store.py from ABO3MR/df: direct link, hf CLI and curl.
- Browser
- Download file 3.09 kB
-
https://huggingface.co/spaces/ABO3MR/df/resolve/main/core/store.py
- Command line
-
hf download hf://spaces/ABO3MR/df/core/store.py
-
curl -L -o store.py https://huggingface.co/spaces/ABO3MR/df/resolve/main/core/store.py
3.09 kB
| """تخزين آمن في ملفات JSON لكل جروب + عدادات الأعضاء + كاش المعرفات والأسماء.""" | |
| import json | |
| import threading | |
| from config import STATE | |
| class Store: | |
| def __init__(self, name): | |
| self.path = STATE / f"{name}.json" | |
| self._lock = threading.Lock() | |
| try: | |
| self.d = json.loads(self.path.read_text(encoding="utf-8")) | |
| except Exception: | |
| self.d = {} | |
| def get(self, chat, key, default=None): | |
| return self.d.get(str(chat), {}).get(key, default) | |
| def set(self, chat, key, value): | |
| with self._lock: | |
| self.d.setdefault(str(chat), {})[key] = value | |
| self._save_unlocked() | |
| def save(self): | |
| with self._lock: | |
| self._save_unlocked() | |
| def _save_unlocked(self): | |
| self.path.write_text(json.dumps(self.d, ensure_ascii=False), encoding="utf-8") | |
| settings = Store("settings") | |
| ranks_db = Store("ranks") | |
| stats = Store("stats") | |
| users_db = Store("users") | |
| def remember_user(chat_id, user): | |
| """يحفظ بيانات العضو (للبحث بـ @المعرف وتتبع تغيير الأسماء).""" | |
| if not user or user.is_bot: | |
| return None | |
| u_map = users_db.get(chat_id, "u", {}) | |
| uid = str(user.id) | |
| prev = u_map.get(uid) | |
| cur = { | |
| "id": user.id, | |
| "name": user.full_name or user.first_name or "", | |
| "first": user.first_name or "", | |
| "user": (user.username or "").lower(), | |
| } | |
| changed = None | |
| if prev and (prev.get("name") != cur["name"] or prev.get("user") != cur["user"]): | |
| changed = (prev, cur) | |
| u_map[uid] = cur | |
| users_db.set(chat_id, "u", u_map) | |
| return changed | |
| def find_user_by_username(chat_id, username): | |
| """يبحث عن عضو محفوظ في الجروب بالمعرف @username.""" | |
| uname = (username or "").strip().lstrip("@").lower() | |
| if not uname: | |
| return None | |
| for info in users_db.get(chat_id, "u", {}).values(): | |
| if info.get("user") == uname: | |
| return info | |
| return None | |
| def get_cached_user(chat_id, user_id): | |
| return users_db.get(chat_id, "u", {}).get(str(user_id)) | |
| def bump(chat_id, user_id, field, n=1): | |
| """يزوّد عداد (msgs / pts / del / edits / invites) لعضو في جروب.""" | |
| d = stats.get(chat_id, field, {}) | |
| k = str(user_id) | |
| d[k] = max(0, d.get(k, 0) + n) | |
| stats.set(chat_id, field, d) | |
| return d[k] | |
| def get_counter(chat_id, user_id, field): | |
| return stats.get(chat_id, field, {}).get(str(user_id), 0) | |
| def clear_counter(chat_id, user_id, field): | |
| d = stats.get(chat_id, field, {}) | |
| d.pop(str(user_id), None) | |
| stats.set(chat_id, field, d) | |
| def total_msgs(chat_id): | |
| return sum(stats.get(chat_id, "msgs", {}).values()) | |
| def activity_label(msgs): | |
| if msgs >= 500: | |
| return "مولّع نار 🔥" | |
| if msgs >= 200: | |
| return "استمر بطل 😍" | |
| if msgs >= 50: | |
| return "تفاعلك حلو 👏" | |
| if msgs >= 10: | |
| return "لسه بادي 🌱" | |
| return "لسه جديد علينا 👋" | |