sb 优化代码

This commit is contained in:
xtaodada 2023-07-31 11:29:27 +08:00
parent e06796d1e9
commit 6f24f45ee2
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 17 additions and 17 deletions

View File

@ -1,2 +1,3 @@
在自己拥有管理员权限的共同群组中封禁一位用户。 在自己拥有管理员权限的共同群组中封禁一位用户。
指令:,sb
指令: `,sb` 回复用户

View File

@ -1,3 +1,4 @@
import contextlib
from asyncio import sleep from asyncio import sleep
from pyrogram.enums import ChatType from pyrogram.enums import ChatType
@ -36,20 +37,18 @@ async def delete_all_messages(chat: Chat, uid):
async def check_uid(chat: Chat, uid: str): async def check_uid(chat: Chat, uid: str):
channel = False channel = False
try: with contextlib.suppress(ValueError):
uid = int(uid) uid = int(uid)
if uid < 0: if uid < 0:
channel = True channel = True
await bot.get_chat_member(chat.id, uid)
except ValueError:
try: try:
await bot.get_chat_member(chat.id, uid)
except ChatAdminRequired:
with contextlib.suppress(PeerIdInvalid):
chat = await bot.get_chat(uid) chat = await bot.get_chat(uid)
uid = chat.id uid = chat.id
if chat.type in [ChatType.CHANNEL, ChatType.SUPERGROUP, ChatType.GROUP]: if chat.type in [ChatType.CHANNEL, ChatType.SUPERGROUP, ChatType.GROUP]:
channel = True channel = True
except PeerIdInvalid:
member = await bot.get_chat_member(chat.id, uid)
uid = member.user.id
return uid, channel return uid, channel
@ -70,7 +69,7 @@ async def get_uid(chat: Chat, message: Message):
uid, channel = await check_uid(chat, message.parameter[0]) uid, channel = await check_uid(chat, message.parameter[0])
delete_all = False delete_all = False
elif len(message.parameter) == 1: elif len(message.parameter) == 1:
uid, channel = await check_uid(chat, message.arguments) uid, channel = await check_uid(chat, message.parameter[0])
return uid, channel, delete_all, sender return uid, channel, delete_all, sender
@ -85,16 +84,17 @@ async def super_ban(message: Message):
chat = message.chat chat = message.chat
try: try:
uid, channel, delete_all, sender = await get_uid(chat, message) uid, channel, delete_all, sender = await get_uid(chat, message)
if not uid:
raise ValueError
if channel and uid == chat.id:
raise ValueError
except (ValueError, PeerIdInvalid, UsernameInvalid, FloodWait): except (ValueError, PeerIdInvalid, UsernameInvalid, FloodWait):
await message.edit(lang("arg_error")) await message.edit(lang("arg_error"))
return add_delete_message_job(message, 10) return add_delete_message_job(message, 30)
if not uid: except Exception as e:
await message.edit(lang("arg_error")) await message.edit(f"出现错误:{e}")
return add_delete_message_job(message, 10) return add_delete_message_job(message, 30)
if channel: if channel:
if uid == chat.id:
await message.edit(lang("arg_error"))
return add_delete_message_job(message, 10)
try: try:
await ban_one(chat, uid) await ban_one(chat, uid)
except ChatAdminRequired: except ChatAdminRequired:
@ -131,8 +131,7 @@ async def super_ban(message: Message):
except Exception: # noqa except Exception: # noqa
pass pass
if not sender: if not sender:
member = await bot.get_chat_member(chat.id, uid) sender = await bot.get_users(uid)
sender = member.user
if count == 0: if count == 0:
text = f'{lang("sb_no")} {sender.mention}' text = f'{lang("sb_no")} {sender.mention}'
else: else: