mirror of
				https://github.com/nonebot/nonebot2.git
				synced 2025-10-31 06:56:39 +00:00 
			
		
		
		
	💡 add rule utils docstring
This commit is contained in:
		| @@ -19,6 +19,9 @@ | ||||
|     * [nonebot.log](log.html) | ||||
|  | ||||
|  | ||||
|     * [nonebot.rule](rule.html) | ||||
|  | ||||
|  | ||||
|     * [nonebot.utils](utils.html) | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -156,7 +156,7 @@ bots = nonebot.get_bots() | ||||
| * **返回** | ||||
|  | ||||
|      | ||||
|     * None | ||||
|     * `None` | ||||
|  | ||||
|  | ||||
|  | ||||
| @@ -196,7 +196,7 @@ nonebot.init(database=Database(...)) | ||||
| * **返回** | ||||
|  | ||||
|      | ||||
|     * None | ||||
|     * `None` | ||||
|  | ||||
|  | ||||
|  | ||||
|   | ||||
							
								
								
									
										90
									
								
								docs/api/rule.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								docs/api/rule.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,90 @@ | ||||
| --- | ||||
| contentSidebar: true | ||||
| sidebarDepth: 0 | ||||
| --- | ||||
|  | ||||
| # NoneBot.rule 模块 | ||||
|  | ||||
| ## 规则 | ||||
|  | ||||
| 每个 `Matcher` 拥有一个 `Rule` ,其中是 `RuleChecker` 的集合,只有当所有 `RuleChecker` 检查结果为 `True` 时继续运行。 | ||||
|  | ||||
| :::tip 提示 | ||||
| `RuleChecker` 既可以是 async function 也可以是 sync function | ||||
| ::: | ||||
|  | ||||
|  | ||||
| ## _class_ `Rule` | ||||
|  | ||||
| 基类:`object` | ||||
|  | ||||
|  | ||||
| * **说明** | ||||
|  | ||||
|     `Matcher` 规则类,当事件传递时,在 `Matcher` 运行前进行检查。 | ||||
|  | ||||
|  | ||||
|  | ||||
| * **示例** | ||||
|  | ||||
|  | ||||
| ```python | ||||
| Rule(async_function) & sync_function | ||||
| # 等价于 | ||||
| from nonebot.utils import run_sync | ||||
| Rule(async_function, run_sync(sync_function)) | ||||
| ``` | ||||
|  | ||||
|  | ||||
| ### `__init__(*checkers)` | ||||
|  | ||||
|  | ||||
| * **参数** | ||||
|  | ||||
|      | ||||
|     * `*checkers: Callable[[Bot, Event, dict], Awaitable[bool]]`: **异步** RuleChecker | ||||
|  | ||||
|  | ||||
|  | ||||
| ### `checkers` | ||||
|  | ||||
|  | ||||
| * **说明** | ||||
|  | ||||
|     存储 `RuleChecker` | ||||
|  | ||||
|  | ||||
|  | ||||
| * **类型** | ||||
|  | ||||
|      | ||||
|     * `Set[Callable[[Bot, Event, dict], Awaitable[bool]]]` | ||||
|  | ||||
|  | ||||
|  | ||||
| ### _async_ `__call__(bot, event, state)` | ||||
|  | ||||
|  | ||||
| * **说明** | ||||
|  | ||||
|     检查是否符合所有规则 | ||||
|  | ||||
|  | ||||
|  | ||||
| * **参数** | ||||
|  | ||||
|      | ||||
|     * `bot: Bot`: Bot 对象 | ||||
|  | ||||
|  | ||||
|     * `event: Event`: Event 对象 | ||||
|  | ||||
|  | ||||
|     * `state: dict`: 当前 State | ||||
|  | ||||
|  | ||||
|  | ||||
| * **返回** | ||||
|  | ||||
|      | ||||
|     * `bool` | ||||
| @@ -19,7 +19,7 @@ sidebarDepth: 0 | ||||
|  | ||||
| * **类型** | ||||
|  | ||||
|     BaseDriver | ||||
|     `BaseDriver` | ||||
|  | ||||
|  | ||||
|  | ||||
| @@ -35,7 +35,7 @@ sidebarDepth: 0 | ||||
|  | ||||
| * **类型** | ||||
|  | ||||
|     BaseWebSocket | ||||
|     `BaseWebSocket` | ||||
|  | ||||
|  | ||||
|  | ||||
| @@ -51,7 +51,7 @@ sidebarDepth: 0 | ||||
|  | ||||
| * **类型** | ||||
|  | ||||
|     BaseBot | ||||
|     `BaseBot` | ||||
|  | ||||
|  | ||||
|  | ||||
| @@ -67,7 +67,7 @@ sidebarDepth: 0 | ||||
|  | ||||
| * **类型** | ||||
|  | ||||
|     BaseEvent | ||||
|     `BaseEvent` | ||||
|  | ||||
|  | ||||
|  | ||||
| @@ -83,7 +83,7 @@ sidebarDepth: 0 | ||||
|  | ||||
| * **类型** | ||||
|  | ||||
|     BaseMessage | ||||
|     `BaseMessage` | ||||
|  | ||||
|  | ||||
|  | ||||
| @@ -99,7 +99,7 @@ sidebarDepth: 0 | ||||
|  | ||||
| * **类型** | ||||
|  | ||||
|     BaseMessageSegment | ||||
|     `BaseMessageSegment` | ||||
|  | ||||
|  | ||||
|  | ||||
| @@ -115,7 +115,7 @@ sidebarDepth: 0 | ||||
|  | ||||
| * **类型** | ||||
|  | ||||
|     Callable[[Bot, Event, dict], Union[Awaitable[None], Awaitable[NoReturn]]] | ||||
|     `Callable[[Bot, Event, dict], Union[Awaitable[None], Awaitable[NoReturn]]]` | ||||
|  | ||||
|  | ||||
|  | ||||
| @@ -131,7 +131,7 @@ sidebarDepth: 0 | ||||
|  | ||||
| * **类型** | ||||
|  | ||||
|     Matcher | ||||
|     `Matcher` | ||||
|  | ||||
|  | ||||
|  | ||||
| @@ -147,7 +147,7 @@ sidebarDepth: 0 | ||||
|  | ||||
| * **类型** | ||||
|  | ||||
|     Rule | ||||
|     `Rule` | ||||
|  | ||||
|  | ||||
|  | ||||
| @@ -163,7 +163,7 @@ sidebarDepth: 0 | ||||
|  | ||||
| * **类型** | ||||
|  | ||||
|     Callable[[Bot, Event, dict], Awaitable[bool]] | ||||
|     `Callable[[Bot, Event, dict], Union[bool, Awaitable[bool]]]` | ||||
|  | ||||
|  | ||||
|  | ||||
| @@ -179,7 +179,7 @@ sidebarDepth: 0 | ||||
|  | ||||
| * **类型** | ||||
|  | ||||
|     Permission | ||||
|     `Permission` | ||||
|  | ||||
|  | ||||
|  | ||||
| @@ -195,7 +195,7 @@ sidebarDepth: 0 | ||||
|  | ||||
| * **类型** | ||||
|  | ||||
|     Callable[[Bot, Event], Awaitable[bool]] | ||||
|     `Callable[[Bot, Event], Union[bool, Awaitable[bool]]]` | ||||
|  | ||||
|  | ||||
|  | ||||
| @@ -211,7 +211,7 @@ sidebarDepth: 0 | ||||
|  | ||||
| * **类型** | ||||
|  | ||||
|     Callable[[Bot, Event, dict], Union[Awaitable[None], Awaitable[NoReturn]]] | ||||
|     `Callable[[Bot, Event, dict], Union[Awaitable[None], Awaitable[NoReturn]]]` | ||||
|  | ||||
|  | ||||
|  | ||||
| @@ -227,7 +227,7 @@ sidebarDepth: 0 | ||||
|  | ||||
| * **类型** | ||||
|  | ||||
|     Callable[[Bot, Event, dict], Union[Awaitable[None], Awaitable[NoReturn]]] | ||||
|     `Callable[[Bot, Event, dict], Union[Awaitable[None], Awaitable[NoReturn]]]` | ||||
|  | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -25,7 +25,7 @@ sidebarDepth: 0 | ||||
| * **返回** | ||||
|  | ||||
|      | ||||
|     * Callable[..., Awaitable[Any]] | ||||
|     * `Callable[..., Awaitable[Any]]` | ||||
|  | ||||
|  | ||||
|  | ||||
| @@ -34,35 +34,6 @@ sidebarDepth: 0 | ||||
| 基类:`json.encoder.JSONEncoder` | ||||
|  | ||||
|  | ||||
| * **类型** | ||||
|  | ||||
|     `json.JSONEncoder` | ||||
|  | ||||
|  | ||||
|  | ||||
| * **说明** | ||||
|  | ||||
|     `JSONEncoder` used when encoding `Message` (List of dataclasses) | ||||
|  | ||||
|  | ||||
|  | ||||
| ### `default(o)` | ||||
|  | ||||
| Implement this method in a subclass such that it returns | ||||
| a serializable object for `o`, or calls the base implementation | ||||
| (to raise a `TypeError`). | ||||
|  | ||||
| For example, to support arbitrary iterators, you could | ||||
| implement default like this: | ||||
|  | ||||
| ```default | ||||
| def default(self, o): | ||||
|     try: | ||||
|         iterable = iter(o) | ||||
|     except TypeError: | ||||
|         pass | ||||
|     else: | ||||
|         return list(iterable) | ||||
|     # Let the base class default method raise the TypeError | ||||
|     return JSONEncoder.default(self, o) | ||||
| ``` | ||||
|     在JSON序列化 `Message` (List[Dataclass]) 时使用的 `JSONEncoder` | ||||
|   | ||||
		Reference in New Issue
	
	Block a user