From b04606c38e01169cf3cc35939daa608b97113ed5 Mon Sep 17 00:00:00 2001 From: Raven95676 Date: Wed, 9 Apr 2025 16:13:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=8E=B7=E5=8F=96=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=9B=AE=E5=BD=95=E7=9A=84StarTool?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/star/star_tools.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/astrbot/core/star/star_tools.py b/astrbot/core/star/star_tools.py index 68468e353..410a49eed 100644 --- a/astrbot/core/star/star_tools.py +++ b/astrbot/core/star/star_tools.py @@ -4,6 +4,9 @@ from astrbot.core.message.message_event_result import MessageChain from astrbot.api.platform import MessageMember, AstrBotMessage from astrbot.core.platform.astr_message_event import MessageSesion from astrbot.core.star.context import Context +from astrbot.core.star.star import star_map +from pathlib import Path +import inspect class StarTools: @@ -142,3 +145,32 @@ class StarTools: name (str): 工具名称 """ cls._context.unregister_llm_tool(name) + + @classmethod + def get_data_dir(cls) -> Path: + """ + 为调用者创建并返回数据目录路径。 + + Returns: + Path: 数据目录的绝对路径 + """ + + frame = inspect.currentframe().f_back + module = inspect.getmodule(frame) + + if not module: + raise RuntimeError("无法获取调用者模块信息") + + metadata = star_map.get(module.__name__, None) + + if not metadata: + raise RuntimeError(f"无法获取模块 {module.__name__} 的元数据信息") + + data_dir = Path("data/plugin_data") / metadata.name + + try: + data_dir.mkdir(parents=True, exist_ok=True) + except PermissionError: + raise RuntimeError(f"无法创建目录 {data_dir}:权限不足") + + return data_dir.resolve()