From 1450f9e472dc2276561744c3666993ae78b415cb Mon Sep 17 00:00:00 2001 From: meetwq Date: Thu, 15 Aug 2024 21:44:50 +0800 Subject: [PATCH] update --- README.md | 6 ++++++ nonebot_plugin_memes_api/config.py | 3 ++- nonebot_plugin_memes_api/manager.py | 3 +++ nonebot_plugin_memes_api/matchers/command.py | 8 ++++++-- nonebot_plugin_memes_api/matchers/statistics.py | 16 +++++++++++++--- nonebot_plugin_memes_api/plot.py | 8 ++++---- nonebot_plugin_memes_api/recorder.py | 1 + pyproject.toml | 2 +- 8 files changed, 36 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 55e3792..f71525a 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,12 @@ DRIVER=~fastapi+~httpx+~websockets - 默认:`http://127.0.0.1:2233` - 说明:`meme-generator` web server 地址 +#### `memes_command_prefixes` + +- 类型:`List[str] | None` +- 默认:`None` +- 说明:命令前缀(仅作用于制作表情的命令);如果不设置默认使用 [NoneBot 命令前缀](https://nonebot.dev/docs/appendices/config#command-start-和-command-separator) + #### `memes_disabled_list` - 类型:`List[str]` diff --git a/nonebot_plugin_memes_api/config.py b/nonebot_plugin_memes_api/config.py index ec1a46a..d8ee1c9 100644 --- a/nonebot_plugin_memes_api/config.py +++ b/nonebot_plugin_memes_api/config.py @@ -1,5 +1,5 @@ from datetime import timedelta -from typing import Literal +from typing import Literal, Optional from nonebot import get_plugin_config from pydantic import BaseModel @@ -17,6 +17,7 @@ class MemeListImageConfig(BaseModel): class Config(BaseModel): meme_generator_base_url: str = "http://127.0.0.1:2233" + memes_command_prefixes: Optional[list[str]] = None memes_disabled_list: list[str] = [] memes_check_resources_on_startup: bool = True memes_prompt_params_error: bool = False diff --git a/nonebot_plugin_memes_api/manager.py b/nonebot_plugin_memes_api/manager.py index af216d7..6e4722d 100644 --- a/nonebot_plugin_memes_api/manager.py +++ b/nonebot_plugin_memes_api/manager.py @@ -58,6 +58,9 @@ async def init(self): self.__refresh_names() self.__refresh_tags() + def get_meme(self, meme_key: str) -> Optional[MemeInfo]: + return self.__meme_dict.get(meme_key, None) + def get_memes(self) -> list[MemeInfo]: return list(self.__meme_dict.values()) diff --git a/nonebot_plugin_memes_api/matchers/command.py b/nonebot_plugin_memes_api/matchers/command.py index 3c6eea9..41625a0 100644 --- a/nonebot_plugin_memes_api/matchers/command.py +++ b/nonebot_plugin_memes_api/matchers/command.py @@ -4,6 +4,7 @@ from typing import Any, NoReturn, Union from arclet.alconna import config as alc_config +from nonebot import get_driver from nonebot.adapters import Bot, Event from nonebot.compat import PYDANTIC_V2, ConfigDict from nonebot.exception import AdapterException @@ -149,6 +150,10 @@ async def handle_params( matchers: list[type[Matcher]] = [] +prefixes = list(get_driver().config.command_start) +if (meme_prefixes := memes_config.memes_command_prefixes) is not None: + prefixes = meme_prefixes + def create_matcher(meme: MemeInfo): options = [ @@ -160,11 +165,10 @@ def create_matcher(meme: MemeInfo): ) ] meme_matcher = on_alconna( - Alconna(meme.keywords[0], *options, arg_meme_params), + Alconna(prefixes, meme.keywords[0], *options, arg_meme_params), aliases=set(meme.keywords[1:]), block=False, priority=12, - use_cmd_start=True, extensions=[ReplyMergeExtension()], ) for shortcut in meme.shortcuts: diff --git a/nonebot_plugin_memes_api/matchers/statistics.py b/nonebot_plugin_memes_api/matchers/statistics.py index 571e8d9..14b4be4 100644 --- a/nonebot_plugin_memes_api/matchers/statistics.py +++ b/nonebot_plugin_memes_api/matchers/statistics.py @@ -15,7 +15,8 @@ ) from nonebot_plugin_session import EventSession, SessionIdType -from ..plot import plot_duration_counts, plot_key_and_duration_counts +from ..manager import meme_manager +from ..plot import plot_duration_counts, plot_meme_and_duration_counts from ..recorder import get_meme_generation_records, get_meme_generation_times from ..utils import add_timezone from .utils import find_meme @@ -171,6 +172,9 @@ async def _( meme_records = await get_meme_generation_records( session, id_type, time_start=start ) + meme_records = [ + record for record in meme_records if meme_manager.get_meme(record.meme_key) + ] meme_times = [record.time for record in meme_records] meme_keys = [record.meme_key for record in meme_records] @@ -209,11 +213,17 @@ def fmt_time(time: datetime) -> str: if meme: title = ( - f"表情“{meme.key}”{humanized}调用统计" + f"表情“{'/'.join(meme.keywords)}”{humanized}调用统计" f"(总调用次数为 {key_counts.get(meme.key, 0)})" ) output = await plot_duration_counts(duration_counts, title) else: title = f"{humanized}表情调用统计(总调用次数为 {sum(key_counts.values())})" - output = await plot_key_and_duration_counts(key_counts, duration_counts, title) + meme_counts: dict[str, int] = {} + for key, count in key_counts.items(): + if meme := meme_manager.get_meme(key): + meme_counts["/".join(meme.keywords)] = count + output = await plot_meme_and_duration_counts( + meme_counts, duration_counts, title + ) await UniMessage.image(raw=output).send() diff --git a/nonebot_plugin_memes_api/plot.py b/nonebot_plugin_memes_api/plot.py index 0cb4ea4..468ea6f 100644 --- a/nonebot_plugin_memes_api/plot.py +++ b/nonebot_plugin_memes_api/plot.py @@ -26,11 +26,11 @@ @run_sync -def plot_key_and_duration_counts( - key_counts: dict[str, int], duration_counts: dict[str, int], title: str +def plot_meme_and_duration_counts( + meme_counts: dict[str, int], duration_counts: dict[str, int], title: str ) -> BytesIO: - up_x = list(key_counts.keys()) - up_y = list(key_counts.values()) + up_x = list(meme_counts.keys()) + up_y = list(meme_counts.values()) low_x = list(duration_counts.keys()) low_y = list(duration_counts.values()) up_height = len(up_x) * 0.3 diff --git a/nonebot_plugin_memes_api/recorder.py b/nonebot_plugin_memes_api/recorder.py index 5da4a8b..4c62b25 100644 --- a/nonebot_plugin_memes_api/recorder.py +++ b/nonebot_plugin_memes_api/recorder.py @@ -14,6 +14,7 @@ class MemeGenerationRecord(Model): """表情调用记录""" + __tablename__ = "nonebot_plugin_memes_api_memegenerationrecord" __table_args__ = {"extend_existing": True} id: Mapped[int] = mapped_column(primary_key=True) diff --git a/pyproject.toml b/pyproject.toml index 4559954..c76432f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "nonebot_plugin_memes_api" -version = "0.4.6" +version = "0.4.7" description = "Nonebot2 plugin for making memes" authors = ["meetwq "] license = "MIT"