File size: 17,627 Bytes
ca4eb6d
 
ea508b7
982e098
11ae35a
6902782
c62ca93
982e098
11ae35a
83fe5dc
f0f9ad9
ca4eb6d
 
83fe5dc
89a7a7a
982e098
 
ea508b7
87abec5
11ae35a
 
 
ca4eb6d
 
 
 
c1d601a
ca4eb6d
cbec4fb
6cef7ec
 
 
 
 
ca4eb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1d601a
ca4eb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89a7a7a
ca4eb6d
 
 
 
89a7a7a
ca4eb6d
 
 
 
 
 
 
 
 
6cef7ec
ca4eb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89a7a7a
ca4eb6d
 
 
 
 
89a7a7a
ca4eb6d
 
 
 
 
 
 
 
 
6cef7ec
ca4eb6d
 
 
 
 
 
 
 
 
 
6cef7ec
ca4eb6d
 
 
 
 
 
 
 
 
 
6cef7ec
ca4eb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
6cef7ec
982e098
 
 
 
 
 
ca4eb6d
982e098
 
83fe5dc
982e098
 
 
 
89a7a7a
982e098
 
 
 
 
 
89a7a7a
982e098
 
6cef7ec
982e098
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89ad488
982e098
ca4eb6d
 
ea508b7
ca4eb6d
 
 
 
 
 
6902782
89ad488
 
 
 
 
 
ca4eb6d
982e098
ca4eb6d
 
7b9084d
89ad488
ca4eb6d
89a7a7a
89ad488
982e098
89a7a7a
 
 
 
89ad488
6cef7ec
982e098
89a7a7a
 
 
 
 
89ad488
 
6dcea66
89ad488
ca4eb6d
ea508b7
 
89ad488
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca4eb6d
89ad488
 
6cef7ec
89ad488
 
 
 
 
 
 
 
 
83fe5dc
89ad488
 
 
 
 
 
 
 
 
 
6cef7ec
 
 
 
 
 
 
 
 
 
89ad488
 
 
 
 
 
 
 
 
ca4eb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2aca525
 
 
ca4eb6d
 
 
ea508b7
ca4eb6d
 
 
ea508b7
ca4eb6d
 
 
 
 
 
 
 
 
 
 
ea508b7
 
ca4eb6d
 
ea508b7
 
6cef7ec
ea508b7
 
 
 
 
89ad488
6cef7ec
ea508b7
 
 
 
 
ca4eb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2aca525
 
 
ca4eb6d
 
 
ea508b7
ca4eb6d
 
 
ea508b7
ca4eb6d
 
 
 
 
 
 
 
 
 
 
ea508b7
 
ca4eb6d
 
ea508b7
 
6cef7ec
ea508b7
 
 
 
 
89ad488
6cef7ec
ea508b7
 
 
 
 
 
ca4eb6d
6cef7ec
ca4eb6d
 
9da6fea
709df35
096ee5f
709df35
 
9da6fea
d5348c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec5da4d
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
from html import escape
from secrets import choice
from traceback import format_exc
from typing import List

from pyrogram import emoji, enums, filters
from pyrogram.errors import ChannelPrivate, ChatAdminRequired, RPCError
from pyrogram.types import Message, User

from Powers import LOGGER
from Powers.bot_class import Gojo
from Powers.database.antispam_db import GBan
from Powers.database.greetings_db import Greetings
from Powers.supports import get_support_staff
from Powers.utils.cmd_senders import send_cmd
from Powers.utils.custom_filters import (admin_filter, bot_admin_filter,
                                         captcha_filter, command)
from Powers.utils.kbhelpers import ikb
from Powers.utils.msg_types import Types, get_wlcm_type
from Powers.utils.parser import escape_markdown, mention_html
from Powers.utils.string import (build_keyboard, escape_invalid_curly_brackets,
                                 parse_button)

# Initialize
gdb = GBan()

