From 54cc57a2b2a4acb921bb2f80bb2c9ef141d2eb6c Mon Sep 17 00:00:00 2001 From: snowy Date: Mon, 1 Apr 2024 12:29:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8F=AF=E4=BB=A5=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E5=8F=91=E9=80=81=E9=AB=98=E6=B8=85=E5=A4=A7=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- liteyuki/liteyuki_main/core.py | 42 +++++++++++++++++++++++++++--- liteyuki/resources/lang/zh-CN.lang | 1 - liteyuki/utils/message.py | 6 ++--- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/liteyuki/liteyuki_main/core.py b/liteyuki/liteyuki_main/core.py index 8e197d1e..2f7e1673 100644 --- a/liteyuki/liteyuki_main/core.py +++ b/liteyuki/liteyuki_main/core.py @@ -1,9 +1,11 @@ +import base64 from typing import Any import nonebot import pip from git import Repo -from nonebot import require, get_driver +from nonebot import Bot, require, get_driver +from nonebot.exception import MockApiException from nonebot.permission import SUPERUSER from liteyuki.utils.config import config, load_from_yaml @@ -13,12 +15,15 @@ from liteyuki.utils.ly_typing import T_Bot, T_MessageEvent from liteyuki.utils.message import Markdown as md from .reloader import Reloader from liteyuki.utils import htmlrender +from ..utils.liteyuki_api import liteyuki_api require("nonebot_plugin_alconna") from nonebot_plugin_alconna import on_alconna, Alconna, Args, Subcommand, Arparma driver = get_driver() +markdown_image = False + cmd_liteyuki = on_alconna( Alconna( "liteyuki" @@ -137,14 +142,43 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot): @switch_image_mode.handle() async def _(bot: T_Bot, event: T_MessageEvent): + global markdown_image # 切换图片模式,False以图片形式发送,True以markdown形式发送 ulang = get_user_lang(str(event.user_id)) stored_config: StoredConfig = common_db.first(StoredConfig(), default=StoredConfig()) stored_config.config["markdown_image"] = not stored_config.config.get("markdown_image", False) + markdown_image = stored_config.config["markdown_image"] common_db.upsert(stored_config) - await switch_image_mode.finish( - f"{ulang.get('liteyuki.image_mode_switched', MODE=ulang.get('liteyuki.image_mode_on') - if stored_config.config.get('image_mode') else ulang.get('liteyuki.image_mode_off'))}") + await switch_image_mode.finish(ulang.get("liteyuki.image_mode_on" if stored_config.config["markdown_image"] else "liteyuki.image_mode_off")) + + +# system hook + +@Bot.on_calling_api +async def test_for_md_image(bot: T_Bot, api: str, data: dict): + if api in ["send_msg", "send_private_msg", "send_group_msg"] and markdown_image: + if api == "send_msg" and data.get("message_type") == "private" or api == "send_private_msg": + session_type = "private" + session_id = data.get("user_id") + elif api == "send_msg" and data.get("message_type") == "group" or api == "send_group_msg": + session_type = "group" + session_id = data.get("group_id") + else: + return + if len(data.get("message", [])) == 1 and data["message"][0].get("type") == "image": + file: str = data["message"][0].data.get("file") + # file:// http:// base64:// + if file.startswith("http"): + result = await md.send_md(await md.image_async(file), bot, message_type=session_type, session_id=session_id) + elif file.startswith("file"): + file = file.replace("file://", "") + result = await md.send_image(open(file, "rb").read(), bot, message_type=session_type, session_id=session_id) + elif file.startswith("base64"): + file_bytes = base64.b64decode(file.replace("base64://", "")) + result = await md.send_image(file_bytes, bot, message_type=session_type, session_id=session_id) + else: + return + raise MockApiException(result=result) @driver.on_startup diff --git a/liteyuki/resources/lang/zh-CN.lang b/liteyuki/resources/lang/zh-CN.lang index 034a0e9e..322004f3 100644 --- a/liteyuki/resources/lang/zh-CN.lang +++ b/liteyuki/resources/lang/zh-CN.lang @@ -93,6 +93,5 @@ user.profile.input_value=请输入 {ATTR} 的值 user.profile.set_success=成功将 {ATTR} 设置为 {VALUE} user.profile.set_failed=设置 {ATTR} 失败,请检查输入是否合法 -liteyuki.image_mode_switched=图片模式已切换为 {MODE} liteyuki.image_mode_on=开启Markdown图片模式 liteyuki.image_mode_off=关闭Markdown图片模式 \ No newline at end of file diff --git a/liteyuki/utils/message.py b/liteyuki/utils/message.py index fb8ca90d..475ae0d0 100644 --- a/liteyuki/utils/message.py +++ b/liteyuki/utils/message.py @@ -165,7 +165,7 @@ class Markdown: @staticmethod def image(url: str, size: tuple[int, int]) -> str: - """生成图片 + """构建图片链接 Args: size: url: 图片链接 @@ -178,12 +178,12 @@ class Markdown: @staticmethod async def image_async(url: str) -> str: - """获取图片,自动获取大小 + """获取图片,自动请求获取大小,异步 Args: url: 图片链接 Returns: - 图片bytes + 图片Markdown语法: ![image #{width}px #{height}px](link) """ try: