56 lines
1.6 KiB
Python
56 lines
1.6 KiB
Python
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 pydantic import BaseModel
|
|
|
|
from konabot.common.path import DATA_PATH
|
|
|
|
from .random_text_record import RandomTextManager
|
|
|
|
|
|
class FortuneConfig(BaseModel):
|
|
plugin_fortune_collect_groups: list[int] = []
|
|
|
|
|
|
fortune_wtf = RandomTextManager(DATA_PATH / "fortune_wtf.txt")
|
|
fortune_insert_lock = asyncio.Lock()
|
|
fortune_config = get_plugin_config(FortuneConfig)
|
|
|
|
|
|
async def is_collect_target(evt: GroupMessageEvent) -> bool:
|
|
if evt.group_id not in fortune_config.plugin_fortune_collect_groups:
|
|
return False
|
|
return True
|
|
|
|
|
|
evt_collector = on_message(rule=is_collect_target)
|
|
|
|
@evt_collector.handle()
|
|
async def _(msg: UniMsg):
|
|
txt = msg.extract_plain_text()
|
|
if len(txt) > 50:
|
|
return
|
|
if txt.startswith("说点怪话") or txt.startswith("说些怪话") or txt.startswith("怪话过滤"):
|
|
return
|
|
async with fortune_insert_lock:
|
|
fortune_wtf.insert(txt)
|
|
|
|
|
|
cmd_guaihua = on_command("说点怪话", rule=is_collect_target)
|
|
|
|
@cmd_guaihua.handle()
|
|
async def _(bot: Bot):
|
|
await cmd_guaihua.send(await UniMessage().text(fortune_wtf.choice()).export(bot))
|
|
|
|
|
|
cmd_guaihuas = on_command("说些怪话", rule=is_collect_target)
|
|
|
|
@cmd_guaihuas.handle()
|
|
async def _(bot: Bot):
|
|
for _ in range(3):
|
|
await cmd_guaihuas.send(await UniMessage().text(fortune_wtf.choice()).export(bot))
|
|
await asyncio.sleep(1)
|
|
|