From ce0b1c1855ebcea5c32692dbecbe572c8b14b240 Mon Sep 17 00:00:00 2001 From: Akirami <839592615@qq.com> Date: Thu, 8 Sep 2022 14:36:46 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=F0=9F=8E=A8=20=E6=94=B9=E8=BF=9B=20`Comman?= =?UTF-8?q?dGroup`=20=E4=B8=8E=20`MatcherGroup`=20=E7=9A=84=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot/plugin/on.py | 183 ++++++++++++++++++------------------------- 1 file changed, 78 insertions(+), 105 deletions(-) diff --git a/nonebot/plugin/on.py b/nonebot/plugin/on.py index 62b24c9e913b..8116fbd2d461 100644 --- a/nonebot/plugin/on.py +++ b/nonebot/plugin/on.py @@ -6,9 +6,10 @@ """ import re import inspect +from functools import wraps from types import ModuleType from datetime import datetime, timedelta -from typing import Any, Set, Dict, List, Type, Tuple, Union, Optional +from typing import Any, Set, Dict, List, Type, Tuple, Union, Callable, Optional from nonebot.adapters import Event from nonebot.matcher import Matcher @@ -463,7 +464,51 @@ def on_type( return on(rule=is_type(*event_types) & rule, **kwargs, _depth=_depth + 1) -class CommandGroup: +class Group: + def __init__(self, **kwargs): + """创建一个事件响应器组合,参数为默认值,与 `on` 一致""" + self.matchers: List[Type[Matcher]] = [] + """组内事件响应器列表""" + self.base_kwargs: Dict[str, Any] = kwargs + """其他传递给 `on` 的参数默认值""" + + def __init_subclass__(cls) -> None: + def add_matcher( + func: Callable[..., Type[Matcher]] + ) -> Callable[..., Type[Matcher]]: + @wraps(func) + def wrapper(self, *args, **kwargs) -> Type[Matcher]: + matcher = func(self, *args, **kwargs) + self.matchers.append(matcher) + return matcher + + return wrapper + + cls_attrs = inspect.getmembers(cls, inspect.isfunction) + for key, attr in cls_attrs: + return_annotation = inspect.signature(attr).return_annotation + if return_annotation == Type[Matcher]: + setattr(cls, key, add_matcher(attr)) + + def _get_final_kwargs( + self, update: Dict[str, Any], *, exclude: Optional[Set[str]] = None + ) -> dict[str, Any]: + """获取最终传递给 `on` 的参数 + + 参数: + update: 更新的关键字参数 + exclude: 需要排除的参数 + """ + final_kwargs = self.base_kwargs.copy() + final_kwargs.update(update) + if exclude: + for key in exclude: + final_kwargs.pop(key, None) + final_kwargs["_depth"] = 2 + return final_kwargs + + +class CommandGroup(Group): """命令组,用于声明一组有相同名称前缀的命令。 参数: @@ -479,12 +524,10 @@ class CommandGroup: """ def __init__(self, cmd: Union[str, Tuple[str, ...]], **kwargs): - self.basecmd: Tuple[str, ...] = (cmd,) if isinstance(cmd, str) else cmd """命令前缀""" - if "aliases" in kwargs: - del kwargs["aliases"] - self.base_kwargs: Dict[str, Any] = kwargs - """其他传递给 `on_command` 的参数默认值""" + super().__init__(**kwargs) + self.basecmd: Tuple[str, ...] = (cmd,) if isinstance(cmd, str) else cmd + self.base_kwargs.pop("aliases", None) def command(self, cmd: Union[str, Tuple[str, ...]], **kwargs) -> Type[Matcher]: """注册一个新的命令。新参数将会覆盖命令组默认值 @@ -503,10 +546,7 @@ def command(self, cmd: Union[str, Tuple[str, ...]], **kwargs) -> Type[Matcher]: """ sub_cmd = (cmd,) if isinstance(cmd, str) else cmd cmd = self.basecmd + sub_cmd - - final_kwargs = self.base_kwargs.copy() - final_kwargs.update(kwargs) - return on_command(cmd, **final_kwargs, _depth=1) + return on_command(cmd, **self._get_final_kwargs(kwargs)) def shell_command( self, cmd: Union[str, Tuple[str, ...]], **kwargs @@ -528,22 +568,12 @@ def shell_command( """ sub_cmd = (cmd,) if isinstance(cmd, str) else cmd cmd = self.basecmd + sub_cmd - - final_kwargs = self.base_kwargs.copy() - final_kwargs.update(kwargs) - return on_shell_command(cmd, **final_kwargs, _depth=1) + return on_shell_command(cmd, **self._get_final_kwargs(kwargs)) -class MatcherGroup: +class MatcherGroup(Group): """事件响应器组合,统一管理。为 `Matcher` 创建提供默认属性。""" - def __init__(self, **kwargs): - """创建一个事件响应器组合,参数为默认值,与 `on` 一致""" - self.matchers: List[Type[Matcher]] = [] - """组内事件响应器列表""" - self.base_kwargs: Dict[str, Any] = kwargs - """其他传递给 `on` 的参数默认值""" - def on(self, **kwargs) -> Type[Matcher]: """注册一个基础事件响应器,可自定义类型。 @@ -558,11 +588,7 @@ def on(self, **kwargs) -> Type[Matcher]: block: 是否阻止事件向更低优先级传递 state: 默认 state """ - final_kwargs = self.base_kwargs.copy() - final_kwargs.update(kwargs) - matcher = on(**final_kwargs, _depth=1) - self.matchers.append(matcher) - return matcher + return on(**self._get_final_kwargs(kwargs)) def on_metaevent(self, **kwargs) -> Type[Matcher]: """注册一个元事件响应器。 @@ -576,13 +602,8 @@ def on_metaevent(self, **kwargs) -> Type[Matcher]: block: 是否阻止事件向更低优先级传递 state: 默认 state """ - final_kwargs = self.base_kwargs.copy() - final_kwargs.update(kwargs) - final_kwargs.pop("type", None) - final_kwargs.pop("permission", None) - matcher = on_metaevent(**final_kwargs, _depth=1) - self.matchers.append(matcher) - return matcher + final_kwargs = self._get_final_kwargs(kwargs, exclude={"type", "permission"}) + return on_metaevent(**final_kwargs) def on_message(self, **kwargs) -> Type[Matcher]: """注册一个消息事件响应器。 @@ -597,12 +618,8 @@ def on_message(self, **kwargs) -> Type[Matcher]: block: 是否阻止事件向更低优先级传递 state: 默认 state """ - final_kwargs = self.base_kwargs.copy() - final_kwargs.update(kwargs) - final_kwargs.pop("type", None) - matcher = on_message(**final_kwargs, _depth=1) - self.matchers.append(matcher) - return matcher + final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"}) + return on_message(**final_kwargs) def on_notice(self, **kwargs) -> Type[Matcher]: """注册一个通知事件响应器。 @@ -616,13 +633,8 @@ def on_notice(self, **kwargs) -> Type[Matcher]: block: 是否阻止事件向更低优先级传递 state: 默认 state """ - final_kwargs = self.base_kwargs.copy() - final_kwargs.update(kwargs) - final_kwargs.pop("type", None) - final_kwargs.pop("permission", None) - matcher = on_notice(**final_kwargs, _depth=1) - self.matchers.append(matcher) - return matcher + final_kwargs = self._get_final_kwargs(kwargs, exclude={"type", "permission"}) + return on_notice(**final_kwargs) def on_request(self, **kwargs) -> Type[Matcher]: """注册一个请求事件响应器。 @@ -636,13 +648,8 @@ def on_request(self, **kwargs) -> Type[Matcher]: block: 是否阻止事件向更低优先级传递 state: 默认 state """ - final_kwargs = self.base_kwargs.copy() - final_kwargs.update(kwargs) - final_kwargs.pop("type", None) - final_kwargs.pop("permission", None) - matcher = on_request(**final_kwargs, _depth=1) - self.matchers.append(matcher) - return matcher + final_kwargs = self._get_final_kwargs(kwargs, exclude={"type", "permission"}) + return on_request(**final_kwargs) def on_startswith( self, msg: Union[str, Tuple[str, ...]], **kwargs @@ -661,12 +668,8 @@ def on_startswith( block: 是否阻止事件向更低优先级传递 state: 默认 state """ - final_kwargs = self.base_kwargs.copy() - final_kwargs.update(kwargs) - final_kwargs.pop("type", None) - matcher = on_startswith(msg, **final_kwargs, _depth=1) - self.matchers.append(matcher) - return matcher + final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"}) + return on_startswith(msg, **final_kwargs) def on_endswith(self, msg: Union[str, Tuple[str, ...]], **kwargs) -> Type[Matcher]: """注册一个消息事件响应器,并且当消息的**文本部分**以指定内容结尾时响应。 @@ -683,12 +686,8 @@ def on_endswith(self, msg: Union[str, Tuple[str, ...]], **kwargs) -> Type[Matche block: 是否阻止事件向更低优先级传递 state: 默认 state """ - final_kwargs = self.base_kwargs.copy() - final_kwargs.update(kwargs) - final_kwargs.pop("type", None) - matcher = on_endswith(msg, **final_kwargs, _depth=1) - self.matchers.append(matcher) - return matcher + final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"}) + return on_endswith(msg, **final_kwargs) def on_fullmatch(self, msg: Union[str, Tuple[str, ...]], **kwargs) -> Type[Matcher]: """注册一个消息事件响应器,并且当消息的**文本部分**与指定内容完全一致时响应。 @@ -705,12 +704,8 @@ def on_fullmatch(self, msg: Union[str, Tuple[str, ...]], **kwargs) -> Type[Match block: 是否阻止事件向更低优先级传递 state: 默认 state """ - final_kwargs = self.base_kwargs.copy() - final_kwargs.update(kwargs) - final_kwargs.pop("type", None) - matcher = on_fullmatch(msg, **final_kwargs, _depth=1) - self.matchers.append(matcher) - return matcher + final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"}) + return on_fullmatch(msg, **final_kwargs) def on_keyword(self, keywords: Set[str], **kwargs) -> Type[Matcher]: """注册一个消息事件响应器,并且当消息纯文本部分包含关键词时响应。 @@ -726,12 +721,8 @@ def on_keyword(self, keywords: Set[str], **kwargs) -> Type[Matcher]: block: 是否阻止事件向更低优先级传递 state: 默认 state """ - final_kwargs = self.base_kwargs.copy() - final_kwargs.update(kwargs) - final_kwargs.pop("type", None) - matcher = on_keyword(keywords, **final_kwargs, _depth=1) - self.matchers.append(matcher) - return matcher + final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"}) + return on_keyword(keywords, **final_kwargs) def on_command( self, @@ -755,12 +746,8 @@ def on_command( block: 是否阻止事件向更低优先级传递 state: 默认 state """ - final_kwargs = self.base_kwargs.copy() - final_kwargs.update(kwargs) - final_kwargs.pop("type", None) - matcher = on_command(cmd, aliases=aliases, **final_kwargs, _depth=1) - self.matchers.append(matcher) - return matcher + final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"}) + return on_command(cmd, aliases=aliases, **final_kwargs) def on_shell_command( self, @@ -788,14 +775,8 @@ def on_shell_command( block: 是否阻止事件向更低优先级传递 state: 默认 state """ - 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, _depth=1 - ) - self.matchers.append(matcher) - return matcher + final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"}) + return on_shell_command(cmd, aliases=aliases, parser=parser, **final_kwargs) def on_regex( self, pattern: str, flags: Union[int, re.RegexFlag] = 0, **kwargs @@ -816,12 +797,8 @@ def on_regex( block: 是否阻止事件向更低优先级传递 state: 默认 state """ - final_kwargs = self.base_kwargs.copy() - final_kwargs.update(kwargs) - final_kwargs.pop("type", None) - matcher = on_regex(pattern, flags=flags, **final_kwargs, _depth=1) - self.matchers.append(matcher) - return matcher + final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"}) + return on_regex(pattern, flags=flags, **final_kwargs) def on_type( self, types: Union[Type[Event], Tuple[Type[Event]]], **kwargs @@ -839,9 +816,5 @@ def on_type( block: 是否阻止事件向更低优先级传递 state: 默认 state """ - final_kwargs = self.base_kwargs.copy() - final_kwargs.update(kwargs) - final_kwargs.pop("type", None) - matcher = on_type(types, **final_kwargs, _depth=1) - self.matchers.append(matcher) - return matcher + final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"}) + return on_type(types, **final_kwargs) From aca2ab1889e1390eeed0a90799e8d4a678577897 Mon Sep 17 00:00:00 2001 From: Akirami <839592615@qq.com> Date: Thu, 8 Sep 2022 14:51:44 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=20`Group`?= =?UTF-8?q?=20=E7=B1=BB=E5=9E=8B=E6=A0=87=E6=B3=A8=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot/plugin/on.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nonebot/plugin/on.py b/nonebot/plugin/on.py index 8116fbd2d461..f3a1e9873e49 100644 --- a/nonebot/plugin/on.py +++ b/nonebot/plugin/on.py @@ -492,7 +492,7 @@ def wrapper(self, *args, **kwargs) -> Type[Matcher]: def _get_final_kwargs( self, update: Dict[str, Any], *, exclude: Optional[Set[str]] = None - ) -> dict[str, Any]: + ) -> Dict[str, Any]: """获取最终传递给 `on` 的参数 参数: From 561d35c8377fa5d989d2b36bd21d4c10c8b5341b Mon Sep 17 00:00:00 2001 From: Akirami <839592615@qq.com> Date: Thu, 8 Sep 2022 15:18:17 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=F0=9F=94=A5=20=E7=A7=BB=E9=99=A4=20`Group`?= =?UTF-8?q?=20=E7=9A=84=20`=5F=5Finit=5Fsubclass=5F=5F`=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot/plugin/on.py | 78 +++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/nonebot/plugin/on.py b/nonebot/plugin/on.py index f3a1e9873e49..d320d9f14a4c 100644 --- a/nonebot/plugin/on.py +++ b/nonebot/plugin/on.py @@ -472,24 +472,6 @@ def __init__(self, **kwargs): self.base_kwargs: Dict[str, Any] = kwargs """其他传递给 `on` 的参数默认值""" - def __init_subclass__(cls) -> None: - def add_matcher( - func: Callable[..., Type[Matcher]] - ) -> Callable[..., Type[Matcher]]: - @wraps(func) - def wrapper(self, *args, **kwargs) -> Type[Matcher]: - matcher = func(self, *args, **kwargs) - self.matchers.append(matcher) - return matcher - - return wrapper - - cls_attrs = inspect.getmembers(cls, inspect.isfunction) - for key, attr in cls_attrs: - return_annotation = inspect.signature(attr).return_annotation - if return_annotation == Type[Matcher]: - setattr(cls, key, add_matcher(attr)) - def _get_final_kwargs( self, update: Dict[str, Any], *, exclude: Optional[Set[str]] = None ) -> Dict[str, Any]: @@ -546,7 +528,9 @@ def command(self, cmd: Union[str, Tuple[str, ...]], **kwargs) -> Type[Matcher]: """ sub_cmd = (cmd,) if isinstance(cmd, str) else cmd cmd = self.basecmd + sub_cmd - return on_command(cmd, **self._get_final_kwargs(kwargs)) + matcher = on_command(cmd, **self._get_final_kwargs(kwargs)) + self.matchers.append(matcher) + return matcher def shell_command( self, cmd: Union[str, Tuple[str, ...]], **kwargs @@ -568,7 +552,9 @@ def shell_command( """ sub_cmd = (cmd,) if isinstance(cmd, str) else cmd cmd = self.basecmd + sub_cmd - return on_shell_command(cmd, **self._get_final_kwargs(kwargs)) + matcher = on_shell_command(cmd, **self._get_final_kwargs(kwargs)) + self.matchers.append(matcher) + return matcher class MatcherGroup(Group): @@ -588,7 +574,9 @@ def on(self, **kwargs) -> Type[Matcher]: block: 是否阻止事件向更低优先级传递 state: 默认 state """ - return on(**self._get_final_kwargs(kwargs)) + matcher = on(**self._get_final_kwargs(kwargs)) + self.matchers.append(matcher) + return matcher def on_metaevent(self, **kwargs) -> Type[Matcher]: """注册一个元事件响应器。 @@ -603,7 +591,9 @@ def on_metaevent(self, **kwargs) -> Type[Matcher]: state: 默认 state """ final_kwargs = self._get_final_kwargs(kwargs, exclude={"type", "permission"}) - return on_metaevent(**final_kwargs) + matcher = on_metaevent(**final_kwargs) + self.matchers.append(matcher) + return matcher def on_message(self, **kwargs) -> Type[Matcher]: """注册一个消息事件响应器。 @@ -619,7 +609,9 @@ def on_message(self, **kwargs) -> Type[Matcher]: state: 默认 state """ final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"}) - return on_message(**final_kwargs) + matcher = on_message(**final_kwargs) + self.matchers.append(matcher) + return matcher def on_notice(self, **kwargs) -> Type[Matcher]: """注册一个通知事件响应器。 @@ -634,7 +626,9 @@ def on_notice(self, **kwargs) -> Type[Matcher]: state: 默认 state """ final_kwargs = self._get_final_kwargs(kwargs, exclude={"type", "permission"}) - return on_notice(**final_kwargs) + matcher = on_notice(**final_kwargs) + self.matchers.append(matcher) + return matcher def on_request(self, **kwargs) -> Type[Matcher]: """注册一个请求事件响应器。 @@ -649,7 +643,9 @@ def on_request(self, **kwargs) -> Type[Matcher]: state: 默认 state """ final_kwargs = self._get_final_kwargs(kwargs, exclude={"type", "permission"}) - return on_request(**final_kwargs) + matcher = on_request(**final_kwargs) + self.matchers.append(matcher) + return matcher def on_startswith( self, msg: Union[str, Tuple[str, ...]], **kwargs @@ -669,7 +665,9 @@ def on_startswith( state: 默认 state """ final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"}) - return on_startswith(msg, **final_kwargs) + matcher = on_startswith(msg, **final_kwargs) + self.matchers.append(matcher) + return matcher def on_endswith(self, msg: Union[str, Tuple[str, ...]], **kwargs) -> Type[Matcher]: """注册一个消息事件响应器,并且当消息的**文本部分**以指定内容结尾时响应。 @@ -687,7 +685,9 @@ def on_endswith(self, msg: Union[str, Tuple[str, ...]], **kwargs) -> Type[Matche state: 默认 state """ final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"}) - return on_endswith(msg, **final_kwargs) + matcher = on_endswith(msg, **final_kwargs) + self.matchers.append(matcher) + return matcher def on_fullmatch(self, msg: Union[str, Tuple[str, ...]], **kwargs) -> Type[Matcher]: """注册一个消息事件响应器,并且当消息的**文本部分**与指定内容完全一致时响应。 @@ -705,7 +705,9 @@ def on_fullmatch(self, msg: Union[str, Tuple[str, ...]], **kwargs) -> Type[Match state: 默认 state """ final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"}) - return on_fullmatch(msg, **final_kwargs) + matcher = on_fullmatch(msg, **final_kwargs) + self.matchers.append(matcher) + return matcher def on_keyword(self, keywords: Set[str], **kwargs) -> Type[Matcher]: """注册一个消息事件响应器,并且当消息纯文本部分包含关键词时响应。 @@ -722,7 +724,9 @@ def on_keyword(self, keywords: Set[str], **kwargs) -> Type[Matcher]: state: 默认 state """ final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"}) - return on_keyword(keywords, **final_kwargs) + matcher = on_keyword(keywords, **final_kwargs) + self.matchers.append(matcher) + return matcher def on_command( self, @@ -747,7 +751,9 @@ def on_command( state: 默认 state """ final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"}) - return on_command(cmd, aliases=aliases, **final_kwargs) + matcher = on_command(cmd, aliases=aliases, **final_kwargs) + self.matchers.append(matcher) + return matcher def on_shell_command( self, @@ -776,7 +782,9 @@ def on_shell_command( state: 默认 state """ final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"}) - return on_shell_command(cmd, aliases=aliases, parser=parser, **final_kwargs) + 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, **kwargs @@ -798,7 +806,9 @@ def on_regex( state: 默认 state """ final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"}) - return on_regex(pattern, flags=flags, **final_kwargs) + matcher = on_regex(pattern, flags=flags, **final_kwargs) + self.matchers.append(matcher) + return matcher def on_type( self, types: Union[Type[Event], Tuple[Type[Event]]], **kwargs @@ -817,4 +827,6 @@ def on_type( state: 默认 state """ final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"}) - return on_type(types, **final_kwargs) + matcher = on_type(types, **final_kwargs) + self.matchers.append(matcher) + return matcher From db929423ad08190c298759680af65310922af096 Mon Sep 17 00:00:00 2001 From: Akirami <839592615@qq.com> Date: Thu, 8 Sep 2022 15:20:29 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=F0=9F=90=9B=20=E4=B8=8D=E5=AF=B9=E5=A4=96?= =?UTF-8?q?=E6=9A=B4=E9=9C=B2=20`Group`=20=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot/plugin/on.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nonebot/plugin/on.py b/nonebot/plugin/on.py index d320d9f14a4c..7b43f0c6a3fb 100644 --- a/nonebot/plugin/on.py +++ b/nonebot/plugin/on.py @@ -464,7 +464,7 @@ def on_type( return on(rule=is_type(*event_types) & rule, **kwargs, _depth=_depth + 1) -class Group: +class _Group: def __init__(self, **kwargs): """创建一个事件响应器组合,参数为默认值,与 `on` 一致""" self.matchers: List[Type[Matcher]] = [] @@ -490,7 +490,7 @@ def _get_final_kwargs( return final_kwargs -class CommandGroup(Group): +class CommandGroup(_Group): """命令组,用于声明一组有相同名称前缀的命令。 参数: @@ -557,7 +557,7 @@ def shell_command( return matcher -class MatcherGroup(Group): +class MatcherGroup(_Group): """事件响应器组合,统一管理。为 `Matcher` 创建提供默认属性。""" def on(self, **kwargs) -> Type[Matcher]: From a2bee894d69ad493043f4835684fa2ad6d7de142 Mon Sep 17 00:00:00 2001 From: Akirami <839592615@qq.com> Date: Thu, 8 Sep 2022 15:30:51 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=20`=5FGro?= =?UTF-8?q?up`=20=E4=BC=A0=E9=80=92=E9=94=99=E8=AF=AF=E7=9A=84=20`=5Fdepth?= =?UTF-8?q?`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot/plugin/on.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nonebot/plugin/on.py b/nonebot/plugin/on.py index 7b43f0c6a3fb..65a87c8fddfe 100644 --- a/nonebot/plugin/on.py +++ b/nonebot/plugin/on.py @@ -486,7 +486,7 @@ def _get_final_kwargs( if exclude: for key in exclude: final_kwargs.pop(key, None) - final_kwargs["_depth"] = 2 + final_kwargs["_depth"] = 1 return final_kwargs From f242f1b84d9a1f1ac9dc4722760c55cba6391f7b Mon Sep 17 00:00:00 2001 From: Akirami <839592615@qq.com> Date: Fri, 9 Sep 2022 18:58:55 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=20`Comman?= =?UTF-8?q?dGroup`=20=E7=9A=84=20`=5F=5Frepr=5F=5F`=20=E4=B8=A2=E5=A4=B1?= =?UTF-8?q?=20cmd=20=E5=89=8D=E7=BC=80=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot/plugin/on.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/nonebot/plugin/on.py b/nonebot/plugin/on.py index cdc810673da4..5b18eb1054d2 100644 --- a/nonebot/plugin/on.py +++ b/nonebot/plugin/on.py @@ -470,9 +470,6 @@ def __init__(self, **kwargs): self.base_kwargs: Dict[str, Any] = kwargs """其他传递给 `on` 的参数默认值""" - def __repr__(self) -> str: - return f"{self.__class__.__name__}(matchers={len(self.matchers)})" - def _get_final_kwargs( self, update: Dict[str, Any], *, exclude: Optional[Set[str]] = None ) -> Dict[str, Any]: @@ -512,6 +509,9 @@ def __init__(self, cmd: Union[str, Tuple[str, ...]], **kwargs): self.basecmd: Tuple[str, ...] = (cmd,) if isinstance(cmd, str) else cmd self.base_kwargs.pop("aliases", None) + def __repr__(self) -> str: + return f"CommandGroup(cmd={self.basecmd}, matchers={len(self.matchers)})" + def command(self, cmd: Union[str, Tuple[str, ...]], **kwargs) -> Type[Matcher]: """注册一个新的命令。新参数将会覆盖命令组默认值 @@ -561,6 +561,9 @@ def shell_command( class MatcherGroup(_Group): """事件响应器组合,统一管理。为 `Matcher` 创建提供默认属性。""" + def __repr__(self) -> str: + return f"MatcherGroup(matchers={len(self.matchers)})" + def on(self, **kwargs) -> Type[Matcher]: """注册一个基础事件响应器,可自定义类型。