File size: 9,350 Bytes
982e098
 
51e1c40
982e098
51e1c40
 
982e098
 
 
51e1c40
 
982e098
51e1c40
83fe5dc
51e1c40
 
83fe5dc
51e1c40
 
982e098
 
51e1c40
 
 
982e098
51e1c40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89ad488
 
51e1c40
 
 
 
 
 
 
89ad488
51e1c40
6cef7ec
 
51e1c40
 
 
 
 
982e098
51e1c40
 
 
 
 
89ad488
 
51e1c40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
982e098
51e1c40
 
 
982e098
51e1c40
 
982e098
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89ad488
982e098
 
 
 
 
 
6cef7ec
982e098
 
51e1c40
 
89ad488
 
51e1c40
 
982e098
 
 
51e1c40
982e098
 
51e1c40
 
89ad488
982e098
 
83fe5dc
51e1c40
982e098
 
 
 
89ad488
982e098
 
 
 
 
 
51e1c40
 
 
 
 
 
 
 
 
 
982e098
51e1c40
982e098
51e1c40
 
982e098
51e1c40
 
 
982e098
51e1c40
 
6cef7ec
89ad488
982e098
 
 
51e1c40
 
 
89ad488
 
51e1c40
 
 
982e098
51e1c40
 
 
 
6cef7ec
51e1c40
 
6cef7ec
51e1c40
 
6cef7ec
51e1c40
 
6cef7ec
51e1c40
 
6cef7ec
51e1c40
 
 
 
982e098
89ad488
 
 
 
 
 
 
51e1c40
 
 
 
 
 
 
982e098
 
51e1c40
982e098
 
51e1c40
 
 
 
 
 
 
 
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import os
from random import choice, shuffle
from traceback import format_exc
from typing import List

from pyrogram import filters
from pyrogram.enums import ChatMemberStatus as CMS
from pyrogram.enums import ParseMode as PM
from pyrogram.types import CallbackQuery, ChatPermissions
from pyrogram.types import InlineKeyboardButton as IKB
from pyrogram.types import InlineKeyboardMarkup as ikm
from pyrogram.types import Message, User

from Powers import LOGGER, MESSAGE_DUMP
from Powers.bot_class import Gojo
from Powers.database.captcha_db import CAPTCHA, CAPTCHA_DATA
from Powers.supports import get_support_staff
from Powers.utils.captcha_helper import (genrator, get_image_captcha,
                                         get_qr_captcha)
from Powers.utils.custom_filters import admin_filter, captcha_filter, command
from Powers.utils.extras import BAN_GIFS


@Gojo.on_message(command("captcha") & admin_filter & ~filters.private)
async def start_captcha(_, m: Message):
    captcha = CAPTCHA()
    split = m.command
    if len(split) == 1:
        is_cap = captcha.is_captcha(m.chat.id)
        if is_cap:
            txt = "Captcha verification is currently **on** for this chat"
        else:
            txt = "Captcha verification is currently **off** for this chat"
        await m.reply_text(txt)
    else:
        on_off = split[1].lower()
        if on_off in ["on", "yes", "enable"]:
            captcha.insert_captcha(m.chat.id)
            await m.reply_text("Captcha verification is now **on** for this chat")
        elif on_off in ["off", "no", "disable"]:
            captcha.remove_captcha(m.chat.id)
            await m.reply_text("Captcha verification is now **off** for this chat")
        else:
            await m.reply_text("**USAGE**\n/captcha [on | yes | enable | off | no | disable]")

    return


@Gojo.on_message(command("captchamode") & admin_filter & ~filters.private)
async def set_captcha_mode(c: Gojo, m: Message):
    split = m.command
    captcha = CAPTCHA()
    if len(split) == 1:
        if curr := captcha.get_captcha(m.chat.id):
            capatcha_type = curr["captcha_type"]
            await m.reply_text(
                f"Current captcha verification methode is {capatcha_type}\nAvailable methodes:\n■ qr\n■ image")
        else:
            await m.reply_text("Captcha verification is off for the current chat")
    else:
        type_ = split[1].lower()
        if type_ == "qr":
            await m.reply_text("This feature is not implemented yet\nUse /captchamode image")
        elif type_ == "image":
            captcha.update_type(m.chat.id, "image")
            await m.reply_text("Captcha verication is now changed to image")
        else:
            await m.reply_text("**USAGE**\n/captchamode [qr | image]")

    return


@Gojo.on_callback_query(filters.regex("^captcha_"))
async def captcha_codes_check(c: Gojo, q: CallbackQuery):
    split = q.data.split("_")
    chat = int(split[1])
    user = int(split[2])
    code = split[3]

    if q.from_user.id != user:
        await q.answer("Not for you BAKA!")
        return

    c_data = CAPTCHA_DATA()
    code_ = c_data.get_cap_data(chat, user)

    if code_ == code:
        cap = "You guessed the captcha right...Now you can talk in the chat with no restrictions"
        c_data.remove_cap_data(chat, user)
        await q.answer(cap, True)
        try:
            await q.message.chat.unban_member(user)
        except Exception as e:
            await q.message.reply_text(f"Unable to unmute {q.from_user.mention} this user")
            await q.message.reply_text(e)
            return
        await c.send_message(chat, f"{q.from_user.mention} now you are free to talk")
        await q.message.delete()
    else:
        caps = q.message.caption.split(":")
        tries = int(caps[1].strip()) - 1
        caps.pop(-1)
        caps.append(f" {tries}")
        new_cap = ":".join(caps)
        await q.answer(f"Wrong\nTries left: {tries}", True)
        if not tries:
            txt = f"{q.from_user.mention} was not able to pass captcha verification thus banned from the group"
            try:
                await q.message.chat.ban_member(user)
            except Exception as e:
                await q.message.reply_text("Failed to ban member")
                return
            await q.message.delete()
            keyboard = ikm(
                [
                    [
                        IKB(
                            "Unban",
                            callback_data=f"unban_={user}",
                        ),
                    ],
                ],
            )
            anim = choice(BAN_GIFS)
            try:
                await c.send_animation(
                    chat_id=q.message.chat.id,
                    animation=str(anim),
                    caption=txt,
                    reply_markup=keyboard,
                    parse_mode=PM.HTML,
                )
            except Exception:

                await c.send_animation(
                    chat_id=q.message.chat.id,
                    text=txt,
                    reply_markup=keyboard,
                    parse_mode=PM.HTML,
                )
                await c.send_message(MESSAGE_DUMP, f"#REMOVE from BAN_GFIS\n{anim}")
            c_data.remove_cap_data(chat, user)
            c_data.del_message_id(q.message.chat.id, user)
        else:
            await q.edit_message_caption(new_cap, reply_markup=q.message.reply_markup)

    return


@Gojo.on_message(filters.group & captcha_filter & filters.new_chat_members, group=3)
async def on_chat_members_updatess(c: Gojo, m: Message):
    chat = m.chat.id

    users: List[User] = m.new_chat_members
    for user in users:
        captcha = CAPTCHA()
        cap_data = CAPTCHA_DATA()

        if user.is_bot:
            continue
        SUPPORT_STAFF = get_support_staff()

        try:
            status = (await m.chat.get_member(user)).status
            if status in [CMS.OWNER, CMS.ADMINISTRATOR]:
                continue
        except Exception:
            pass
        if user.id in SUPPORT_STAFF:
            continue
        captcha_info = captcha.get_captcha(chat)
        captcha_type = captcha_info["captcha_type"]
        is_already = cap_data.is_already_data(chat, user.id)

        mess = False
        try:
            if is_already:
                mess = await c.get_messages(chat, int(is_already))
        except Exception:
            cap_data.del_message_id(chat, is_already)
            mess = False
            is_already = False

        if is_already and mess.empty:
            cap_data.del_message_id(chat, is_already)
            continue

        try:
            await c.restrict_chat_member(chat, user.id, ChatPermissions())
        except Exception as e:
            LOGGER.error(e)
            LOGGER.error(format_exc())
            continue

        if not is_already:
            captcha_type = "image"  # I am not going to apply qr captcha in this update
            if captcha_type == "image":
                img, code = await get_image_captcha(chat, user.id)
                cap = f"Please {user.mention} please choose the correct code from the one given bellow\nYou have three tries if you get all three wrong u will be banned from the chat.\nTries left: 3"
                cap_data.load_cap_data(chat, user.id, code)
                rand = [code]
                while len(rand) != 5:
                    hehe = genrator()
                    if hehe != code:
                        rand.append(hehe)

                shuffle(rand)

                ini = f"captcha_{chat}_{user.id}_"

                kb = ikm(
                    [
                        [
                            IKB(rand[0], ini + rand[0])
                        ],
                        [
                            IKB(rand[1], ini + rand[1])
                        ],
                        [
                            IKB(rand[2], ini + rand[2])
                        ],
                        [
                            IKB(rand[3], ini + rand[3])
                        ],
                        [
                            IKB(rand[4], ini + rand[4])
                        ]
                    ]
                )
                await c.send_photo(chat, img, caption=cap, reply_markup=kb)
                os.remove(img)
            elif captcha_type == "qr":
                pic = await get_qr_captcha(chat, user.id, c.me.username)
                cap = f"Please {user.mention} scan this qr code with your phone to verify that you are human"
                ms = await c.send_photo(chat, pic, caption=cap)
                os.remove(pic)
                cap_data.store_message_id(chat, user.id, ms.id)
        elif mess:
            kb = ikm(
                [
                    [
                        IKB("Click here to verify", url=mess.link)
                    ]
                ]
            )
            await c.send_message(f"{user.mention} your verification is already pending", reply_markup=kb)
            continue
        else:
            await c.unban_chat_member(chat, user.id)
            continue


__PLUGIN__ = "captcha"

__HELP__ = """
• /captcha [on|yes|enable|off|no|disable] : To enable or disable captcha verification
• /captchamode [qr|image] : To change captcha mode
"""