Skip to content

Commit 1450f9e

Browse files
committed
update
1 parent c914b5f commit 1450f9e

8 files changed

Lines changed: 36 additions & 11 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ DRIVER=~fastapi+~httpx+~websockets
6868
- 默认:`http://127.0.0.1:2233`
6969
- 说明:`meme-generator` web server 地址
7070

71+
#### `memes_command_prefixes`
72+
73+
- 类型:`List[str] | None`
74+
- 默认:`None`
75+
- 说明:命令前缀(仅作用于制作表情的命令);如果不设置默认使用 [NoneBot 命令前缀](https://nonebot.dev/docs/appendices/config#command-start-和-command-separator)
76+
7177
#### `memes_disabled_list`
7278

7379
- 类型:`List[str]`

nonebot_plugin_memes_api/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import timedelta
2-
from typing import Literal
2+
from typing import Literal, Optional
33

44
from nonebot import get_plugin_config
55
from pydantic import BaseModel
@@ -17,6 +17,7 @@ class MemeListImageConfig(BaseModel):
1717

1818
class Config(BaseModel):
1919
meme_generator_base_url: str = "http://127.0.0.1:2233"
20+
memes_command_prefixes: Optional[list[str]] = None
2021
memes_disabled_list: list[str] = []
2122
memes_check_resources_on_startup: bool = True
2223
memes_prompt_params_error: bool = False

nonebot_plugin_memes_api/manager.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ async def init(self):
5858
self.__refresh_names()
5959
self.__refresh_tags()
6060

61+
def get_meme(self, meme_key: str) -> Optional[MemeInfo]:
62+
return self.__meme_dict.get(meme_key, None)
63+
6164
def get_memes(self) -> list[MemeInfo]:
6265
return list(self.__meme_dict.values())
6366

nonebot_plugin_memes_api/matchers/command.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Any, NoReturn, Union
55

66
from arclet.alconna import config as alc_config
7+
from nonebot import get_driver
78
from nonebot.adapters import Bot, Event
89
from nonebot.compat import PYDANTIC_V2, ConfigDict
910
from nonebot.exception import AdapterException
@@ -149,6 +150,10 @@ async def handle_params(
149150

150151
matchers: list[type[Matcher]] = []
151152

153+
prefixes = list(get_driver().config.command_start)
154+
if (meme_prefixes := memes_config.memes_command_prefixes) is not None:
155+
prefixes = meme_prefixes
156+
152157

153158
def create_matcher(meme: MemeInfo):
154159
options = [
@@ -160,11 +165,10 @@ def create_matcher(meme: MemeInfo):
160165
)
161166
]
162167
meme_matcher = on_alconna(
163-
Alconna(meme.keywords[0], *options, arg_meme_params),
168+
Alconna(prefixes, meme.keywords[0], *options, arg_meme_params),
164169
aliases=set(meme.keywords[1:]),
165170
block=False,
166171
priority=12,
167-
use_cmd_start=True,
168172
extensions=[ReplyMergeExtension()],
169173
)
170174
for shortcut in meme.shortcuts:

nonebot_plugin_memes_api/matchers/statistics.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
)
1616
from nonebot_plugin_session import EventSession, SessionIdType
1717

18-
from ..plot import plot_duration_counts, plot_key_and_duration_counts
18+
from ..manager import meme_manager
19+
from ..plot import plot_duration_counts, plot_meme_and_duration_counts
1920
from ..recorder import get_meme_generation_records, get_meme_generation_times
2021
from ..utils import add_timezone
2122
from .utils import find_meme
@@ -171,6 +172,9 @@ async def _(
171172
meme_records = await get_meme_generation_records(
172173
session, id_type, time_start=start
173174
)
175+
meme_records = [
176+
record for record in meme_records if meme_manager.get_meme(record.meme_key)
177+
]
174178
meme_times = [record.time for record in meme_records]
175179
meme_keys = [record.meme_key for record in meme_records]
176180

@@ -209,11 +213,17 @@ def fmt_time(time: datetime) -> str:
209213

210214
if meme:
211215
title = (
212-
f"表情“{meme.key}{humanized}调用统计"
216+
f"表情“{'/'.join(meme.keywords)}{humanized}调用统计"
213217
f"(总调用次数为 {key_counts.get(meme.key, 0)})"
214218
)
215219
output = await plot_duration_counts(duration_counts, title)
216220
else:
217221
title = f"{humanized}表情调用统计(总调用次数为 {sum(key_counts.values())})"
218-
output = await plot_key_and_duration_counts(key_counts, duration_counts, title)
222+
meme_counts: dict[str, int] = {}
223+
for key, count in key_counts.items():
224+
if meme := meme_manager.get_meme(key):
225+
meme_counts["/".join(meme.keywords)] = count
226+
output = await plot_meme_and_duration_counts(
227+
meme_counts, duration_counts, title
228+
)
219229
await UniMessage.image(raw=output).send()

nonebot_plugin_memes_api/plot.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626

2727

2828
@run_sync
29-
def plot_key_and_duration_counts(
30-
key_counts: dict[str, int], duration_counts: dict[str, int], title: str
29+
def plot_meme_and_duration_counts(
30+
meme_counts: dict[str, int], duration_counts: dict[str, int], title: str
3131
) -> BytesIO:
32-
up_x = list(key_counts.keys())
33-
up_y = list(key_counts.values())
32+
up_x = list(meme_counts.keys())
33+
up_y = list(meme_counts.values())
3434
low_x = list(duration_counts.keys())
3535
low_y = list(duration_counts.values())
3636
up_height = len(up_x) * 0.3

nonebot_plugin_memes_api/recorder.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
class MemeGenerationRecord(Model):
1515
"""表情调用记录"""
1616

17+
__tablename__ = "nonebot_plugin_memes_api_memegenerationrecord"
1718
__table_args__ = {"extend_existing": True}
1819

1920
id: Mapped[int] = mapped_column(primary_key=True)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "nonebot_plugin_memes_api"
3-
version = "0.4.6"
3+
version = "0.4.7"
44
description = "Nonebot2 plugin for making memes"
55
authors = ["meetwq <meetwq@gmail.com>"]
66
license = "MIT"

0 commit comments

Comments
 (0)