mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-25 11:37:13 +00:00
fix: 插件执行时不正确的逻辑
This commit is contained in:
+19
-8
@@ -103,10 +103,12 @@ def load_plugins():
|
|||||||
def initialize_plugins():
|
def initialize_plugins():
|
||||||
""" 初始化插件 """
|
""" 初始化插件 """
|
||||||
logging.info("初始化插件")
|
logging.info("初始化插件")
|
||||||
|
import pkg.plugin.models as models
|
||||||
for plugin in iter_plugins():
|
for plugin in iter_plugins():
|
||||||
if not plugin['enabled']:
|
if not plugin['enabled']:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
|
models.__current_registering_plugin__ = plugin['name']
|
||||||
plugin['instance'] = plugin["class"](plugin_host=context.get_plugin_host())
|
plugin['instance'] = plugin["class"](plugin_host=context.get_plugin_host())
|
||||||
logging.info("插件 {} 已初始化".format(plugin['name']))
|
logging.info("插件 {} 已初始化".format(plugin['name']))
|
||||||
except:
|
except:
|
||||||
@@ -270,10 +272,12 @@ class PluginHost:
|
|||||||
|
|
||||||
def emit(self, event_name: str, **kwargs) -> EventContext:
|
def emit(self, event_name: str, **kwargs) -> EventContext:
|
||||||
""" 触发事件 """
|
""" 触发事件 """
|
||||||
|
import json
|
||||||
|
|
||||||
event_context = EventContext(event_name)
|
event_context = EventContext(event_name)
|
||||||
logging.debug("触发事件: {} ({})".format(event_name, event_context.eid))
|
logging.debug("触发事件: {} ({})".format(event_name, event_context.eid))
|
||||||
for plugin in iter_plugins():
|
for plugin in iter_plugins():
|
||||||
|
|
||||||
if not plugin['enabled']:
|
if not plugin['enabled']:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -286,7 +290,13 @@ class PluginHost:
|
|||||||
# logging.error("插件 {} 初始化时发生错误: {}".format(plugin['name'], sys.exc_info()))
|
# logging.error("插件 {} 初始化时发生错误: {}".format(plugin['name'], sys.exc_info()))
|
||||||
# continue
|
# 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:
|
try:
|
||||||
already_prevented_default = event_context.is_prevented_default()
|
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:
|
if event_context.is_prevented_default() and not already_prevented_default:
|
||||||
logging.debug("插件 {} 已要求阻止事件 {} 的默认行为".format(plugin['name'], event_name))
|
logging.debug("插件 {} 已要求阻止事件 {} 的默认行为".format(plugin['name'], event_name))
|
||||||
|
|
||||||
if event_context.is_prevented_postorder():
|
|
||||||
logging.debug("插件 {} 阻止了后序插件的执行".format(plugin['name']))
|
|
||||||
break
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error("插件{}触发事件{}时发生错误".format(plugin['name'], event_name))
|
logging.error("插件{}触发事件{}时发生错误".format(plugin['name'], event_name))
|
||||||
logging.error(traceback.format_exc())
|
logging.error(traceback.format_exc())
|
||||||
|
|
||||||
logging.debug("事件 {} ({}) 处理完毕,返回值: {}".format(event_name, event_context.eid,
|
# print("done:{}".format(plugin['name']))
|
||||||
event_context.__return_value__))
|
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
|
return event_context
|
||||||
|
|||||||
@@ -158,6 +158,7 @@ class Plugin:
|
|||||||
:return:
|
:return:
|
||||||
None
|
None
|
||||||
"""
|
"""
|
||||||
|
global __current_registering_plugin__
|
||||||
|
|
||||||
def wrapper(func):
|
def wrapper(func):
|
||||||
plugin_hooks = host.__plugins__[__current_registering_plugin__]["hooks"]
|
plugin_hooks = host.__plugins__[__current_registering_plugin__]["hooks"]
|
||||||
@@ -166,6 +167,8 @@ class Plugin:
|
|||||||
plugin_hooks[event] = []
|
plugin_hooks[event] = []
|
||||||
plugin_hooks[event].append(func)
|
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
|
host.__plugins__[__current_registering_plugin__]["hooks"] = plugin_hooks
|
||||||
|
|
||||||
return func
|
return func
|
||||||
@@ -188,7 +191,6 @@ def register(name: str, description: str, version: str, author: str):
|
|||||||
global __current_registering_plugin__
|
global __current_registering_plugin__
|
||||||
|
|
||||||
__current_registering_plugin__ = name
|
__current_registering_plugin__ = name
|
||||||
|
|
||||||
# print("registering plugin: n='{}', d='{}', v={}, a='{}'".format(name, description, version, author))
|
# print("registering plugin: n='{}', d='{}', v={}, a='{}'".format(name, description, version, author))
|
||||||
host.__plugins__[name] = {
|
host.__plugins__[name] = {
|
||||||
"name": name,
|
"name": name,
|
||||||
|
|||||||
@@ -148,6 +148,8 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes
|
|||||||
ratelimit.add_usage(session_name)
|
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 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(
|
logging.info(
|
||||||
"回复[{}]文字消息:{}".format(session_name,
|
"回复[{}]文字消息:{}".format(session_name,
|
||||||
reply[0][:min(100, len(reply[0]))] + (
|
reply[0][:min(100, len(reply[0]))] + (
|
||||||
|
|||||||
Reference in New Issue
Block a user