添加怪话过滤功能
This commit is contained in:
9
konabot/docs/sys/怪话过滤.txt
Normal file
9
konabot/docs/sys/怪话过滤.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
指令介绍
|
||||||
|
怪话过滤 - 去除含有关键词的怪话
|
||||||
|
|
||||||
|
使用方法
|
||||||
|
`怪话过滤 说的道理`
|
||||||
|
去除所有含有“说的道理”的怪话
|
||||||
|
|
||||||
|
另见
|
||||||
|
怪话(1)
|
||||||
12
konabot/docs/user/怪话.txt
Normal file
12
konabot/docs/user/怪话.txt
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
指令介绍
|
||||||
|
说点怪话/说些怪话 - 让 BOT 学群友胡言乱语
|
||||||
|
|
||||||
|
适用范围
|
||||||
|
为保证安全,只有少数授权的群聊可以使用该指令
|
||||||
|
|
||||||
|
使用方法
|
||||||
|
`说点怪话 今天吃什么`
|
||||||
|
期待 Bot 会回答你什么吧
|
||||||
|
|
||||||
|
`说些怪话 明天不想上体育课`
|
||||||
|
Bot 会回复你三句怪话
|
||||||
@ -2,9 +2,10 @@ import asyncio
|
|||||||
from nonebot import get_plugin_config, on_command, on_message
|
from nonebot import get_plugin_config, on_command, on_message
|
||||||
from nonebot.adapters import Bot
|
from nonebot.adapters import Bot
|
||||||
from nonebot.adapters.onebot.v11.event import GroupMessageEvent
|
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 pydantic import BaseModel
|
||||||
|
|
||||||
|
from konabot.common.nb.is_admin import is_admin
|
||||||
from konabot.common.path import DATA_PATH
|
from konabot.common.path import DATA_PATH
|
||||||
|
|
||||||
from .random_text_record import RandomTextManager
|
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 cmd_guaihuas.send(await UniMessage().text(fortune_wtf.choice()).export(bot))
|
||||||
await asyncio.sleep(1)
|
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))
|
||||||
|
|
||||||
|
|||||||
@ -31,6 +31,14 @@ class RandomTextManager:
|
|||||||
continue
|
continue
|
||||||
self._cache.append((ts, base64.b64decode(cn).decode("utf-8")))
|
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):
|
def insert(self, text: str, timestamp: float | None = None):
|
||||||
if timestamp is None:
|
if timestamp is None:
|
||||||
timestamp = time.time()
|
timestamp = time.time()
|
||||||
@ -51,3 +59,12 @@ class RandomTextManager:
|
|||||||
|
|
||||||
return random.choices(contents, weights)[0]
|
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)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user