add state factory support #113

This commit is contained in:
yanyongyu 2020-12-20 11:59:23 +08:00
parent 168cc3801a
commit 1b00fe7921
4 changed files with 239 additions and 164 deletions

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.rule import Rule
from nonebot.log import logger from nonebot.log import logger
from nonebot.permission import Permission, USER 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 from nonebot.exception import PausedException, RejectedException, FinishedException
if TYPE_CHECKING: if TYPE_CHECKING:
@ -95,6 +95,11 @@ class Matcher(metaclass=MatcherMeta):
:类型: ``T_State`` :类型: ``T_State``
:说明: 事件响应器默认状态 :说明: 事件响应器默认状态
""" """
_default_state_factory: Optional[T_StateFactory] = None
"""
:类型: ``Optional[T_State]``
:说明: 事件响应器默认工厂函数
"""
_default_parser: Optional[T_ArgsParser] = None _default_parser: Optional[T_ArgsParser] = None
""" """
@ -126,6 +131,7 @@ class Matcher(metaclass=MatcherMeta):
*, *,
module: Optional[str] = None, module: Optional[str] = None,
default_state: Optional[T_State] = None, default_state: Optional[T_State] = None,
default_state_factory: Optional[T_StateFactory] = None,
expire_time: Optional[datetime] = None) -> Type["Matcher"]: expire_time: Optional[datetime] = None) -> Type["Matcher"]:
""" """
:说明: :说明:
@ -143,6 +149,7 @@ class Matcher(metaclass=MatcherMeta):
* ``block: bool``: 是否阻止事件向更低优先级的响应器传播 * ``block: bool``: 是否阻止事件向更低优先级的响应器传播
* ``module: Optional[str]``: 事件响应器所在模块名称 * ``module: Optional[str]``: 事件响应器所在模块名称
* ``default_state: Optional[T_State]``: 默认状态 ``state`` * ``default_state: Optional[T_State]``: 默认状态 ``state``
* ``default_state_factory: Optional[T_StateFactory]``: 默认状态 ``state`` 的工厂函数
* ``expire_time: Optional[datetime]``: 事件响应器最终有效时间点过时即被删除 * ``expire_time: Optional[datetime]``: 事件响应器最终有效时间点过时即被删除
:返回: :返回:
@ -161,7 +168,8 @@ class Matcher(metaclass=MatcherMeta):
"expire_time": expire_time, "expire_time": expire_time,
"priority": priority, "priority": priority,
"block": block, "block": block,
"_default_state": default_state or {} "_default_state": default_state or {},
"_default_state_factory": default_state_factory
}) })
matchers[priority].append(NewMatcher) matchers[priority].append(NewMatcher)
@ -452,11 +460,13 @@ class Matcher(metaclass=MatcherMeta):
e_t = current_event.set(event) e_t = current_event.set(event)
try: try:
# Refresh preprocess state # 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)): for _ in range(len(self.handlers)):
handler = self.handlers.pop(0) handler = self.handlers.pop(0)
await self.run_handler(handler, bot, event, self.state) await self.run_handler(handler, bot, event, state_)
except RejectedException: except RejectedException:
self.handlers.insert(0, handler) # type: ignore 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.log import logger
from nonebot.matcher import Matcher from nonebot.matcher import Matcher
from nonebot.permission import Permission 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 from nonebot.rule import Rule, startswith, endswith, keyword, command, regex
plugins: Dict[str, "Plugin"] = {} plugins: Dict[str, "Plugin"] = {}
@ -108,7 +108,8 @@ def on(type: str = "",
temp: bool = False, temp: bool = False,
priority: int = 1, priority: int = 1,
block: bool = False, 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``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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, priority=priority,
block=block, block=block,
handlers=handlers, handlers=handlers,
default_state=state) default_state=state,
default_state_factory=state_factory)
_tmp_matchers.get().add(matcher) _tmp_matchers.get().add(matcher)
return matcher return matcher
def on_metaevent(rule: Optional[Union[Rule, T_RuleChecker]] = None, def on_metaevent(
*, rule: Optional[Union[Rule, T_RuleChecker]] = None,
handlers: Optional[List[T_Handler]] = None, *,
temp: bool = False, handlers: Optional[List[T_Handler]] = None,
priority: int = 1, temp: bool = False,
block: bool = False, priority: int = 1,
state: Optional[T_State] = None) -> Type[Matcher]: 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``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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, priority=priority,
block=block, block=block,
handlers=handlers, handlers=handlers,
default_state=state) default_state=state,
default_state_factory=state_factory)
_tmp_matchers.get().add(matcher) _tmp_matchers.get().add(matcher)
return matcher return matcher
@ -185,7 +192,8 @@ def on_message(rule: Optional[Union[Rule, T_RuleChecker]] = None,
temp: bool = False, temp: bool = False,
priority: int = 1, priority: int = 1,
block: bool = True, 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``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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, priority=priority,
block=block, block=block,
handlers=handlers, handlers=handlers,
default_state=state) default_state=state,
default_state_factory=state_factory)
_tmp_matchers.get().add(matcher) _tmp_matchers.get().add(matcher)
return matcher return matcher
@ -223,7 +233,8 @@ def on_notice(rule: Optional[Union[Rule, T_RuleChecker]] = None,
temp: bool = False, temp: bool = False,
priority: int = 1, priority: int = 1,
block: bool = False, 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``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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, priority=priority,
block=block, block=block,
handlers=handlers, handlers=handlers,
default_state=state) default_state=state,
default_state_factory=state_factory)
_tmp_matchers.get().add(matcher) _tmp_matchers.get().add(matcher)
return matcher return matcher
@ -260,7 +273,8 @@ def on_request(rule: Optional[Union[Rule, T_RuleChecker]] = None,
temp: bool = False, temp: bool = False,
priority: int = 1, priority: int = 1,
block: bool = False, 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``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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, priority=priority,
block=block, block=block,
handlers=handlers, handlers=handlers,
default_state=state) default_state=state,
default_state_factory=state_factory)
_tmp_matchers.get().add(matcher) _tmp_matchers.get().add(matcher)
return matcher return matcher
@ -308,7 +324,8 @@ def on_startswith(msg: str,
* ``temp: bool``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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 = self.base_kwargs.copy()
final_kwargs.update(kwargs) final_kwargs.update(kwargs)
matcher = Matcher.new(**final_kwargs) matcher = on(**final_kwargs)
self.matchers.append(matcher) self.matchers.append(matcher)
_tmp_matchers.get().add(matcher)
return matcher return matcher
def on_metaevent(self, **kwargs) -> Type[Matcher]: def on_metaevent(self, **kwargs) -> Type[Matcher]:
@ -547,7 +568,8 @@ class MatcherGroup:
* ``temp: bool``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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 = self.base_kwargs.copy()
final_kwargs.update(kwargs) final_kwargs.update(kwargs)
final_kwargs.pop("type", None) final_kwargs.pop("type", None)
matcher = Matcher.new("meta_event", **final_kwargs) matcher = on_metaevent(**final_kwargs)
self.matchers.append(matcher) self.matchers.append(matcher)
_tmp_matchers.get().add(matcher)
return matcher return matcher
def on_message(self, **kwargs) -> Type[Matcher]: def on_message(self, **kwargs) -> Type[Matcher]:
@ -575,7 +596,8 @@ class MatcherGroup:
* ``temp: bool``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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 = self.base_kwargs.copy()
final_kwargs.update(kwargs) final_kwargs.update(kwargs)
final_kwargs.pop("type", None) final_kwargs.pop("type", None)
matcher = Matcher.new("message", **final_kwargs) matcher = on_message(**final_kwargs)
self.matchers.append(matcher) self.matchers.append(matcher)
_tmp_matchers.get().add(matcher)
return matcher return matcher
def on_notice(self, **kwargs) -> Type[Matcher]: def on_notice(self, **kwargs) -> Type[Matcher]:
@ -602,7 +623,8 @@ class MatcherGroup:
* ``temp: bool``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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 = self.base_kwargs.copy()
final_kwargs.update(kwargs) final_kwargs.update(kwargs)
final_kwargs.pop("type", None) final_kwargs.pop("type", None)
matcher = Matcher.new("notice", **final_kwargs) matcher = on_notice(**final_kwargs)
self.matchers.append(matcher) self.matchers.append(matcher)
_tmp_matchers.get().add(matcher)
return matcher return matcher
def on_request(self, **kwargs) -> Type[Matcher]: def on_request(self, **kwargs) -> Type[Matcher]:
@ -629,7 +650,8 @@ class MatcherGroup:
* ``temp: bool``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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 = self.base_kwargs.copy()
final_kwargs.update(kwargs) final_kwargs.update(kwargs)
final_kwargs.pop("type", None) final_kwargs.pop("type", None)
matcher = Matcher.new("request", **final_kwargs) matcher = on_request(**final_kwargs)
self.matchers.append(matcher) self.matchers.append(matcher)
_tmp_matchers.get().add(matcher)
return matcher return matcher
def on_startswith(self, def on_startswith(self,
@ -662,7 +683,8 @@ class MatcherGroup:
* ``temp: bool``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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``: 是否为临时事件响应器仅执行一次 * ``temp: bool``: 是否为临时事件响应器仅执行一次
* ``priority: int``: 事件响应器优先级 * ``priority: int``: 事件响应器优先级
* ``block: bool``: 是否阻止事件向更低优先级传递 * ``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.rule import Rule
from nonebot.matcher import Matcher from nonebot.matcher import Matcher
from nonebot.permission import Permission 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"] = ... plugins: Dict[str, "Plugin"] = ...
@ -41,17 +41,20 @@ def on(type: str = ...,
temp: bool = ..., temp: bool = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., 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]] = ..., def on_metaevent(
*, rule: Optional[Union[Rule, T_RuleChecker]] = ...,
handlers: Optional[List[T_Handler]] = ..., *,
temp: bool = ..., handlers: Optional[List[T_Handler]] = ...,
priority: int = ..., temp: bool = ...,
block: bool = ..., priority: int = ...,
state: Optional[T_State] = ...) -> Type[Matcher]: 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 = ..., temp: bool = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., 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 = ..., temp: bool = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., 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 = ..., temp: bool = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]: state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
... ...
def on_startswith(msg: str, def on_startswith(
rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = ..., msg: str,
*, rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = ...,
permission: Optional[Permission] = ..., *,
handlers: Optional[List[T_Handler]] = ..., permission: Optional[Permission] = ...,
temp: bool = ..., handlers: Optional[List[T_Handler]] = ...,
priority: int = ..., temp: bool = ...,
block: bool = ..., priority: int = ...,
state: Optional[T_State] = ...) -> Type[Matcher]: block: bool = ...,
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
... ...
@ -106,7 +114,8 @@ def on_endswith(msg: str,
temp: bool = ..., temp: bool = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., 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 = ..., temp: bool = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., 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 = ..., temp: bool = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., 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 = ..., temp: bool = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., 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 = ..., temp: bool = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., 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 = ..., temp: bool = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]: state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
... ...
def on_metaevent(self, def on_metaevent(
*, self,
rule: Optional[Union[Rule, T_RuleChecker]] = None, *,
handlers: Optional[List[T_Handler]] = None, rule: Optional[Union[Rule, T_RuleChecker]] = None,
temp: bool = False, handlers: Optional[List[T_Handler]] = None,
priority: int = 1, temp: bool = False,
block: bool = False, priority: int = 1,
state: Optional[T_State] = None) -> Type[Matcher]: block: bool = False,
state: Optional[T_State] = None,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
... ...
def on_message(self, def on_message(
*, self,
rule: Optional[Union[Rule, T_RuleChecker]] = None, *,
permission: Optional[Permission] = None, rule: Optional[Union[Rule, T_RuleChecker]] = None,
handlers: Optional[List[T_Handler]] = None, permission: Optional[Permission] = None,
temp: bool = False, handlers: Optional[List[T_Handler]] = None,
priority: int = 1, temp: bool = False,
block: bool = True, priority: int = 1,
state: Optional[T_State] = None) -> Type[Matcher]: block: bool = True,
state: Optional[T_State] = None,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
... ...
def on_notice(self, def on_notice(
*, self,
rule: Optional[Union[Rule, T_RuleChecker]] = None, *,
handlers: Optional[List[T_Handler]] = None, rule: Optional[Union[Rule, T_RuleChecker]] = None,
temp: bool = False, handlers: Optional[List[T_Handler]] = None,
priority: int = 1, temp: bool = False,
block: bool = False, priority: int = 1,
state: Optional[T_State] = None) -> Type[Matcher]: block: bool = False,
state: Optional[T_State] = None,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
... ...
def on_request(self, def on_request(
*, self,
rule: Optional[Union[Rule, T_RuleChecker]] = None, *,
handlers: Optional[List[T_Handler]] = None, rule: Optional[Union[Rule, T_RuleChecker]] = None,
temp: bool = False, handlers: Optional[List[T_Handler]] = None,
priority: int = 1, temp: bool = False,
block: bool = False, priority: int = 1,
state: Optional[T_State] = None) -> Type[Matcher]: block: bool = False,
state: Optional[T_State] = None,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
... ...
def on_startswith(self, def on_startswith(
*, self,
msg: str, *,
rule: Optional[Optional[Union[Rule, msg: str,
T_RuleChecker]]] = ..., rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = ...,
permission: Optional[Permission] = ..., permission: Optional[Permission] = ...,
handlers: Optional[List[T_Handler]] = ..., handlers: Optional[List[T_Handler]] = ...,
temp: bool = ..., temp: bool = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ...) -> Type[Matcher]: state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
... ...
def on_endswith(self, def on_endswith(
*, self,
msg: str, *,
rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = ..., msg: str,
permission: Optional[Permission] = ..., rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = ...,
handlers: Optional[List[T_Handler]] = ..., permission: Optional[Permission] = ...,
temp: bool = ..., handlers: Optional[List[T_Handler]] = ...,
priority: int = ..., temp: bool = ...,
block: bool = ..., priority: int = ...,
state: Optional[T_State] = ...) -> Type[Matcher]: block: bool = ...,
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
... ...
def on_keyword(self, def on_keyword(
*, self,
keywords: Set[str], *,
rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = ..., keywords: Set[str],
permission: Optional[Permission] = ..., rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = ...,
handlers: Optional[List[T_Handler]] = ..., permission: Optional[Permission] = ...,
temp: bool = ..., handlers: Optional[List[T_Handler]] = ...,
priority: int = ..., temp: bool = ...,
block: bool = ..., priority: int = ...,
state: Optional[T_State] = ...) -> Type[Matcher]: block: bool = ...,
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
... ...
def on_command(self, def on_command(
*, self,
cmd: Union[str, Tuple[str, ...]], *,
rule: Optional[Union[Rule, T_RuleChecker]] = ..., cmd: Union[str, Tuple[str, ...]],
aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = ..., rule: Optional[Union[Rule, T_RuleChecker]] = ...,
permission: Optional[Permission] = ..., aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = ...,
handlers: Optional[List[T_Handler]] = ..., permission: Optional[Permission] = ...,
temp: bool = ..., handlers: Optional[List[T_Handler]] = ...,
priority: int = ..., temp: bool = ...,
block: bool = ..., priority: int = ...,
state: Optional[T_State] = ...) -> Type[Matcher]: block: bool = ...,
state: Optional[T_State] = ...,
state_factory: Optional[T_StateFactory] = ...) -> Type[Matcher]:
... ...
def on_regex(self, def on_regex(
*, self,
pattern: str, *,
flags: Union[int, re.RegexFlag] = 0, pattern: str,
rule: Optional[Rule] = ..., flags: Union[int, re.RegexFlag] = 0,
permission: Optional[Permission] = ..., rule: Optional[Rule] = ...,
handlers: Optional[List[T_Handler]] = ..., permission: Optional[Permission] = ...,
temp: bool = ..., handlers: Optional[List[T_Handler]] = ...,
priority: int = ..., temp: bool = ...,
block: bool = ..., priority: int = ...,
state: Optional[T_State] = ...) -> Type[Matcher]: 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 类型 事件处理状态 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]] T_EventPreProcessor = Callable[["Bot", "Event", T_State], Awaitable[None]]
""" """