From 15c751b1c8d5e21259f81b458c2d5a538532cfff Mon Sep 17 00:00:00 2001 From: snowyServer Date: Tue, 19 Mar 2024 20:38:25 +0800 Subject: [PATCH] Changes --- main.py | 2 +- src/__init__.py | 0 .../__init__.py | 0 .../loader.py | 5 +- .../webdash.py | 0 src/plugins/liteyuki_markdowntest.py | 97 +++++++++++++++++++ src/plugins/liteyuki_plugin_npm/manager.py | 4 +- .../liteyuki_plugin_user/profile_manager.py | 4 +- src/utils/adapter.py | 10 +- src/utils/config.py | 2 +- 10 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 src/__init__.py rename src/{plugins/liteyuki_plugin_main => liteyuki_main}/__init__.py (100%) rename src/{plugins/liteyuki_plugin_main => liteyuki_main}/loader.py (67%) rename src/{plugins/liteyuki_plugin_main => liteyuki_main}/webdash.py (100%) create mode 100644 src/plugins/liteyuki_markdowntest.py diff --git a/main.py b/main.py index 583ca4a2..b20b20e8 100644 --- a/main.py +++ b/main.py @@ -10,7 +10,7 @@ driver = nonebot.get_driver() for adapter in adapters: driver.register_adapter(adapter) -nonebot.load_plugin("src.plugins.liteyuki_plugin_main") +nonebot.load_plugin("src.liteyuki_main") if __name__ == "__main__": nonebot.run() diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/plugins/liteyuki_plugin_main/__init__.py b/src/liteyuki_main/__init__.py similarity index 100% rename from src/plugins/liteyuki_plugin_main/__init__.py rename to src/liteyuki_main/__init__.py diff --git a/src/plugins/liteyuki_plugin_main/loader.py b/src/liteyuki_main/loader.py similarity index 67% rename from src/plugins/liteyuki_plugin_main/loader.py rename to src/liteyuki_main/loader.py index 7d6173d5..4ddce8ce 100644 --- a/src/plugins/liteyuki_plugin_main/loader.py +++ b/src/liteyuki_main/loader.py @@ -9,8 +9,5 @@ THIS_PLUGIN_NAME = os.path.basename(os.path.dirname(__file__)) RESOURCE_PATH = "src/resources" load_resource_from_dir(RESOURCE_PATH) -for plugin_dir in os.listdir("src/plugins"): - if plugin_dir != THIS_PLUGIN_NAME: - nonebot.plugin.load_plugin(f"src.plugins.{plugin_dir}") - +nonebot.plugin.load_plugins("src/plugins") nonebot.plugin.load_plugins("plugins") \ No newline at end of file diff --git a/src/plugins/liteyuki_plugin_main/webdash.py b/src/liteyuki_main/webdash.py similarity index 100% rename from src/plugins/liteyuki_plugin_main/webdash.py rename to src/liteyuki_main/webdash.py diff --git a/src/plugins/liteyuki_markdowntest.py b/src/plugins/liteyuki_markdowntest.py new file mode 100644 index 00000000..72b73c9b --- /dev/null +++ b/src/plugins/liteyuki_markdowntest.py @@ -0,0 +1,97 @@ +import nonebot +from nonebot import on_command +from nonebot.adapters.onebot.v11 import MessageSegment +from nonebot.params import CommandArg +from nonebot.permission import SUPERUSER + +from src.utils.adapter import T_Message, T_Bot, v11, T_MessageEvent + +md_test = on_command("mdts", aliases={"会话md"}, permission=SUPERUSER) +md_group = on_command("mdg", aliases={"群md"}, permission=SUPERUSER) + +placeholder = { + "[": "[", + "]": "]", + "&": "&", + ",": ",", + "\n" : r"\n", + "\"" : r'\\\"' +} + +@md_test.handle() +async def _(bot: T_Bot, event: T_MessageEvent, arg: v11.Message = CommandArg()): + arg = str(arg).replace("\\", "\\\\").replace("\n", "\\n") + print(arg) + for k, v in placeholder.items(): + arg = arg.replace(k, v) + sfm = await bot.call_api( + api="send_private_forward_msg", + user_id=bot.self_id, + messages=[ + { + "type": "node", + "data": { + "name": "Liteyuki", + "uin": bot.self_id, + "content": [ + { + "type": "markdown", + "data": { + "content": '{"content":"%s"}' % arg + } + } + ] + }, + }, + ] + ) + await md_test.finish( + message=v11.Message( + MessageSegment( + type="longmsg", + data={ + "id": sfm["forward_id"] + } + ) + ) + ) + +@md_group.handle() +async def _(bot: T_Bot, event: T_MessageEvent, arg: v11.Message = CommandArg()): + group_id, arg = str(arg).split(" ", 1) + print(arg) + for k, v in placeholder.items(): + arg = arg.replace(k, v) + nonebot.logger.info("Markdown 测试") + sfm = await bot.call_api( + api="send_private_forward_msg", + user_id=bot.self_id, + messages=[ + { + "type": "node", + "data": { + "name": "Liteyuki", + "uin": bot.self_id, + "content": [ + { + "type": "markdown", + "data": { + "content": '{"content":"%s"}' % arg + } + } + ] + }, + }, + ] + ) + await bot.send_group_msg( + message=v11.Message( + MessageSegment( + type="longmsg", + data={ + "id": sfm["forward_id"] + } + ) + ), + group_id=group_id + ) \ No newline at end of file diff --git a/src/plugins/liteyuki_plugin_npm/manager.py b/src/plugins/liteyuki_plugin_npm/manager.py index 792ffe41..8bd15197 100644 --- a/src/plugins/liteyuki_plugin_npm/manager.py +++ b/src/plugins/liteyuki_plugin_npm/manager.py @@ -2,7 +2,7 @@ import nonebot.plugin from nonebot import on_command from nonebot.permission import SUPERUSER -from src.utils.adapter import MessageEvent +from src.utils.adapter import T_MessageEvent from src.utils.language import get_user_lang list_plugins = on_command("list-plugin", aliases={"列出插件"}, priority=0) @@ -10,7 +10,7 @@ toggle_plugin = on_command("enable-plugin", aliases={"启用插件", "禁用插 @list_plugins.handle() -async def _(event: MessageEvent): +async def _(event: T_MessageEvent): lang = get_user_lang(str(event.user_id)) reply = lang.get("npm.loaded_plugins") for plugin in nonebot.get_loaded_plugins(): diff --git a/src/plugins/liteyuki_plugin_user/profile_manager.py b/src/plugins/liteyuki_plugin_user/profile_manager.py index 5c8e4a71..ba765a88 100644 --- a/src/plugins/liteyuki_plugin_user/profile_manager.py +++ b/src/plugins/liteyuki_plugin_user/profile_manager.py @@ -1,7 +1,7 @@ from nonebot import on_command from nonebot.params import CommandArg -from src.utils.adapter import Bot, Message, MessageEvent +from src.utils.adapter import T_Bot, T_Message, T_MessageEvent from src.utils.data_manager import User, user_db from src.utils.language import get_user_lang @@ -16,7 +16,7 @@ attr_cmd = on_command("profile", aliases={"个人设置"}, priority=0) @attr_cmd.handle() -async def _(bot: Bot, event: MessageEvent, args: Message = CommandArg()): +async def _(bot: T_Bot, event: T_MessageEvent, args: T_Message = CommandArg()): user = user_db.first(User, "user_id = ?", str(event.user_id), default=User(user_id=str(event.user_id))) ulang = get_user_lang(str(event.user_id)) diff --git a/src/utils/adapter.py b/src/utils/adapter.py index e7950f31..5043ee79 100644 --- a/src/utils/adapter.py +++ b/src/utils/adapter.py @@ -1,9 +1,9 @@ from nonebot.adapters.onebot import v11, v12 -Bot = v11.Bot | v12.Bot -GroupMessageEvent = v11.GroupMessageEvent | v12.GroupMessageEvent -PrivateMessageEvent = v11.PrivateMessageEvent | v12.PrivateMessageEvent -MessageEvent = v11.MessageEvent | v12.MessageEvent +T_Bot = v11.Bot | v12.Bot +T_GroupMessageEvent = v11.GroupMessageEvent | v12.GroupMessageEvent +T_PrivateMessageEvent = v11.PrivateMessageEvent | v12.PrivateMessageEvent +T_MessageEvent = v11.MessageEvent | v12.MessageEvent -Message = v11.Message | v12.Message +T_Message = v11.Message | v12.Message diff --git a/src/utils/config.py b/src/utils/config.py index 381731cc..5395229f 100644 --- a/src/utils/config.py +++ b/src/utils/config.py @@ -12,7 +12,7 @@ class BasicConfig(BaseModel): port: int = 20216 superusers: list[str] = [] command_start: list[str] = ["/", ""] - nickname: set[str] = {"Liteyuki"} + nickname: list[str] = ["liteyuki"] def load_from_yaml(file: str) -> dict: