|
| 1 | +import asyncio |
| 2 | + |
| 3 | +from pyrogram import Client, filters |
| 4 | +from pyrogram.errors import FloodWait |
| 5 | +from pyrogram.types import Message |
| 6 | + |
| 7 | +from utils.misc import modules_help, prefix |
| 8 | + |
| 9 | + |
| 10 | +@Client.on_message(filters.command("gcast", prefix) & filters.me) |
| 11 | +async def gcast(client: Client, message: Message): |
| 12 | + if message.reply_to_message: |
| 13 | + msg = message.reply_to_message.text |
| 14 | + elif len(message.command) > 1: |
| 15 | + msg = " ".join(message.command[1:]) |
| 16 | + else: |
| 17 | + await message.edit("Provide text or reply to a message to broadcast globally.") |
| 18 | + return |
| 19 | + |
| 20 | + await message.edit("Starting global broadcast...") |
| 21 | + done, errors = 0, 0 |
| 22 | + |
| 23 | + async for dialog in client.get_dialogs(): |
| 24 | + if dialog.chat.type in ["supergroup", "group"]: |
| 25 | + try: |
| 26 | + await client.send_message(dialog.chat.id, msg) |
| 27 | + done += 1 |
| 28 | + except FloodWait as e: |
| 29 | + await asyncio.sleep(e.value + 10) |
| 30 | + except Exception as e: |
| 31 | + errors += 1 |
| 32 | + print(f"Error: {e}") |
| 33 | + |
| 34 | + await message.edit(f"Broadcast completed: {done} successful, {errors} failed.") |
| 35 | + |
| 36 | + |
| 37 | +@Client.on_message(filters.command("gucast", prefix) & filters.me) |
| 38 | +async def gucast(client: Client, message: Message): |
| 39 | + if message.reply_to_message: |
| 40 | + msg = message.reply_to_message.text |
| 41 | + elif len(message.command) > 1: |
| 42 | + msg = " ".join(message.command[1:]) |
| 43 | + else: |
| 44 | + await message.edit("Provide text or reply to a message to broadcast globally to users.") |
| 45 | + return |
| 46 | + |
| 47 | + await message.edit("Starting global user broadcast...") |
| 48 | + done, errors = 0, 0 |
| 49 | + |
| 50 | + async for dialog in client.get_dialogs(): |
| 51 | + if dialog.chat.type == "private" and not dialog.chat.is_bot: |
| 52 | + try: |
| 53 | + await client.send_message(dialog.chat.id, msg) |
| 54 | + done += 1 |
| 55 | + except Exception as e: |
| 56 | + errors += 1 |
| 57 | + print(f"Error: {e}") |
| 58 | + |
| 59 | + await message.edit(f"Broadcast completed: {done} successful, {errors} failed.") |
| 60 | + |
| 61 | + |
| 62 | +modules_help["gcast"] = { |
| 63 | + "gcast [text/reply to text]*": "Broadcast message to all groups.", |
| 64 | + "gucast [text/reply to text]*": "Broadcast message to all private users." |
| 65 | +} |
0 commit comments