From 8f0b0bf0d0a51b4bbc9c3dd53017dbc7ed92568e Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Tue, 14 Nov 2023 11:15:19 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=8F=92=E4=BB=B6run?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=8F=82=E6=95=B0=E4=BC=A0=E9=80=92=E8=A7=84?= =?UTF-8?q?=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/plugins/helloworld/helloworld.py | 15 +++---- cores/qqbot/global_object.py | 52 ++++++++++++++++++++++++- model/command/command.py | 21 +++++++++- 3 files changed, 77 insertions(+), 11 deletions(-) diff --git a/addons/plugins/helloworld/helloworld.py b/addons/plugins/helloworld/helloworld.py index f0ac7ae8a..c76d4a9f9 100644 --- a/addons/plugins/helloworld/helloworld.py +++ b/addons/plugins/helloworld/helloworld.py @@ -7,6 +7,7 @@ from botpy.message import Message, DirectMessage from model.platform.qq import QQ import time import threading +from cores.qqbot.global_object import AstrMessageEvent class HelloWorldPlugin: """ @@ -25,27 +26,27 @@ class HelloWorldPlugin: 例子:做一个名为"yuanshen"的插件;当接收到消息为“原神 可莉”, 如果不想要处理此消息,则返回False, None;如果想要处理,但是执行失败了,返回True, tuple([False, "请求失败啦~", "yuanshen"]) ;执行成功了,返回True, tuple([True, "结果文本", "yuanshen"]) """ - def run(self, message: str, role: str, platform: str, message_obj, qq_platform: QQ): + def run(self, ame: AstrMessageEvent): - if platform == "gocq": + if ame.platform == "gocq": """ QQ平台指令处理逻辑 """ img_url = "https://gchat.qpic.cn/gchatpic_new/905617992/720871955-2246763964-C6EE1A52CC668EC982453065C4FA8747/0?term=2&is_origin=0" - if message == "helloworld": + if ame.message_str == "helloworld": return True, tuple([True, [Plain("Hello World!!"), Image.fromURL(url=img_url)], "helloworld"]) - elif message == "hiloop": + elif ame.message_str == "hiloop": if self.myThread is None: - self.myThread = threading.Thread(target=self.helloworldThread, args=(message_obj, qq_platform)) + self.myThread = threading.Thread(target=self.helloworldThread, args=(ame.message_obj, ame.gocq_platform)) self.myThread.start() return True, tuple([True, [Plain("A lot of Helloworlds!!"), Image.fromURL(url=img_url)], "helloworld"]) else: return False, None - elif platform == "qqchan": + elif ame.platform == "qqchan": """ 频道处理逻辑(频道暂时只支持回复字符串类型的信息,返回的信息都会被转成字符串,如果不想处理某一个平台的信息,直接返回False, None就行) """ - if message == "helloworld": + if ame.message_str == "helloworld": return True, tuple([True, "Hello World!!", "helloworld"]) else: return False, None diff --git a/cores/qqbot/global_object.py b/cores/qqbot/global_object.py index 08240fd91..0c132e76d 100644 --- a/cores/qqbot/global_object.py +++ b/cores/qqbot/global_object.py @@ -1,7 +1,31 @@ -class GlobalObject(): +from model.platform.qqchan import QQChan, NakuruGuildMember, NakuruGuildMessage +from model.platform.qq import QQ +from nakuru import ( + CQHTTP, + GroupMessage, + GroupMemberIncrease, + FriendMessage, + GuildMessage, + Notify +) +from typing import Union + +class GlobalObject: ''' 存放一些公用的数据,用于在不同模块(如core与command)之间传递 ''' + nick: str # gocq 的昵称 + base_config: dict # config.yaml + cached_plugins: dict # 缓存的插件 + web_search: bool # 是否开启了网页搜索 + reply_prefix: str + admin_qq: str + admin_qqchan: str + uniqueSession: bool + cnt_total: int + platform_qq: QQ + platform_qqchan: QQ + def __init__(self): self.nick = None # gocq 的昵称 self.base_config = None # config.yaml @@ -13,4 +37,28 @@ class GlobalObject(): self.uniqueSession = False self.cnt_total = 0 self.platform_qq = None - self.platform_qqchan = None \ No newline at end of file + self.platform_qqchan = None + +class AstrMessageEvent(): + message_str: str # 纯消息字符串 + message_obj: Union[GroupMessage, FriendMessage, GuildMessage, NakuruGuildMessage] # 消息对象 + gocq_platform: QQ + qq_sdk_platform: QQChan + platform: str # `gocq` 或 `qqchan` + role: str # `admin` 或 `member` + global_object: GlobalObject # 一些公用数据 + + def __init__(self, message_str: str, + message_obj: Union[GroupMessage, FriendMessage, GuildMessage, NakuruGuildMessage], + gocq_platform: QQ, + qq_sdk_platform: QQChan, + platform: str, + role: str, + global_object: GlobalObject): + self.message_str = message_str + self.message_obj = message_obj + self.gocq_platform = gocq_platform + self.qq_sdk_platform = qq_sdk_platform + self.platform = platform + self.role = role + self.global_object = global_object diff --git a/model/command/command.py b/model/command/command.py index 9a5fa3e6b..855b7f255 100644 --- a/model/command/command.py +++ b/model/command/command.py @@ -25,7 +25,7 @@ from nakuru.entities.components import ( Image ) from PIL import Image as PILImage -from cores.qqbot.global_object import GlobalObject +from cores.qqbot.global_object import GlobalObject, AstrMessageEvent from pip._internal import main as pipmain PLATFORM_QQCHAN = 'qqchan' @@ -45,11 +45,28 @@ class Command: message_obj): # 插件 cached_plugins = self.global_object.cached_plugins + ame = AstrMessageEvent( + message_str=message, + message_obj=message_obj, + gocq_platform=self.global_object.platform_qq, + qq_sdk_platform=self.global_object.platform_qqchan, + platform=platform, + role=role, + global_object=self.global_object + ) for k, v in cached_plugins.items(): try: - hit, res = v["clsobj"].run(message, role, platform, message_obj, self.global_object.platform_qq) + hit, res = v["clsobj"].run(ame) if hit: return True, res + except TypeError as e: + # 参数不匹配,尝试使用旧的参数方案 + try: + hit, res = v["clsobj"].run(message, role, platform, message_obj, self.global_object.platform_qq) + if hit: + return True, res + except BaseException as e: + gu.log(f"{k}插件加载出现问题,原因: {str(e)}\n已安装插件: {cached_plugins.keys}\n如果你没有相关装插件的想法, 请直接忽略此报错, 不影响其他功能的运行。", level=gu.LEVEL_WARNING) except BaseException as e: gu.log(f"{k}插件加载出现问题,原因: {str(e)}\n已安装插件: {cached_plugins.keys}\n如果你没有相关装插件的想法, 请直接忽略此报错, 不影响其他功能的运行。", level=gu.LEVEL_WARNING)