File size: 21,874 Bytes
056f521 |
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 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 |
import html
from telegram import ChatPermissions, Update
from telegram.constants import ParseMode
from telegram.error import BadRequest
from telegram.ext import CommandHandler, ContextTypes, MessageHandler, filters
from telegram.helpers import mention_html, mention_markdown
import Database.sql.blsticker_sql as sql
from Mikobot import LOGGER, dispatcher
from Mikobot.plugins.connection import connected
from Mikobot.plugins.disable import DisableAbleCommandHandler
from Mikobot.plugins.helper_funcs.alternate import send_message
from Mikobot.plugins.helper_funcs.chat_status import check_admin, user_not_admin
from Mikobot.plugins.helper_funcs.misc import split_message
from Mikobot.plugins.helper_funcs.string_handling import extract_time
from Mikobot.plugins.log_channel import loggable
from Mikobot.plugins.warns import warn
async def blackliststicker(update: Update, context: ContextTypes.DEFAULT_TYPE):
msg = update.effective_message # type: Optional[Message]
chat = update.effective_chat # type: Optional[Chat]
user = update.effective_user # type: Optional[User]
bot, args = context.bot, context.args
conn = await connected(bot, update, chat, user.id, need_admin=False)
if conn:
chat_id = conn
chat_obj = await dispatcher.bot.getChat(conn)
chat_name = chat_obj.title
else:
if chat.type == "private":
return
chat_id = update.effective_chat.id
chat_name = chat.title
sticker_list = "<b>List blacklisted stickers currently in {}:</b>\n".format(
chat_name,
)
all_stickerlist = sql.get_chat_stickers(chat_id)
if len(args) > 0 and args[0].lower() == "copy":
for trigger in all_stickerlist:
sticker_list += "<code>{}</code>\n".format(html.escape(trigger))
elif len(args) == 0:
for trigger in all_stickerlist:
sticker_list += " - <code>{}</code>\n".format(html.escape(trigger))
split_text = split_message(sticker_list)
for text in split_text:
if sticker_list == "<b>List blacklisted stickers currently in {}:</b>\n".format(
chat_name,
).format(html.escape(chat_name)):
await send_message(
update.effective_message,
"There are no blacklist stickers in <b>{}</b>!".format(
html.escape(chat_name),
),
parse_mode=ParseMode.HTML,
)
return
await send_message(
update.effective_message,
text,
parse_mode=ParseMode.HTML,
)
@check_admin(is_user=True)
async def add_blackliststicker(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot = context.bot
msg = update.effective_message # type: Optional[Message]
chat = update.effective_chat # type: Optional[Chat]
user = update.effective_user # type: Optional[User]
words = msg.text.split(None, 1)
bot = context.bot
conn = await connected(bot, update, chat, user.id)
if conn:
chat_id = conn
chat_obj = await dispatcher.bot.getChat(conn)
chat_name = chat_obj.title
else:
chat_id = update.effective_chat.id
if chat.type == "private":
return
else:
chat_name = chat.title
if len(words) > 1:
text = words[1].replace("https://t.me/addstickers/", "")
to_blacklist = list(
{trigger.strip() for trigger in text.split("\n") if trigger.strip()},
)
added = 0
for trigger in to_blacklist:
try:
get = await bot.getStickerSet(trigger)
sql.add_to_stickers(chat_id, trigger.lower())
added += 1
except BadRequest:
await send_message(
update.effective_message,
"Sticker `{}` can not be found!".format(trigger),
parse_mode="markdown",
)
if added == 0:
return
if len(to_blacklist) == 1:
await send_message(
update.effective_message,
"Sticker <code>{}</code> added to blacklist stickers in <b>{}</b>!".format(
html.escape(to_blacklist[0]),
html.escape(chat_name),
),
parse_mode=ParseMode.HTML,
)
else:
await send_message(
update.effective_message,
"<code>{}</code> stickers added to blacklist sticker in <b>{}</b>!".format(
added,
html.escape(chat_name),
),
parse_mode=ParseMode.HTML,
)
elif msg.reply_to_message:
added = 0
trigger = msg.reply_to_message.sticker.set_name
if trigger is None:
await send_message(
update.effective_message,
"Sticker is invalid!",
)
return
try:
get = await bot.getStickerSet(trigger)
sql.add_to_stickers(chat_id, trigger.lower())
added += 1
except BadRequest:
await send_message(
update.effective_message,
"Sticker `{}` can not be found!".format(trigger),
parse_mode="markdown",
)
if added == 0:
return
await send_message(
update.effective_message,
"Sticker <code>{}</code> added to blacklist stickers in <b>{}</b>!".format(
trigger,
html.escape(chat_name),
),
parse_mode=ParseMode.HTML,
)
else:
await send_message(
update.effective_message,
"Tell me what stickers you want to add to the blacklist.",
)
@check_admin(is_user=True)
async def unblackliststicker(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot = context.bot
msg = update.effective_message # type: Optional[Message]
chat = update.effective_chat # type: Optional[Chat]
user = update.effective_user # type: Optional[User]
words = msg.text.split(None, 1)
bot = context.bot
conn = await connected(bot, update, chat, user.id)
if conn:
chat_id = conn
chat_obj = await dispatcher.bot.getChat(conn)
chat_name = chat_obj.title
else:
chat_id = update.effective_chat.id
if chat.type == "private":
return
else:
chat_name = chat.title
if len(words) > 1:
text = words[1].replace("https://t.me/addstickers/", "")
to_unblacklist = list(
{trigger.strip() for trigger in text.split("\n") if trigger.strip()},
)
successful = 0
for trigger in to_unblacklist:
success = sql.rm_from_stickers(chat_id, trigger.lower())
if success:
successful += 1
if len(to_unblacklist) == 1:
if successful:
await send_message(
update.effective_message,
"Sticker <code>{}</code> deleted from blacklist in <b>{}</b>!".format(
html.escape(to_unblacklist[0]),
html.escape(chat_name),
),
parse_mode=ParseMode.HTML,
)
else:
await send_message(
update.effective_message,
"This sticker is not on the blacklist...!",
)
elif successful == len(to_unblacklist):
await send_message(
update.effective_message,
"Sticker <code>{}</code> deleted from blacklist in <b>{}</b>!".format(
successful,
html.escape(chat_name),
),
parse_mode=ParseMode.HTML,
)
elif not successful:
await send_message(
update.effective_message,
"None of these stickers exist, so they cannot be removed.",
parse_mode=ParseMode.HTML,
)
else:
await send_message(
update.effective_message,
"Sticker <code>{}</code> deleted from blacklist. {} did not exist, so it's not deleted.".format(
successful,
len(to_unblacklist) - successful,
),
parse_mode=ParseMode.HTML,
)
elif msg.reply_to_message:
trigger = msg.reply_to_message.sticker.set_name
if trigger is None:
await send_message(
update.effective_message,
"Sticker is invalid!",
)
return
success = sql.rm_from_stickers(chat_id, trigger.lower())
if success:
await send_message(
update.effective_message,
"Sticker <code>{}</code> deleted from blacklist in <b>{}</b>!".format(
trigger,
chat_name,
),
parse_mode=ParseMode.HTML,
)
else:
await send_message(
update.effective_message,
"{} not found on blacklisted stickers...!".format(trigger),
)
else:
await send_message(
update.effective_message,
"Tell me what stickers you want to add to the blacklist.",
)
@loggable
@check_admin(is_user=True)
async def blacklist_mode(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat = update.effective_chat # type: Optional[Chat]
user = update.effective_user # type: Optional[User]
msg = update.effective_message # type: Optional[Message]
bot, args = context.bot, context.args
conn = await connected(bot, update, chat, user.id, need_admin=True)
if conn:
chat = await dispatcher.bot.getChat(conn)
chat_id = conn
chat_obj = await dispatcher.bot.getChat(conn)
chat_name = chat_obj.title
else:
if update.effective_message.chat.type == "private":
await send_message(
update.effective_message,
"You can do this command in groups, not PM",
)
return ""
chat = update.effective_chat
chat_id = update.effective_chat.id
chat_name = update.effective_message.chat.title
if args:
if args[0].lower() in ["off", "nothing", "no"]:
settypeblacklist = "turn off"
sql.set_blacklist_strength(chat_id, 0, "0")
elif args[0].lower() in ["del", "delete"]:
settypeblacklist = "left, the message will be deleted"
sql.set_blacklist_strength(chat_id, 1, "0")
elif args[0].lower() == "warn":
settypeblacklist = "warned"
sql.set_blacklist_strength(chat_id, 2, "0")
elif args[0].lower() == "mute":
settypeblacklist = "muted"
sql.set_blacklist_strength(chat_id, 3, "0")
elif args[0].lower() == "kick":
settypeblacklist = "kicked"
sql.set_blacklist_strength(chat_id, 4, "0")
elif args[0].lower() == "ban":
settypeblacklist = "banned"
sql.set_blacklist_strength(chat_id, 5, "0")
elif args[0].lower() == "tban":
if len(args) == 1:
teks = """It looks like you are trying to set a temporary value to blacklist, but has not determined the time; use `/blstickermode tban <timevalue>`.
Examples of time values: 4m = 4 minute, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks."""
await send_message(
update.effective_message,
teks,
parse_mode="markdown",
)
return
settypeblacklist = "temporary banned for {}".format(args[1])
sql.set_blacklist_strength(chat_id, 6, str(args[1]))
elif args[0].lower() == "tmute":
if len(args) == 1:
teks = """It looks like you are trying to set a temporary value to blacklist, but has not determined the time; use `/blstickermode tmute <timevalue>`.
Examples of time values: 4m = 4 minute, 3h = 3 hours, 6d = 6 days, 5w = 5 weeks."""
await send_message(
update.effective_message,
teks,
parse_mode="markdown",
)
return
settypeblacklist = "temporary muted for {}".format(args[1])
sql.set_blacklist_strength(chat_id, 7, str(args[1]))
else:
await send_message(
update.effective_message,
"I only understand off/del/warn/ban/kick/mute/tban/tmute!",
)
return
if conn:
text = "Blacklist sticker mode changed, users will be `{}` at *{}*!".format(
settypeblacklist,
chat_name,
)
else:
text = "Blacklist sticker mode changed, users will be `{}`!".format(
settypeblacklist,
)
await send_message(
update.effective_message,
text,
parse_mode="markdown",
)
return (
"<b>{}:</b>\n"
"<b>Admin:</b> {}\n"
"Changed sticker blacklist mode. users will be {}.".format(
html.escape(chat.title),
mention_html(user.id, html.escape(user.first_name)),
settypeblacklist,
)
)
else:
getmode, getvalue = sql.get_blacklist_setting(chat.id)
if getmode == 0:
settypeblacklist = "not active"
elif getmode == 1:
settypeblacklist = "delete"
elif getmode == 2:
settypeblacklist = "warn"
elif getmode == 3:
settypeblacklist = "mute"
elif getmode == 4:
settypeblacklist = "kick"
elif getmode == 5:
settypeblacklist = "ban"
elif getmode == 6:
settypeblacklist = "temporarily banned for {}".format(getvalue)
elif getmode == 7:
settypeblacklist = "temporarily muted for {}".format(getvalue)
if conn:
text = "Blacklist sticker mode is currently set to *{}* in *{}*.".format(
settypeblacklist,
chat_name,
)
else:
text = "Blacklist sticker mode is currently set to *{}*.".format(
settypeblacklist,
)
await send_message(
update.effective_message,
text,
parse_mode=ParseMode.MARKDOWN,
)
return ""
@user_not_admin
async def del_blackliststicker(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot = context.bot
chat = update.effective_chat # type: Optional[Chat]
message = update.effective_message # type: Optional[Message]
user = update.effective_user
to_match = message.sticker
if not to_match or not to_match.set_name:
return
bot = context.bot
getmode, value = sql.get_blacklist_setting(chat.id)
chat_filters = sql.get_chat_stickers(chat.id)
for trigger in chat_filters:
if to_match.set_name.lower() == trigger.lower():
try:
if getmode == 0:
return
elif getmode == 1:
await message.delete()
elif getmode == 2:
await message.delete()
warn(
update.effective_user,
chat,
"Using sticker '{}' which in blacklist stickers".format(
trigger,
),
message,
update.effective_user,
# conn=False,
)
return
elif getmode == 3:
await message.delete()
await bot.restrict_chat_member(
chat.id,
update.effective_user.id,
permissions=ChatPermissions(can_send_messages=False),
)
await bot.sendMessage(
chat.id,
"{} muted because using '{}' which in blacklist stickers".format(
mention_markdown(user.id, user.first_name),
trigger,
),
parse_mode="markdown",
message_thread_id=(
message.message_thread_id if chat.is_forum else None
),
)
return
elif getmode == 4:
await message.delete()
res = chat.unban_member(update.effective_user.id)
if res:
await bot.sendMessage(
chat.id,
"{} kicked because using '{}' which in blacklist stickers".format(
mention_markdown(user.id, user.first_name),
trigger,
),
parse_mode="markdown",
message_thread_id=(
message.message_thread_id if chat.is_forum else None
),
)
return
elif getmode == 5:
await message.delete()
await chat.ban_member(user.id)
await bot.sendMessage(
chat.id,
"{} banned because using '{}' which in blacklist stickers".format(
mention_markdown(user.id, user.first_name),
trigger,
),
parse_mode="markdown",
message_thread_id=(
message.message_thread_id if chat.is_forum else None
),
)
return
elif getmode == 6:
await message.delete()
bantime = await extract_time(message, value)
await chat.ban_member(user.id, until_date=bantime)
await bot.sendMessage(
chat.id,
"{} banned for {} because using '{}' which in blacklist stickers".format(
mention_markdown(user.id, user.first_name),
value,
trigger,
),
parse_mode="markdown",
message_thread_id=(
message.message_thread_id if chat.is_forum else None
),
)
return
elif getmode == 7:
await message.delete()
mutetime = await extract_time(message, value)
await bot.restrict_chat_member(
chat.id,
user.id,
permissions=ChatPermissions(can_send_messages=False),
until_date=mutetime,
)
await bot.sendMessage(
chat.id,
"{} muted for {} because using '{}' which in blacklist stickers".format(
mention_markdown(user.id, user.first_name),
value,
trigger,
),
parse_mode="markdown",
message_thread_id=(
message.message_thread_id if chat.is_forum else None
),
)
return
except BadRequest as excp:
if excp.message != "Message to delete not found":
LOGGER.exception("Error while deleting blacklist message.")
break
async def __import_data__(chat_id, data, message):
# set chat blacklist
blacklist = data.get("sticker_blacklist", {})
for trigger in blacklist:
sql.add_to_stickers(chat_id, trigger)
def __migrate__(old_chat_id, new_chat_id):
sql.migrate_chat(old_chat_id, new_chat_id)
def __chat_settings__(chat_id, user_id):
blacklisted = sql.num_stickers_chat_filters(chat_id)
return "There are `{} `blacklisted stickers.".format(blacklisted)
def __stats__():
return "• {} blacklist stickers, across {} chats.".format(
sql.num_stickers_filters(),
sql.num_stickers_filter_chats(),
)
__mod_name__ = "Stickers Blacklist"
BLACKLIST_STICKER_HANDLER = DisableAbleCommandHandler(
"blsticker", blackliststicker, admin_ok=True, block=False
)
ADDBLACKLIST_STICKER_HANDLER = DisableAbleCommandHandler(
"addblsticker", add_blackliststicker, block=False
)
UNBLACKLIST_STICKER_HANDLER = CommandHandler(
["unblsticker", "rmblsticker"], unblackliststicker, block=False
)
BLACKLISTMODE_HANDLER = CommandHandler("blstickermode", blacklist_mode, block=False)
BLACKLIST_STICKER_DEL_HANDLER = MessageHandler(
filters.Sticker.ALL & filters.ChatType.GROUPS, del_blackliststicker, block=False
)
dispatcher.add_handler(BLACKLIST_STICKER_HANDLER)
dispatcher.add_handler(ADDBLACKLIST_STICKER_HANDLER)
dispatcher.add_handler(UNBLACKLIST_STICKER_HANDLER)
dispatcher.add_handler(BLACKLISTMODE_HANDLER)
dispatcher.add_handler(BLACKLIST_STICKER_DEL_HANDLER)
|