From 032f55942f1fdb0c146b1cb19ab7f31175d629cf Mon Sep 17 00:00:00 2001 From: Asankilp Date: Tue, 31 Dec 2024 19:03:20 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=B7=BB=E5=8A=A0=E8=AE=B0?= =?UTF-8?q?=E5=BF=86=E5=8A=9F=E8=83=BD=E5=91=BD=E4=BB=A4=EF=BC=9A=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E6=9F=A5=E7=9C=8B=E5=92=8C=E9=87=8D=E7=BD=AE=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E8=AE=B0=E5=BF=86=E7=9A=84=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins/marshoai_basic/__init__.py | 2 +- .../plugins/marshoai_memory/__init__.py | 1 + .../plugins/marshoai_memory/command.py | 47 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 nonebot_plugin_marshoai/plugins/marshoai_memory/command.py diff --git a/nonebot_plugin_marshoai/plugins/marshoai_basic/__init__.py b/nonebot_plugin_marshoai/plugins/marshoai_basic/__init__.py index 4863cec2..7a548109 100755 --- a/nonebot_plugin_marshoai/plugins/marshoai_basic/__init__.py +++ b/nonebot_plugin_marshoai/plugins/marshoai_basic/__init__.py @@ -1,6 +1,6 @@ import os -from zhDateTime import DateTime +from zhDateTime import DateTime # type: ignore from nonebot_plugin_marshoai.plugin import PluginMetadata, String, on_function_call diff --git a/nonebot_plugin_marshoai/plugins/marshoai_memory/__init__.py b/nonebot_plugin_marshoai/plugins/marshoai_memory/__init__.py index 00603707..92317523 100644 --- a/nonebot_plugin_marshoai/plugins/marshoai_memory/__init__.py +++ b/nonebot_plugin_marshoai/plugins/marshoai_memory/__init__.py @@ -13,6 +13,7 @@ from nonebot_plugin_marshoai.instances import client from nonebot_plugin_marshoai.plugin import PluginMetadata, on_function_call from nonebot_plugin_marshoai.plugin.func_call.params import String +from .command import * from .config import plugin_config __marsho_meta__ = PluginMetadata( diff --git a/nonebot_plugin_marshoai/plugins/marshoai_memory/command.py b/nonebot_plugin_marshoai/plugins/marshoai_memory/command.py new file mode 100644 index 00000000..64042a9a --- /dev/null +++ b/nonebot_plugin_marshoai/plugins/marshoai_memory/command.py @@ -0,0 +1,47 @@ +import json + +from arclet.alconna import Alconna, Args, Subcommand +from nonebot import logger +from nonebot.adapters import Bot, Event +from nonebot.matcher import Matcher +from nonebot.typing import T_State +from nonebot_plugin_alconna import on_alconna +from nonebot_plugin_localstore import get_plugin_data_file + +from nonebot_plugin_marshoai.config import config + +marsho_memory_cmd = on_alconna( + Alconna( + f"{config.marshoai_default_name}.memory", + Subcommand("view", alias={"v"}), + Subcommand("reset", alias={"r"}), + ), + priority=10, + block=True, +) + +memory_path = get_plugin_data_file("memory.json") + + +@marsho_memory_cmd.assign("view") +async def view_memory(matcher: Matcher, state: T_State, event: Event): + user_id = str(event.get_user_id()) + with open(memory_path, "r", encoding="utf-8") as f: + memory_data = json.load(f) + memorys = memory_data.get(user_id, []) + if not memorys: + await matcher.finish("好像对ta还没有任何记忆呢~") + await matcher.finish("这些是有关ta的记忆:" + "\n".join(memorys)) + + +@marsho_memory_cmd.assign("reset") +async def reset_memory(matcher: Matcher, state: T_State, event: Event): + user_id = str(event.get_user_id()) + with open(memory_path, "r", encoding="utf-8") as f: + memory_data = json.load(f) + if user_id in memory_data: + del memory_data[user_id] + with open(memory_path, "w", encoding="utf-8") as f: + json.dump(memory_data, f, ensure_ascii=False, indent=4) + await matcher.finish("记忆已重置~") + await matcher.finish("没有找到该用户的记忆~")