sycgram/plugins/updown.py

61 lines
1.8 KiB
Python
Raw Normal View History

import asyncio
2022-04-09 08:53:44 +00:00
import os
from core import command
from loguru import logger
from pyrogram import Client
from pyrogram.types import Message
2022-04-09 10:10:34 +00:00
from tools.constants import DOWNLOAD_PATH, SYCGRAM
from tools.helpers import Parameters, delete_this, show_cmd_tip, show_exception
2022-04-09 08:53:44 +00:00
@Client.on_message(command("upload"))
async def upload(cli: Client, msg: Message):
2022-04-11 07:54:54 +00:00
"""上传文件"""
2022-04-09 08:53:44 +00:00
cmd, where = Parameters.get(msg)
if not where:
return await show_cmd_tip(msg, cmd)
replied_msg_id = msg.reply_to_message.message_id \
if msg.reply_to_message else None
_, filename = os.path.split(where)
try:
res = await cli.send_document(
2022-04-09 08:53:44 +00:00
chat_id=msg.chat.id,
document=where,
2022-04-09 10:10:34 +00:00
caption=f"```From {SYCGRAM}```",
2022-04-09 08:53:44 +00:00
file_name=filename,
reply_to_message_id=replied_msg_id
)
except Exception as e:
return await show_exception(msg, e)
else:
if res:
await delete_this(msg)
else:
await msg.edit_text("⚠️ Maybe fail to upload ...")
2022-04-09 08:53:44 +00:00
@Client.on_message(command("download"))
async def download(_: Client, msg: Message):
2022-04-11 07:54:54 +00:00
"""下载目标消息的文件"""
2022-04-09 08:53:44 +00:00
cmd, where = Parameters.get(msg)
replied_msg = msg.reply_to_message
if not replied_msg:
return await show_cmd_tip(msg, cmd)
2022-04-09 13:00:23 +00:00
2022-04-09 08:53:44 +00:00
try:
2022-04-09 13:00:23 +00:00
res = await replied_msg.download(
file_name=DOWNLOAD_PATH if not where else where)
2022-04-09 08:53:44 +00:00
except ValueError:
return await show_cmd_tip(msg, cmd)
except Exception as e:
logger.error(e)
return await show_exception(msg, e)
else:
if res:
await msg.edit_text("✅ Download this successfully.")
await asyncio.sleep(3)
await delete_this(msg)
else:
await msg.edit_text("⚠️ Maybe fail to download ...")