File size: 7,950 Bytes
5011423
1b5719b
7956eb0
eef97a7
1b5719b
 
 
 
 
3580517
54d8b6b
1b5719b
7956eb0
 
98ab4e9
1b5719b
 
7956eb0
1b5719b
 
7956eb0
 
 
 
4056428
7956eb0
 
 
effac34
7956eb0
 
 
 
 
 
 
 
 
 
 
98ab4e9
7956eb0
 
 
 
 
 
 
 
 
 
 
 
 
5011423
 
 
 
 
 
 
 
 
 
7956eb0
5011423
 
 
 
 
 
 
 
 
 
 
7956eb0
5011423
 
7956eb0
5011423
7956eb0
 
5011423
 
 
 
 
7956eb0
5011423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7956eb0
5011423
7956eb0
 
d2c4d77
 
7956eb0
 
5011423
4056428
3580517
5011423
 
 
7956eb0
5011423
 
 
 
7956eb0
5011423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4056428
5011423
 
 
 
 
7956eb0
5011423
7956eb0
 
 
b978403
7956eb0
 
4056428
7956eb0
 
 
 
 
 
 
 
 
 
 
 
 
effac34
b002286
7956eb0
3580517
7956eb0
 
4056428
 
7956eb0
 
 
89c3556
 
b002286
7956eb0
1c7d78c
d7e1221
7956eb0
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from datetime import datetime, timedelta
from re import escape as re_escape
from time import time
from traceback import format_exc

from pyrogram import filters
from pyrogram.errors import ChatAdminRequired, RPCError, UserAdminInvalid
from pyrogram.types import ChatPermissions, Message

from Powers import DEV_USERS, LOGGER, MESSAGE_DUMP, SUDO_USERS, WHITELIST_USERS
from Powers.bot_class import Gojo
from Powers.database.antispam_db import ANTISPAM_BANNED, GBan
from Powers.database.approve_db import Approve
from Powers.database.blacklist_db import Blacklist
from Powers.database.group_blacklist import BLACKLIST_CHATS
from Powers.database.pins_db import Pins
from Powers.database.warns_db import Warns, WarnSettings
from Powers.utils.caching import ADMIN_CACHE, admin_cache_reload
from Powers.utils.parser import mention_html
from Powers.utils.regex_utils import regex_searcher

# Initialise
gban_db = GBan()


@Gojo.on_message(filters.linked_channel)
async def antichanpin_cleanlinked(c: Gojo, m: Message):
    try:
        msg_id = m.id
        pins_db = Pins(m.chat.id)
        curr = pins_db.get_settings()
        if curr["antichannelpin"]:
            await c.unpin_chat_message(chat_id=m.chat.id, message_id=msg_id)
        if curr["cleanlinked"]:
            await c.delete_messages(m.chat.id, msg_id)
    except ChatAdminRequired:
        await m.reply_text(
            "Disabled antichannelpin as I don't have enough admin rights!",
        )
        pins_db.antichannelpin_off()
        LOGGER.warning(f"Disabled antichannelpin in {m.chat.id} as i'm not an admin.")
    except Exception as ef:
        LOGGER.error(ef)
        LOGGER.error(format_exc())

    return


