2023-07-31 03:29:27 +00:00
|
|
|
import contextlib
|
2022-07-05 12:52:03 +00:00
|
|
|
from asyncio import sleep
|
|
|
|
|
|
|
|
from pyrogram.enums import ChatType
|
2023-07-01 12:18:58 +00:00
|
|
|
from pyrogram.errors import (
|
|
|
|
ChatAdminRequired,
|
|
|
|
FloodWait,
|
|
|
|
PeerIdInvalid,
|
|
|
|
UserAdminInvalid,
|
|
|
|
UsernameInvalid,
|
|
|
|
)
|
2022-07-05 12:52:03 +00:00
|
|
|
from pyrogram.types import Chat
|
|
|
|
|
2024-09-28 14:35:08 +00:00
|
|
|
from pagermaid.dependence import add_delete_message_job
|
2022-07-05 12:52:03 +00:00
|
|
|
from pagermaid.listener import listener
|
2022-12-04 14:44:22 +00:00
|
|
|
from pagermaid.services import bot
|
|
|
|
from pagermaid.enums import Message
|
2022-07-05 12:52:03 +00:00
|
|
|
from pagermaid.utils import lang
|
2024-09-28 14:35:08 +00:00
|
|
|
from pagermaid.utils.bot_utils import log
|
2022-07-05 12:52:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def mention_group(chat: Chat):
|
2023-07-01 12:18:58 +00:00
|
|
|
return (
|
|
|
|
f'<a href="https://t.me/{chat.username}">{chat.title}</a>'
|
|
|
|
if chat.username
|
|
|
|
else f"<code>{chat.title}</code>"
|
|
|
|
)
|
2022-07-05 12:52:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def ban_one(chat: Chat, uid):
|
|
|
|
await bot.ban_chat_member(chat.id, uid)
|
|
|
|
|
|
|
|
|
|
|
|
async def delete_all_messages(chat: Chat, uid):
|
|
|
|
await bot.delete_user_history(chat.id, uid)
|
|
|
|
|
|
|
|
|
2022-07-10 12:44:31 +00:00
|
|
|
async def check_uid(chat: Chat, uid: str):
|
2022-07-05 12:52:03 +00:00
|
|
|
channel = False
|
2023-07-31 03:29:27 +00:00
|
|
|
with contextlib.suppress(ValueError):
|
2022-07-05 12:52:03 +00:00
|
|
|
uid = int(uid)
|
|
|
|
if uid < 0:
|
|
|
|
channel = True
|
2023-07-31 03:29:27 +00:00
|
|
|
try:
|
2022-07-10 12:44:31 +00:00
|
|
|
await bot.get_chat_member(chat.id, uid)
|
2023-07-31 03:29:27 +00:00
|
|
|
except ChatAdminRequired:
|
|
|
|
with contextlib.suppress(PeerIdInvalid):
|
2022-07-10 12:44:31 +00:00
|
|
|
chat = await bot.get_chat(uid)
|
|
|
|
uid = chat.id
|
|
|
|
if chat.type in [ChatType.CHANNEL, ChatType.SUPERGROUP, ChatType.GROUP]:
|
|
|
|
channel = True
|
2022-07-05 12:52:03 +00:00
|
|
|
return uid, channel
|
|
|
|
|
|
|
|
|
2022-07-10 12:44:31 +00:00
|
|
|
async def get_uid(chat: Chat, message: Message):
|
2022-07-05 12:52:03 +00:00
|
|
|
uid = None
|
|
|
|
channel = False
|
|
|
|
delete_all = True
|
2022-07-10 12:44:31 +00:00
|
|
|
sender = None
|
2022-07-05 12:52:03 +00:00
|
|
|
if reply := message.reply_to_message:
|
|
|
|
if sender := reply.from_user:
|
|
|
|
uid = sender.id
|
|
|
|
if sender := reply.sender_chat:
|
|
|
|
uid = sender.id
|
|
|
|
channel = True
|
|
|
|
if message.arguments:
|
|
|
|
delete_all = False
|
|
|
|
elif len(message.parameter) == 2:
|
2022-07-10 12:44:31 +00:00
|
|
|
uid, channel = await check_uid(chat, message.parameter[0])
|
2022-07-05 12:52:03 +00:00
|
|
|
delete_all = False
|
|
|
|
elif len(message.parameter) == 1:
|
2023-07-31 03:29:27 +00:00
|
|
|
uid, channel = await check_uid(chat, message.parameter[0])
|
2022-07-10 12:44:31 +00:00
|
|
|
return uid, channel, delete_all, sender
|
2022-07-05 12:52:03 +00:00
|
|
|
|
|
|
|
|
2023-07-01 12:18:58 +00:00
|
|
|
@listener(
|
|
|
|
command="sb",
|
|
|
|
description=lang("sb_des"),
|
|
|
|
need_admin=True,
|
|
|
|
groups_only=True,
|
|
|
|
parameters="[reply|id|username> <do_not_del_all]",
|
|
|
|
)
|
2022-07-05 12:52:03 +00:00
|
|
|
async def super_ban(message: Message):
|
|
|
|
chat = message.chat
|
|
|
|
try:
|
2022-07-10 12:44:31 +00:00
|
|
|
uid, channel, delete_all, sender = await get_uid(chat, message)
|
2023-07-31 03:29:27 +00:00
|
|
|
if not uid:
|
|
|
|
raise ValueError
|
|
|
|
if channel and uid == chat.id:
|
|
|
|
raise ValueError
|
2022-07-05 12:52:03 +00:00
|
|
|
except (ValueError, PeerIdInvalid, UsernameInvalid, FloodWait):
|
2022-12-04 14:44:22 +00:00
|
|
|
await message.edit(lang("arg_error"))
|
2023-07-31 03:29:27 +00:00
|
|
|
return add_delete_message_job(message, 30)
|
|
|
|
except Exception as e:
|
|
|
|
await message.edit(f"出现错误:{e}")
|
|
|
|
return add_delete_message_job(message, 30)
|
2022-07-05 12:52:03 +00:00
|
|
|
if channel:
|
|
|
|
try:
|
|
|
|
await ban_one(chat, uid)
|
|
|
|
except ChatAdminRequired:
|
2022-12-04 14:44:22 +00:00
|
|
|
await message.edit(lang("sb_no_per"))
|
|
|
|
return add_delete_message_job(message, 10)
|
2022-07-05 12:52:03 +00:00
|
|
|
except Exception as e:
|
2022-12-04 14:44:22 +00:00
|
|
|
await message.edit(f"出现错误:{e}")
|
|
|
|
return add_delete_message_job(message, 10)
|
|
|
|
await message.edit(lang("sb_channel"))
|
|
|
|
return add_delete_message_job(message, 10)
|
2022-07-10 09:53:10 +00:00
|
|
|
try:
|
|
|
|
common = await bot.get_common_chats(uid)
|
|
|
|
except PeerIdInvalid:
|
|
|
|
common = [chat]
|
2022-07-05 12:52:03 +00:00
|
|
|
count, groups = 0, []
|
|
|
|
for i in common:
|
|
|
|
try:
|
|
|
|
await ban_one(i, uid)
|
|
|
|
if delete_all:
|
|
|
|
await delete_all_messages(i, uid)
|
|
|
|
count += 1
|
|
|
|
groups.append(mention_group(i))
|
|
|
|
except ChatAdminRequired:
|
|
|
|
continue
|
|
|
|
except UserAdminInvalid:
|
|
|
|
continue
|
|
|
|
except FloodWait as e:
|
|
|
|
await sleep(e.value)
|
|
|
|
await ban_one(i, uid)
|
|
|
|
if delete_all:
|
|
|
|
await delete_all_messages(i, uid)
|
|
|
|
count += 1
|
|
|
|
groups.append(mention_group(i))
|
|
|
|
except Exception: # noqa
|
|
|
|
pass
|
2022-07-10 12:44:31 +00:00
|
|
|
if not sender:
|
2023-07-31 03:29:27 +00:00
|
|
|
sender = await bot.get_users(uid)
|
2022-07-05 12:52:03 +00:00
|
|
|
if count == 0:
|
2022-07-10 12:44:31 +00:00
|
|
|
text = f'{lang("sb_no")} {sender.mention}'
|
2022-07-05 12:52:03 +00:00
|
|
|
else:
|
2022-07-10 12:44:31 +00:00
|
|
|
text = f'{lang("sb_per")} {count} {lang("sb_in")} {sender.mention}'
|
2022-07-05 12:52:03 +00:00
|
|
|
await message.edit(text)
|
2023-07-01 12:18:58 +00:00
|
|
|
groups = f'\n{lang("sb_pro")}\n' + "\n".join(groups) if groups else ""
|
|
|
|
await log(f"{text}\nuid: `{uid}` {groups}")
|
2022-12-04 14:44:22 +00:00
|
|
|
add_delete_message_job(message, 10)
|