MCPcopy
hub / github.com/nonebot/nonebot2 / Rule

Class Rule

nonebot/internal/rule.py:15–121  ·  view source on GitHub ↗

{ref}`nonebot.matcher.Matcher` 规则类。 当事件传递时,在 {ref}`nonebot.matcher.Matcher` 运行前进行检查。 参数: *checkers: RuleChecker 用法: ```python Rule(async_function) & sync_function # 等价于 Rule(async_function, sync_function) ```

Source from the content-addressed store, hash-verified

13
14
15class Rule:
16 """{ref}`nonebot.matcher.Matcher` 规则类。
17
18 当事件传递时,在 {ref}`nonebot.matcher.Matcher` 运行前进行检查。
19
20 参数:
21 *checkers: RuleChecker
22
23 用法:
24 ```python
25 Rule(async_function) & sync_function
26 # 等价于
27 Rule(async_function, sync_function)
28 ```
29 """
30
31 __slots__ = ("checkers",)
32
33 HANDLER_PARAM_TYPES: ClassVar[list[type[Param]]] = [
34 DependParam,
35 BotParam,
36 EventParam,
37 StateParam,
38 DefaultParam,
39 ]
40
41 def __init__(self, *checkers: T_RuleChecker | Dependent[bool]) -> None:
42 self.checkers: set[Dependent[bool]] = {
43 (
44 checker
45 if isinstance(checker, Dependent)
46 else Dependent[bool].parse(
47 call=checker, allow_types=self.HANDLER_PARAM_TYPES
48 )
49 )
50 for checker in checkers
51 }
52 """存储 `RuleChecker`"""
53
54 def __repr__(self) -> str:
55 return f"Rule({', '.join(repr(checker) for checker in self.checkers)})"
56
57 async def __call__(
58 self,
59 bot: Bot,
60 event: Event,
61 state: T_State,
62 stack: AsyncExitStack | None = None,
63 dependency_cache: T_DependencyCache | None = None,
64 ) -> bool:
65 """检查是否符合所有规则
66
67 参数:
68 bot: Bot 对象
69 event: Event 对象
70 state: 当前 State
71 stack: 异步上下文栈
72 dependency_cache: 依赖缓存

Callers 15

startswithFunction · 0.90
endswithFunction · 0.90
fullmatchFunction · 0.90
keywordFunction · 0.90
commandFunction · 0.90
shell_commandFunction · 0.90
regexFunction · 0.90
to_meFunction · 0.90
is_typeFunction · 0.90
onFunction · 0.90
MatcherClass · 0.90
newMethod · 0.90

Calls

no outgoing calls

Tested by 2

test_ruleFunction · 0.72
test_matcher_checkFunction · 0.72