ChatType = enums.ChatType


async def escape_mentions_using_curly_brackets_wl(
        user: User,
        m: Message,
        text: str,
        parse_words: list,
) -> str:
    teks = await escape_invalid_curly_brackets(text, parse_words)
    if teks:
        teks = teks.format(
            first=escape(user.first_name),
            last=escape(user.last_name or user.first_name),
            fullname=" ".join(
                [
                    escape(user.first_name),
                    escape(user.last_name),
                ]
                if user.last_name
                else [escape(user.first_name)],
            ),
            username=(
                "@" + (await escape_markdown(escape(user.username)))
                if user.username
                else (await (mention_html(escape(user.first_name), user.id)))
            ),
            mention=await (mention_html(escape(user.first_name), user.id)),
            chatname=escape(m.chat.title)
            if m.chat.type != ChatType.PRIVATE
            else escape(user.first_name),
            id=user.id,
        )
    else:
        teks = ""

    return teks


@Gojo.on_message(command("cleanwelcome") & admin_filter)
async def cleanwlcm(_, m: Message):
    db = Greetings(m.chat.id)
    status = db.get_current_cleanwelcome_settings()
    args = m.text.split(" ", 1)

    if len(args) >= 2:
        if args[1].lower() == "on":
            db.set_current_cleanwelcome_settings(True)
            await m.reply_text("Turned on!")
            return
        if args[1].lower() == "off":
            db.set_current_cleanwelcome_settings(False)
            await m.reply_text("Turned off!")
            return
        await m.reply_text("what are you trying to do ??")
        return
    await m.reply_text(f"Current settings:- {status}")
    return


@Gojo.on_message(command("cleangoodbye") & admin_filter)
async def cleangdbye(_, m: Message):
    db = Greetings(m.chat.id)
    status = db.get_current_cleangoodbye_settings()
    args = m.text.split(" ", 1)

    if len(args) >= 2:
        if args[1].lower() == "on":
            db.set_current_cleangoodbye_settings(True)
            await m.reply_text("Turned on!")
            return
        if args[1].lower() == "off":
            db.set_current_cleangoodbye_settings(False)
            await m.reply_text("Turned off!")
            return
        await m.reply_text("what are you trying to do ??")
        return
    await m.reply_text(f"Current settings:- {status}")
    return


@Gojo.on_message(command("cleanservice") & admin_filter)
async def cleanservice(_, m: Message):
    db = Greetings(m.chat.id)
    status = db.get_current_cleanservice_settings()
    args = m.text.split(" ", 1)

    if len(args) >= 2:
        if args[1].lower() == "on":
            db.set_current_cleanservice_settings(True)
            await m.reply_text("Turned on!")
            return
        if args[1].lower() == "off":
            db.set_current_cleanservice_settings(False)
            await m.reply_text("Turned off!")
            return
        await m.reply_text("what are you trying to do ??")
        return
    await m.reply_text(f"Current settings:- {status}")
    return


@Gojo.on_message(command("setwelcome") & admin_filter & bot_admin_filter)
async def save_wlcm(_, m: Message):
    db = Greetings(m.chat.id)
    if m and not m.from_user:
        return
    args = m.text.split(None, 1)

    if len(args) >= 4096:
        await m.reply_text(
            "Word limit exceed !!",
        )
        return
    if not (m.reply_to_message and m.reply_to_message.text) and len(m.command) == 0:
        await m.reply_text(
            "Error: There is no text in here! and only text with buttons are supported currently !",
        )
        return
    text, msgtype, file = await get_wlcm_type(m)
    if not m.reply_to_message and msgtype == Types.TEXT and len(m.command) <= 2:
        await m.reply_text(f"<code>{m.text}</code>\n\nError: There is no data in here!")
        return

    if not text and not file:
        await m.reply_text(
            "Please provide some data!",
        )
        return

    if not msgtype:
        await m.reply_text("Please provide some data for this to reply with!")
        return

    db.set_welcome_text(text, msgtype, file)
    await m.reply_text("Saved welcome!")
    return


