From 76df21036abe068a58de9f0fba14850d74dd0a11 Mon Sep 17 00:00:00 2001 From: NovaNo1r Date: Wed, 23 Feb 2022 21:45:53 -0800 Subject: [PATCH 1/2] Fixed error in custom_welcome_msg and message_builder --- basic_plugins/admin_bot_manage/_data_source.py | 2 +- basic_plugins/group_handle/__init__.py | 2 +- utils/message_builder.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/basic_plugins/admin_bot_manage/_data_source.py b/basic_plugins/admin_bot_manage/_data_source.py index 0a09a3b8..f2fb183c 100644 --- a/basic_plugins/admin_bot_manage/_data_source.py +++ b/basic_plugins/admin_bot_manage/_data_source.py @@ -75,7 +75,7 @@ async def custom_group_welcome( await AsyncHttpx.download_file( img, DATA_PATH / "custom_welcome_msg" / f"{group_id}.jpg" ) - img_result = image(abspath=DATA_PATH / "custom_welcome_msg" / f"{group_id}.jpg") + img_result = image(DATA_PATH / "custom_welcome_msg" / f"{group_id}.jpg") logger.info(f"USER {user_id} GROUP {group_id} 更换群欢迎消息图片") except Exception as e: logger.error(f"GROUP {group_id} 替换群消息失败 e:{e}") diff --git a/basic_plugins/group_handle/__init__.py b/basic_plugins/group_handle/__init__.py index c4583aff..da3dd75c 100755 --- a/basic_plugins/group_handle/__init__.py +++ b/basic_plugins/group_handle/__init__.py @@ -129,7 +129,7 @@ async def _(bot: Bot, event: GroupIncreaseNoticeEvent): at_flag = True if (DATA_PATH / "custom_welcome_msg" / f"{event.group_id}.jpg").exists(): img = image( - abspath=DATA_PATH / "custom_welcome_msg" / f"{event.group_id}.jpg" + DATA_PATH / "custom_welcome_msg" / f"{event.group_id}.jpg" ) if msg or img: msg = msg.strip() + img diff --git a/utils/message_builder.py b/utils/message_builder.py index 0e01e627..2885eaee 100755 --- a/utils/message_builder.py +++ b/utils/message_builder.py @@ -25,7 +25,7 @@ def image( """ if abspath: if isinstance(abspath, Path): - return MessageSegment.image(file) + return MessageSegment.image(abspath) return ( MessageSegment.image("file:///" + abspath) if os.path.exists(abspath) From ca18d45786002dd35d828eed2dad843ddd7641c4 Mon Sep 17 00:00:00 2001 From: HibiKier <45528451+HibiKier@users.noreply.github.com> Date: Thu, 24 Feb 2022 16:12:25 +0800 Subject: [PATCH 2/2] Update message_builder.py --- utils/message_builder.py | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/utils/message_builder.py b/utils/message_builder.py index 2885eaee..8b8a2104 100755 --- a/utils/message_builder.py +++ b/utils/message_builder.py @@ -10,7 +10,6 @@ import os def image( file: Union[str, Path, bytes] = None, path: str = None, - abspath: Union[str, Path] = None, b64: str = None, ) -> Union[MessageSegment, str]: """ @@ -20,18 +19,9 @@ def image( 参数: :param file: 图片文件名称,默认在 resource/img 目录下 :param path: 图片所在路径,默认在 resource/img 目录下 - :param abspath: 图片绝对路径 :param b64: 图片base64 """ - if abspath: - if isinstance(abspath, Path): - return MessageSegment.image(abspath) - return ( - MessageSegment.image("file:///" + abspath) - if os.path.exists(abspath) - else "" - ) - elif isinstance(file, Path): + if isinstance(file, Path): if file.exists(): return MessageSegment.image(file) logger.warning(f"图片 {file.absolute()}缺失...") @@ -41,14 +31,11 @@ def image( elif b64: return MessageSegment.image(b64 if "base64://" in b64 else "base64://" + b64) else: - if "http" in file: + if file.startswith("http"): return MessageSegment.image(file) if len(file.split(".")) == 1: file += ".jpg" - file = ( - IMAGE_PATH / path / file if path else IMAGE_PATH / file - ) - if file.exists(): + if (file := IMAGE_PATH / path / file if path else IMAGE_PATH / file).exists(): return MessageSegment.image(file) else: logger.warning(f"图片 {file} 缺失...")