File size: 18,194 Bytes
c7dfe8b f28056b c7dfe8b 32a799e c7dfe8b |
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 |
# <============================================== IMPORTS =========================================================>
import html
import time
from datetime import datetime
from io import BytesIO
from telegram import ChatMemberAdministrator, Update
from telegram.constants import ParseMode
from telegram.error import BadRequest, Forbidden, TelegramError
from telegram.ext import CommandHandler, ContextTypes, MessageHandler, filters
from telegram.helpers import mention_html
import Database.sql.global_bans_sql as sql
from Database.sql.users_sql import get_user_com_chats
from Mikobot import (
DEV_USERS,
DRAGONS,
EVENT_LOGS,
OWNER_ID,
STRICT_GBAN,
SUPPORT_CHAT,
dispatcher,
function,
)
from Mikobot.plugins.helper_funcs.chat_status import (
check_admin,
is_user_admin,
support_plus,
)
from Mikobot.plugins.helper_funcs.extraction import extract_user, extract_user_and_text
from Mikobot.plugins.helper_funcs.misc import send_to_list
# <=======================================================================================================>
GBAN_ENFORCE_GROUP = 6
GBAN_ERRORS = {
"User is an administrator of the chat",
"Chat not found",
"Not enough rights to restrict/unrestrict chat member",
"User_not_participant",
"Peer_id_invalid",
"Group chat was deactivated",
"Need to be inviter of a user to kick it from a basic group",
"Chat_admin_required",
"Only the creator of a basic group can kick group administrators",
"Channel_private",
"Not in the chat",
"Can't remove chat owner",
}
UNGBAN_ERRORS = {
"User is an administrator of the chat",
"Chat not found",
"Not enough rights to restrict/unrestrict chat member",
"User_not_participant",
"Method is available for supergroup and channel chats only",
"Not in the chat",
"Channel_private",
"Chat_admin_required",
"Peer_id_invalid",
"User not found",
}
# <================================================ FUNCTION =======================================================>
@support_plus
async def gban(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
message = update.effective_message
user = update.effective_user
chat = update.effective_chat
log_message = ""
user_id, reason = await extract_user_and_text(message, context, args)
if not user_id:
await message.reply_text(
"You don't seem to be referring to a user or the ID specified is incorrect..",
)
return
if int(user_id) in DEV_USERS:
await message.reply_text(
"That user is part of the Association\nI can't act against our own.",
)
return
if int(user_id) in DRAGONS:
await message.reply_text(
"I spy, with my little eye... a disaster! Why are you guys turning on each other?",
)
return
if user_id == bot.id:
await message.reply_text("You uhh...want me to kick myself?")
return
if user_id in [777000, 1087968824]:
await message.reply_text("Fool! You can't attack Telegram's native tech!")
return
try:
user_chat = await bot.get_chat(user_id)
except BadRequest as excp:
if excp.message == "User not found":
await message.reply_text("I can't seem to find this user.")
return ""
else:
return
if user_chat.type != "private":
await message.reply_text("That's not a user!")
return
if sql.is_user_gbanned(user_id):
if not reason:
await message.reply_text(
"This user is already gbanned; I'd change the reason, but you haven't given me one...",
)
return
old_reason = sql.update_gban_reason(
user_id,
user_chat.username or user_chat.first_name,
reason,
)
if old_reason:
await message.reply_text(
"This user is already gbanned, for the following reason:\n"
"<code>{}</code>\n"
"I've gone and updated it with your new reason!".format(
html.escape(old_reason),
),
parse_mode=ParseMode.HTML,
)
else:
await message.reply_text(
"This user is already gbanned, but had no reason set; I've gone and updated it!",
)
return
await message.reply_text("On it!")
start_time = time.time()
datetime_fmt = "%Y-%m-%dT%H:%M"
current_time = datetime.utcnow().strftime(datetime_fmt)
if chat.type != "private":
chat_origin = "<b>{} ({})</b>\n".format(html.escape(chat.title), chat.id)
else:
chat_origin = "<b>{}</b>\n".format(chat.id)
log_message = (
f"#GBANNED\n"
f"<b>Originated from:</b> <code>{chat_origin}</code>\n"
f"<b>Admin:</b> {mention_html(user.id, user.first_name)}\n"
f"<b>Banned User:</b> {mention_html(user_chat.id, user_chat.first_name)}\n"
f"<b>Banned User ID:</b> <code>{user_chat.id}</code>\n"
f"<b>Event Stamp:</b> <code>{current_time}</code>"
)
if reason:
if chat.type == chat.SUPERGROUP and chat.username:
log_message += f'\n<b>Reason:</b> <a href="https://telegram.me/{chat.username}/{message.message_id}">{reason}</a>'
else:
log_message += f"\n<b>Reason:</b> <code>{reason}</code>"
if EVENT_LOGS:
try:
log = await bot.send_message(
EVENT_LOGS, log_message, parse_mode=ParseMode.HTML
)
except BadRequest as excp:
log = await bot.send_message(
EVENT_LOGS,
log_message
+ "\n\nFormatting has been disabled due to an unexpected error.",
)
else:
send_to_list(bot, DRAGONS, log_message, html=True)
sql.gban_user(user_id, user_chat.username or user_chat.first_name, reason)
chats = get_user_com_chats(user_id)
gbanned_chats = 0
for chat in chats:
chat_id = int(chat)
# Check if this group has disabled gbans
if not sql.does_chat_gban(chat_id):
continue
try:
await bot.ban_chat_member(chat_id, user_id)
gbanned_chats += 1
except BadRequest as excp:
if excp.message in GBAN_ERRORS:
pass
else:
await message.reply_text(f"Could not gban due to: {excp.message}")
if EVENT_LOGS:
await bot.send_message(
EVENT_LOGS,
f"Could not gban due to {excp.message}",
parse_mode=ParseMode.HTML,
)
else:
send_to_list(
bot,
DRAGONS,
f"Could not gban due to: {excp.message}",
)
sql.ungban_user(user_id)
return
except TelegramError:
pass
if EVENT_LOGS:
await log.edit_text(
log_message + f"\n<b>Chats affected:</b> <code>{gbanned_chats}</code>",
parse_mode=ParseMode.HTML,
)
else:
send_to_list(
bot,
DRAGONS,
f"Gban complete! (User banned in <code>{gbanned_chats}</code> chats)",
html=True,
)
end_time = time.time()
gban_time = round((end_time - start_time), 2)
if gban_time > 60:
gban_time = round((gban_time / 60), 2)
await message.reply_text("Done! Gbanned.", parse_mode=ParseMode.HTML)
else:
await message.reply_text("Done! Gbanned.", parse_mode=ParseMode.HTML)
try:
await bot.send_message(
user_id,
"#EVENT"
"You have been marked as Malicious and as such have been banned from any future groups we manage."
f"\n<b>Reason:</b> <code>{html.escape(user.reason)}</code>"
f"</b>Appeal Chat:</b> @{SUPPORT_CHAT}",
parse_mode=ParseMode.HTML,
)
except:
pass # bot probably blocked by user
@support_plus
async def ungban(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot, args = context.bot, context.args
message = update.effective_message
user = update.effective_user
chat = update.effective_chat
log_message = ""
user_id = await extract_user(message, context, args)
if not user_id:
await message.reply_text(
"You don't seem to be referring to a user or the ID specified is incorrect..",
)
return
user_chat = await bot.get_chat(user_id)
if user_chat.type != "private":
await message.reply_text("That's not a user!")
return
if not sql.is_user_gbanned(user_id):
await message.reply_text("This user is not gbanned!")
return
await message.reply_text(
f"I'll give {user_chat.first_name} a second chance, globally."
)
start_time = time.time()
datetime_fmt = "%Y-%m-%dT%H:%M"
current_time = datetime.utcnow().strftime(datetime_fmt)
if chat.type != "private":
chat_origin = f"<b>{html.escape(chat.title)} ({chat.id})</b>\n"
else:
chat_origin = f"<b>{chat.id}</b>\n"
log_message = (
f"#UNGBANNED\n"
f"<b>Originated from:</b> <code>{chat_origin}</code>\n"
f"<b>Admin:</b> {mention_html(user.id, user.first_name)}\n"
f"<b>Unbanned User:</b> {mention_html(user_chat.id, user_chat.first_name)}\n"
f"<b>Unbanned User ID:</b> <code>{user_chat.id}</code>\n"
f"<b>Event Stamp:</b> <code>{current_time}</code>"
)
if EVENT_LOGS:
try:
log = await bot.send_message(
EVENT_LOGS, log_message, parse_mode=ParseMode.HTML
)
except BadRequest as excp:
log = await bot.send_message(
EVENT_LOGS,
log_message
+ "\n\nFormatting has been disabled due to an unexpected error.",
)
else:
send_to_list(bot, DRAGONS, log_message, html=True)
chats = get_user_com_chats(user_id)
ungbanned_chats = 0
for chat in chats:
chat_id = int(chat)
# Check if this group has disabled gbans
if not sql.does_chat_gban(chat_id):
continue
try:
member = await bot.get_chat_member(chat_id, user_id)
if member.status == "kicked":
await bot.unban_chat_member(chat_id, user_id)
ungbanned_chats += 1
except BadRequest as excp:
if excp.message in UNGBAN_ERRORS:
pass
else:
await message.reply_text(f"Could not un-gban due to: {excp.message}")
if EVENT_LOGS:
await bot.send_message(
EVENT_LOGS,
f"Could not un-gban due to: {excp.message}",
parse_mode=ParseMode.HTML,
)
else:
await bot.send_message(
OWNER_ID,
f"Could not un-gban due to: {excp.message}",
)
return
except TelegramError:
pass
sql.ungban_user(user_id)
if EVENT_LOGS:
await log.edit_text(
log_message + f"\n<b>Chats affected:</b> {ungbanned_chats}",
parse_mode=ParseMode.HTML,
)
else:
send_to_list(bot, DRAGONS, "un-gban complete!")
end_time = time.time()
ungban_time = round((end_time - start_time), 2)
if ungban_time > 60:
ungban_time = round((ungban_time / 60), 2)
await message.reply_text(f"Person has been un-gbanned. Took {ungban_time} min")
else:
await message.reply_text(f"Person has been un-gbanned. Took {ungban_time} sec")
@support_plus
async def gbanlist(update: Update, context: ContextTypes.DEFAULT_TYPE):
banned_users = sql.get_gban_list()
if not banned_users:
await update.effective_message.reply_text(
"There aren't any gbanned users! You're kinder than I expected...",
)
return
banfile = "Screw these guys.\n"
for user in banned_users:
banfile += f"[x] {user['name']} - {user['user_id']}\n"
if user["reason"]:
banfile += f"Reason: {user['reason']}\n"
with BytesIO(str.encode(banfile)) as output:
output.name = "gbanlist.txt"
await update.effective_message.reply_document(
document=output,
filename="gbanlist.txt",
caption="Here is the list of currently gbanned users.",
)
async def check_and_ban(update, user_id, should_message=True):
if sql.is_user_gbanned(user_id):
await update.effective_chat.ban_member(user_id)
if should_message:
text = (
f"<b>Alert</b>: this user is globally banned.\n"
f"<code>*bans them from here*</code>.\n"
f"<b>Appeal chat</b>: @{SUPPORT_CHAT}\n"
f"<b>User ID</b>: <code>{user_id}</code>"
)
user = sql.get_gbanned_user(user_id)
if user.reason:
text += f"\n<b>Ban Reason:</b> <code>{html.escape(user.reason)}</code>"
await update.effective_message.reply_text(text, parse_mode=ParseMode.HTML)
async def enforce_gban(update: Update, context: ContextTypes.DEFAULT_TYPE):
# Not using @restrict handler to avoid spamming - just ignore if cant gban.
bot = context.bot
try:
get_member = await update.effective_chat.get_member(
bot.id,
)
if isinstance(get_member, ChatMemberAdministrator):
restrict_permission = get_member.can_restrict_members
else:
return
except Forbidden:
return
if sql.does_chat_gban(update.effective_chat.id) and restrict_permission:
user = update.effective_user
chat = update.effective_chat
msg = update.effective_message
if user and not await is_user_admin(chat, user.id):
await check_and_ban(update, user.id)
return
if msg.new_chat_members:
new_members = update.effective_message.new_chat_members
for mem in new_members:
await check_and_ban(update, mem.id)
if msg.reply_to_message:
user = msg.reply_to_message.from_user
if user and not await is_user_admin(chat, user.id):
await check_and_ban(update, user.id, should_message=False)
@check_admin(is_user=True)
async def gbanstat(update: Update, context: ContextTypes.DEFAULT_TYPE):
args = context.args
if len(args) > 0:
if args[0].lower() in ["on", "yes"]:
sql.enable_gbans(update.effective_chat.id)
await update.effective_message.reply_text(
"Antispam is now enabled ✅ "
"I am now protecting your group from potential remote threats!",
)
elif args[0].lower() in ["off", "no"]:
sql.disable_gbans(update.effective_chat.id)
await update.effective_message.reply_text(
"I am not now protecting your group from potential remote threats!",
)
else:
await update.effective_message.reply_text(
"Give me some arguments to choose a setting! on/off, yes/no!\n\n"
"Your current setting is: {}\n"
"When True, any gbans that happen will also happen in your group. "
"When False, they won't, leaving you at the possible mercy of "
"spammers.".format(sql.does_chat_gban(update.effective_chat.id)),
)
def __stats__():
return f"• {sql.num_gbanned_users()} gbanned users."
def __user_info__(user_id):
is_gbanned = sql.is_user_gbanned(user_id)
text = "Malicious: <b>{}</b>"
if user_id in [777000, 1087968824]:
return ""
if user_id == dispatcher.bot.id:
return ""
if int(user_id) in DRAGONS:
return ""
if is_gbanned:
text = text.format("Yes")
user = sql.get_gbanned_user(user_id)
if user.reason:
text += f"\n<b>Reason:</b> <code>{html.escape(user.reason)}</code>"
text += f"\n<b>Appeal Chat:</b> @{SUPPORT_CHAT}"
else:
text = text.format("???")
return text
def __migrate__(old_chat_id, new_chat_id):
sql.migrate_chat(old_chat_id, new_chat_id)
def __chat_settings__(chat_id, user_id):
return f"This chat is enforcing *gbans*: `{sql.does_chat_gban(chat_id)}`."
# <=================================================== HELP ====================================================>
__help__ = f"""
➠ *Admins only:*
» /antispam <on/off/yes/no>: Will toggle our antispam tech or return your current settings.
➠ Anti-Spam, used by bot devs to ban spammers across all groups. This helps protect \
you and your groups by removing spam flooders as quickly as possible.
➠ *Note:* Users can appeal gbans or report spammers at @darkp_aradiise
"""
# <================================================ HANDLER =======================================================>
GBAN_HANDLER = CommandHandler("gban", gban, block=False)
UNGBAN_HANDLER = CommandHandler("ungban", ungban, block=False)
GBAN_LIST = CommandHandler("gbanlist", gbanlist, block=False)
GBAN_STATUS = CommandHandler(
"antispam", gbanstat, filters=filters.ChatType.GROUPS, block=False
)
GBAN_ENFORCER = MessageHandler(
filters.ALL & filters.ChatType.GROUPS, enforce_gban, block=False
)
function(GBAN_HANDLER)
function(UNGBAN_HANDLER)
function(GBAN_LIST)
function(GBAN_STATUS)
__mod_name__ = "ANTI-SPAM"
__handlers__ = [GBAN_HANDLER, UNGBAN_HANDLER, GBAN_LIST, GBAN_STATUS]
if STRICT_GBAN: # enforce GBANS if this is set
function(GBAN_ENFORCER, GBAN_ENFORCE_GROUP)
__handlers__.append((GBAN_ENFORCER, GBAN_ENFORCE_GROUP))
# <================================================ END =======================================================>
|