From e01cc09a28ea0dce5affbdf26a29105922487dd0 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Tue, 17 Jan 2023 12:07:08 +0800 Subject: [PATCH] =?UTF-8?q?feat(plugin):=20=E6=96=B0=E7=9A=84=5F=5Finit=5F?= =?UTF-8?q?=5F=E6=A0=87=E5=87=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/plugin/host.py | 2 +- tests/plugin_examples/cmdcn/cmdcn.py | 4 ++-- tests/plugin_examples/hello_plugin/main.py | 7 +++++-- tests/plugin_examples/urlikethisijustsix/urlt.py | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/plugin/host.py b/pkg/plugin/host.py index 98cd4a7d..fea638aa 100644 --- a/pkg/plugin/host.py +++ b/pkg/plugin/host.py @@ -74,7 +74,7 @@ def initialize_plugins(): if not plugin['enabled']: continue try: - plugin['instance'] = plugin["class"]() + plugin['instance'] = plugin["class"](plugin_host=context.get_plugin_host()) except: logging.error("插件{}初始化时发生错误: {}".format(plugin['name'], sys.exc_info())) diff --git a/tests/plugin_examples/cmdcn/cmdcn.py b/tests/plugin_examples/cmdcn/cmdcn.py index 9692f1a5..788b0935 100644 --- a/tests/plugin_examples/cmdcn/cmdcn.py +++ b/tests/plugin_examples/cmdcn/cmdcn.py @@ -1,5 +1,5 @@ from pkg.plugin.models import * -from pkg.plugin.host import EventContext +from pkg.plugin.host import EventContext, PluginHost """ 基本命令的中文形式支持 @@ -26,7 +26,7 @@ __mapping__ = { @register(name="CmdCN", description="命令中文支持", version="0.1", author="RockChinQ") class CmdCnPlugin(Plugin): - def __init__(self): + def __init__(self, plugin_host: PluginHost): pass # 私聊发送指令 diff --git a/tests/plugin_examples/hello_plugin/main.py b/tests/plugin_examples/hello_plugin/main.py index 99f5849c..3a5ba8bb 100644 --- a/tests/plugin_examples/hello_plugin/main.py +++ b/tests/plugin_examples/hello_plugin/main.py @@ -1,5 +1,5 @@ from pkg.plugin.models import * -from pkg.plugin.host import EventContext +from pkg.plugin.host import EventContext, PluginHost """ 在收到私聊或群聊消息"hello"时,回复"hello, <发送者id>!"或"hello, everyone!" @@ -10,7 +10,9 @@ from pkg.plugin.host import EventContext @register(name="Hello", description="hello world", version="0.1", author="RockChinQ") class HelloPlugin(Plugin): - def __init__(self): + # 插件加载时触发 + # plugin_host (pkg.plugin.host.PluginHost) 提供了与主程序交互的一些方法,详细请查看其源码 + def __init__(self, plugin_host: PluginHost): pass # 当收到个人消息时触发 @@ -43,5 +45,6 @@ class HelloPlugin(Plugin): # 阻止该事件默认行为(向接口获取回复) event.prevent_default() + # 插件卸载时触发 def __del__(self): pass diff --git a/tests/plugin_examples/urlikethisijustsix/urlt.py b/tests/plugin_examples/urlikethisijustsix/urlt.py index 7db16ff8..40cbc9da 100644 --- a/tests/plugin_examples/urlikethisijustsix/urlt.py +++ b/tests/plugin_examples/urlikethisijustsix/urlt.py @@ -19,7 +19,7 @@ __random_reply__ = ['好好好', "啊对对对", "好好好好", "你说得对", @register(name="啊对对对", description="你都这样了,我就顺从你吧", version="0.1", author="RockChinQ") class AdddPlugin(Plugin): - def __init__(self): + def __init__(self, plugin_host: PluginHost): pass # 绑定私聊消息事件和群消息事件