🔀 Merge pull request #115

Add default state factory support
This commit is contained in:
Ju4tCode 2020-12-20 12:20:22 +08:00 committed by GitHub
commit 338d5e6f17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 386 additions and 200 deletions

View File

@ -167,6 +167,21 @@ sidebarDepth: 0
### `_default_state_factory`
* **类型**
`Optional[T_State]`
* **说明**
事件响应器默认工厂函数
### `_default_parser`
@ -202,7 +217,7 @@ sidebarDepth: 0
### _classmethod_ `new(type_='', rule=None, permission=None, handlers=None, temp=False, priority=1, block=False, *, module=None, default_state=None, expire_time=None)`
### _classmethod_ `new(type_='', rule=None, permission=None, handlers=None, temp=False, priority=1, block=False, *, module=None, default_state=None, default_state_factory=None, expire_time=None)`
* **说明**
@ -241,6 +256,9 @@ sidebarDepth: 0
* `default_state: Optional[T_State]`: 默认状态 `state`
* `default_state_factory: Optional[T_StateFactory]`: 默认状态 `state` 的工厂函数
* `expire_time: Optional[datetime]`: 事件响应器最终有效时间点,过时即被删除

View File

@ -100,7 +100,7 @@ def something_else():
* **说明**: 插件内定义的导出内容
## `on(type='', rule=None, permission=None, *, handlers=None, temp=False, priority=1, block=False, state=None)`
## `on(type='', rule=None, permission=None, *, handlers=None, temp=False, priority=1, block=False, state=None, state_factory=None)`
* **说明**
@ -133,7 +133,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数
@ -144,7 +147,7 @@ def something_else():
## `on_metaevent(rule=None, *, handlers=None, temp=False, priority=1, block=False, state=None)`
## `on_metaevent(rule=None, *, handlers=None, temp=False, priority=1, block=False, state=None, state_factory=None)`
* **说明**
@ -171,7 +174,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数
@ -182,7 +188,7 @@ def something_else():
## `on_message(rule=None, permission=None, *, handlers=None, temp=False, priority=1, block=True, state=None)`
## `on_message(rule=None, permission=None, *, handlers=None, temp=False, priority=1, block=True, state=None, state_factory=None)`
* **说明**
@ -212,7 +218,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数
@ -223,7 +232,7 @@ def something_else():
## `on_notice(rule=None, *, handlers=None, temp=False, priority=1, block=False, state=None)`
## `on_notice(rule=None, *, handlers=None, temp=False, priority=1, block=False, state=None, state_factory=None)`
* **说明**
@ -250,7 +259,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数
@ -261,7 +273,7 @@ def something_else():
## `on_request(rule=None, *, handlers=None, temp=False, priority=1, block=False, state=None)`
## `on_request(rule=None, *, handlers=None, temp=False, priority=1, block=False, state=None, state_factory=None)`
* **说明**
@ -288,7 +300,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数
@ -332,7 +347,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数
@ -376,7 +394,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数
@ -420,7 +441,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数
@ -469,7 +493,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数
@ -518,7 +545,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数
@ -666,7 +696,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数
@ -704,7 +737,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数
@ -745,7 +781,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数
@ -783,7 +822,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数
@ -821,7 +863,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数
@ -865,7 +910,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数
@ -909,7 +957,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数
@ -953,7 +1004,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数
@ -1002,7 +1056,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数
@ -1051,7 +1108,10 @@ def something_else():
* `block: bool`: 是否阻止事件向更低优先级传递
* `state: Optional[T_State]`: 默认的 state
* `state: Optional[T_State]`: 默认 state
* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数

View File

@ -30,6 +30,22 @@ sidebarDepth: 0
## `T_StateFactory`
* **类型**
`Callable[[Bot, Event], Awaitable[T_State]]`
* **说明**
事件处理状态 State 类工厂函数
## `T_EventPreProcessor`

View File

