diff --git a/konabot/docs/sys/怪话过滤.txt b/konabot/docs/sys/怪话过滤.txt new file mode 100644 index 0000000..8e99522 --- /dev/null +++ b/konabot/docs/sys/怪话过滤.txt @@ -0,0 +1,9 @@ +指令介绍 + 怪话过滤 - 去除含有关键词的怪话 + +使用方法 + `怪话过滤 说的道理` + 去除所有含有“说的道理”的怪话 + +另见 + 怪话(1) diff --git a/konabot/docs/user/怪话.txt b/konabot/docs/user/怪话.txt new file mode 100644 index 0000000..4e2dd85 --- /dev/null +++ b/konabot/docs/user/怪话.txt @@ -0,0 +1,12 @@ +指令介绍 + 说点怪话/说些怪话 - 让 BOT 学群友胡言乱语 + +适用范围 + 为保证安全,只有少数授权的群聊可以使用该指令 + +使用方法 + `说点怪话 今天吃什么` + 期待 Bot 会回答你什么吧 + + `说些怪话 明天不想上体育课` + Bot 会回复你三句怪话 diff --git a/konabot/plugins/fortune/__init__.py b/konabot/plugins/fortune/__init__.py index 072cf40..76bccfe 100644 --- a/konabot/plugins/fortune/__init__.py +++ b/konabot/plugins/fortune/__init__.py @@ -2,9 +2,10 @@ import asyncio from nonebot import get_plugin_config, on_command, on_message from nonebot.adapters import Bot from nonebot.adapters.onebot.v11.event import GroupMessageEvent -from nonebot_plugin_alconna import UniMessage, UniMsg +from nonebot_plugin_alconna import Alconna, Args, UniMessage, UniMsg, on_alconna from pydantic import BaseModel +from konabot.common.nb.is_admin import is_admin from konabot.common.path import DATA_PATH from .random_text_record import RandomTextManager @@ -53,3 +54,15 @@ async def _(bot: Bot): await cmd_guaihuas.send(await UniMessage().text(fortune_wtf.choice()).export(bot)) await asyncio.sleep(1) + +cmd_filter_guaihua = on_alconna(Alconna( + "怪话过滤", + Args["keyword", str], +), rule=is_admin) + +@cmd_filter_guaihua.handle() +async def _(keyword: str, bot: Bot): + async with fortune_insert_lock: + c = fortune_wtf.filter_out(keyword) + await cmd_filter_guaihua.send(await UniMessage().text(f"删除了 {c} 条怪话").export(bot)) + diff --git a/konabot/plugins/fortune/random_text_record.py b/konabot/plugins/fortune/random_text_record.py index 7e1c6fe..7739c20 100644 --- a/konabot/plugins/fortune/random_text_record.py +++ b/konabot/plugins/fortune/random_text_record.py @@ -31,6 +31,14 @@ class RandomTextManager: continue self._cache.append((ts, base64.b64decode(cn).decode("utf-8"))) + def save(self): + lines = [ + str(ts) + "|" + base64.b64encode(cn.encode("utf-8")).decode() + for ts, cn in self._cache + ] + with self.fp.open("w") as f: + f.writelines(lines) + def insert(self, text: str, timestamp: float | None = None): if timestamp is None: timestamp = time.time() @@ -51,3 +59,12 @@ class RandomTextManager: return random.choices(contents, weights)[0] + def filter_out(self, keyword: str): + len1 = len(self._cache) + self._cache = [ + (ts, cn) for ts, cn in self._cache + if keyword not in cn + ] + self.save() + return len1 - len(self._cache) +