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}")