import logging from telegram import Update from telegram.ext import Application, MessageHandler, CommandHandler, filters import config from strings_ar import S from core import router from core import ranks as R from core.store import settings, remember_user, bump from modules import ( games, admin, admin_pro, settings_mod, locks, fun, services, bank, music, activate, menus, toggles, whisper, quran_prayer, special_replies, werewolf, ) logging.basicConfig(level=logging.INFO) BOT_STATUS = {"connected": False, "username": ""} async def start(update: Update, ctx): if ctx.args: payload = ctx.args[0] if await whisper.handle_start_deeplink(update, ctx, payload): return if await werewolf.handle_start_deeplink(update, ctx, payload): return custom = settings.get("global", "start_tpl", "") text = custom if custom else S["start"].format(bot=admin_pro.get_bot_name()) await update.message.reply_text(text) async def on_text(update: Update, ctx): m = update.message if not m or not m.text or not m.from_user: return chat = update.effective_chat is_group = chat.type != "private" # الخاص: استقبال نص الهمسة إن وُجدت جلسة نشطة if not is_group: if await whisper.handle_private_text(update, ctx): return else: # تسجيل العضو في كاش المعرفات + تنبيه تغيير الأسماء إن كان مفعلاً changed = remember_user(chat.id, m.from_user) if changed and toggles.tg_on(chat.id, "name_alert"): old, new = changed try: await m.reply_text(f"🔔 تنبيه تغيير الاسم:\n✧︙الاسم القديم: {old.get('name')}\n✧︙الاسم الجديد: {new.get('name')}") except Exception: pass # حظر عام if admin.is_gbanned(m.from_user.id): try: await chat.ban_member(m.from_user.id) except Exception: pass return # كتم عام if admin_pro.is_gmuted(m.from_user.id): try: await m.delete() except Exception: pass return if await locks.enforce(update, ctx): return if await settings_mod.force_gate(update, ctx): return await services.count_msg(update, ctx) if await special_replies.handle_pending(update, ctx): return if await settings_mod.pending_flow(update, ctx): return f, arg = router.find(m.text, chat.id) if is_group and not activate.is_active(chat.id): if f is activate.activate_cmd: await f(update, ctx, arg) elif f: await m.reply_text(S["not_active"], reply_markup=activate._activate_kb()) return if f: if is_group and settings.get(chat.id, "all_off", False) and await R.effective_level(update) < R.level("ادمن"): return await f(update, ctx, arg) return if is_group: if await games.check_answer(update, ctx, False): return if await special_replies.try_special_reply(update, ctx): return await settings_mod.custom_reply(update, ctx) async def on_other(update: Update, ctx): m = update.effective_message if not m or update.effective_chat.type == "private": return if m.from_user and admin_pro.is_gmuted(m.from_user.id): try: await m.delete() except Exception: pass return # استقبال الميديا عند إضافة رد مميز if await special_replies.handle_pending(update, ctx): return await locks.enforce(update, ctx) async def on_edit(update: Update, ctx): await services.count_edit(update, ctx) await locks.enforce(update, ctx, edited=True) async def on_join_wrap(update: Update, ctx): chat = update.effective_chat m = update.message if chat and chat.type != "private" and m: # تسجيل عدد الإضافات للعضو الذي أضاف أعضاء جدد if m.from_user and m.new_chat_members: added = [u for u in m.new_chat_members if u.id != m.from_user.id and not u.is_bot] if added: bump(chat.id, m.from_user.id, "invites", len(added)) # قفل الدخول / قفل البوتات for u in m.new_chat_members: if u.is_bot and settings.get(chat.id, "lock_البوتات", False): try: await chat.ban_member(u.id) except Exception: pass elif not u.is_bot and settings.get(chat.id, "lock_الدخول", False): try: await chat.ban_member(u.id) await chat.unban_member(u.id) except Exception: pass if settings.get(chat.id, "lock_الاشعارات", False): try: await m.delete() except Exception: pass await settings_mod.on_join(update, ctx) async def on_leave_wrap(update: Update, ctx): chat = update.effective_chat if chat and chat.type != "private" and settings.get(chat.id, "lock_الاشعارات", False): try: await update.message.delete() except Exception: pass await settings_mod.on_leave(update, ctx) async def post_init(app): me = await app.bot.get_me() BOT_STATUS["connected"] = True BOT_STATUS["username"] = me.username or "" await music.start_music() quran_prayer.setup_jobs(app) def build_app(): if not config.API_ID or not config.API_HASH or not config.BOT_TOKEN: raise SystemExit("يرجى ضبط API_ID و API_HASH و BOT_TOKEN في متغيرات البيئة (.env أو Hugging Face Secrets).") app = ( Application.builder() .token(config.BOT_TOKEN) .api_id(config.API_ID) .api_hash(config.API_HASH) .post_init(post_init) .build() ) app.add_handler(CommandHandler("start", start)) all_mods = ( games, admin, admin_pro, settings_mod, locks, fun, services, bank, music, activate, menus, toggles, whisper, quran_prayer, special_replies, werewolf, ) for mod in all_mods: for h in getattr(mod, "HANDLERS", []): app.add_handler(h) app.add_handler(MessageHandler(filters.StatusUpdate.NEW_CHAT_MEMBERS, on_join_wrap)) app.add_handler(MessageHandler(filters.StatusUpdate.LEFT_CHAT_MEMBER, on_leave_wrap)) app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND & ~filters.UpdateType.EDITED, on_text)) app.add_handler(MessageHandler(filters.UpdateType.EDITED, on_edit)) app.add_handler(MessageHandler(~filters.TEXT & ~filters.StatusUpdate.ALL, on_other)) return app def main(): app = build_app() app.run_polling() if __name__ == "__main__": main()