From 34b32a2048542ce3b5a0ff4d2704ee140bb9a10f Mon Sep 17 00:00:00 2001 From: xtaodada Date: Sat, 26 Nov 2022 11:43:11 +0800 Subject: [PATCH] =?UTF-8?q?clear=5Fblocked=20=E6=B8=85=E7=90=86=E6=89=80?= =?UTF-8?q?=E6=9C=89=E5=B7=B2=E6=8B=89=E9=BB=91=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- clear_blocked/main.py | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 clear_blocked/main.py diff --git a/clear_blocked/main.py b/clear_blocked/main.py new file mode 100644 index 0000000..673a9ce --- /dev/null +++ b/clear_blocked/main.py @@ -0,0 +1,50 @@ +import contextlib + +from asyncio import sleep + +from pyrogram.errors import FloodWait +from pyrogram.raw.functions.contacts import GetBlocked + +from pagermaid.listener import listener +from pagermaid.enums import Client, Message + + +async def clear_blocked_func(client: Client, message: Message): + offset = 0 + success, failed, skipped = 0, 0, 0 + while True: + blocked = await client.invoke(GetBlocked(offset=offset, limit=100)) + if not blocked.users: + break + for user in blocked.users: + if (user.bot or user.scam or user.fake) and not user.deleted: + skipped += 1 + continue + try: + await client.unblock_user(user.id) + success += 1 + except FloodWait as e: + with contextlib.suppress(Exception): + await message.edit(f"🧹 Clearing blocked users...\n\nWill run after {e.value} seconds.") + await sleep(e.value + 1) + await client.unblock_user(user.id) + success += 1 + except Exception: + failed += 1 + offset += 100 + if offset > blocked.count: + break + return success, failed, skipped + + +@listener(command="clear_blocked", description="Clear blocked users.", need_admin=True) +async def clear_blocked(client: Client, message: Message): + """ Clear blocked users. """ + message: Message = await message.edit("🧹 Clearing blocked users...") + try: + success, failed, skipped = await clear_blocked_func(client, message) + except Exception as e: + await message.edit(f"❌ Failed to clear blocked users: {e}") + return + await message.edit(f"🧹 Clear blocked users complete. \n" + f"Success: {success}, Failed: {failed}, Skipped: {skipped}")