mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-02-21 01:55:26 +08:00
✨ Feature: 公开自定义 on
函数所需的函数 (#1856)
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
This commit is contained in:
parent
efc4f5a0d5
commit
283560daa7
@ -33,24 +33,39 @@ from . import get_plugin_by_module_name
|
|||||||
from .manager import _current_plugin_chain
|
from .manager import _current_plugin_chain
|
||||||
|
|
||||||
|
|
||||||
def _store_matcher(matcher: Type[Matcher]) -> None:
|
def store_matcher(matcher: Type[Matcher]) -> None:
|
||||||
|
"""存储一个事件响应器到插件。
|
||||||
|
|
||||||
|
参数:
|
||||||
|
matcher: 事件响应器
|
||||||
|
"""
|
||||||
# only store the matcher defined when plugin loading
|
# only store the matcher defined when plugin loading
|
||||||
if plugin_chain := _current_plugin_chain.get():
|
if plugin_chain := _current_plugin_chain.get():
|
||||||
plugin_chain[-1].matcher.add(matcher)
|
plugin_chain[-1].matcher.add(matcher)
|
||||||
|
|
||||||
|
|
||||||
def _get_matcher_plugin(depth: int = 1) -> Optional[Plugin]:
|
def get_matcher_plugin(depth: int = 1) -> Optional[Plugin]:
|
||||||
|
"""获取事件响应器定义所在插件。
|
||||||
|
|
||||||
|
参数:
|
||||||
|
depth: 调用栈深度
|
||||||
|
"""
|
||||||
# matcher defined when plugin loading
|
# matcher defined when plugin loading
|
||||||
if plugin_chain := _current_plugin_chain.get():
|
if plugin_chain := _current_plugin_chain.get():
|
||||||
return plugin_chain[-1]
|
return plugin_chain[-1]
|
||||||
|
|
||||||
# matcher defined when plugin running
|
# matcher defined when plugin running
|
||||||
if module := _get_matcher_module(depth + 1):
|
if module := get_matcher_module(depth + 1):
|
||||||
if plugin := get_plugin_by_module_name(module.__name__):
|
if plugin := get_plugin_by_module_name(module.__name__):
|
||||||
return plugin
|
return plugin
|
||||||
|
|
||||||
|
|
||||||
def _get_matcher_module(depth: int = 1) -> Optional[ModuleType]:
|
def get_matcher_module(depth: int = 1) -> Optional[ModuleType]:
|
||||||
|
"""获取事件响应器定义所在模块。
|
||||||
|
|
||||||
|
参数:
|
||||||
|
depth: 调用栈深度
|
||||||
|
"""
|
||||||
current_frame = inspect.currentframe()
|
current_frame = inspect.currentframe()
|
||||||
if current_frame is None:
|
if current_frame is None:
|
||||||
return None
|
return None
|
||||||
@ -93,11 +108,11 @@ def on(
|
|||||||
priority=priority,
|
priority=priority,
|
||||||
block=block,
|
block=block,
|
||||||
handlers=handlers,
|
handlers=handlers,
|
||||||
plugin=_get_matcher_plugin(_depth + 1),
|
plugin=get_matcher_plugin(_depth + 1),
|
||||||
module=_get_matcher_module(_depth + 1),
|
module=get_matcher_module(_depth + 1),
|
||||||
default_state=state,
|
default_state=state,
|
||||||
)
|
)
|
||||||
_store_matcher(matcher)
|
store_matcher(matcher)
|
||||||
return matcher
|
return matcher
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import re
|
import re
|
||||||
|
from types import ModuleType
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from typing import Set, List, Type, Tuple, Union, Optional
|
from typing import Set, List, Type, Tuple, Union, Optional
|
||||||
|
|
||||||
@ -9,6 +10,11 @@ from nonebot.dependencies import Dependent
|
|||||||
from nonebot.rule import Rule, ArgumentParser
|
from nonebot.rule import Rule, ArgumentParser
|
||||||
from nonebot.typing import T_State, T_Handler, T_RuleChecker, T_PermissionChecker
|
from nonebot.typing import T_State, T_Handler, T_RuleChecker, T_PermissionChecker
|
||||||
|
|
||||||
|
from .plugin import Plugin
|
||||||
|
|
||||||
|
def store_matcher(matcher: Type[Matcher]) -> None: ...
|
||||||
|
def get_matcher_plugin(depth: int = ...) -> Optional[Plugin]: ...
|
||||||
|
def get_matcher_module(depth: int = ...) -> Optional[ModuleType]: ...
|
||||||
def on(
|
def on(
|
||||||
type: str = "",
|
type: str = "",
|
||||||
rule: Optional[Union[Rule, T_RuleChecker]] = ...,
|
rule: Optional[Union[Rule, T_RuleChecker]] = ...,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user