提供全局图片统一bytes发送配置

This commit is contained in:
HibiKier 2024-08-10 02:38:41 +08:00
parent e0a3fe526e
commit d05b1fb9b2
2 changed files with 13 additions and 3 deletions

View File

@ -10,6 +10,9 @@ NICKNAME=["真寻", "小真寻", "绪山真寻", "小寻子"]
SESSION_EXPIRE_TIMEOUT=30 SESSION_EXPIRE_TIMEOUT=30
# 全局图片统一使用bytes发送当真寻与协议端不在同一服务器上时为True
IMAGE_TO_BYTES = False
PLATFORM_SUPERUSERS = ' PLATFORM_SUPERUSERS = '
{ {
"qq": [""], "qq": [""],
@ -17,7 +20,6 @@ PLATFORM_SUPERUSERS = '
} }
' '
# DRIVER=~fastapi
DRIVER=~fastapi+~httpx+~websockets DRIVER=~fastapi+~httpx+~websockets
# kook adapter toekn # kook adapter toekn
@ -51,7 +53,7 @@ DRIVER=~fastapi+~httpx+~websockets
# application_commands的{"*": ["*"]}代表将全部应用命令注册为全局应用命令 # application_commands的{"*": ["*"]}代表将全部应用命令注册为全局应用命令
# {"admin": ["123", "456"]}则代表将admin命令注册为id是123、456服务器的局部命令其余命令不注册 # {"admin": ["123", "456"]}则代表将admin命令注册为id是123、456服务器的局部命令其余命令不注册
LOG_LEVEL=DEBUG # LOG_LEVEL=DEBUG
# 服务器和端口 # 服务器和端口
HOST = 127.0.0.1 HOST = 127.0.0.1
PORT = 8080 PORT = 8080

View File

@ -1,6 +1,7 @@
from io import BytesIO from io import BytesIO
from pathlib import Path from pathlib import Path
import nonebot
from nonebot.adapters.onebot.v11 import Message, MessageSegment from nonebot.adapters.onebot.v11 import Message, MessageSegment
from nonebot_plugin_alconna import At, Image, Text, UniMessage from nonebot_plugin_alconna import At, Image, Text, UniMessage
@ -8,6 +9,8 @@ from zhenxun.configs.config import NICKNAME
from zhenxun.services.log import logger from zhenxun.services.log import logger
from zhenxun.utils._build_image import BuildImage from zhenxun.utils._build_image import BuildImage
driver = nonebot.get_driver()
MESSAGE_TYPE = ( MESSAGE_TYPE = (
str | int | float | Path | bytes | BytesIO | BuildImage | At | Image | Text str | int | float | Path | bytes | BytesIO | BuildImage | At | Image | Text
) )
@ -25,6 +28,7 @@ class MessageUtils:
返回: 返回:
list[Text | Text]: 构造完成的消息列表 list[Text | Text]: 构造完成的消息列表
""" """
is_bytes = driver.config.image_to_bytes == "True"
message_list = [] message_list = []
for msg in msg_list: for msg in msg_list:
if isinstance(msg, (Image, Text, At)): if isinstance(msg, (Image, Text, At)):
@ -33,7 +37,11 @@ class MessageUtils:
message_list.append(Text(str(msg))) message_list.append(Text(str(msg)))
elif isinstance(msg, Path): elif isinstance(msg, Path):
if msg.exists(): if msg.exists():
message_list.append(Image(path=msg)) if is_bytes:
image = BuildImage.open(msg)
message_list.append(Image(raw=image.pic2bytes()))
else:
message_list.append(Image(path=msg))
else: else:
logger.warning(f"图片路径不存在: {msg}") logger.warning(f"图片路径不存在: {msg}")
elif isinstance(msg, bytes): elif isinstance(msg, bytes):