From 28d1f5ead90fa750b391214b44f87639a3e5d562 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Sat, 25 Feb 2023 20:29:21 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8F=92=E4=BB=B6=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E6=97=B6=E4=B8=8D=E6=AD=A3=E7=A1=AE=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/plugin/host.py | 27 +++++++++++++++++++-------- pkg/plugin/models.py | 4 +++- pkg/qqbot/process.py | 2 ++ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/pkg/plugin/host.py b/pkg/plugin/host.py index eb29e38e..962670a5 100644 --- a/pkg/plugin/host.py +++ b/pkg/plugin/host.py @@ -103,10 +103,12 @@ def load_plugins(): def initialize_plugins(): """ 初始化插件 """ logging.info("初始化插件") + import pkg.plugin.models as models for plugin in iter_plugins(): if not plugin['enabled']: continue try: + models.__current_registering_plugin__ = plugin['name'] plugin['instance'] = plugin["class"](plugin_host=context.get_plugin_host()) logging.info("插件 {} 已初始化".format(plugin['name'])) except: @@ -270,10 +272,12 @@ class PluginHost: def emit(self, event_name: str, **kwargs) -> EventContext: """ 触发事件 """ + import json + event_context = EventContext(event_name) logging.debug("触发事件: {} ({})".format(event_name, event_context.eid)) for plugin in iter_plugins(): - + if not plugin['enabled']: continue @@ -286,7 +290,13 @@ class PluginHost: # logging.error("插件 {} 初始化时发生错误: {}".format(plugin['name'], sys.exc_info())) # continue - for hook in plugin['hooks'].get(event_name, []): + if 'hooks' not in plugin or event_name not in plugin['hooks']: + continue + + hooks = [] + if event_name in plugin["hooks"]: + hooks = plugin["hooks"][event_name] + for hook in hooks: try: already_prevented_default = event_context.is_prevented_default() @@ -298,15 +308,16 @@ class PluginHost: if event_context.is_prevented_default() and not already_prevented_default: logging.debug("插件 {} 已要求阻止事件 {} 的默认行为".format(plugin['name'], event_name)) - if event_context.is_prevented_postorder(): - logging.debug("插件 {} 阻止了后序插件的执行".format(plugin['name'])) - break - except Exception as e: logging.error("插件{}触发事件{}时发生错误".format(plugin['name'], event_name)) logging.error(traceback.format_exc()) - logging.debug("事件 {} ({}) 处理完毕,返回值: {}".format(event_name, event_context.eid, - event_context.__return_value__)) + # print("done:{}".format(plugin['name'])) + if event_context.is_prevented_postorder(): + logging.debug("插件 {} 阻止了后序插件的执行".format(plugin['name'])) + break + + logging.debug("事件 {} ({}) 处理完毕,返回值: {}".format(event_name, event_context.eid, + event_context.__return_value__)) return event_context diff --git a/pkg/plugin/models.py b/pkg/plugin/models.py index 69610a7c..c190678b 100644 --- a/pkg/plugin/models.py +++ b/pkg/plugin/models.py @@ -158,6 +158,7 @@ class Plugin: :return: None """ + global __current_registering_plugin__ def wrapper(func): plugin_hooks = host.__plugins__[__current_registering_plugin__]["hooks"] @@ -166,6 +167,8 @@ class Plugin: plugin_hooks[event] = [] plugin_hooks[event].append(func) + # print("registering hook: p='{}', e='{}', f={}".format(__current_registering_plugin__, event, func)) + host.__plugins__[__current_registering_plugin__]["hooks"] = plugin_hooks return func @@ -188,7 +191,6 @@ def register(name: str, description: str, version: str, author: str): global __current_registering_plugin__ __current_registering_plugin__ = name - # print("registering plugin: n='{}', d='{}', v={}, a='{}'".format(name, description, version, author)) host.__plugins__[name] = { "name": name, diff --git a/pkg/qqbot/process.py b/pkg/qqbot/process.py index b87eaf3a..16b46857 100644 --- a/pkg/qqbot/process.py +++ b/pkg/qqbot/process.py @@ -148,6 +148,8 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes ratelimit.add_usage(session_name) if reply is not None and len(reply) > 0 and (type(reply[0]) == str or type(reply[0]) == mirai.Plain): + if type(reply[0]) == mirai.Plain: + reply[0] = reply[0].text logging.info( "回复[{}]文字消息:{}".format(session_name, reply[0][:min(100, len(reply[0]))] + (