diff --git a/README.md b/README.md index 7dfd58b..f6a1b67 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,9 @@ _✨ 看看 NoneBot 群大佬们的日常 ✨_
+ + Pydantic Version 1 Or 2 + license @@ -145,6 +148,11 @@ Telegram:[@lgc2333](https://t.me/lgc2333) ## 📝 更新日志 +### 0.3.0 + +- 适配 Pydantic V1 & V2 +- 换用 alconna + ### 0.2.0 - 自动更新图片列表 diff --git a/nonebot_plugin_nonememe/__init__.py b/nonebot_plugin_nonememe/__init__.py index 78b6157..a643866 100644 --- a/nonebot_plugin_nonememe/__init__.py +++ b/nonebot_plugin_nonememe/__init__.py @@ -2,12 +2,12 @@ from nonebot.plugin import PluginMetadata, inherit_supported_adapters require("nonebot_plugin_apscheduler") -require("nonebot_plugin_saa") +require("nonebot_plugin_alconna") from . import __main__ as __main__ # noqa: E402 from .config import ConfigModel # noqa: E402 -__version__ = "0.2.0" +__version__ = "0.3.0" __plugin_meta__ = PluginMetadata( name="NoneMeme", description="NoneBot 群大佬的日常", @@ -18,6 +18,6 @@ type="application", homepage="https://github.com/lgc-NB2Dev/nonebot-plugin-nonememe", config=ConfigModel, - supported_adapters=inherit_supported_adapters("nonebot_plugin_saa"), + supported_adapters=inherit_supported_adapters("nonebot_plugin_alconna"), extra={"License": "MIT", "Author": "student_2333"}, ) diff --git a/nonebot_plugin_nonememe/__main__.py b/nonebot_plugin_nonememe/__main__.py index 9756adb..44febc0 100644 --- a/nonebot_plugin_nonememe/__main__.py +++ b/nonebot_plugin_nonememe/__main__.py @@ -2,12 +2,12 @@ from typing import List, NoReturn from nonebot import logger, on_command -from nonebot.adapters import Message -from nonebot.internal.adapter import Event +from nonebot.adapters import Event, Message +from nonebot.exception import FinishedException from nonebot.matcher import Matcher from nonebot.params import CommandArg from nonebot.typing import T_State -from nonebot_plugin_saa import Image, MessageFactory, Text +from nonebot_plugin_alconna.uniseg import UniMessage from .config import config from .data_source import MemeItem, get_meme, meme_list, search_meme_items @@ -16,12 +16,16 @@ async def finish_with_meme(meme_item: MemeItem) -> NoReturn: try: image_bytes = await get_meme(meme_item) - except Exception: + except Exception as e: logger.exception("Failed to get meme") - await MessageFactory("获取图片失败,请检查后台日志").finish() - await MessageFactory( - [Text(f"# {meme_item.name}"), Image(image_bytes)], - ).finish(reply=True) + await UniMessage("获取图片失败,请检查后台日志").send() + raise FinishedException from e + await ( + UniMessage.text(f"# {meme_item.name}") + .image(raw=image_bytes) + .send(reply_to=True) + ) + raise FinishedException cmd_meme = on_command("nonememe", aliases={"nb草图", "nb梗图"}) diff --git a/nonebot_plugin_nonememe/config.py b/nonebot_plugin_nonememe/config.py index a6d2394..668f8ab 100644 --- a/nonebot_plugin_nonememe/config.py +++ b/nonebot_plugin_nonememe/config.py @@ -1,19 +1,19 @@ from typing import Optional, Union -from typing_extensions import NotRequired, TypedDict +from typing_extensions import TypedDict -from nonebot import get_driver +from nonebot import get_plugin_config from pydantic import BaseModel -class CronDict(TypedDict): - year: NotRequired[Union[str, int]] - month: NotRequired[Union[str, int]] - day: NotRequired[Union[str, int]] - week: NotRequired[Union[str, int]] - day_of_week: NotRequired[Union[str, int]] - hour: NotRequired[Union[str, int]] - minute: NotRequired[Union[str, int]] - second: NotRequired[Union[str, int]] +class CronDict(TypedDict, total=False): + year: Union[str, int] + month: Union[str, int] + day: Union[str, int] + week: Union[str, int] + day_of_week: Union[str, int] + hour: Union[str, int] + minute: Union[str, int] + second: Union[str, int] class ConfigModel(BaseModel): @@ -26,4 +26,4 @@ class ConfigModel(BaseModel): nonememe_search_limit: int = 5 -config: ConfigModel = ConfigModel.parse_obj(get_driver().config.dict()) +config = get_plugin_config(ConfigModel) diff --git a/nonebot_plugin_nonememe/data_source.py b/nonebot_plugin_nonememe/data_source.py index 958fa33..78d9252 100644 --- a/nonebot_plugin_nonememe/data_source.py +++ b/nonebot_plugin_nonememe/data_source.py @@ -8,8 +8,9 @@ import json5 from httpx import AsyncClient from nonebot import get_driver, logger +from nonebot.compat import type_validate_json from nonebot_plugin_apscheduler import scheduler -from pydantic import BaseModel, parse_raw_as +from pydantic import BaseModel from .config import config @@ -102,7 +103,7 @@ async def update_meme_list(): raise logger.warning("Failed to fetch meme list, use cache instead") - got_meme_list = parse_raw_as( + got_meme_list = type_validate_json( List[MemeItem], await cache_json_path.read_text(encoding="u8"), ) diff --git a/pyproject.toml b/pyproject.toml index b7c93ab..a8366c7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,13 +1,12 @@ [project] name = "nonebot-plugin-nonememe" -version = "0.2.0.post4" +dynamic = ["version"] description = "The daily life of the NoneBot group members" authors = [{ name = "student_2333", email = "lgc2333@126.com" }] dependencies = [ "nonebot2>=2.2.0", - "nonebot-plugin-send-anything-anywhere>=0.3.2", + "nonebot-plugin-alconna>=0.40.0rc1", "nonebot-plugin-apscheduler>=0.3.0", - "pydantic>=1.10.0,<2", "httpx>=0.24.1", "json5>=0.9.14", "anyio>=3.6.2", @@ -23,6 +22,10 @@ homepage = "https://github.com/lgc-NB2Dev/nonebot-plugin-nonememe" [tool.pdm.build] includes = [] +[tool.pdm.version] +source = "file" +path = "nonebot_plugin_nonememe/__init__.py" + [build-system] requires = ["pdm-backend"] build-backend = "pdm.backend"