Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
MeetWq committed Aug 15, 2024
1 parent c914b5f commit 1450f9e
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 11 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]`
Expand Down
3 changes: 2 additions & 1 deletion nonebot_plugin_memes_api/config.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions nonebot_plugin_memes_api/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
8 changes: 6 additions & 2 deletions nonebot_plugin_memes_api/matchers/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = [
Expand All @@ -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:
Expand Down
16 changes: 13 additions & 3 deletions nonebot_plugin_memes_api/matchers/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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()
8 changes: 4 additions & 4 deletions nonebot_plugin_memes_api/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions nonebot_plugin_memes_api/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <meetwq@gmail.com>"]
license = "MIT"
Expand Down

0 comments on commit 1450f9e

Please sign in to comment.