From 608a63f698896acbae362d58ab68abcaa2a35116 Mon Sep 17 00:00:00 2001 From: ChensenCHX <2087826155@qq.com> Date: Thu, 27 Mar 2025 19:35:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B8=A6=E4=B8=8Alocals()=EF=BC=8C=E8=AE=A9?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E5=92=8C=E6=95=B0=E6=8D=AE=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=9B=B4=E7=AE=80=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/chat/bot.py | 2 +- template/actions_template.py | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) 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, }