diff --git a/nonebot_plugin_whateat_pic/matcher.py b/nonebot_plugin_whateat_pic/matcher.py index 2f63c2e..6daeb87 100644 --- a/nonebot_plugin_whateat_pic/matcher.py +++ b/nonebot_plugin_whateat_pic/matcher.py @@ -130,8 +130,9 @@ async def handle_me_pic(event: Event): await UniMessage.text(f"cd冷却中,还有{remain_time:.2f}秒").finish() else: pic_path, pic_name = random_pic("me") - send_msg = UniMessage(Text(f"🎉{BOT_NAME}建议你喝🎉\n{pic_name}")) - send_msg.append(Image(path=pic_path)) + send_msg: UniMessage = UniMessage(Text(f"🎉{BOT_NAME}建议你喝🎉\n{pic_name}")) + async with aiofiles.open(pic_path, "rb") as f: + send_msg.append(Image(raw=await f.read())) await send_msg.finish() diff --git a/nonebot_plugin_whateat_pic/random_pic.py b/nonebot_plugin_whateat_pic/random_pic.py new file mode 100644 index 0000000..0fc7309 --- /dev/null +++ b/nonebot_plugin_whateat_pic/random_pic.py @@ -0,0 +1,20 @@ +import secrets +from pathlib import Path +from typing import Literal + +from .config import config + + +def random_pic(menu_type: Literal["drink", "eat", "me"]) -> tuple[Path, str]: + """ + 随机获取一张图片 + """ + if menu_type not in ["drink", "eat", "me"]: + raise ValueError("menu_type must be 'drink' or 'eat'") + + menu_dir = Path(config.whatpic_res_path) / f"{menu_type}_pic" + pic_list = [entry.name for entry in menu_dir.iterdir() if entry.is_file()] + + pic_name = secrets.choice(pic_list) + pic_path = Path(config.whatpic_res_path) / f"{menu_type}_pic" / pic_name + return pic_path, pic_name.split(".")[0]