transfer 优化代码

This commit is contained in:
omg-xtao 2023-03-12 21:08:05 +08:00 committed by GitHub
parent 7543dc80f6
commit 2b97d1ed30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,6 +5,8 @@ from os.path import exists, isfile
from pagermaid.enums import Client, Message from pagermaid.enums import Client, Message
from pagermaid.listener import listener from pagermaid.listener import listener
from pathlib import Path
async def make_zip(source_dir, output_filename): async def make_zip(source_dir, output_filename):
zipf = zipfile.ZipFile(output_filename, "w") zipf = zipfile.ZipFile(output_filename, "w")
@ -23,7 +25,7 @@ async def make_zip(source_dir, output_filename):
async def transfer(bot: Client, message: Message): async def transfer(bot: Client, message: Message):
params = message.parameter params = message.parameter
if len(params) < 2: if len(params) < 2:
message: Message = await message.edit("参数缺失,请使用 `upload [filepath]` 或 `download [filepath]`") message: Message = await message.edit("参数缺失,请使用 `upload [filepath (包括扩展名)]` 或 `download [filepath (包括扩展名)]`")
await message.delay_delete(3) await message.delay_delete(3)
return return
params[1] = " ".join(params[1:]) params[1] = " ".join(params[1:])
@ -46,18 +48,17 @@ async def transfer(bot: Client, message: Message):
message: Message = await message.edit("上传完毕") message: Message = await message.edit("上传完毕")
elif params[0] == "download": elif params[0] == "download":
if reply := message.reply_to_message: if reply := message.reply_to_message:
message: Message = await message.edit('无法下载此类型的文件。') file_path = Path(file_list[0])
if exists(file_path):
message: Message = await message.edit("路径已存在文件")
else:
message: Message = await message.edit('下载中。。。')
try: try:
_file = await reply.download(in_memory=True) _file = await reply.download(file_name=file_list[0])
except Exception: except Exception:
await message.edit('无法下载此类型的文件。') await message.edit('无法下载此类型的文件。')
return return
if not exists(file_list[0]):
with open(file_list[0], "wb") as f:
f.write(_file.getvalue())
message: Message = await message.edit(f"保存成功, 保存路径 `{file_list[0]}`") message: Message = await message.edit(f"保存成功, 保存路径 `{file_list[0]}`")
else:
message: Message = await message.edit("路径已存在文件")
else: else:
message: Message = await message.edit("未回复消息或回复消息中不包含文件") message: Message = await message.edit("未回复消息或回复消息中不包含文件")
else: else: