diff --git a/src/plugins/chat/bot.py b/src/plugins/chat/bot.py index 52eaba1e..6291a2a8 100644 --- a/src/plugins/chat/bot.py +++ b/src/plugins/chat/bot.py @@ -230,7 +230,7 @@ class ChatBot: if global_config.enable_action_execute and response_actions: for action in response_actions: logger.info(f"正在处理{action}") - response = usable_action[action](response) + response = usable_action[action](response, locals()) # 记录开始思考的时间,避免从思考到回复的时间太久 thinking_start_time = thinking_message.thinking_start_time diff --git a/template/actions_template.py b/template/actions_template.py index bf0f74d6..8663d894 100644 --- a/template/actions_template.py +++ b/template/actions_template.py @@ -1,4 +1,4 @@ -from typing import Callable +from typing import Callable, Any import time from ..chat.config import global_config @@ -6,18 +6,21 @@ from src.common.logger import get_module_logger logger = get_module_logger("Actions") # 示例函数 -def refuse_response(response: list[str]) -> list[str]: +def refuse_response(response: list[str], env: dict[str, Any]) -> list[str]: logger.info(f"{global_config.BOT_NICKNAME}认为不需要进行回复。") return [] -def ping_response(response: list[str]) -> list[str]: +def ping_response(response: list[str], env: dict[str, Any]) -> list[str]: logger.info(f"{global_config.BOT_NICKNAME}认为这是测试是否在线。") return [f"Pong! at {time.asctime()}."] -# 可用函数表 注意每个函数都应该接收一个list[str](输入的响应), 输出一个list[str](输出的响应) +# 可用函数表 注意每个函数都应该接收一个list[str]与dict[str, Any] +# 它们是输入的响应与上下文中的环境变量 +# 特别地, env['userinfo'].user_id 是本次响应中消息发送者的qq号 +# 每个函数都应当输出一个list[str](输出的响应) # 显然 你可以在函数里做各种操作来修改响应,做出其他动作,etc # 注意到MaiMBot基于Python 3.9, 所以这里的注册顺序实际上决定了tag的执行顺序,越上方的越靠前 -usable_action: dict[str, Callable[[list[str]], list[str]]] = { +usable_action: dict[str, Callable[[list[str], dict], list[str]]] = { "[ping]" : ping_response, "[refuse]" : refuse_response, }