Skip to content

Commit 460e8b9

Browse files
committed
添加了插件的加载完成和卸载完成的钩子事件
1 parent 8a8ec49 commit 460e8b9

5 files changed

Lines changed: 74 additions & 1 deletion

File tree

astrbot/api/event/filter/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
)
3636
from astrbot.core.star.register import register_regex as regex
3737

38+
from astrbot.core.star.register import register_on_plugin_loaded as on_plugin_loaded
39+
from astrbot.core.star.register import register_on_plugin_unloaded as on_plugin_unloaded
3840
__all__ = [
3941
"CustomFilter",
4042
"EventMessageType",
@@ -54,6 +56,8 @@
5456
"on_llm_request",
5557
"on_llm_response",
5658
"on_plugin_error",
59+
"on_plugin_loaded",
60+
"on_plugin_unloaded",
5761
"on_platform_loaded",
5862
"on_waiting_llm_request",
5963
"permission_type",

astrbot/core/star/register/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
register_on_llm_tool_respond,
1515
register_on_platform_loaded,
1616
register_on_plugin_error,
17+
register_on_plugin_loaded,
18+
register_on_plugin_unloaded,
1719
register_on_using_llm_tool,
1820
register_on_waiting_llm_request,
1921
register_permission_type,
@@ -34,6 +36,8 @@
3436
"register_on_llm_request",
3537
"register_on_llm_response",
3638
"register_on_plugin_error",
39+
"register_on_plugin_loaded",
40+
"register_on_plugin_unloaded",
3741
"register_on_platform_loaded",
3842
"register_on_waiting_llm_request",
3943
"register_permission_type",

astrbot/core/star/register/star_handler.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,41 @@ def decorator(awaitable):
357357
return decorator
358358

359359

360+
def register_on_plugin_loaded(**kwargs):
361+
"""当有插件加载完成时
362+
363+
Hook 参数:
364+
metadata
365+
366+
说明:
367+
当有插件加载完成时,触发该事件并获取到该插件的元数据
368+
"""
369+
370+
def decorator(awaitable):
371+
_ = get_handler_or_create(awaitable, EventType.OnPluginLoadedEvent, **kwargs)
372+
return awaitable
373+
374+
return decorator
375+
376+
377+
def register_on_plugin_unloaded(**kwargs):
378+
"""当有插件卸载完成时
379+
380+
Hook 参数:
381+
metadata
382+
383+
说明:
384+
当有插件卸载完成时,触发该事件并获取到该插件的元数据
385+
"""
386+
387+
def decorator(awaitable):
388+
_ = get_handler_or_create(awaitable, EventType.OnPluginUnloadedEvent, **kwargs)
389+
return awaitable
390+
391+
return decorator
392+
393+
394+
360395
def register_on_waiting_llm_request(**kwargs):
361396
"""当等待调用 LLM 时的通知事件(在获取锁之前)
362397

astrbot/core/star/star_handler.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ def get_handlers_by_event_type(
144144
not in (
145145
EventType.OnAstrBotLoadedEvent,
146146
EventType.OnPlatformLoadedEvent,
147+
EventType.OnPluginLoadedEvent,
148+
EventType.OnPluginUnloadedEvent,
147149
)
148150
and not plugin.reserved
149151
):
@@ -201,6 +203,8 @@ class EventType(enum.Enum):
201203
OnLLMToolRespondEvent = enum.auto() # 调用函数工具后
202204
OnAfterMessageSentEvent = enum.auto() # 发送消息后
203205
OnPluginErrorEvent = enum.auto() # 插件处理消息异常时
206+
OnPluginLoadedEvent = enum.auto() # 插件加载完成
207+
OnPluginUnloadedEvent = enum.auto() # 插件卸载完成
204208

205209

206210
H = TypeVar("H", bound=Callable[..., Any])

astrbot/core/star/star_manager.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from .context import Context
3434
from .filter.permission import PermissionType, PermissionTypeFilter
3535
from .star import star_map, star_registry
36-
from .star_handler import star_handlers_registry
36+
from .star_handler import star_handlers_registry, EventType
3737
from .updator import PluginUpdator
3838

3939
try:
@@ -772,6 +772,19 @@ async def load(
772772
if hasattr(metadata.star_cls, "initialize") and metadata.star_cls:
773773
await metadata.star_cls.initialize()
774774

775+
# 触发插件加载事件
776+
handlers = star_handlers_registry.get_handlers_by_event_type(
777+
EventType.OnPluginLoadedEvent,
778+
)
779+
for handler in handlers:
780+
try:
781+
logger.info(
782+
f"hook(on_plugin_loaded) -> {star_map[handler.handler_module_path].name} - {handler.handler_name}",
783+
)
784+
await handler.handler(metadata)
785+
except Exception:
786+
logger.error(traceback.format_exc())
787+
775788
except BaseException as e:
776789
logger.error(f"----- 插件 {root_dir_name} 载入失败 -----")
777790
errors = traceback.format_exc()
@@ -1159,6 +1172,19 @@ async def _terminate_plugin(star_metadata: StarMetadata) -> None:
11591172
elif "terminate" in star_metadata.star_cls_type.__dict__:
11601173
await star_metadata.star_cls.terminate()
11611174

1175+
# 触发插件卸载事件
1176+
handlers = star_handlers_registry.get_handlers_by_event_type(
1177+
EventType.OnPluginUnloadedEvent,
1178+
)
1179+
for handler in handlers:
1180+
try:
1181+
logger.info(
1182+
f"hook(on_plugin_loaded) -> {star_map[handler.handler_module_path].name} - {handler.handler_name}",
1183+
)
1184+
await handler.handler(star_metadata)
1185+
except Exception:
1186+
logger.error(traceback.format_exc())
1187+
11621188
async def turn_on_plugin(self, plugin_name: str) -> None:
11631189
plugin = self.context.get_registered_star(plugin_name)
11641190
if plugin is None:

0 commit comments

Comments
 (0)