@Gojo.on_message(command("setgoodbye") & admin_filter & bot_admin_filter)
async def save_gdbye(_, m: Message):
    db = Greetings(m.chat.id)
    if m and not m.from_user:
        return
    args = m.text.split(None, 1)

    if len(args) >= 4096:
        await m.reply_text(
            "Word limit exceeds !!",
        )
        return
    if not (m.reply_to_message and m.reply_to_message.text) and len(m.command) == 0:
        await m.reply_text(
            "Error: There is no text in here! and only text with buttons are supported currently !",
        )
        return
    text, msgtype, file = await get_wlcm_type(m)

    if not m.reply_to_message and msgtype == Types.TEXT and len(m.command) <= 2:
        await m.reply_text(f"<code>{m.text}</code>\n\nError: There is no data in here!")
        return

    if not text and not file:
        await m.reply_text(
            "Please provide some data!",
        )
        return

    if not msgtype:
        await m.reply_text("Please provide some data for this to reply with!")
        return

    db.set_goodbye_text(text, msgtype, file)
    await m.reply_text("Saved goodbye!")
    return


@Gojo.on_message(command("resetgoodbye") & admin_filter & bot_admin_filter)
async def resetgb(_, m: Message):
    db = Greetings(m.chat.id)
    if m and not m.from_user:
        return
    text = "Sad to see you leaving {first}.\nTake Care!"
    db.set_goodbye_text(text, None)
    await m.reply_text("Ok Done!")
    return


@Gojo.on_message(command("resetwelcome") & admin_filter & bot_admin_filter)
async def resetwlcm(_, m: Message):
    db = Greetings(m.chat.id)
    if m and not m.from_user:
        return
    text = "Hey {first}, welcome to {chatname}!"
    db.set_welcome_text(text, None)
    await m.reply_text("Done!")
    return


@Gojo.on_message(filters.service & filters.group, group=59)
async def cleannnnn(_, m: Message):
    db = Greetings(m.chat.id)
    clean = db.get_current_cleanservice_settings()
    try:
        if clean:
            await m.delete()
    except Exception:
        pass


@Gojo.on_message(filters.group & filters.new_chat_members & ~captcha_filter, group=69)
async def member_has_joined(c: Gojo, m: Message):
    users: List[User] = m.new_chat_members
    db = Greetings(m.chat.id)
    for user in users:
        banned_users = gdb.check_gban(user.id)
        try:
            if user.id == c.me.id:
                continue
            if user.id in get_support_staff("dev"):
                await c.send_animation(
                    chat_id=m.chat.id,
                    animation="./extras/william.gif",
                    caption=f"😳 My **DEV** {user.mention} has also joined the chat!",
                )
                continue
            if banned_users:
                await m.chat.ban_member(user.id)
                await c.send_message(
                    m.chat.id,
                    f"{user.mention} was globally banned so i banned!",
                )
                continue
            if user.is_bot:
                continue  # ignore bots
        except ChatAdminRequired:
            continue
        status = db.get_welcome_status()
        oo = db.get_welcome_text()
        UwU = db.get_welcome_media()
        mtype = db.get_welcome_msgtype()
        parse_words = [
            "first",
            "last",
            "fullname",
            "username",
            "mention",
            "id",
            "chatname",
        ]
        hmm = await escape_mentions_using_curly_brackets_wl(user, m, oo, parse_words)
        if not status:
            continue
        tek, button = await parse_button(hmm)
        button = await build_keyboard(button)
        button = ikb(button) if button else None

        if "%%%" in tek:
            filter_reply = tek.split("%%%")
            teks = choice(filter_reply)
        else:
            teks = tek

        if not teks:
            teks = f"A wild {user.mention} appeared in {m.chat.title}! Everyone be aware."

        ifff = db.get_current_cleanwelcome_id()
        gg = db.get_current_cleanwelcome_settings()
        if ifff and gg:
            try:
                await c.delete_messages(m.chat.id, int(ifff))
            except RPCError:
                pass
        if not teks:
            teks = "Hey {first}, welcome to {chatname}"
        try:
            if not UwU:
                jj = await c.send_message(
                    m.chat.id,
                    text=teks,
                    reply_markup=button,
                    disable_web_page_preview=True,
                )
            else:
                jj = await (await send_cmd(c, mtype))(
                    m.chat.id,
                    UwU,
                    caption=teks,
                    reply_markup=button,
                )

            if jj:
                db.set_cleanwlcm_id(int(jj.id))
        except ChannelPrivate:
            continue
        except RPCError as e:
            LOGGER.error(e)
            LOGGER.error(format_exc(e))


@Gojo.on_message(filters.group & filters.left_chat_member, group=99)
async def member_has_left(c: Gojo, m: Message):
    db = Greetings(m.chat.id)
    status = db.get_goodbye_status()
    oo = db.get_goodbye_text()
    UwU = db.get_goodbye_media()
    mtype = db.get_goodbye_msgtype()
    parse_words = [
        "first",
        "last",
        "fullname",
        "id",
        "username",
        "mention",
        "chatname",
    ]

    user = m.left_chat_member or m.from_user

    hmm = await escape_mentions_using_curly_brackets_wl(user, m, oo, parse_words)
    if not status:
        return
    tek, button = await parse_button(hmm)
    button = await build_keyboard(button)
    button = ikb(button) if button else None

    if "%%%" in tek:
        filter_reply = tek.split("%%%")
        teks = choice(filter_reply)
    else:
        teks = tek

    if not teks:  # Just in case
        teks = f"Thanks for being part of this group {user.mention}. But I don't like your arrogance and leaving the group {emoji.EYES}"

    ifff = db.get_current_cleangoodbye_id()
    iii = db.get_current_cleangoodbye_settings()
    if ifff and iii:
        try:
            await c.delete_messages(m.chat.id, int(ifff))
        except RPCError:
            pass
    if user.id in get_support_staff("dev"):
        await c.send_message(
            m.chat.id,
            f"Will miss you my master {user.mention} :(",
        )
        return
    if not teks:
        teks = "Sad to see you leaving {first}\nTake Care!"
    try:
        ooo = (
            await (await send_cmd(c, mtype))(
                m.chat.id,
                UwU,
                caption=teks,
                reply_markup=button,
            ) if UwU else await c.send_message(
                m.chat.id,
                text=teks,
                reply_markup=button,
                disable_web_page_preview=True,
            )
        )
        if ooo:
            db.set_cleangoodbye_id(int(ooo.id))
        return
    except ChannelPrivate:
        pass
    except RPCError as e:
        LOGGER.error(e)
        LOGGER.error(format_exc(e))
        return


@Gojo.on_message(command("welcome") & admin_filter)
async def welcome(c: Gojo, m: Message):
    db = Greetings(m.chat.id)
    status = db.get_welcome_status()
    oo = db.get_welcome_text()
    args = m.text.split(" ", 1)

    if m and not m.from_user:
        return

    if len(args) >= 2:
        if args[1].lower() == "noformat":
            await m.reply_text(
                f"""Current welcome settings:-
            Welcome power: {status}
            Clean Welcome: {db.get_current_cleanwelcome_settings()}
            Cleaning service: {db.get_current_cleanservice_settings()}
            Welcome text in no formating:
            """,
            )
            await c.send_message(
                m.chat.id, text=oo, parse_mode=enums.ParseMode.DISABLED
            )
            return
        if args[1].lower() == "on":
            db.set_current_welcome_settings(True)
            await m.reply_text("I will greet newly joined member from now on.")
            return
        if args[1].lower() == "off":
            db.set_current_welcome_settings(False)
            await m.reply_text("I will stay quiet when someone joins.")
            return
        await m.reply_text("what are you trying to do ??")
        return
    await m.reply_text(
        f"""Current welcome settings:-
    Welcome power: {status}
    Clean Welcome: {db.get_current_cleanwelcome_settings()}
    Cleaning service: {db.get_current_cleanservice_settings()}
    Welcome text:
    """,
    )
    UwU = db.get_welcome_media()
    mtype = db.get_welcome_msgtype()
    tek, button = await parse_button(oo)
    button = await build_keyboard(button)
    button = ikb(button) if button else None
    if not UwU:
        await c.send_message(
            m.chat.id,
            text=tek,
            reply_markup=button,
            disable_web_page_preview=True,
        )
    else:
        await (await send_cmd(c, mtype))(
            m.chat.id,
            UwU,
            caption=tek,
            reply_markup=button,
        )
    return


