From a87d3e64ffe4cdbf3252259e9ee6f906c4a5bb2a Mon Sep 17 00:00:00 2001 From: FuQuan233 Date: Fri, 14 Feb 2025 20:27:09 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20use=20nonebot=5Fplugin=5Flocalst?= =?UTF-8?q?ore=20to=20save=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 - nonebot_plugin_llmchat/__init__.py | 23 ++++++++++++++++------- nonebot_plugin_llmchat/config.py | 1 - pyproject.toml | 1 + 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 880303d..efd07e9 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,6 @@ _✨ 支持多API预设配置的AI群聊插件 ✨_ | LLMCHAT__REQUEST_TIMEOUT | 否 | 30 | API请求超时时间(秒) | | LLMCHAT__DEFAULT_PRESENT | 否 | off | 默认使用的预设名称,配置为off则为关闭 | | LLMCHAT__RANDOM_TRIGGER_PROB | 否 | 0.05 | 随机触发概率(0-1] | -| LLMCHAT__STORAGE_PATH | 否 | data/llmchat_state.json | 状态存储文件路径 | | LLMCHAT__DEFAULT_PROMPT | 否 | 你的回答应该尽量简洁、幽默、可以使用一些语气词、颜文字。你应该拒绝回答任何政治相关的问题。 | 默认提示词 | 其中LLMCHAT__API_PRESETS为一个列表,每项配置有以下的配置项 diff --git a/nonebot_plugin_llmchat/__init__.py b/nonebot_plugin_llmchat/__init__.py index d8e9baa..4f49c0a 100644 --- a/nonebot_plugin_llmchat/__init__.py +++ b/nonebot_plugin_llmchat/__init__.py @@ -1,5 +1,5 @@ import aiofiles -from nonebot import get_plugin_config, on_message, logger, on_command, get_driver +from nonebot import get_plugin_config, on_message, logger, on_command, get_driver, require from nonebot.plugin import PluginMetadata from nonebot.adapters.onebot.v11 import GroupMessageEvent, Message from nonebot.adapters.onebot.v11.permission import GROUP_ADMIN, GROUP_OWNER @@ -19,6 +19,9 @@ import random from apscheduler.schedulers.asyncio import AsyncIOScheduler import time +require("nonebot_plugin_localstore") +import nonebot_plugin_localstore as store + __plugin_meta__ = PluginMetadata( name="llmchat", description="支持多API预设配置的AI群聊插件", @@ -283,9 +286,15 @@ async def handle_preset(event: GroupMessageEvent, args: Message = CommandArg()): # region 持久化与定时任务 + +# 获取插件数据目录 +data_dir = store.get_plugin_data_dir() +# 获取插件数据文件 +data_file = store.get_plugin_data_file("llmchat_state.json") + async def save_state(): """保存群组状态到文件""" - logger.info(f"开始保存群组状态到文件:{pluginConfig.storage_path}") + logger.info(f"开始保存群组状态到文件:{data_file}") data = { gid: { "preset": state.preset_name, @@ -297,17 +306,17 @@ async def save_state(): for gid, state in group_states.items() } - os.makedirs(os.path.dirname(pluginConfig.storage_path), exist_ok=True) - async with aiofiles.open(pluginConfig.storage_path, "w") as f: + os.makedirs(os.path.dirname(data_file), exist_ok=True) + async with aiofiles.open(data_file, "w") as f: await f.write(json.dumps(data, ensure_ascii=False)) async def load_state(): """从文件加载群组状态""" - logger.info(f"从文件加载群组状态:{pluginConfig.storage_path}") - if not os.path.exists(pluginConfig.storage_path): + logger.info(f"从文件加载群组状态:{data_file}") + if not os.path.exists(data_file): return - async with aiofiles.open(pluginConfig.storage_path, "r") as f: + async with aiofiles.open(data_file, "r") as f: data = json.loads(await f.read()) for gid, state_data in data.items(): state = GroupState() diff --git a/nonebot_plugin_llmchat/config.py b/nonebot_plugin_llmchat/config.py index b9abfd4..e0aa632 100644 --- a/nonebot_plugin_llmchat/config.py +++ b/nonebot_plugin_llmchat/config.py @@ -18,7 +18,6 @@ class ScopedConfig(BaseModel): request_timeout: int = Field(30, description="API请求超时时间(秒)") default_preset: str = Field("off", description="默认使用的预设名称") random_trigger_prob: float = Field(0.05, ge=0.0, le=1.0, description="随机触发概率(0-1]") - storage_path: str = Field("data/llmchat_state.json", description="状态存储文件路径") default_prompt: str = Field("你的回答应该尽量简洁、幽默、可以使用一些语气词、颜文字。你应该拒绝回答任何政治相关的问题。", description="默认提示词") class Config(BaseModel): diff --git a/pyproject.toml b/pyproject.toml index aea90f2..703f48e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,6 +17,7 @@ nonebot2 = "^2.2.0" aiofiles = ">=24.0.0" nonebot-plugin-apscheduler = "^0.5.0" nonebot-adapter-onebot = "^2.0.0" +nonebot-plugin-localstore = "^0.7.3" [build-system] requires = ["poetry-core>=1.0.0"]