forked from SpEcHiDe/UniBorg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblacklist.py
82 lines (74 loc) · 2.88 KB
/
blacklist.py
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""Filters
Available Commands:
.addblacklist
.listblacklist
.rmblacklist"""
import asyncio
import io
import re
import sql_helpers.blacklist_sql as sql
from telethon import events
from telethon.tl import types, functions
@borg.on(slitu.admin_cmd(incoming=True))
async def on_new_message(event):
if await slitu.is_admin(event.client, event.chat_id, event.from_id):
return
if borg.me.id == event.from_id:
return
name = event.raw_text
snips = sql.get_chat_blacklist(event.chat_id)
for snip in snips:
pattern = r"( |^|[^\w])" + re.escape(snip) + r"( |$|[^\w])"
if re.search(pattern, name, flags=re.IGNORECASE):
try:
await event.delete()
except Exception as e:
await event.reply("I do not have DELETE permission in this chat")
sql.rm_from_blacklist(event.chat_id, snip.lower())
break
@borg.on(slitu.admin_cmd(pattern="addblacklist ((.|\n)*)"))
async def on_add_black_list(event):
text = event.pattern_match.group(1)
to_blacklist = list(
{trigger.strip() for trigger in text.split("\n") if trigger.strip()}
)
for trigger in to_blacklist:
sql.add_to_blacklist(event.chat_id, trigger.lower())
await event.edit("Added {} triggers to the blacklist in the current chat".format(len(to_blacklist)))
@borg.on(slitu.admin_cmd(pattern="listblacklist"))
async def on_view_blacklist(event):
all_blacklisted = sql.get_chat_blacklist(event.chat_id)
OUT_STR = "Blacklists in the Current Chat:\n"
if len(all_blacklisted) > 0:
for trigger in all_blacklisted:
OUT_STR += f"👉 {trigger} \n"
else:
OUT_STR = "No BlackLists. Start Saving using `.addblacklist`"
if len(OUT_STR) > Config.MAX_MESSAGE_SIZE_LIMIT:
with io.BytesIO(str.encode(OUT_STR)) as out_file:
out_file.name = "blacklist.text"
await event.client.send_file(
event.chat_id,
out_file,
force_document=True,
allow_cache=False,
caption="BlackLists in the Current Chat",
reply_to=event
)
await event.delete()
else:
await event.edit(OUT_STR)
@borg.on(slitu.admin_cmd(pattern="rmblacklist ((.|\n)*)"))
async def on_delete_blacklist(event):
text = event.pattern_match.group(1)
to_unblacklist = list(
{trigger.strip() for trigger in text.split("\n") if trigger.strip()}
)
successful = 0
for trigger in to_unblacklist:
if sql.rm_from_blacklist(event.chat_id, trigger.lower()):
successful += 1
await event.edit(f"Removed {successful} / {len(to_unblacklist)} from the blacklist")