From d4109d32ed173412bf38fe2ee4b95f36193eb0fa Mon Sep 17 00:00:00 2001 From: xtaodada Date: Thu, 7 Mar 2024 18:52:58 +0800 Subject: [PATCH] fix: video sticker --- defs/sticker_download.py | 8 +++----- modules/sticker_download.py | 23 +++++++++++++++++------ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/defs/sticker_download.py b/defs/sticker_download.py index 7378967..eb8cb35 100644 --- a/defs/sticker_download.py +++ b/defs/sticker_download.py @@ -79,7 +79,7 @@ async def converter(src_file: Union[Path, str]) -> Path: src_file.unlink(missing_ok=True) else: logs.error("转换 %s -> %s 时出错: %s", src_file.name, target_file.name, stderr.decode("utf-8")) - raise ValueError + raise ValueError("转换 %s -> %s 时出错" % (src_file.name, target_file.name)) return target_file @@ -162,14 +162,12 @@ async def get_from_sticker_set(short_name: str, uid: int, client: "Client", repl async def get_from_sticker(client: "Client", message: "Message") -> Path: - sticker_path = temp_path / f"{message.sticker.file_unique_id}.webp" - await client.download_media(message, file_name=sticker_path.as_posix()) + sticker_path = await client.download_media(message) return await converter(sticker_path) async def get_from_custom_emoji(client: "Client", sticker: "Sticker") -> Path: - sticker_path = temp_path / f"{sticker.file_unique_id}.webp" - await client.download_media(sticker.file_id, file_name=sticker_path.as_posix()) + sticker_path = await client.download_media(sticker.file_id) return await converter(sticker_path) diff --git a/modules/sticker_download.py b/modules/sticker_download.py index 4e648a1..d82fd25 100644 --- a/modules/sticker_download.py +++ b/modules/sticker_download.py @@ -43,8 +43,11 @@ async def process_sticker_set(client: "Client", message: "Message"): async def process_single_sticker(client: "Client", message: "Message"): await message.reply_chat_action(ChatAction.TYPING) if temp := await cache.get(f"sticker:export:{message.from_user.id}"): - await export_add(temp, message.sticker, client) - await message.reply_text("成功加入导出列表,结束选择请输入 /sticker_export_end", quote=True) + try: + await export_add(temp, message.sticker, client) + await message.reply_text("成功加入导出列表,结束选择请输入 /sticker_export_end", quote=True) + except ValueError as exc: + await message.reply(str(exc), quote=True) else: reply = await message.reply("正在转换贴纸...请耐心等待", quote=True) target_file = None @@ -52,11 +55,13 @@ async def process_single_sticker(client: "Client", message: "Message"): target_file = await get_from_sticker(client, message) await message.reply_chat_action(ChatAction.UPLOAD_DOCUMENT) await message.reply_document(target_file.as_posix(), quote=True) + with contextlib.suppress(Exception): + await reply.delete() + except ValueError as exc: + await reply.edit(str(exc)) finally: if target_file: target_file.unlink(missing_ok=True) - with contextlib.suppress(Exception): - await reply.delete() raise ContinuePropagation @@ -70,17 +75,23 @@ async def process_custom_emoji(client: "Client", message: "Message"): await message.reply("无法获取贴纸", quote=True) raise ContinuePropagation reply = await message.reply(f"正在下载 {len(stickers)} 个 emoji ...请耐心等待", quote=True) + exc = None for sticker in stickers: target_file = None try: target_file = await get_from_custom_emoji(client, sticker) await message.reply_chat_action(ChatAction.UPLOAD_DOCUMENT) await message.reply_document(target_file.as_posix(), quote=True) + except ValueError as exc_: + exc = exc_ finally: if target_file: target_file.unlink(missing_ok=True) - with contextlib.suppress(Exception): - await reply.delete() + if exc: + await reply.edit(str(exc)) + else: + with contextlib.suppress(Exception): + await reply.delete() raise ContinuePropagation