mirror of
				https://github.com/nonebot/nonebot2.git
				synced 2025-10-31 15:06:42 +00:00 
			
		
		
		
	🐛 fix rule override bug
This commit is contained in:
		| @@ -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, | ||||
| @@ -893,9 +885,9 @@ class MatcherGroup: | ||||
|         :参数: | ||||
|  | ||||
|           * ``cmd: Union[str, Tuple[str, ...]]``: 指定命令内容 | ||||
|         * ``rule: Optional[Union[Rule, T_RuleChecker]]``: 事件响应规则 | ||||
|           * ``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``: 是否为临时事件响应器(仅执行一次) | ||||
| @@ -908,29 +900,19 @@ 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=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]: | ||||
|   | ||||
| @@ -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]] = ..., | ||||
|   | ||||
		Reference in New Issue
	
	Block a user