带上locals(),让操作和数据获取更简单

pull/601/head
ChensenCHX 2025-03-27 19:35:51 +08:00
parent c3f8a9dd03
commit 0475f98423
2 changed files with 9 additions and 6 deletions

View File

@ -232,7 +232,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

View File

@ -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,
}