Merge pull request #34 from Infamous-Hydra/Infamous-Hydra-patch-6
Browse files- Mikobot/plugins/blacklist.py +522 -86
Mikobot/plugins/blacklist.py
CHANGED
@@ -1,112 +1,548 @@
|
|
1 |
# <============================================== IMPORTS =========================================================>
|
|
|
2 |
import re
|
3 |
-
from datetime import datetime, timedelta
|
4 |
|
5 |
-
from
|
6 |
-
from
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
from Mikobot import
|
14 |
-
from Mikobot.
|
15 |
-
from Mikobot.
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# <=======================================================================================================>
|
18 |
|
19 |
-
|
20 |
|
21 |
|
22 |
# <================================================ FUNCTION =======================================================>
|
23 |
-
@
|
24 |
-
@
|
25 |
-
async def
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
@app.on_message(filters.command("blacklisted") & ~filters.private)
|
37 |
-
@capture_err
|
38 |
-
async def get_filterss(_, message):
|
39 |
-
data = await get_blacklisted_words(message.chat.id)
|
40 |
-
if not data:
|
41 |
-
await message.reply_text("**No blacklisted words in this chat.**")
|
42 |
else:
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
if len(
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
return
|
74 |
-
if user.id
|
75 |
return
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
try:
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
|
98 |
# <=================================================== HELP ====================================================>
|
99 |
|
100 |
|
|
|
|
|
101 |
__help__ = """
|
102 |
-
❌ *Get your word/sentence blacklisted*
|
103 |
|
104 |
-
|
|
|
|
|
|
|
|
|
105 |
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
-
» /whitelist [WORD|SENTENCE] - Whitelist A Word Or A Sentence.
|
109 |
"""
|
110 |
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
# <================================================ END =======================================================>
|
|
|
1 |
# <============================================== IMPORTS =========================================================>
|
2 |
+
import html
|
3 |
import re
|
|
|
4 |
|
5 |
+
from telegram import ChatPermissions, Update
|
6 |
+
from telegram.constants import ParseMode
|
7 |
+
from telegram.error import BadRequest
|
8 |
+
from telegram.ext import CommandHandler, ContextTypes, MessageHandler, filters
|
9 |
+
from telegram.helpers import mention_html
|
10 |
|
11 |
+
import Database.sql.blacklist_sql as sql
|
12 |
+
from Database.sql.approve_sql import is_approved
|
13 |
+
from Mikobot import LOGGER, dispatcher, function
|
14 |
+
from Mikobot.plugins.connection import connected
|
15 |
+
from Mikobot.plugins.disable import DisableAbleCommandHandler
|
16 |
+
from Mikobot.plugins.helper_funcs.alternate import send_message, typing_action
|
17 |
+
from Mikobot.plugins.helper_funcs.chat_status import check_admin, user_not_admin
|
18 |
+
from Mikobot.plugins.helper_funcs.extraction import extract_text
|
19 |
+
from Mikobot.plugins.helper_funcs.misc import split_message
|
20 |
+
from Mikobot.plugins.helper_funcs.string_handling import extract_time
|
21 |
+
from Mikobot.plugins.log_channel import loggable
|
22 |
+
from Mikobot.plugins.warns import warn
|
23 |
|
24 |
# <=======================================================================================================>
|
25 |
|
26 |
+
BLACKLIST_GROUP = 11
|
27 |
|
28 |
|
29 |
# <================================================ FUNCTION =======================================================>
|
30 |
+
@check_admin(is_user=True)
|
31 |
+
@typing_action
|
32 |
+
async def blacklist(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
33 |
+
chat = update.effective_chat
|
34 |
+
user = update.effective_user
|
35 |
+
args = context.args
|
36 |
+
|
37 |
+
conn = await connected(context.bot, update, chat, user.id, need_admin=False)
|
38 |
+
if conn:
|
39 |
+
chat_id = conn
|
40 |
+
chat_obj = await dispatcher.bot.getChat(conn)
|
41 |
+
chat_name = chat_obj.title
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
else:
|
43 |
+
if chat.type == "private":
|
44 |
+
return
|
45 |
+
chat_id = update.effective_chat.id
|
46 |
+
chat_name = chat.title
|
47 |
+
|
48 |
+
filter_list = "Current blacklisted words in <b>{}</b>:\n".format(chat_name)
|
49 |
+
|
50 |
+
all_blacklisted = sql.get_chat_blacklist(chat_id)
|
51 |
+
|
52 |
+
if len(args) > 0 and args[0].lower() == "copy":
|
53 |
+
for trigger in all_blacklisted:
|
54 |
+
filter_list += "<code>{}</code>\n".format(html.escape(trigger))
|
55 |
+
else:
|
56 |
+
for trigger in all_blacklisted:
|
57 |
+
filter_list += " - <code>{}</code>\n".format(html.escape(trigger))
|
58 |
+
|
59 |
+
# for trigger in all_blacklisted:
|
60 |
+
# filter_list += " - <code>{}</code>\n".format(html.escape(trigger))
|
61 |
+
|
62 |
+
split_text = split_message(filter_list)
|
63 |
+
for text in split_text:
|
64 |
+
if filter_list == "Current blacklisted words in <b>{}</b>:\n".format(
|
65 |
+
html.escape(chat_name),
|
66 |
+
):
|
67 |
+
await send_message(
|
68 |
+
update.effective_message,
|
69 |
+
"No blacklisted words in <b>{}</b>!".format(html.escape(chat_name)),
|
70 |
+
parse_mode=ParseMode.HTML,
|
71 |
+
)
|
72 |
+
return
|
73 |
+
await send_message(update.effective_message, text, parse_mode=ParseMode.HTML)
|
74 |
+
|
75 |
+
|
76 |
+
@check_admin(is_user=True)
|
77 |
+
@typing_action
|
78 |
+
async def add_blacklist(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
79 |
+
msg = update.effective_message
|
80 |
+
chat = update.effective_chat
|
81 |
+
user = update.effective_user
|
82 |
+
words = msg.text.split(None, 1)
|
83 |
+
|
84 |
+
conn = await connected(context.bot, update, chat, user.id)
|
85 |
+
if conn:
|
86 |
+
chat_id = conn
|
87 |
+
chat_obj = await dispatcher.bot.getChat(conn)
|
88 |
+
chat_name = chat_obj.title
|
89 |
+
else:
|
90 |
+
chat_id = update.effective_chat.id
|
91 |
+
if chat.type == "private":
|
92 |
+
return
|
93 |
+
else:
|
94 |
+
chat_name = chat.title
|
95 |
+
|
96 |
+
if len(words) > 1:
|
97 |
+
text = words[1]
|
98 |
+
to_blacklist = list(
|
99 |
+
{trigger.strip() for trigger in text.split("\n") if trigger.strip()},
|
100 |
+
)
|
101 |
+
for trigger in to_blacklist:
|
102 |
+
sql.add_to_blacklist(chat_id, trigger.lower())
|
103 |
+
|
104 |
+
if len(to_blacklist) == 1:
|
105 |
+
await send_message(
|
106 |
+
update.effective_message,
|
107 |
+
"Added blacklist <code>{}</code> in chat: <b>{}</b>!".format(
|
108 |
+
html.escape(to_blacklist[0]),
|
109 |
+
html.escape(chat_name),
|
110 |
+
),
|
111 |
+
parse_mode=ParseMode.HTML,
|
112 |
+
)
|
113 |
+
|
114 |
+
else:
|
115 |
+
await send_message(
|
116 |
+
update.effective_message,
|
117 |
+
"Added blacklist trigger: <code>{}</code> in <b>{}</b>!".format(
|
118 |
+
len(to_blacklist),
|
119 |
+
html.escape(chat_name),
|
120 |
+
),
|
121 |
+
parse_mode=ParseMode.HTML,
|
122 |
+
)
|
123 |
+
|
124 |
+
else:
|
125 |
+
await send_message(
|
126 |
+
update.effective_message,
|
127 |
+
"Tell me which words you would like to add in blacklist.",
|
128 |
+
)
|
129 |
+
|
130 |
+
|
131 |
+
@check_admin(is_user=True)
|
132 |
+
@typing_action
|
133 |
+
async def unblacklist(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
134 |
+
msg = update.effective_message
|
135 |
+
chat = update.effective_chat
|
136 |
+
user = update.effective_user
|
137 |
+
words = msg.text.split(None, 1)
|
138 |
+
|
139 |
+
conn = await connected(context.bot, update, chat, user.id)
|
140 |
+
if conn:
|
141 |
+
chat_id = conn
|
142 |
+
chat_obj = await dispatcher.bot.getChat(conn)
|
143 |
+
chat_name = chat_obj.title
|
144 |
+
else:
|
145 |
+
chat_id = update.effective_chat.id
|
146 |
+
if chat.type == "private":
|
147 |
+
return
|
148 |
+
else:
|
149 |
+
chat_name = chat.title
|
150 |
+
|
151 |
+
if len(words) > 1:
|
152 |
+
text = words[1]
|
153 |
+
to_unblacklist = list(
|
154 |
+
{trigger.strip() for trigger in text.split("\n") if trigger.strip()},
|
155 |
+
)
|
156 |
+
successful = 0
|
157 |
+
for trigger in to_unblacklist:
|
158 |
+
success = sql.rm_from_blacklist(chat_id, trigger.lower())
|
159 |
+
if success:
|
160 |
+
successful += 1
|
161 |
+
|
162 |
+
if len(to_unblacklist) == 1:
|
163 |
+
if successful:
|
164 |
+
await send_message(
|
165 |
+
update.effective_message,
|
166 |
+
"Removed <code>{}</code> from blacklist in <b>{}</b>!".format(
|
167 |
+
html.escape(to_unblacklist[0]),
|
168 |
+
html.escape(chat_name),
|
169 |
+
),
|
170 |
+
parse_mode=ParseMode.HTML,
|
171 |
+
)
|
172 |
+
else:
|
173 |
+
await send_message(
|
174 |
+
update.effective_message,
|
175 |
+
"This is not a blacklist trigger!",
|
176 |
+
)
|
177 |
+
|
178 |
+
elif successful == len(to_unblacklist):
|
179 |
+
await send_message(
|
180 |
+
update.effective_message,
|
181 |
+
"Removed <code>{}</code> from blacklist in <b>{}</b>!".format(
|
182 |
+
successful,
|
183 |
+
html.escape(chat_name),
|
184 |
+
),
|
185 |
+
parse_mode=ParseMode.HTML,
|
186 |
+
)
|
187 |
+
|
188 |
+
elif not successful:
|
189 |
+
await send_message(
|
190 |
+
update.effective_message,
|
191 |
+
"None of these triggers exist so it can't be removed.",
|
192 |
+
parse_mode=ParseMode.HTML,
|
193 |
+
)
|
194 |
+
|
195 |
+
else:
|
196 |
+
await send_message(
|
197 |
+
update.effective_message,
|
198 |
+
"Removed <code>{}</code> from blacklist. {} did not exist, "
|
199 |
+
"so were not removed.".format(
|
200 |
+
successful,
|
201 |
+
len(to_unblacklist) - successful,
|
202 |
+
),
|
203 |
+
parse_mode=ParseMode.HTML,
|
204 |
+
)
|
205 |
+
else:
|
206 |
+
await send_message(
|
207 |
+
update.effective_message,
|
208 |
+
"Tell me which words you would like to remove from blacklist!",
|
209 |
+
)
|
210 |
+
|
211 |
+
|
212 |
+
@loggable
|
213 |
+
@check_admin(is_user=True)
|
214 |
+
@typing_action
|
215 |
+
async def blacklist_mode(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
216 |
+
chat = update.effective_chat
|
217 |
+
user = update.effective_user
|
218 |
+
msg = update.effective_message
|
219 |
+
args = context.args
|
220 |
+
|
221 |
+
conn = await connected(context.bot, update, chat, user.id, need_admin=True)
|
222 |
+
if conn:
|
223 |
+
chat = await dispatcher.bot.getChat(conn)
|
224 |
+
chat_id = conn
|
225 |
+
chat_obj = await dispatcher.bot.getChat(conn)
|
226 |
+
chat_name = chat_obj.title
|
227 |
+
else:
|
228 |
+
if update.effective_message.chat.type == "private":
|
229 |
+
await send_message(
|
230 |
+
update.effective_message,
|
231 |
+
"This command can be only used in group not in PM",
|
232 |
+
)
|
233 |
+
return ""
|
234 |
+
chat = update.effective_chat
|
235 |
+
chat_id = update.effective_chat.id
|
236 |
+
chat_name = update.effective_message.chat.title
|
237 |
+
|
238 |
+
if args:
|
239 |
+
if args[0].lower() in ["off", "nothing", "no"]:
|
240 |
+
settypeblacklist = "do nothing"
|
241 |
+
sql.set_blacklist_strength(chat_id, 0, "0")
|
242 |
+
elif args[0].lower() in ["del", "delete"]:
|
243 |
+
settypeblacklist = "delete blacklisted message"
|
244 |
+
sql.set_blacklist_strength(chat_id, 1, "0")
|
245 |
+
elif args[0].lower() == "warn":
|
246 |
+
settypeblacklist = "warn the sender"
|
247 |
+
sql.set_blacklist_strength(chat_id, 2, "0")
|
248 |
+
elif args[0].lower() == "mute":
|
249 |
+
settypeblacklist = "mute the sender"
|
250 |
+
sql.set_blacklist_strength(chat_id, 3, "0")
|
251 |
+
elif args[0].lower() == "kick":
|
252 |
+
settypeblacklist = "kick the sender"
|
253 |
+
sql.set_blacklist_strength(chat_id, 4, "0")
|
254 |
+
elif args[0].lower() == "ban":
|
255 |
+
settypeblacklist = "ban the sender"
|
256 |
+
sql.set_blacklist_strength(chat_id, 5, "0")
|
257 |
+
elif args[0].lower() == "tban":
|
258 |
+
if len(args) == 1:
|
259 |
+
teks = """It looks like you tried to set time value for blacklist but you didn't specified time; Try, `/blacklistmode tban <timevalue>`.
|
260 |
+
|
261 |
+
Examples of time value: 4m = 4 minutes, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks."""
|
262 |
+
await send_message(
|
263 |
+
update.effective_message, teks, parse_mode="markdown"
|
264 |
+
)
|
265 |
+
return ""
|
266 |
+
restime = await extract_time(msg, args[1])
|
267 |
+
if not restime:
|
268 |
+
teks = """Invalid time value!
|
269 |
+
Example of time value: 4m = 4 minutes, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks."""
|
270 |
+
await send_message(
|
271 |
+
update.effective_message, teks, parse_mode="markdown"
|
272 |
+
)
|
273 |
+
return ""
|
274 |
+
settypeblacklist = "temporarily ban for {}".format(args[1])
|
275 |
+
sql.set_blacklist_strength(chat_id, 6, str(args[1]))
|
276 |
+
elif args[0].lower() == "tmute":
|
277 |
+
if len(args) == 1:
|
278 |
+
teks = """It looks like you tried to set time value for blacklist but you didn't specified time; try, `/blacklistmode tmute <timevalue>`.
|
279 |
+
|
280 |
+
Examples of time value: 4m = 4 minutes, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks."""
|
281 |
+
await send_message(
|
282 |
+
update.effective_message, teks, parse_mode="markdown"
|
283 |
+
)
|
284 |
+
return ""
|
285 |
+
restime = await extract_time(msg, args[1])
|
286 |
+
if not restime:
|
287 |
+
teks = """Invalid time value!
|
288 |
+
Examples of time value: 4m = 4 minutes, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks."""
|
289 |
+
await send_message(
|
290 |
+
update.effective_message, teks, parse_mode="markdown"
|
291 |
+
)
|
292 |
+
return ""
|
293 |
+
settypeblacklist = "temporarily mute for {}".format(args[1])
|
294 |
+
sql.set_blacklist_strength(chat_id, 7, str(args[1]))
|
295 |
+
else:
|
296 |
+
await send_message(
|
297 |
+
update.effective_message,
|
298 |
+
"I only understand: off/del/warn/ban/kick/mute/tban/tmute!",
|
299 |
+
)
|
300 |
+
return ""
|
301 |
+
if conn:
|
302 |
+
text = "Changed blacklist mode: `{}` in *{}*!".format(
|
303 |
+
settypeblacklist,
|
304 |
+
chat_name,
|
305 |
+
)
|
306 |
+
else:
|
307 |
+
text = "Changed blacklist mode: `{}`!".format(settypeblacklist)
|
308 |
+
await send_message(update.effective_message, text, parse_mode="markdown")
|
309 |
+
return (
|
310 |
+
"<b>{}:</b>\n"
|
311 |
+
"<b>Admin:</b> {}\n"
|
312 |
+
"Changed the blacklist mode. will {}.".format(
|
313 |
+
html.escape(chat.title),
|
314 |
+
mention_html(user.id, html.escape(user.first_name)),
|
315 |
+
settypeblacklist,
|
316 |
+
)
|
317 |
+
)
|
318 |
+
else:
|
319 |
+
getmode, getvalue = sql.get_blacklist_setting(chat.id)
|
320 |
+
if getmode == 0:
|
321 |
+
settypeblacklist = "do nothing"
|
322 |
+
elif getmode == 1:
|
323 |
+
settypeblacklist = "delete"
|
324 |
+
elif getmode == 2:
|
325 |
+
settypeblacklist = "warn"
|
326 |
+
elif getmode == 3:
|
327 |
+
settypeblacklist = "mute"
|
328 |
+
elif getmode == 4:
|
329 |
+
settypeblacklist = "kick"
|
330 |
+
elif getmode == 5:
|
331 |
+
settypeblacklist = "ban"
|
332 |
+
elif getmode == 6:
|
333 |
+
settypeblacklist = "temporarily ban for {}".format(getvalue)
|
334 |
+
elif getmode == 7:
|
335 |
+
settypeblacklist = "temporarily mute for {}".format(getvalue)
|
336 |
+
if conn:
|
337 |
+
text = "Current blacklistmode: *{}* in *{}*.".format(
|
338 |
+
settypeblacklist,
|
339 |
+
chat_name,
|
340 |
+
)
|
341 |
+
else:
|
342 |
+
text = "Current blacklistmode: *{}*.".format(settypeblacklist)
|
343 |
+
await send_message(
|
344 |
+
update.effective_message, text, parse_mode=ParseMode.MARKDOWN
|
345 |
+
)
|
346 |
+
return ""
|
347 |
+
|
348 |
+
|
349 |
+
def findall(p, s):
|
350 |
+
i = s.find(p)
|
351 |
+
while i != -1:
|
352 |
+
yield i
|
353 |
+
i = s.find(p, i + 1)
|
354 |
+
|
355 |
+
|
356 |
+
@user_not_admin
|
357 |
+
async def del_blacklist(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
358 |
+
chat = update.effective_chat
|
359 |
+
message = update.effective_message
|
360 |
+
user = update.effective_user
|
361 |
+
bot = context.bot
|
362 |
+
to_match = await extract_text(message)
|
363 |
+
if not to_match:
|
364 |
return
|
365 |
+
if is_approved(chat.id, user.id):
|
366 |
return
|
367 |
+
getmode, value = sql.get_blacklist_setting(chat.id)
|
368 |
+
|
369 |
+
chat_filters = sql.get_chat_blacklist(chat.id)
|
370 |
+
for trigger in chat_filters:
|
371 |
+
pattern = r"( |^|[^\w])" + re.escape(trigger) + r"( |$|[^\w])"
|
372 |
+
if re.search(pattern, to_match, flags=re.IGNORECASE):
|
373 |
try:
|
374 |
+
if getmode == 0:
|
375 |
+
return
|
376 |
+
elif getmode == 1:
|
377 |
+
try:
|
378 |
+
await message.delete()
|
379 |
+
except BadRequest:
|
380 |
+
pass
|
381 |
+
elif getmode == 2:
|
382 |
+
try:
|
383 |
+
await message.delete()
|
384 |
+
except BadRequest:
|
385 |
+
pass
|
386 |
+
warn(
|
387 |
+
update.effective_user,
|
388 |
+
chat,
|
389 |
+
("Using blacklisted trigger: {}".format(trigger)),
|
390 |
+
message,
|
391 |
+
update.effective_user,
|
392 |
+
)
|
393 |
+
return
|
394 |
+
elif getmode == 3:
|
395 |
+
await message.delete()
|
396 |
+
await bot.restrict_chat_member(
|
397 |
+
chat.id,
|
398 |
+
update.effective_user.id,
|
399 |
+
permissions=ChatPermissions(can_send_messages=False),
|
400 |
+
)
|
401 |
+
await bot.sendMessage(
|
402 |
+
chat.id,
|
403 |
+
f"Muted {user.first_name} for using Blacklisted word: {trigger}!",
|
404 |
+
message_thread_id=message.message_thread_id
|
405 |
+
if chat.is_forum
|
406 |
+
else None,
|
407 |
+
)
|
408 |
+
return
|
409 |
+
elif getmode == 4:
|
410 |
+
await message.delete()
|
411 |
+
res = chat.unban_member(update.effective_user.id)
|
412 |
+
if res:
|
413 |
+
await bot.sendMessage(
|
414 |
+
chat.id,
|
415 |
+
f"Kicked {user.first_name} for using Blacklisted word: {trigger}!",
|
416 |
+
message_thread_id=message.message_thread_id
|
417 |
+
if chat.is_forum
|
418 |
+
else None,
|
419 |
+
)
|
420 |
+
return
|
421 |
+
elif getmode == 5:
|
422 |
+
await message.delete()
|
423 |
+
await chat.ban_member(user.id)
|
424 |
+
await bot.sendMessage(
|
425 |
+
chat.id,
|
426 |
+
f"Banned {user.first_name} for using Blacklisted word: {trigger}",
|
427 |
+
message_thread_id=message.message_thread_id
|
428 |
+
if chat.is_forum
|
429 |
+
else None,
|
430 |
+
)
|
431 |
+
return
|
432 |
+
elif getmode == 6:
|
433 |
+
await message.delete()
|
434 |
+
bantime = await extract_time(message, value)
|
435 |
+
await chat.ban_member(user.id, until_date=bantime)
|
436 |
+
await bot.sendMessage(
|
437 |
+
chat.id,
|
438 |
+
f"Banned {user.first_name} until '{value}' for using Blacklisted word: {trigger}!",
|
439 |
+
message_thread_id=message.message_thread_id
|
440 |
+
if chat.is_forum
|
441 |
+
else None,
|
442 |
+
)
|
443 |
+
return
|
444 |
+
elif getmode == 7:
|
445 |
+
await message.delete()
|
446 |
+
mutetime = await extract_time(message, value)
|
447 |
+
await bot.restrict_chat_member(
|
448 |
+
chat.id,
|
449 |
+
user.id,
|
450 |
+
until_date=mutetime,
|
451 |
+
permissions=ChatPermissions(can_send_messages=False),
|
452 |
+
)
|
453 |
+
await bot.sendMessage(
|
454 |
+
chat.id,
|
455 |
+
f"Muted {user.first_name} until '{value}' for using Blacklisted word: {trigger}!",
|
456 |
+
message_thread_id=message.message_thread_id
|
457 |
+
if chat.is_forum
|
458 |
+
else None,
|
459 |
+
)
|
460 |
+
return
|
461 |
+
except BadRequest as excp:
|
462 |
+
if excp.message != "Message to delete not found":
|
463 |
+
LOGGER.exception("Error while deleting blacklist message.")
|
464 |
+
break
|
465 |
+
|
466 |
+
|
467 |
+
async def __import_data__(chat_id, data, message):
|
468 |
+
# set chat blacklist
|
469 |
+
blacklist = data.get("blacklist", {})
|
470 |
+
for trigger in blacklist:
|
471 |
+
sql.add_to_blacklist(chat_id, trigger)
|
472 |
+
|
473 |
+
|
474 |
+
def __migrate__(old_chat_id, new_chat_id):
|
475 |
+
sql.migrate_chat(old_chat_id, new_chat_id)
|
476 |
+
|
477 |
+
|
478 |
+
def __chat_settings__(chat_id, user_id):
|
479 |
+
blacklisted = sql.num_blacklist_chat_filters(chat_id)
|
480 |
+
return "There are {} blacklisted words.".format(blacklisted)
|
481 |
+
|
482 |
+
|
483 |
+
def __stats__():
|
484 |
+
return "• {} blacklist triggers, across {} chats.".format(
|
485 |
+
sql.num_blacklist_filters(),
|
486 |
+
sql.num_blacklist_filter_chats(),
|
487 |
+
)
|
488 |
|
489 |
|
490 |
# <=================================================== HELP ====================================================>
|
491 |
|
492 |
|
493 |
+
__mod_name__ = "BLACKLIST"
|
494 |
+
|
495 |
__help__ = """
|
|
|
496 |
|
497 |
+
➠ Blacklists are used to stop certain triggers from being said in a group. Any time the trigger is mentioned, the message will immediately be deleted. A good combo is sometimes to pair this up with warn filters!
|
498 |
+
|
499 |
+
➠ *NOTE*: Blacklists do not affect group admins.
|
500 |
+
|
501 |
+
» /blacklist: View the current blacklisted words.
|
502 |
|
503 |
+
Admin only:
|
504 |
+
» /addblacklist <triggers>: Add a trigger to the blacklist. Each line is considered one trigger, so using different lines will allow you to add multiple triggers.
|
505 |
+
» /unblacklist <triggers>: Remove triggers from the blacklist. Same newline logic applies here, so you can remove multiple triggers at once.
|
506 |
+
» /blacklistmode <off/del/warn/ban/kick/mute/tban/tmute>: Action to perform when someone sends blacklisted words.
|
507 |
+
|
508 |
+
➠ Blacklist sticker is used to stop certain stickers. Whenever a sticker is sent, the message will be deleted immediately.
|
509 |
+
➠ *NOTE:* Blacklist stickers do not affect the group admin
|
510 |
+
» /blsticker: See current blacklisted sticker
|
511 |
+
➠ *Only admin:*
|
512 |
+
» /addblsticker <sticker link>: Add the sticker trigger to the black list. Can be added via reply sticker
|
513 |
+
» /unblsticker <sticker link>: Remove triggers from blacklist. The same newline logic applies here, so you can delete multiple triggers at once
|
514 |
+
» /rmblsticker <sticker link>: Same as above
|
515 |
+
» /blstickermode <delete/ban/tban/mute/tmute>: sets up a default action on what to do if users use blacklisted stickers
|
516 |
+
Note:
|
517 |
+
» <sticker link> can be https://t.me/addstickers/<sticker> or just <sticker> or reply to the sticker message
|
518 |
|
|
|
519 |
"""
|
520 |
|
521 |
+
# <================================================ HANDLER =======================================================>
|
522 |
+
BLACKLIST_HANDLER = DisableAbleCommandHandler(
|
523 |
+
"blacklist", blacklist, admin_ok=True, block=False
|
524 |
+
)
|
525 |
+
ADD_BLACKLIST_HANDLER = CommandHandler("addblacklist", add_blacklist, block=False)
|
526 |
+
UNBLACKLIST_HANDLER = CommandHandler("unblacklist", unblacklist, block=False)
|
527 |
+
BLACKLISTMODE_HANDLER = CommandHandler("blacklistmode", blacklist_mode, block=False)
|
528 |
+
BLACKLIST_DEL_HANDLER = MessageHandler(
|
529 |
+
(filters.TEXT | filters.COMMAND | filters.Sticker.ALL | filters.PHOTO)
|
530 |
+
& filters.ChatType.GROUPS,
|
531 |
+
del_blacklist,
|
532 |
+
block=False,
|
533 |
+
)
|
534 |
+
|
535 |
+
function(BLACKLIST_HANDLER)
|
536 |
+
function(ADD_BLACKLIST_HANDLER)
|
537 |
+
function(UNBLACKLIST_HANDLER)
|
538 |
+
function(BLACKLISTMODE_HANDLER)
|
539 |
+
function(BLACKLIST_DEL_HANDLER, group=BLACKLIST_GROUP)
|
540 |
+
|
541 |
+
__handlers__ = [
|
542 |
+
BLACKLIST_HANDLER,
|
543 |
+
ADD_BLACKLIST_HANDLER,
|
544 |
+
UNBLACKLIST_HANDLER,
|
545 |
+
BLACKLISTMODE_HANDLER,
|
546 |
+
(BLACKLIST_DEL_HANDLER, BLACKLIST_GROUP),
|
547 |
+
]
|
548 |
# <================================================ END =======================================================>
|