添加怪话过滤功能
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
2025-10-24 01:21:54 +08:00
parent 3da2c2266f
commit a30c7b8093
4 changed files with 52 additions and 1 deletions

View File

@ -0,0 +1,9 @@
指令介绍
怪话过滤 - 去除含有关键词的怪话
使用方法
`怪话过滤 说的道理`
去除所有含有“说的道理”的怪话
另见
怪话(1)

View File

@ -0,0 +1,12 @@
指令介绍
说点怪话/说些怪话 - 让 BOT 学群友胡言乱语
适用范围
为保证安全,只有少数授权的群聊可以使用该指令
使用方法
`说点怪话 今天吃什么`
期待 Bot 会回答你什么吧
`说些怪话 明天不想上体育课`
Bot 会回复你三句怪话

View File

@ -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))

View File

@ -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)