File size: 7,142 Bytes
3a3b605
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67d64de
 
 
d979420
 
67d64de
 
d979420
67d64de
d979420
3a3b605
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67d64de
3a3b605
 
 
67d64de
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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()