@Gojo.on_message(filters.text & filters.group, group=5)
async def bl_watcher(_, m: Message):
    if m and not m.from_user:
        return

    bl_db = Blacklist(m.chat.id)
    try:
        async def perform_action_blacklist(m: Message, action: str, trigger: str):
            if action == "kick":
                tim = datetime.now() + timedelta(minutes=45)
                await m.chat.ban_member(m.from_user.id, tim)
                await m.reply_text(
                    text="Kicked {user} for sending a blacklisted word!".format(
                        user=m.from_user.username or f"<b>{m.from_user.first_name}</b>",
                    ),
                )

            elif action == "ban":
                (
                    await m.chat.ban_member(
                        m.from_user.id,
                    )
                )
                await m.reply_text(
                    text="Banned {user} for sending a blacklisted word!".format(
                        user=m.from_user.username or f"<b>{m.from_user.first_name}</b>",
                    ),
                )

            elif action == "mute":
                await m.chat.restrict_member(
                    m.from_user.id,
                    ChatPermissions(),
                )

                await m.reply_text(
                    text="Muted {user} for sending a blacklisted word!".format(
                        user=m.from_user.username or f"<b>{m.from_user.first_name}</b>",
                    ),
                )

            elif action == "warn":
                warns_settings_db = WarnSettings(m.chat.id)
                warns_db = Warns(m.chat.id)
                warn_settings = warns_settings_db.get_warnings_settings()
                warn_reason = bl_db.get_reason()
                _, num = warns_db.warn_user(m.from_user.id, warn_reason)
                if num >= warn_settings["warn_limit"]:
                    if warn_settings["warn_mode"] == "kick":
                        await m.chat.ban_member(
                            m.from_user.id,
                            until_date=int(time() + 45),
                        )
                        action = "kicked"
                    elif warn_settings["warn_mode"] == "ban":
                        await m.chat.ban_member(m.from_user.id)
                        action = "banned"
                    elif warn_settings["warn_mode"] == "mute":
                        await m.chat.restrict_member(m.from_user.id, ChatPermissions())
                        action = "muted"
                    await m.reply_text(
                        (
                            f"Warnings {num}/{warn_settings['warn_limit']}\n"
                            f"{(await mention_html(m.from_user.first_name, m.from_user.id))} has been <b>{action}!</b>"
                        ),
                    )
                    return
                await m.reply_text(
                    (
                        f"{(await mention_html(m.from_user.first_name, m.from_user.id))} warned {num}/{warn_settings['warn_limit']}\n" +
                       "Last warn was for:\n<i>{}</i>".format(warn_reason.format(trigger))
                    ),
                )
            return

        SUPPORT_STAFF = DEV_USERS.union(SUDO_USERS).union(WHITELIST_USERS)
        if m.from_user.id in SUPPORT_STAFF:
            # Don't work on Support Staff!
            return

        # If no blacklists, then return
        chat_blacklists = bl_db.get_blacklists()
        if not chat_blacklists:
            return

        # Get admins from admin_cache, reduces api calls
        try:
            admin_ids = {i[0] for i in ADMIN_CACHE[m.chat.id]}
        except KeyError:
            admin_ids = await admin_cache_reload(m, "blacklist_watcher")

        if m.from_user.id in admin_ids:
            return

        # Get approved user from cache/database
        app_users = Approve(m.chat.id).list_approved()
        if m.from_user.id in {i[0] for i in app_users}:
            return

        # Get action for blacklist
        action = bl_db.get_action()
        for trigger in chat_blacklists:
            pattern = r"( |^|[^\w])" + re_escape(trigger) + r"( |$|[^\w])"
            match = await regex_searcher(pattern, m.text.lower())
            if not match:
                continue
            if match:
                try:
                    await perform_action_blacklist(m, action, trigger)

                    await m.delete()
                except RPCError as ef:
                    LOGGER.error(ef)
                    LOGGER.error(format_exc())
                break
        return
    except Exception:
        return


@Gojo.on_message(filters.user(list(ANTISPAM_BANNED)) & filters.group, 5)
async def gban_watcher(c: Gojo, m: Message):
    from Powers import SUPPORT_GROUP

    if m and not m.from_user:
        return

    try:
        _banned = gban_db.check_gban(m.from_user.id)
    except Exception as ef:
        LOGGER.error(ef)
        LOGGER.error(format_exc())
        return

    if _banned:
        try:
            await m.chat.ban_member(m.from_user.id)
            await m.delete(m.id)  # Delete users message!
            user_gbanned = await mention_html(m.from_user.first_name, m.from_user.id)
            await m.reply_text(
                text=f"This user ({user_gbanned}) has been banned globally!\n\nTo get unbanned, appeal at @{SUPPORT_GROUP}")
            return
        except (ChatAdminRequired, UserAdminInvalid):
            pass  # For now just ignore the user in future will let the admins know once or after few times think abt it later

        except RPCError as ef:
            await c.send_message(
                MESSAGE_DUMP,
                text=f"""<b>Gban Watcher Error!</b>
        <b>Chat:</b> <code>{m.chat.id}</code>
        <b>Error:</b> <code>{ef}</code>""",
            )


@Gojo.on_message(filters.chat(BLACKLIST_CHATS))
async def bl_chats_watcher(c: Gojo, m: Message):
    from Powers import SUPPORT_GROUP

    await c.send_message(
        m.chat.id,
        (
            "This is a blacklisted group!\n"
            f"For Support, Join @{SUPPORT_GROUP}\n"
            "Now, I'm outta here!"
        ),
    )
    await c.leave_chat(m.chat.id)
    return