插件 hook 管理器
| 12 | |
| 13 | |
| 14 | class PluginHookManager: |
| 15 | """插件 hook 管理器""" |
| 16 | |
| 17 | def __init__(self, plugin_name: str): |
| 18 | self.plugin_name = plugin_name |
| 19 | self.target_send_hooks: list[Callable] = [] |
| 20 | self.user_activity_hooks: list[Callable] = [] |
| 21 | self.group_activity_hooks: list[Callable] = [] |
| 22 | |
| 23 | def register_target_send_hook(self, func: Callable): |
| 24 | """注册 target_send 方法 hook""" |
| 25 | existing = [h for h in self.target_send_hooks if h.__name__ == func.__name__] |
| 26 | if existing: |
| 27 | self.target_send_hooks[:] = [h for h in self.target_send_hooks if h.__name__ != func.__name__] |
| 28 | logger.debug(f"[库洛签到·BotHook] 更新 target_send hook: {func.__name__}") |
| 29 | else: |
| 30 | logger.debug(f"[库洛签到·BotHook] 注册 target_send hook: {func.__name__}") |
| 31 | self.target_send_hooks.append(func) |
| 32 | |
| 33 | def register_user_activity_hook(self, func: Callable): |
| 34 | """注册用户活跃度 hook""" |
| 35 | existing = [h for h in self.user_activity_hooks if h.__name__ == func.__name__] |
| 36 | if existing: |
| 37 | self.user_activity_hooks[:] = [h for h in self.user_activity_hooks if h.__name__ != func.__name__] |
| 38 | logger.debug(f"[库洛签到·BotHook] 更新 user_activity hook: {func.__name__}") |
| 39 | else: |
| 40 | logger.debug(f"[库洛签到·BotHook] 注册 user_activity hook: {func.__name__}") |
| 41 | self.user_activity_hooks.append(func) |
| 42 | |
| 43 | def register_group_activity_hook(self, func: Callable): |
| 44 | """注册群活跃度 hook""" |
| 45 | existing = [h for h in self.group_activity_hooks if h.__name__ == func.__name__] |
| 46 | if existing: |
| 47 | self.group_activity_hooks[:] = [h for h in self.group_activity_hooks if h.__name__ != func.__name__] |
| 48 | logger.debug(f"[库洛签到·BotHook] 更新 group_activity hook: {func.__name__}") |
| 49 | else: |
| 50 | logger.debug(f"[库洛签到·BotHook] 注册 group_activity hook: {func.__name__}") |
| 51 | self.group_activity_hooks.append(func) |
| 52 | |
| 53 | |
| 54 | def get_or_create_hook_manager(plugin_name: str) -> PluginHookManager: |
no outgoing calls
no test coverage detected