|
| 1 | +# Moon-Userbot - telegram userbot |
| 2 | +# Copyright (C) 2020-present Moon Userbot Organization |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | + |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | + |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +import asyncio |
| 18 | +from pyrogram import Client, filters |
| 19 | +from pyrogram.errors import UserBlocked |
| 20 | +from pyrogram.types import Message |
| 21 | +from utils.misc import modules_help, prefix |
| 22 | +from utils.scripts import format_exc |
| 23 | +from utils.db import db |
| 24 | + |
| 25 | +BOT_KEY = "LINKGEN_BOT" |
| 26 | + |
| 27 | +def is_supported_media(message: Message) -> bool: |
| 28 | + return bool(message.audio or message.document or message.video) |
| 29 | + |
| 30 | +async def get_file_link(client: Client, file_message: Message, bot_username: str) -> str: |
| 31 | + try: |
| 32 | + forwarded = await file_message.forward(bot_username) |
| 33 | + await asyncio.sleep(3) |
| 34 | + async for msg in client.get_chat_history(bot_username, limit=1): |
| 35 | + if msg.text and "http" in msg.text: |
| 36 | + return msg.text |
| 37 | + return "No response from bot" |
| 38 | + except UserBlocked: |
| 39 | + return f"❌ <b>Please unblock</b> <code>{bot_username}</code> <b>first</b>" |
| 40 | + except Exception as e: |
| 41 | + return f"<b>Error:</b> <i>{format_exc(e)}</i>" |
| 42 | + |
| 43 | +@Client.on_message(filters.command("l", prefix) & filters.me) |
| 44 | +async def generate_link(client: Client, message: Message): |
| 45 | + replied = message.reply_to_message |
| 46 | + bot_username = db.get("custom.linkgen", BOT_KEY) |
| 47 | + |
| 48 | + if not bot_username: |
| 49 | + return await message.edit(f"❌ <b>No bot set.</b> Use <code>{prefix}addbot @botusername</code> first.") |
| 50 | + |
| 51 | + if not replied: |
| 52 | + return await message.edit("<b>❌ Reply to a supported file</b> (audio/document/video).") |
| 53 | + |
| 54 | + if not is_supported_media(replied): |
| 55 | + return await message.edit("<b>❌ Unsupported file type.</b>") |
| 56 | + |
| 57 | + status_msg = await message.edit("<i>⏳ Generating download link...</i>") |
| 58 | + |
| 59 | + try: |
| 60 | + link = await get_file_link(client, replied, bot_username) |
| 61 | + await status_msg.edit(f"<b>✅ Download Link:</b>\n\n<code>{link}</code>") |
| 62 | + except Exception as e: |
| 63 | + await status_msg.edit(f"<b>⚠️ Error:</b> <i>{format_exc(e)}</i>") |
| 64 | + |
| 65 | +@Client.on_message(filters.command("addbot", prefix) & filters.me) |
| 66 | +async def add_bot(_, message: Message): |
| 67 | + if len(message.command) < 2: |
| 68 | + return await message.edit(f"❌ <b>Usage:</b> <code>{prefix}addbot @botusername</code>") |
| 69 | + |
| 70 | + bot_username = message.command[1] |
| 71 | + if not bot_username.startswith("@"): |
| 72 | + return await message.edit("❌ <b>Please provide a valid bot username starting with</b> <code>@</code>") |
| 73 | + |
| 74 | + db.set("custom.linkgen", BOT_KEY, bot_username) |
| 75 | + await message.edit(f"✅ <b>Bot set to:</b> <code>{bot_username}</code>") |
| 76 | + |
| 77 | +@Client.on_message(filters.command("delbot", prefix) & filters.me) |
| 78 | +async def delete_bot(_, message: Message): |
| 79 | + db.set("custom.linkgen", BOT_KEY, None) |
| 80 | + await message.edit(f"✅ <b>Bot removed successfully.</b>\nYou can add again using <code>{prefix}addbot @botusername</code>.") |
| 81 | + |
| 82 | +modules_help["linkgen"] = { |
| 83 | + "l [reply]": "Generate direct download link for audio/document/video files", |
| 84 | + "addbot @botusername": "Set bot used to generate file links", |
| 85 | + "delbot": "Remove currently set bot" |
| 86 | +} |
0 commit comments