diff --git a/nonebot_plugin_marshoai/azure.py b/nonebot_plugin_marshoai/azure.py index 1c583c8..a8b8f1c 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 6cee272..cb22e40 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 = ""