From b6eb422c0aa7c99a54d01b5a9d2edad4d4b4942c Mon Sep 17 00:00:00 2001 From: xtaodada Date: Tue, 5 Jul 2022 20:52:03 +0800 Subject: [PATCH] =?UTF-8?q?sb=20=E5=9C=A8=E8=87=AA=E5=B7=B1=E6=8B=A5?= =?UTF-8?q?=E6=9C=89=E7=AE=A1=E7=90=86=E5=91=98=E6=9D=83=E9=99=90=E7=9A=84?= =?UTF-8?q?=E5=85=B1=E5=90=8C=E7=BE=A4=E7=BB=84=E4=B8=AD=E5=B0=81=E7=A6=81?= =?UTF-8?q?=E4=B8=80=E4=BD=8D=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- list.json | 10 +++++ sb/main.py | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 sb/main.py diff --git a/list.json b/list.json index d56cd9b..ba6c803 100644 --- a/list.json +++ b/list.json @@ -279,6 +279,16 @@ "supported": true, "des-short": "回复你要临时禁言的人的消息来实现XX秒的禁言", "des": "回复你要临时禁言的人的消息来实现XX秒的禁言。\n指令:,portball [理由]|<时间/秒>" + }, + { + "name": "sb", + "version": "1.0", + "section": "chat", + "maintainer": "xtaodada", + "size": "3.5 kb", + "supported": true, + "des-short": "在自己拥有管理员权限的共同群组中封禁一位用户。", + "des": "在自己拥有管理员权限的共同群组中封禁一位用户。\n指令:,sb" } ] } diff --git a/sb/main.py b/sb/main.py new file mode 100644 index 0000000..b28ae41 --- /dev/null +++ b/sb/main.py @@ -0,0 +1,111 @@ +from asyncio import sleep + +from pyrogram.enums import ChatType +from pyrogram.errors import ChatAdminRequired, FloodWait, PeerIdInvalid, UserAdminInvalid, UsernameInvalid +from pyrogram.types import Chat + +from pagermaid import log, bot +from pagermaid.listener import listener +from pagermaid.single_utils import Message +from pagermaid.utils import lang + + +def mention_group(chat: Chat): + return f'{chat.title}' if chat.username else f'{chat.title}' + + +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) + + +async def check_uid(uid: str): + channel = False + try: + uid = int(uid) + if uid < 0: + channel = True + await bot.get_chat(uid) + except ValueError: + chat = await bot.get_chat(uid) + uid = chat.id + if chat.type in [ChatType.CHANNEL, ChatType.SUPERGROUP, ChatType.GROUP]: + channel = True + return uid, channel + + +async def get_uid(message: Message): + uid = None + channel = False + delete_all = True + 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: + uid, channel = await check_uid(message.parameter[0]) + delete_all = False + elif len(message.parameter) == 1: + uid, channel = await check_uid(message.arguments) + return uid, channel, delete_all + + +@listener(command="sb", + description=lang('sb_des'), + need_admin=True, + parameters=" ") +async def super_ban(message: Message): + chat = message.chat + try: + uid, channel, delete_all = await get_uid(message) + except (ValueError, PeerIdInvalid, UsernameInvalid, FloodWait): + return await message.edit(lang("arg_error")) + if not uid: + return await message.edit(lang("arg_error")) + if channel: + if uid == chat.id: + return await message.edit(lang("arg_error")) + try: + await ban_one(chat, uid) + except ChatAdminRequired: + return await message.edit(lang("sb_no_per")) + except Exception as e: + return await message.edit(f"出现错误:{e}") + return await message.edit(lang("sb_channel")) + common = await bot.get_common_chats(uid) + 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 + user = await bot.get_users(uid) + if count == 0: + text = f'{lang("sb_no")} {user.mention}' + else: + text = f'{lang("sb_per")} {count} {lang("sb_in")} {user.mention}' + await message.edit(text) + groups = f'\n{lang("sb_pro")}\n' + "\n".join(groups) if groups else '' + await log(f'{text}\nuid: `{uid}` {groups}')