From 45d25b90aac16568805df552f0924d0e5aa3ac71 Mon Sep 17 00:00:00 2001 From: MoeSnowyFox Date: Sat, 16 Nov 2024 17:54:50 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=20=E4=BF=AE=E5=A4=8D=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=9A=84=E4=BE=9D=E8=B5=96,=20=E6=B7=BB=E5=8A=A0refresh=5Fdata?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E6=89=8B=E5=8A=A8=E5=88=B7=E6=96=B0=E6=95=B0?= =?UTF-8?q?=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_marshoai/azure.py | 5 +++++ nonebot_plugin_marshoai/util.py | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/nonebot_plugin_marshoai/azure.py b/nonebot_plugin_marshoai/azure.py index 1c583c80..a8b8f1c6 100644 --- a/nonebot_plugin_marshoai/azure.py +++ b/nonebot_plugin_marshoai/azure.py @@ -48,6 +48,7 @@ nickname_cmd = on_alconna( Args["name?", str], ) ) +refresh_data = on_alconna("refresh_data", permission=SUPERUSER) model_name = config.marshoai_default_model context = MarshoContext() token = config.marshoai_token @@ -127,6 +128,10 @@ async def nickname(event: Event, name=None): await set_nickname(user_id, name) await nickname_cmd.finish("已设置昵称为:" + name) +@refresh_data.handle() +async def refresh_data(): + await refresh_nickname_json() + await refresh_data.finish("已刷新数据") @marsho_cmd.handle() async def marsho(target: MsgTarget, event: Event, text: Optional[UniMsg] = None): diff --git a/nonebot_plugin_marshoai/util.py b/nonebot_plugin_marshoai/util.py index 6cee272d..cb22e408 100644 --- a/nonebot_plugin_marshoai/util.py +++ b/nonebot_plugin_marshoai/util.py @@ -7,7 +7,7 @@ import httpx import nonebot_plugin_localstore as store from datetime import datetime -from cashews.backends.redis.client_side import logger +from nonebot.log import logger from zhDateTime import DateTime # type: ignore from azure.ai.inference.aio import ChatCompletionsClient from azure.ai.inference.models import SystemMessage @@ -110,6 +110,7 @@ async def set_nickname(user_id: str, name: str): async def get_nicknames(): + '''获取nickname_json, 优先来源于全局变量''' global nickname_json if nickname_json is None: filename = store.get_plugin_data_file("nickname.json") @@ -120,6 +121,16 @@ async def get_nicknames(): nickname_json = {} return nickname_json +async def refresh_nickname_json(): + '''强制刷新nickname_json, 刷新全局变量''' + global nickname_json + filename = store.get_plugin_data_file("nickname.json") + try: + with open(filename, "r", encoding="utf-8") as f: + nickname_json = json.load(f) + except Exception: + logger.error("Error loading nickname.json") + def get_prompt(): prompts = ""