Files
zhenxun_bot/zhenxun/builtin_plugins/hooks/auth_route.py
T
Copaan 5596497947 bugfix:修复notice事件扩散问题 (#2132)
* bugfix:修复notice事件扩散问题

* 优化并发调度

* bugfix:修复签到样式

* bugfix:功能调用统计修复

* bugfix:修复私聊时功能调用统计显示已退群问题

* 提高插件适配兼容性

* 优化发送队列

* 修改权限检查设计

* 继续修改权限检查设计

* 完善权限检查设计

* 优化sqlite配置

* 优化数据库初始化

* 代码整理,无用代码清理

* bugfix:修复启动时数据库校验问题

* bugfix:修复预算裁剪过于激进问题
2026-05-28 22:57:28 +08:00

61 lines
1.8 KiB
Python

from __future__ import annotations
from collections.abc import Awaitable, Callable
from nonebot.matcher import Matcher
from zhenxun.utils.enum import PluginType
from .auth.context import EventContext, set_route_modules
RouteContextGetter = Callable[[str, dict | None], Awaitable[set[str]]]
CommandMatcherChecker = Callable[[type[Matcher]], bool]
AlconnaShortcutChecker = Callable[[type[Matcher]], bool]
async def route_precheck(
matcher: Matcher,
context: EventContext,
*,
route_modules_with_commands: set[str],
get_route_context: RouteContextGetter,
is_command_matcher_class: CommandMatcherChecker,
matcher_has_alconna_shortcuts: AlconnaShortcutChecker,
) -> bool:
"""Skip expensive auth checks for command matchers proven to be off-route."""
module = matcher.plugin_name or ""
if not module:
return False
if _is_hidden_plugin(matcher):
return False
if not is_command_matcher_class(type(matcher)):
return False
route_modules = context.route_modules if context.route_modules_loaded else None
if route_modules is None:
route_modules = await get_route_context(
context.plain_text,
context.event_cache,
)
set_route_modules(None, context, route_modules)
if module in route_modules_with_commands and module not in route_modules:
if matcher_has_alconna_shortcuts(type(matcher)):
return False
if context.event_cache is not None:
context.event_cache["route_skip"] = True
return True
return False
def _is_hidden_plugin(matcher: Matcher) -> bool:
plugin = matcher.plugin
if not plugin or not plugin.metadata:
return False
extra = plugin.metadata.extra or {}
return extra.get("plugin_type") == PluginType.HIDDEN
__all__ = ["route_precheck"]