1
0
forked from bot/app

feat: 可以设置发送高清大图

This commit is contained in:
远野千束 2024-04-01 12:29:04 +08:00
parent e43cb0ab07
commit 54cc57a2b2
3 changed files with 41 additions and 8 deletions

View File

@ -1,9 +1,11 @@
import base64
from typing import Any from typing import Any
import nonebot import nonebot
import pip import pip
from git import Repo 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 nonebot.permission import SUPERUSER
from liteyuki.utils.config import config, load_from_yaml 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 liteyuki.utils.message import Markdown as md
from .reloader import Reloader from .reloader import Reloader
from liteyuki.utils import htmlrender from liteyuki.utils import htmlrender
from ..utils.liteyuki_api import liteyuki_api
require("nonebot_plugin_alconna") require("nonebot_plugin_alconna")
from nonebot_plugin_alconna import on_alconna, Alconna, Args, Subcommand, Arparma from nonebot_plugin_alconna import on_alconna, Alconna, Args, Subcommand, Arparma
driver = get_driver() driver = get_driver()
markdown_image = False
cmd_liteyuki = on_alconna( cmd_liteyuki = on_alconna(
Alconna( Alconna(
"liteyuki" "liteyuki"
@ -137,14 +142,43 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot):
@switch_image_mode.handle() @switch_image_mode.handle()
async def _(bot: T_Bot, event: T_MessageEvent): async def _(bot: T_Bot, event: T_MessageEvent):
global markdown_image
# 切换图片模式False以图片形式发送True以markdown形式发送 # 切换图片模式False以图片形式发送True以markdown形式发送
ulang = get_user_lang(str(event.user_id)) ulang = get_user_lang(str(event.user_id))
stored_config: StoredConfig = common_db.first(StoredConfig(), default=StoredConfig()) stored_config: StoredConfig = common_db.first(StoredConfig(), default=StoredConfig())
stored_config.config["markdown_image"] = not stored_config.config.get("markdown_image", False) 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) common_db.upsert(stored_config)
await switch_image_mode.finish( await switch_image_mode.finish(ulang.get("liteyuki.image_mode_on" if stored_config.config["markdown_image"] else "liteyuki.image_mode_off"))
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'))}")
# 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 @driver.on_startup

View File

@ -93,6 +93,5 @@ user.profile.input_value=请输入 {ATTR} 的值
user.profile.set_success=成功将 {ATTR} 设置为 {VALUE} user.profile.set_success=成功将 {ATTR} 设置为 {VALUE}
user.profile.set_failed=设置 {ATTR} 失败,请检查输入是否合法 user.profile.set_failed=设置 {ATTR} 失败,请检查输入是否合法
liteyuki.image_mode_switched=图片模式已切换为 {MODE}
liteyuki.image_mode_on=开启Markdown图片模式 liteyuki.image_mode_on=开启Markdown图片模式
liteyuki.image_mode_off=关闭Markdown图片模式 liteyuki.image_mode_off=关闭Markdown图片模式

View File

@ -165,7 +165,7 @@ class Markdown:
@staticmethod @staticmethod
def image(url: str, size: tuple[int, int]) -> str: def image(url: str, size: tuple[int, int]) -> str:
"""生成图片 """构建图片链接
Args: Args:
size: size:
url: 图片链接 url: 图片链接
@ -178,12 +178,12 @@ class Markdown:
@staticmethod @staticmethod
async def image_async(url: str) -> str: async def image_async(url: str) -> str:
"""获取图片,自动获取大小 """获取图片,自动请求获取大小,异步
Args: Args:
url: 图片链接 url: 图片链接
Returns: Returns:
图片bytes 图片Markdown语法: ![image #{width}px #{height}px](link)
""" """
try: try: