File size: 3,707 Bytes
87abec5
11ae35a
bb1bfd1
11ae35a
c0ed2cf
87abec5
11ae35a
 
 
 
ca4eb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89ad488
 
bb1bfd1
89ad488
 
 
ca4eb6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89ad488
 
bb1bfd1
89ad488
 
 
 
bb1bfd1
89ad488
ca4eb6d
 
 
 
 
 
 
89ad488
ca4eb6d
 
6cef7ec
 
ca4eb6d
 
 
 
 
 
 
bb1bfd1
 
 
 
 
 
 
 
 
 
 
6cef7ec
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
from traceback import format_exc

from pyrogram.enums import ChatType as CT
from pyrogram.errors import PeerIdInvalid, RPCError
from pyrogram.types import Message

from Powers import LOGGER
from Powers.bot_class import Gojo
from Powers.database.group_blacklist import GroupBlacklist
from Powers.utils.custom_filters import command

# initialise database
db = GroupBlacklist()


@Gojo.on_message(command("blchat", dev_cmd=True))
async def blacklist_chat(c: Gojo, m: Message):
    if len(m.text.split()) >= 2:
        chat_ids = m.text.split()[1:]
        replymsg = await m.reply_text(f"Adding {len(chat_ids)} chats to blacklist")
        for chat in chat_ids:
            try:
                get_chat = await c.get_chat(chat)
                chat_id = get_chat.id
                db.add_chat(chat_id)
            except PeerIdInvalid:
                await replymsg.edit_text(
                    "Haven't seen this group in this session, maybe try again later?",
                )
            except RPCError as ef:
                LOGGER.error(ef)
                LOGGER.error(format_exc())
        await replymsg.edit_text(
            f"Added the following chats to Blacklist.\n<code>{', '.join(chat_ids)}</code>.",
        )
    elif m.chat.type == CT.PRIVATE:
        await m.reply_text("Use in groups")
    else:
        chat_id = m.chat.id
        db.add_chat(chat_id)
        await m.reply_text("Added this chat to blacklist chats")
    return


@Gojo.on_message(
    command(["rmblchat", "unblchat"], dev_cmd=True),
)
async def unblacklist_chat(c: Gojo, m: Message):
    if len(m.text.split()) >= 2:
        chat_ids = m.text.split()[1:]
        replymsg = await m.reply_text(f"Removing {len(chat_ids)} chats from blacklist")
        bl_chats = db.list_all_chats()
        for chat in chat_ids:
            try:
                get_chat = await c.get_chat(chat)
                chat_id = get_chat.id
                if chat_id not in bl_chats:
                    # If chat is not blaklisted, continue loop
                    continue
                db.remove_chat(chat_id)
            except PeerIdInvalid:
                await replymsg.edit_text(
                    "Haven't seen this group in this session, maybe try again later?",
                )
            except RPCError as ef:
                LOGGER.error(ef)
                LOGGER.error(format_exc())
        await replymsg.edit_text(
            f"Removed the following chats to Blacklist.\n<code>{', '.join(chat_ids)}</code>.",
        )
    elif m.chat.type == CT.PRIVATE:
        await m.reply_text("Use in groups")
    else:
        chat_id = m.chat.id
        bl_chats = bl_chats = db.list_all_chats()
        if chat_id in bl_chats:
            await m.reply_text("Removed this chat from blacklist chats")
        else:
            await m.reply_text("This chat is not in my list of blacklisted chats")
    return


@Gojo.on_message(
    command(["blchatlist", "blchats"], dev_cmd=True),
)
async def list_blacklist_chats(_, m: Message):
    if bl_chats := db.list_all_chats():
        txt = (
            (
                    "These Chats are Blacklisted:\n"
                    + "\n".join(f"<code>{i}</code>" for i in bl_chats)
            ),
        )

    else:
        txt = "No chats are currently blacklisted!"
    await m.reply_text(txt)
    return


__PLUGIN__ = "Chat blacklist"

__HELP__ = """
**Chat blacklist**

**Dev commands:**
• /blchat [space separated id or username of chats]: Add chats to black list if given or the current chat.
• /rmblchat [space separated id or username of chats]: Remove chats from black list if given or the current chat.
• /blchats: Give the list of blacklisted chats
"""