@Gojo.on_message(command("goodbye") & admin_filter)
async def goodbye(c: Gojo, m: Message):
    db = Greetings(m.chat.id)
    status = db.get_goodbye_status()
    oo = db.get_goodbye_text()
    args = m.text.split(" ", 1)
    if m and not m.from_user:
        return
    if len(args) >= 2:
        if args[1].lower() == "noformat":
            await m.reply_text(
                f"""Current goodbye settings:-
            Goodbye power: {status}
            Clean Goodbye: {db.get_current_cleangoodbye_settings()}
            Cleaning service: {db.get_current_cleanservice_settings()}
            Goodbye text in no formating:
            """,
            )
            await c.send_message(
                m.chat.id, text=oo, parse_mode=enums.ParseMode.DISABLED
            )
            return
        if args[1].lower() == "on":
            db.set_current_goodbye_settings(True)
            await m.reply_text("I don't want but I will say goodbye to the fugitives")
            return
        if args[1].lower() == "off":
            db.set_current_goodbye_settings(False)
            await m.reply_text("I will stay quiet for fugitives")
            return
        await m.reply_text("what are you trying to do ??")
        return
    await m.reply_text(
        f"""Current Goodbye settings:-
    Goodbye power: {status}
    Clean Goodbye: {db.get_current_cleangoodbye_settings()}
    Cleaning service: {db.get_current_cleanservice_settings()}
    Goodbye text:
    """,
    )
    UwU = db.get_goodbye_media()
    mtype = db.get_goodbye_msgtype()
    tek, button = await parse_button(oo)
    button = await build_keyboard(button)
    button = ikb(button) if button else None
    if not UwU:
        await c.send_message(
            m.chat.id,
            text=tek,
            reply_markup=button,
            disable_web_page_preview=True,
        )
    else:
        await (await send_cmd(c, mtype))(
            m.chat.id,
            UwU,
            caption=tek,
            reply_markup=button,
        )
    return


__PLUGIN__ = "greetings"
__alt_name__ = ["welcome", "goodbye", "cleanservice"]

__HELP__ = """
**Greetings**

Customize your group's welcome / goodbye messages that can be personalised in multiple ways.

**Note:**
× Currently it supports only text!
× Gojo must be an admin to greet and goodbye users.

**Admin Commands:**
• /setwelcome <reply> : Sets a custom welcome message.
• /setgoodbye <reply> : Sets a custom goodbye message.
• /resetwelcome : Resets to bot default welcome message.
• /resetgoodbye : Resets to bot default goodbye message.
• /welcome <on/off> | noformat : enable/disable | Shows the current welcome message | settings.
• /goodbye <on/off> | noformat : enable/disable | Shows the current goodbye message | settings.
• /cleanwelcome <on/off> : Shows or sets the current clean welcome settings.
• /cleangoodbye <on/off> : Shows or sets the current clean goodbye settings.

**Cleaner:**
• /cleanservice <on/off> : Use it to clean all service messages automatically or to view current status.

**Format**
Check /markdownhelp for help related to formatting!"""