diff --git a/docs/api/plugin.md b/docs/api/plugin.md index 960521da..1d134c52 100644 --- a/docs/api/plugin.md +++ b/docs/api/plugin.md @@ -960,7 +960,7 @@ def something_else(): -### `on_startswith(msg, rule=None, **kwargs)` +### `on_startswith(msg, **kwargs)` * **说明** @@ -1007,7 +1007,7 @@ def something_else(): -### `on_endswith(msg, rule=None, **kwargs)` +### `on_endswith(msg, **kwargs)` * **说明** @@ -1054,7 +1054,7 @@ def something_else(): -### `on_keyword(keywords, rule=None, **kwargs)` +### `on_keyword(keywords, **kwargs)` * **说明** @@ -1101,7 +1101,7 @@ def something_else(): -### `on_command(cmd, rule=None, aliases=None, **kwargs)` +### `on_command(cmd, aliases=None, **kwargs)` * **说明** @@ -1118,12 +1118,12 @@ def something_else(): * `cmd: Union[str, Tuple[str, ...]]`: 指定命令内容 - * `rule: Optional[Union[Rule, T_RuleChecker]]`: 事件响应规则 - - * `aliases: Optional[Set[Union[str, Tuple[str, ...]]]]`: 命令别名 + * `rule: Optional[Union[Rule, T_RuleChecker]]`: 事件响应规则 + + * `permission: Optional[Permission]`: 事件响应权限 @@ -1153,64 +1153,64 @@ def something_else(): -### `on_shell_command(cmd, rule=None, aliases=None, parser=None, **kwargs)` +### `on_shell_command(cmd, aliases=None, parser=None, **kwargs)` * **说明** + 注册一个支持 `shell_like` 解析参数的命令消息事件响应器。 -注册一个支持 `shell_like` 解析参数的命令消息事件响应器。 + 与普通的 `on_command` 不同的是,在添加 `parser` 参数时, 响应器会自动处理消息。 -与普通的 `on_command` 不同的是,在添加 `parser` 参数时, 响应器会自动处理消息。 + 并将用户输入的原始参数列表保存在 `state["argv"]`, `parser` 处理的参数保存在 `state["args"]` 中 -并将用户输入的原始参数列表保存在 `state["argv"]`, `parser` 处理的参数保存在 `state["args"]` 中 * **参数** + + * `cmd: Union[str, Tuple[str, ...]]`: 指定命令内容 -* `cmd: Union[str, Tuple[str, ...]]`: 指定命令内容 + * `aliases: Optional[Set[Union[str, Tuple[str, ...]]]]`: 命令别名 -* `rule: Optional[Union[Rule, T_RuleChecker]]`: 事件响应规则 + * `parser: Optional[ArgumentParser]`: `nonebot.rule.ArgumentParser` 对象 -* `aliases: Optional[Set[Union[str, Tuple[str, ...]]]]`: 命令别名 + * `rule: Optional[Union[Rule, T_RuleChecker]]`: 事件响应规则 -* `parser: Optional[ArgumentParser]`: `nonebot.rule.ArgumentParser` 对象 + * `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]`: 默认 state -* `state: Optional[T_State]`: 默认 state + * `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数 -* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数 - * **返回** + + * `Type[Matcher]` -* `Type[Matcher]` - -### `on_regex(pattern, flags=0, rule=None, **kwargs)` +### `on_regex(pattern, flags=0, **kwargs)` * **说明** diff --git a/nonebot/plugin.py b/nonebot/plugin.py index 38d657ac..3270fd12 100644 --- a/nonebot/plugin.py +++ b/nonebot/plugin.py @@ -745,11 +745,7 @@ class MatcherGroup: self.matchers.append(matcher) return matcher - def on_startswith(self, - msg: str, - rule: Optional[Optional[Union[Rule, - T_RuleChecker]]] = None, - **kwargs) -> Type[Matcher]: + def on_startswith(self, msg: str, **kwargs) -> Type[Matcher]: """ :说明: @@ -771,12 +767,14 @@ class MatcherGroup: - ``Type[Matcher]`` """ - return self.on_message(rule=startswith(msg) & rule, **kwargs) + final_kwargs = self.base_kwargs.copy() + final_kwargs.update(kwargs) + final_kwargs.pop("type", None) + matcher = on_startswith(msg, **final_kwargs) + self.matchers.append(matcher) + return matcher - def on_endswith(self, - msg: str, - rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = None, - **kwargs) -> Type[Matcher]: + def on_endswith(self, msg: str, **kwargs) -> Type[Matcher]: """ :说明: @@ -798,12 +796,14 @@ class MatcherGroup: - ``Type[Matcher]`` """ - return self.on_message(rule=endswith(msg) & rule, **kwargs) + final_kwargs = self.base_kwargs.copy() + final_kwargs.update(kwargs) + final_kwargs.pop("type", None) + matcher = on_endswith(msg, **final_kwargs) + self.matchers.append(matcher) + return matcher - def on_keyword(self, - keywords: Set[str], - rule: Optional[Union[Rule, T_RuleChecker]] = None, - **kwargs) -> Type[Matcher]: + def on_keyword(self, keywords: Set[str], **kwargs) -> Type[Matcher]: """ :说明: @@ -825,11 +825,15 @@ class MatcherGroup: - ``Type[Matcher]`` """ - return self.on_message(rule=keyword(*keywords) & rule, **kwargs) + final_kwargs = self.base_kwargs.copy() + final_kwargs.update(kwargs) + final_kwargs.pop("type", None) + matcher = on_keyword(keywords, **final_kwargs) + self.matchers.append(matcher) + return matcher def on_command(self, cmd: Union[str, Tuple[str, ...]], - rule: Optional[Union[Rule, T_RuleChecker]] = None, aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = None, **kwargs) -> Type[Matcher]: """ @@ -842,8 +846,8 @@ class MatcherGroup: :参数: * ``cmd: Union[str, Tuple[str, ...]]``: 指定命令内容 - * ``rule: Optional[Union[Rule, T_RuleChecker]]``: 事件响应规则 * ``aliases: Optional[Set[Union[str, Tuple[str, ...]]]]``: 命令别名 + * ``rule: Optional[Union[Rule, T_RuleChecker]]``: 事件响应规则 * ``permission: Optional[Permission]``: 事件响应权限 * ``handlers: Optional[List[T_Handler]]``: 事件处理函数列表 * ``temp: bool``: 是否为临时事件响应器(仅执行一次) @@ -856,27 +860,15 @@ class MatcherGroup: - ``Type[Matcher]`` """ - - async def _strip_cmd(bot: "Bot", event: "Event", state: T_State): - message = event.get_message() - segment = message.pop(0) - new_message = message.__class__( - str(segment) - [len(state["_prefix"]["raw_command"]):].strip()) # type: ignore - for new_segment in reversed(new_message): - message.insert(0, new_segment) - - handlers = kwargs.pop("handlers", []) - handlers.insert(0, _strip_cmd) - - commands = set([cmd]) | (aliases or set()) - return self.on_message(rule=command(*commands) & rule, - handlers=handlers, - **kwargs) + final_kwargs = self.base_kwargs.copy() + final_kwargs.update(kwargs) + final_kwargs.pop("type", None) + matcher = on_command(cmd, aliases=aliases, **final_kwargs) + self.matchers.append(matcher) + return matcher def on_shell_command(self, cmd: Union[str, Tuple[str, ...]], - rule: Optional[Union[Rule, T_RuleChecker]] = None, aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = None, parser: Optional[ArgumentParser] = None, @@ -884,53 +876,43 @@ class MatcherGroup: """ :说明: - 注册一个支持 ``shell_like`` 解析参数的命令消息事件响应器。 + 注册一个支持 ``shell_like`` 解析参数的命令消息事件响应器。 - 与普通的 ``on_command`` 不同的是,在添加 ``parser`` 参数时, 响应器会自动处理消息。 + 与普通的 ``on_command`` 不同的是,在添加 ``parser`` 参数时, 响应器会自动处理消息。 - 并将用户输入的原始参数列表保存在 ``state["argv"]``, ``parser`` 处理的参数保存在 ``state["args"]`` 中 + 并将用户输入的原始参数列表保存在 ``state["argv"]``, ``parser`` 处理的参数保存在 ``state["args"]`` 中 :参数: - * ``cmd: Union[str, Tuple[str, ...]]``: 指定命令内容 - * ``rule: Optional[Union[Rule, T_RuleChecker]]``: 事件响应规则 - * ``aliases: Optional[Set[Union[str, Tuple[str, ...]]]]``: 命令别名 - * ``parser: Optional[ArgumentParser]``: ``nonebot.rule.ArgumentParser`` 对象 - * ``permission: Optional[Permission]``: 事件响应权限 - * ``handlers: Optional[List[T_Handler]]``: 事件处理函数列表 - * ``temp: bool``: 是否为临时事件响应器(仅执行一次) - * ``priority: int``: 事件响应器优先级 - * ``block: bool``: 是否阻止事件向更低优先级传递 - * ``state: Optional[T_State]``: 默认 state - * ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数 + * ``cmd: Union[str, Tuple[str, ...]]``: 指定命令内容 + * ``aliases: Optional[Set[Union[str, Tuple[str, ...]]]]``: 命令别名 + * ``parser: Optional[ArgumentParser]``: ``nonebot.rule.ArgumentParser`` 对象 + * ``rule: Optional[Union[Rule, T_RuleChecker]]``: 事件响应规则 + * ``permission: Optional[Permission]``: 事件响应权限 + * ``handlers: Optional[List[T_Handler]]``: 事件处理函数列表 + * ``temp: bool``: 是否为临时事件响应器(仅执行一次) + * ``priority: int``: 事件响应器优先级 + * ``block: bool``: 是否阻止事件向更低优先级传递 + * ``state: Optional[T_State]``: 默认 state + * ``state_factory: Optional[T_StateFactory]``: 默认 state 的工厂函数 :返回: - - ``Type[Matcher]`` + - ``Type[Matcher]`` """ - - async def _strip_cmd(bot: "Bot", event: "Event", state: T_State): - message = event.get_message() - segment = message.pop(0) - new_message = message.__class__( - str(segment) - [len(state["_prefix"]["raw_command"]):].strip()) # type: ignore - for new_segment in reversed(new_message): - message.insert(0, new_segment) - - handlers = kwargs.pop("handlers", []) - handlers.insert(0, _strip_cmd) - - commands = set([cmd]) | (aliases or set()) - return self.on_message(rule=shell_command(*commands, parser=parser) & - rule, - handlers=handlers, - **kwargs) + final_kwargs = self.base_kwargs.copy() + final_kwargs.update(kwargs) + final_kwargs.pop("type", None) + matcher = on_shell_command(cmd, + aliases=aliases, + parser=parser, + **final_kwargs) + self.matchers.append(matcher) + return matcher def on_regex(self, pattern: str, flags: Union[int, re.RegexFlag] = 0, - rule: Optional[Rule] = None, **kwargs) -> Type[Matcher]: """ :说明: @@ -956,7 +938,12 @@ class MatcherGroup: - ``Type[Matcher]`` """ - return self.on_message(rule=regex(pattern, flags) & rule, **kwargs) + final_kwargs = self.base_kwargs.copy() + final_kwargs.update(kwargs) + final_kwargs.pop("type", None) + matcher = on_regex(pattern, flags=flags, **final_kwargs) + self.matchers.append(matcher) + return matcher def load_plugin(module_path: str) -> Optional[Plugin]: diff --git a/nonebot/plugin.pyi b/nonebot/plugin.pyi index b1efdbe4..409043d5 100644 --- a/nonebot/plugin.pyi +++ b/nonebot/plugin.pyi @@ -320,8 +320,8 @@ class MatcherGroup: def on_startswith( self, - *, msg: str, + *, rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = ..., permission: Optional[Permission] = ..., handlers: Optional[List[T_Handler]] = ..., @@ -334,8 +334,8 @@ class MatcherGroup: def on_endswith( self, - *, msg: str, + *, rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = ..., permission: Optional[Permission] = ..., handlers: Optional[List[T_Handler]] = ..., @@ -348,8 +348,8 @@ class MatcherGroup: def on_keyword( self, - *, keywords: Set[str], + *, rule: Optional[Optional[Union[Rule, T_RuleChecker]]] = ..., permission: Optional[Permission] = ..., handlers: Optional[List[T_Handler]] = ..., @@ -362,10 +362,10 @@ class MatcherGroup: def on_command( self, - *, cmd: Union[str, Tuple[str, ...]], - rule: Optional[Union[Rule, T_RuleChecker]] = ..., aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = ..., + *, + rule: Optional[Union[Rule, T_RuleChecker]] = ..., permission: Optional[Permission] = ..., handlers: Optional[List[T_Handler]] = ..., temp: bool = ..., @@ -377,11 +377,11 @@ class MatcherGroup: def on_shell_command( self, - *, cmd: Union[str, Tuple[str, ...]], - rule: Optional[Union[Rule, T_RuleChecker]] = ..., aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = ..., parser: Optional[ArgumentParser] = ..., + *, + rule: Optional[Union[Rule, T_RuleChecker]] = ..., permission: Optional[Permission] = ..., handlers: Optional[List[T_Handler]] = ..., temp: bool = ..., @@ -393,9 +393,9 @@ class MatcherGroup: def on_regex( self, - *, pattern: str, flags: Union[int, re.RegexFlag] = 0, + *, rule: Optional[Rule] = ..., permission: Optional[Permission] = ..., handlers: Optional[List[T_Handler]] = ...,