df / core /ranks.py
ABO3MR's picture
Upload folder using huggingface_hub
3a3b605 verified
Raw History Blame Contribute Delete
2.29 kB
from telegram.constants import ChatMemberStatus
from core.store import ranks_db, settings
from strings_ar import RANK_NAMES, FUN_RANKS
import config
ORDER = [
"عضو", "مميز", "ادمن", "مدير", "منشئ", "منشئ اساسي",
"مالك", "مالك اساسي", "مطور", "Dev"
]
def is_owner(user_id):
return bool(config.OWNER_ID and user_id == config.OWNER_ID)
def get_devs():
return settings.get("global", "devs", {})
def is_dev(user_id):
if is_owner(user_id):
return True
return str(user_id) in get_devs()
def level(rank):
return ORDER.index(rank) if rank in ORDER else 0
def get_rank(chat_id, user_id):
if is_owner(user_id):
return "Dev"
if str(user_id) in get_devs():
return "مطور"
return ranks_db.get(chat_id, "r", {}).get(str(user_id), "عضو")
def get_custom_rank_label(chat_id, user_id):
"""الرتبة المخصصة (ضع رتبه) أو رد الرتبة المخصص (تغيير رد)."""
custom = ranks_db.get(chat_id, "custom_badge", {}).get(str(user_id))
if custom:
return custom
base = get_rank(chat_id, user_id)
replies = settings.get(chat_id, "rank_replies", {})
return replies.get(base, base)
async def effective_level(update, user=None):
"""رتبة البوت + حالة التليجرام (المالك/الأدمن)."""
chat = update.effective_chat
u = user or update.effective_user
if not u:
return 0
lv = level(get_rank(chat.id, u.id))
if chat.type != "private":
try:
m = await chat.get_member(u.id)
if m.status == ChatMemberStatus.OWNER:
lv = max(lv, level("مالك"))
elif m.status == ChatMemberStatus.ADMINISTRATOR:
lv = max(lv, level("ادمن"))
except Exception:
pass
return lv
def set_rank(chat_id, user_id, rank):
d = ranks_db.get(chat_id, "r", {})
if rank == "عضو":
d.pop(str(user_id), None)
else:
d[str(user_id)] = rank
ranks_db.set(chat_id, "r", d)
def valid_rank(name):
return name in RANK_NAMES or name in FUN_RANKS
def list_rank(chat_id, rank):
d = ranks_db.get(chat_id, "r", {})
return [int(u) for u, r in d.items() if r == rank]