zhenxun_bot/basic_plugins/hooks/task_hook.py

48 lines
1.6 KiB
Python
Raw Normal View History

2022-02-19 18:20:19 +08:00
from nonebot.exception import MockApiException
2022-02-21 15:59:17 +08:00
from nonebot.adapters.onebot.v11 import Bot, Message
2022-02-19 18:20:19 +08:00
from utils.manager import group_manager
from typing import Dict, Any
import re
@Bot.on_calling_api
async def handle_api_call(bot: Bot, api: str, data: Dict[str, Any]):
r = None
if (
2022-02-21 15:59:17 +08:00
(
2022-04-26 14:45:04 +08:00
(api == "send_msg" and data.get("message_type") == "group")
2022-02-21 15:59:17 +08:00
or api == "send_group_msg"
)
2022-02-20 11:06:04 +08:00
and (
2022-03-03 00:18:59 +08:00
(
r := re.search(
"^\[\[_task\|(.*)]]",
data["message"].strip()
if isinstance(data["message"], str)
else str(data["message"]["text"]).strip(),
)
)
2022-02-21 15:59:17 +08:00
or (
r := re.search(
2022-03-03 00:18:59 +08:00
"^[[_task\|(.*)]]",
data["message"].strip()
if isinstance(data["message"], str)
else str(data["message"]["text"]).strip(),
2022-02-21 15:59:17 +08:00
)
2022-02-20 11:06:04 +08:00
)
)
2022-02-19 18:20:19 +08:00
and r.group(1) in group_manager.get_task_data().keys()
):
task = r.group(1)
group_id = data["group_id"]
2022-02-21 15:59:17 +08:00
if group_manager.get_group_level(
group_id
) < 0 or not await group_manager.check_group_task_status(group_id, task):
2022-02-19 18:20:19 +08:00
raise MockApiException(f"被动技能 {task} 处于关闭状态...")
else:
2022-02-21 15:59:17 +08:00
msg = str(data["message"]).strip()
msg = msg.replace(f"&#91;&#91;_task|{task}&#93;&#93;", "").replace(
f"[[_task|{task}]]", ""
)
data["message"] = Message(msg)