@ -15,7 +15,7 @@ from typing import Type, List, Dict, Union, Callable, Optional, NoReturn, TYPE_C
from nonebot.rule import Rule
from nonebot.log import logger
from nonebot.permission import Permission, USER
from nonebot.typing import T_State, T_Handler, T_ArgsParser
from nonebot.typing import T_State, T_StateFactory, T_Handler, T_ArgsParser
from nonebot.exception import PausedException, RejectedException, FinishedException
if TYPE_CHECKING:
@ -95,6 +95,11 @@ class Matcher(metaclass=MatcherMeta):
:类型: ``T_State``
:说明: 事件响应器默认状态
"""
_default_state_factory: Optional[T_StateFactory] = None
"""
:类型: ``Optional[T_State]``
:说明: 事件响应器默认工厂函数
"""
_default_parser: Optional[T_ArgsParser] = None
"""
@ -126,6 +131,7 @@ class Matcher(metaclass=MatcherMeta):
*,
module: Optional[str] = None,
default_state: Optional[T_State] = None,
default_state_factory: Optional[T_StateFactory] = None,
expire_time: Optional[datetime] = None) -> Type["Matcher"]:
"""
:说明:
@ -143,6 +149,7 @@ class Matcher(metaclass=MatcherMeta):
* ``block: bool``: 是否阻止事件向更低优先级的响应器传播
* ``module: Optional[str]``: 事件响应器所在模块名称
* ``default_state: Optional[T_State]``: 默认状态 ``state``
* ``default_state_factory: Optional[T_StateFactory]``: 默认状态 ``state`` 的工厂函数
* ``expire_time: Optional[datetime]``: 事件响应器最终有效时间点过时即被删除
:返回:
@ -152,16 +159,29 @@ class Matcher(metaclass=MatcherMeta):
NewMatcher = type(
"Matcher", (Matcher,), {
"module": module,
"type": type_,
"rule": rule or Rule(),
"permission": permission or Permission(),
"handlers": handlers or [],
"temp": temp,
"expire_time": expire_time,
"priority": priority,
"block": block,
"_default_state": default_state or {}
"module":
module,
"type":
type_,
"rule":
rule or Rule(),
"permission":
permission or Permission(),
"handlers":
handlers or [],
"temp":
temp,
"expire_time":
expire_time,
"priority":
priority,
"block":
block,
"_default_state":
default_state or {},
"_default_state_factory":
staticmethod(default_state_factory)
if default_state_factory else None
})
matchers[priority].append(NewMatcher)
@ -452,11 +472,13 @@ class Matcher(metaclass=MatcherMeta):
e_t = current_event.set(event)
try:
# Refresh preprocess state
self.state.update(state)
state_ = await self._default_state_factory(
bot, event) if self._default_state_factory else self.state
state_.update(state)
for _ in range(len(self.handlers)):
handler = self.handlers.pop(0)
await self.run_handler(handler, bot, event, self.state)
await self.run_handler(handler, bot, event, state_)
except RejectedException:
self.handlers.insert(0, handler) # type: ignore

View File

@ -18,7 +18,7 @@ from typing import Any, Set, List, Dict, Type, Tuple, Union, Optional
from nonebot.log import logger
from nonebot.matcher import Matcher
from nonebot.permission import Permission
from nonebot.typing import T_State, T_Handler, T_RuleChecker
from nonebot.typing import T_State, T_StateFactory, T_Handler, T_RuleChecker
from nonebot.rule import Rule, startswith, endswith, keyword, command, regex
plugins: Dict[str, "Plugin"] = {}
@ -108,7 +108,8 @@ def on(type: str = "",
temp: bool = False,
priority: int = 1,
block: bool = False,
state: Optional[T_State] = None) -> Type[Matcher]:
state: Optional[T_State] = None,
state_factory: Optional[T_StateFactory] = None) -> Type[Matcher]:
"""
:说明:
@ -123,7 +124,8 @@ def on(type: str = "",
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:
@ -136,18 +138,21 @@ def on(type: str = "",
priority=priority,
block=block,
handlers=handlers,
default_state=state)
default_state=state,
default_state_factory=state_factory)
_tmp_matchers.get().add(matcher)
return matcher
def on_metaevent(rule: Optional[Union[Rule, T_RuleChecker]] = None,
*,
handlers: Optional[List[T_Handler]] = None,
temp: bool = False,
priority: int = 1,
block: bool = False,
state: Optional[T_State] = None) -> Type[Matcher]:
def on_metaevent(
rule: Optional[Union[Rule, T_RuleChecker]] = None,
*,
handlers: Optional[List[T_Handler]] = None,
temp: bool = False,
priority: int = 1,
block: bool = False,
state: Optional[T_State] = None,
state_factory: Optional[T_StateFactory] = None) -> Type[Matcher]:
"""
:说明:
@ -160,7 +165,8 @@ def on_metaevent(rule: Optional[Union[Rule, T_RuleChecker]] = None,
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:
@ -173,7 +179,8 @@ def on_metaevent(rule: Optional[Union[Rule, T_RuleChecker]] = None,
priority=priority,
block=block,
handlers=handlers,
default_state=state)
default_state=state,
default_state_factory=state_factory)
_tmp_matchers.get().add(matcher)
return matcher
@ -185,7 +192,8 @@ def on_message(rule: Optional[Union[Rule, T_RuleChecker]] = None,
temp: bool = False,
priority: int = 1,
block: bool = True,
state: Optional[T_State] = None) -> Type[Matcher]:
state: Optional[T_State] = None,
state_factory: Optional[T_StateFactory] = None) -> Type[Matcher]:
"""
:说明:
@ -199,7 +207,8 @@ def on_message(rule: Optional[Union[Rule, T_RuleChecker]] = None,
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:
@ -212,7 +221,8 @@ def on_message(rule: Optional[Union[Rule, T_RuleChecker]] = None,
priority=priority,
block=block,
handlers=handlers,
default_state=state)
default_state=state,
default_state_factory=state_factory)
_tmp_matchers.get().add(matcher)
return matcher
@ -223,7 +233,8 @@ def on_notice(rule: Optional[Union[Rule, T_RuleChecker]] = None,
temp: bool = False,
priority: int = 1,
block: bool = False,
state: Optional[T_State] = None) -> Type[Matcher]:
state: Optional[T_State] = None,
state_factory: Optional[T_StateFactory] = None) -> Type[Matcher]:
"""
:说明:
@ -236,7 +247,8 @@ def on_notice(rule: Optional[Union[Rule, T_RuleChecker]] = None,
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:
@ -249,7 +261,8 @@ def on_notice(rule: Optional[Union[Rule, T_RuleChecker]] = None,
priority=priority,
block=block,
handlers=handlers,
default_state=state)
default_state=state,
default_state_factory=state_factory)
_tmp_matchers.get().add(matcher)
return matcher
@ -260,7 +273,8 @@ def on_request(rule: Optional[Union[Rule, T_RuleChecker]] = None,
temp: bool = False,
priority: int = 1,
block: bool = False,
state: Optional[T_State] = None) -> Type[Matcher]:
state: Optional[T_State] = None,
state_factory: Optional[T_StateFactory] = None) -> Type[Matcher]:
"""
:说明:
@ -273,7 +287,8 @@ def on_request(rule: Optional[Union[Rule, T_RuleChecker]] = None,
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:
@ -286,7 +301,8 @@ def on_request(rule: Optional[Union[Rule, T_RuleChecker]] = None,
priority=priority,
block=block,
handlers=handlers,
default_state=state)
default_state=state,
default_state_factory=state_factory)
_tmp_matchers.get().add(matcher)
return matcher
@ -308,7 +324,8 @@ def on_startswith(msg: str,
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:
@ -334,7 +351,8 @@ def on_endswith(msg: str,
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:
@ -360,7 +378,8 @@ def on_keyword(keywords: Set[str],
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:
@ -390,7 +409,8 @@ def on_command(cmd: Union[str, Tuple[str, ...]],
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:
@ -430,7 +450,8 @@ def on_regex(pattern: str,
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:
@ -521,7 +542,8 @@ class MatcherGroup:
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:
@ -529,9 +551,8 @@ class MatcherGroup:
"""
final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
matcher = Matcher.new(**final_kwargs)
matcher = on(**final_kwargs)
self.matchers.append(matcher)
_tmp_matchers.get().add(matcher)
return matcher
def on_metaevent(self, **kwargs) -> Type[Matcher]:
@ -547,7 +568,8 @@ class MatcherGroup:
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:
@ -556,9 +578,8 @@ class MatcherGroup:
final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
final_kwargs.pop("type", None)
matcher = Matcher.new("meta_event", **final_kwargs)
matcher = on_metaevent(**final_kwargs)
self.matchers.append(matcher)
_tmp_matchers.get().add(matcher)
return matcher
def on_message(self, **kwargs) -> Type[Matcher]:
@ -575,7 +596,8 @@ class MatcherGroup:
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:
@ -584,9 +606,8 @@ class MatcherGroup:
final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
final_kwargs.pop("type", None)
matcher = Matcher.new("message", **final_kwargs)
matcher = on_message(**final_kwargs)
self.matchers.append(matcher)
_tmp_matchers.get().add(matcher)
return matcher
def on_notice(self, **kwargs) -> Type[Matcher]:
@ -602,7 +623,8 @@ class MatcherGroup:
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:
@ -611,9 +633,8 @@ class MatcherGroup:
final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
final_kwargs.pop("type", None)
matcher = Matcher.new("notice", **final_kwargs)
matcher = on_notice(**final_kwargs)
self.matchers.append(matcher)
_tmp_matchers.get().add(matcher)
return matcher
def on_request(self, **kwargs) -> Type[Matcher]:
@ -629,7 +650,8 @@ class MatcherGroup:
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:
@ -638,9 +660,8 @@ class MatcherGroup:
final_kwargs = self.base_kwargs.copy()
final_kwargs.update(kwargs)
final_kwargs.pop("type", None)
matcher = Matcher.new("request", **final_kwargs)
matcher = on_request(**final_kwargs)
self.matchers.append(matcher)
_tmp_matchers.get().add(matcher)
return matcher
def on_startswith(self,
@ -662,7 +683,8 @@ class MatcherGroup:
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:
@ -688,7 +710,8 @@ class MatcherGroup:
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:
@ -714,7 +737,8 @@ class MatcherGroup:
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:
@ -744,7 +768,8 @@ class MatcherGroup:
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:
@ -786,7 +811,8 @@ class MatcherGroup:
* ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递
* ``state: Optional[T_State]``: 默认的 state
* ``state: Optional[T_State]``: 默认 state
* ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数
:返回:

View File

@ -6,7 +6,7 @@ from typing import Any, Set, List, Dict, Type, Tuple, Union, Optional
from nonebot.rule import Rule
from nonebot.matcher import Matcher
from nonebot.permission import Permission
from nonebot.typing import T_State, T_Handler, T_RuleChecker
from nonebot.typing import T_State, T_StateFactory, T_Handler, T_RuleChecker
plugins: Dict[str, "Plugin"] = ...
@ -41,17 +41,20 @@ def on(type: str = ...,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]:
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
def on_metaevent(rule: Optional[Union[Rule, T_RuleChecker]] = ...,
*,
handlers: Optional[List[T_Handler]] = ...,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]:
def on_metaevent(
rule: Optional[Union[Rule, T_RuleChecker]] = ...,
*,
handlers: Optional[List[T_Handler]] = ...,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
@ -62,7 +65,8 @@ def on_message(rule: Optional[Union[Rule, T_RuleChecker]] = ...,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]:
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
@ -72,7 +76,8 @@ def on_notice(rule: Optional[Union[Rule, T_RuleChecker]] = ...,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]:
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
@ -82,19 +87,22 @@ def on_request(rule: Optional[Union[Rule, T_RuleChecker]] = ...,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]:
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
def on_startswith(msg: str,
rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = ...,
*,
permission: Optional[Permission] = ...,
handlers: Optional[List[T_Handler]] = ...,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]:
def on_startswith(
msg: str,
rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = ...,
*,
permission: Optional[Permission] = ...,
handlers: Optional[List[T_Handler]] = ...,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
@ -106,7 +114,8 @@ def on_endswith(msg: str,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]:
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
@ -118,7 +127,8 @@ def on_keyword(keywords: Set[str],
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]:
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
@ -131,7 +141,8 @@ def on_command(cmd: Union[str, Tuple[str, ...]],
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]:
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
@ -144,7 +155,8 @@ def on_regex(pattern: str,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]:
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
@ -201,7 +213,8 @@ class CommandGroup:
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]:
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
@ -228,109 +241,127 @@ class MatcherGroup:
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]:
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
def on_metaevent(self,
*,
rule: Optional[Union[Rule, T_RuleChecker]] = None,
handlers: Optional[List[T_Handler]] = None,
temp: bool = False,
priority: int = 1,
block: bool = False,
state: Optional[T_State] = None) -> Type[Matcher]:
def on_metaevent(
self,
*,
rule: Optional[Union[Rule, T_RuleChecker]] = None,
handlers: Optional[List[T_Handler]] = None,
temp: bool = False,
priority: int = 1,
block: bool = False,
state: Optional[T_State] = None,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
def on_message(self,
*,
rule: Optional[Union[Rule, T_RuleChecker]] = None,
permission: Optional[Permission] = None,
handlers: Optional[List[T_Handler]] = None,
temp: bool = False,
priority: int = 1,
block: bool = True,
state: Optional[T_State] = None) -> Type[Matcher]:
def on_message(
self,
*,
rule: Optional[Union[Rule, T_RuleChecker]] = None,
permission: Optional[Permission] = None,
handlers: Optional[List[T_Handler]] = None,
temp: bool = False,
priority: int = 1,
block: bool = True,
state: Optional[T_State] = None,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
def on_notice(self,
*,
rule: Optional[Union[Rule, T_RuleChecker]] = None,
handlers: Optional[List[T_Handler]] = None,
temp: bool = False,
priority: int = 1,
block: bool = False,
state: Optional[T_State] = None) -> Type[Matcher]:
def on_notice(
self,
*,
rule: Optional[Union[Rule, T_RuleChecker]] = None,
handlers: Optional[List[T_Handler]] = None,
temp: bool = False,
priority: int = 1,
block: bool = False,
state: Optional[T_State] = None,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
def on_request(self,
*,
rule: Optional[Union[Rule, T_RuleChecker]] = None,
handlers: Optional[List[T_Handler]] = None,
temp: bool = False,
priority: int = 1,
block: bool = False,
state: Optional[T_State] = None) -> Type[Matcher]:
def on_request(
self,
*,
rule: Optional[Union[Rule, T_RuleChecker]] = None,
handlers: Optional[List[T_Handler]] = None,
temp: bool = False,
priority: int = 1,
block: bool = False,
state: Optional[T_State] = None,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
def on_startswith(self,
*,
msg: str,
rule: Optional[Optional[Union[Rule,
T_RuleChecker]]] = ...,
permission: Optional[Permission] = ...,
handlers: Optional[List[T_Handler]] = ...,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]:
def on_startswith(
self,
*,
msg: str,
rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = ...,
permission: Optional[Permission] = ...,
handlers: Optional[List[T_Handler]] = ...,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
def on_endswith(self,
*,
msg: str,
rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = ...,
permission: Optional[Permission] = ...,
handlers: Optional[List[T_Handler]] = ...,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]:
def on_endswith(
self,
*,
msg: str,
rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = ...,
permission: Optional[Permission] = ...,
handlers: Optional[List[T_Handler]] = ...,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
def on_keyword(self,
*,
keywords: Set[str],
rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = ...,
permission: Optional[Permission] = ...,
handlers: Optional[List[T_Handler]] = ...,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]:
def on_keyword(
self,
*,
keywords: Set[str],
rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = ...,
permission: Optional[Permission] = ...,
handlers: Optional[List[T_Handler]] = ...,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
def on_command(self,
*,
cmd: Union[str, Tuple[str, ...]],
rule: Optional[Union[Rule, T_RuleChecker]] = ...,
aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = ...,
permission: Optional[Permission] = ...,
handlers: Optional[List[T_Handler]] = ...,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]:
def on_command(
self,
*,
cmd: Union[str, Tuple[str, ...]],
rule: Optional[Union[Rule, T_RuleChecker]] = ...,
aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = ...,
permission: Optional[Permission] = ...,
handlers: Optional[List[T_Handler]] = ...,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...
def on_regex(self,
*,
pattern: str,
flags: Union[int, re.RegexFlag] = 0,
rule: Optional[Rule] = ...,
permission: Optional[Permission] = ...,
handlers: Optional[List[T_Handler]] = ...,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]:
def on_regex(
self,
*,
pattern: str,
flags: Union[int, re.RegexFlag] = 0,
rule: Optional[Rule] = ...,
permission: Optional[Permission] = ...,
handlers: Optional[List[T_Handler]] = ...,
temp: bool = ...,
priority: int = ...,
block: bool = ...,
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
...

View File

@ -43,6 +43,14 @@ T_State = Dict[Any, Any]
事件处理状态 State 类型
"""
T_StateFactory = Callable[["Bot", "Event"], Awaitable[T_State]]
"""
:类型: ``Callable[[Bot, Event], Awaitable[T_State]]``
:说明:
事件处理状态 State 类工厂函数
"""
T_EventPreProcessor = Callable[["Bot", "Event", T_State], Awaitable[None]]
"""

View File

@ -8,9 +8,14 @@ async def heartbeat(bot: Bot, event: Event, state: T_State) -> bool:
return isinstance(event, HeartbeatMetaEvent)
test_matcher = on_metaevent(heartbeat)
async def factory(bot: Bot, event: Event) -> T_State:
return {"factory": True}
test_matcher = on_metaevent(heartbeat, state_factory=factory)
@test_matcher.receive()
async def handle_heartbeat(bot: Bot, event: Event, state: T_State):
print(state)
print("[i] Heartbeat")