| |
| import logging, functools |
| from telegram import Update |
| from telegram.ext import ContextTypes |
| import config, database |
|
|
| logger = logging.getLogger(__name__) |
|
|
| def is_admin(uid: int) -> bool: |
| return uid in config.ADMIN_IDS |
|
|
| def admin_only(func): |
| @functools.wraps(func) |
| async def wrapper(update: Update, ctx: ContextTypes.DEFAULT_TYPE): |
| uid = update.effective_user.id |
| if not is_admin(uid): |
| logger.warning(f"[GUARD] رُفض uid={uid} → {func.__name__}") |
| if update.message: |
| await update.message.reply_text("❌ هذا الأمر للمشرفين فقط") |
| elif update.callback_query: |
| await update.callback_query.answer("❌ للمشرفين فقط", show_alert=True) |
| return |
| return await func(update, ctx) |
| return wrapper |
|
|
| def register_user(func): |
| @functools.wraps(func) |
| async def wrapper(update: Update, ctx: ContextTypes.DEFAULT_TYPE): |
| user = update.effective_user |
| if not user: |
| return await func(update, ctx) |
| uid = user.id |
| is_new = database.get_user(uid) is None |
| database.upsert_user(uid, user.username or "", user.first_name or "") |
| if database.is_user_banned(uid) and not is_admin(uid): |
| if update.message: |
| await update.message.reply_text("🚫 تم حظرك من استخدام هذا البوت.") |
| elif update.callback_query: |
| await update.callback_query.answer("🚫 أنت محظور", show_alert=True) |
| return |
| if is_new: |
| try: |
| from handlers.admin import notify_new_user |
| await notify_new_user(update, ctx) |
| except Exception as e: |
| logger.debug(f"[GUARD] إشعار مستخدم جديد: {e}") |
| return await func(update, ctx) |
| return wrapper |
|
|