mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-09-06 20:16:47 +00:00
💥 Remove: 移除 Python 3.8 支持 (#2641)
This commit is contained in:
@ -39,7 +39,7 @@ FrontMatter:
|
||||
from itertools import chain
|
||||
from types import ModuleType
|
||||
from contextvars import ContextVar
|
||||
from typing import Set, Dict, List, Type, Tuple, TypeVar, Optional
|
||||
from typing import TypeVar, Optional
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
@ -48,9 +48,9 @@ from nonebot.compat import model_dump, type_validate_python
|
||||
|
||||
C = TypeVar("C", bound=BaseModel)
|
||||
|
||||
_plugins: Dict[str, "Plugin"] = {}
|
||||
_managers: List["PluginManager"] = []
|
||||
_current_plugin_chain: ContextVar[Tuple["Plugin", ...]] = ContextVar(
|
||||
_plugins: dict[str, "Plugin"] = {}
|
||||
_managers: list["PluginManager"] = []
|
||||
_current_plugin_chain: ContextVar[tuple["Plugin", ...]] = ContextVar(
|
||||
"_current_plugin_chain", default=()
|
||||
)
|
||||
|
||||
@ -105,17 +105,17 @@ def get_plugin_by_module_name(module_name: str) -> Optional["Plugin"]:
|
||||
module_name, *has_parent = module_name.rsplit(".", 1)
|
||||
|
||||
|
||||
def get_loaded_plugins() -> Set["Plugin"]:
|
||||
def get_loaded_plugins() -> set["Plugin"]:
|
||||
"""获取当前已导入的所有插件。"""
|
||||
return set(_plugins.values())
|
||||
|
||||
|
||||
def get_available_plugin_names() -> Set[str]:
|
||||
def get_available_plugin_names() -> set[str]:
|
||||
"""获取当前所有可用的插件名(包含尚未加载的插件)。"""
|
||||
return {*chain.from_iterable(manager.available_plugins for manager in _managers)}
|
||||
|
||||
|
||||
def get_plugin_config(config: Type[C]) -> C:
|
||||
def get_plugin_config(config: type[C]) -> C:
|
||||
"""从全局配置获取当前插件需要的配置项。"""
|
||||
return type_validate_python(config, model_dump(get_driver().config))
|
||||
|
||||
|
@ -8,7 +8,8 @@ FrontMatter:
|
||||
import json
|
||||
from pathlib import Path
|
||||
from types import ModuleType
|
||||
from typing import Set, Union, Iterable, Optional
|
||||
from typing import Union, Optional
|
||||
from collections.abc import Iterable
|
||||
|
||||
from nonebot.utils import path_to_module_name
|
||||
|
||||
@ -39,7 +40,7 @@ def load_plugin(module_path: Union[str, Path]) -> Optional[Plugin]:
|
||||
return manager.load_plugin(module_path)
|
||||
|
||||
|
||||
def load_plugins(*plugin_dir: str) -> Set[Plugin]:
|
||||
def load_plugins(*plugin_dir: str) -> set[Plugin]:
|
||||
"""导入文件夹下多个插件,以 `_` 开头的插件不会被导入!
|
||||
|
||||
参数:
|
||||
@ -52,7 +53,7 @@ def load_plugins(*plugin_dir: str) -> Set[Plugin]:
|
||||
|
||||
def load_all_plugins(
|
||||
module_path: Iterable[str], plugin_dir: Iterable[str]
|
||||
) -> Set[Plugin]:
|
||||
) -> set[Plugin]:
|
||||
"""导入指定列表中的插件以及指定目录下多个插件,以 `_` 开头的插件不会被导入!
|
||||
|
||||
参数:
|
||||
@ -64,7 +65,7 @@ def load_all_plugins(
|
||||
return manager.load_all_plugins()
|
||||
|
||||
|
||||
def load_from_json(file_path: str, encoding: str = "utf-8") -> Set[Plugin]:
|
||||
def load_from_json(file_path: str, encoding: str = "utf-8") -> set[Plugin]:
|
||||
"""导入指定 json 文件中的 `plugins` 以及 `plugin_dirs` 下多个插件。
|
||||
以 `_` 开头的插件不会被导入!
|
||||
|
||||
@ -95,7 +96,7 @@ def load_from_json(file_path: str, encoding: str = "utf-8") -> Set[Plugin]:
|
||||
return load_all_plugins(set(plugins), set(plugin_dirs))
|
||||
|
||||
|
||||
def load_from_toml(file_path: str, encoding: str = "utf-8") -> Set[Plugin]:
|
||||
def load_from_toml(file_path: str, encoding: str = "utf-8") -> set[Plugin]:
|
||||
"""导入指定 toml 文件 `[tool.nonebot]` 中的
|
||||
`plugins` 以及 `plugin_dirs` 下多个插件。
|
||||
以 `_` 开头的插件不会被导入!
|
||||
@ -139,7 +140,7 @@ def load_builtin_plugin(name: str) -> Optional[Plugin]:
|
||||
return load_plugin(f"nonebot.plugins.{name}")
|
||||
|
||||
|
||||
def load_builtin_plugins(*plugins: str) -> Set[Plugin]:
|
||||
def load_builtin_plugins(*plugins: str) -> set[Plugin]:
|
||||
"""导入多个 NoneBot 内置插件。
|
||||
|
||||
参数:
|
||||
@ -184,7 +185,7 @@ def require(name: str) -> ModuleType:
|
||||
return plugin.module
|
||||
|
||||
|
||||
def inherit_supported_adapters(*names: str) -> Optional[Set[str]]:
|
||||
def inherit_supported_adapters(*names: str) -> Optional[set[str]]:
|
||||
"""获取已加载插件的适配器支持状态集合。
|
||||
|
||||
如果传入了多个插件名称,返回值会自动取交集。
|
||||
@ -196,7 +197,7 @@ def inherit_supported_adapters(*names: str) -> Optional[Set[str]]:
|
||||
RuntimeError: 插件未加载
|
||||
ValueError: 插件缺少元数据
|
||||
"""
|
||||
final_supported: Optional[Set[str]] = None
|
||||
final_supported: Optional[set[str]] = None
|
||||
|
||||
for name in names:
|
||||
plugin = get_plugin(_module_name_to_plugin_name(name))
|
||||
|
@ -12,10 +12,11 @@ import pkgutil
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
from itertools import chain
|
||||
from typing import Optional
|
||||
from types import ModuleType
|
||||
from importlib.abc import MetaPathFinder
|
||||
from collections.abc import Iterable, Sequence
|
||||
from importlib.machinery import PathFinder, SourceFileLoader
|
||||
from typing import Set, Dict, List, Iterable, Optional, Sequence
|
||||
|
||||
from nonebot.log import logger
|
||||
from nonebot.utils import escape_tag, path_to_module_name
|
||||
@ -44,34 +45,34 @@ class PluginManager:
|
||||
search_path: Optional[Iterable[str]] = None,
|
||||
):
|
||||
# simple plugin not in search path
|
||||
self.plugins: Set[str] = set(plugins or [])
|
||||
self.search_path: Set[str] = set(search_path or [])
|
||||
self.plugins: set[str] = set(plugins or [])
|
||||
self.search_path: set[str] = set(search_path or [])
|
||||
|
||||
# cache plugins
|
||||
self._third_party_plugin_names: Dict[str, str] = {}
|
||||
self._searched_plugin_names: Dict[str, Path] = {}
|
||||
self._third_party_plugin_names: dict[str, str] = {}
|
||||
self._searched_plugin_names: dict[str, Path] = {}
|
||||
self.prepare_plugins()
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"PluginManager(plugins={self.plugins}, search_path={self.search_path})"
|
||||
|
||||
@property
|
||||
def third_party_plugins(self) -> Set[str]:
|
||||
def third_party_plugins(self) -> set[str]:
|
||||
"""返回所有独立插件名称。"""
|
||||
return set(self._third_party_plugin_names.keys())
|
||||
|
||||
@property
|
||||
def searched_plugins(self) -> Set[str]:
|
||||
def searched_plugins(self) -> set[str]:
|
||||
"""返回已搜索到的插件名称。"""
|
||||
return set(self._searched_plugin_names.keys())
|
||||
|
||||
@property
|
||||
def available_plugins(self) -> Set[str]:
|
||||
def available_plugins(self) -> set[str]:
|
||||
"""返回当前插件管理器中可用的插件名称。"""
|
||||
return self.third_party_plugins | self.searched_plugins
|
||||
|
||||
def _previous_plugins(self) -> Set[str]:
|
||||
_pre_managers: List[PluginManager]
|
||||
def _previous_plugins(self) -> set[str]:
|
||||
_pre_managers: list[PluginManager]
|
||||
if self in _managers:
|
||||
_pre_managers = _managers[: _managers.index(self)]
|
||||
else:
|
||||
@ -81,12 +82,12 @@ class PluginManager:
|
||||
*chain.from_iterable(manager.available_plugins for manager in _pre_managers)
|
||||
}
|
||||
|
||||
def prepare_plugins(self) -> Set[str]:
|
||||
def prepare_plugins(self) -> set[str]:
|
||||
"""搜索插件并缓存插件名称。"""
|
||||
# get all previous ready to load plugins
|
||||
previous_plugins = self._previous_plugins()
|
||||
searched_plugins: Dict[str, Path] = {}
|
||||
third_party_plugins: Dict[str, str] = {}
|
||||
searched_plugins: dict[str, Path] = {}
|
||||
third_party_plugins: dict[str, str] = {}
|
||||
|
||||
# check third party plugins
|
||||
for plugin in self.plugins:
|
||||
@ -170,7 +171,7 @@ class PluginManager:
|
||||
f'<r><bg #f8bbd0>Failed to import "{escape_tag(name)}"</bg #f8bbd0></r>'
|
||||
)
|
||||
|
||||
def load_all_plugins(self) -> Set[Plugin]:
|
||||
def load_all_plugins(self) -> set[Plugin]:
|
||||
"""加载所有可用插件。"""
|
||||
|
||||
return set(
|
||||
|
@ -8,7 +8,7 @@ FrontMatter:
|
||||
import contextlib
|
||||
from types import ModuleType
|
||||
from dataclasses import field, dataclass
|
||||
from typing import TYPE_CHECKING, Any, Set, Dict, Type, Optional
|
||||
from typing import TYPE_CHECKING, Any, Type, Optional # noqa: UP035
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
@ -35,19 +35,19 @@ class PluginMetadata:
|
||||
"""插件类型,用于商店分类"""
|
||||
homepage: Optional[str] = None
|
||||
"""插件主页"""
|
||||
config: Optional[Type[BaseModel]] = None
|
||||
config: Optional[Type[BaseModel]] = None # noqa: UP006
|
||||
"""插件配置项"""
|
||||
supported_adapters: Optional[Set[str]] = None
|
||||
supported_adapters: Optional[set[str]] = None
|
||||
"""插件支持的适配器模块路径
|
||||
|
||||
格式为 `<module>[:<Adapter>]`,`~` 为 `nonebot.adapters.` 的缩写。
|
||||
|
||||
`None` 表示支持**所有适配器**。
|
||||
"""
|
||||
extra: Dict[Any, Any] = field(default_factory=dict)
|
||||
extra: dict[Any, Any] = field(default_factory=dict)
|
||||
"""插件额外信息,可由插件编写者自由扩展定义"""
|
||||
|
||||
def get_supported_adapters(self) -> Optional[Set[Type["Adapter"]]]:
|
||||
def get_supported_adapters(self) -> Optional[set[Type["Adapter"]]]: # noqa: UP006
|
||||
"""获取当前已安装的插件支持适配器类列表"""
|
||||
if self.supported_adapters is None:
|
||||
return None
|
||||
@ -73,10 +73,10 @@ class Plugin:
|
||||
"""点分割模块路径"""
|
||||
manager: "PluginManager"
|
||||
"""导入该插件的插件管理器"""
|
||||
matcher: Set[Type[Matcher]] = field(default_factory=set)
|
||||
matcher: set[type[Matcher]] = field(default_factory=set)
|
||||
"""插件加载时定义的 `Matcher`"""
|
||||
parent_plugin: Optional["Plugin"] = None
|
||||
"""父插件"""
|
||||
sub_plugins: Set["Plugin"] = field(default_factory=set)
|
||||
sub_plugins: set["Plugin"] = field(default_factory=set)
|
||||
"""子插件集合"""
|
||||
metadata: Optional[PluginMetadata] = None
|
||||
|
@ -9,8 +9,8 @@ import re
|
||||
import inspect
|
||||
import warnings
|
||||
from types import ModuleType
|
||||
from typing import Any, Union, Optional
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any, Set, Dict, List, Type, Tuple, Union, Optional
|
||||
|
||||
from nonebot.adapters import Event
|
||||
from nonebot.permission import Permission
|
||||
@ -35,7 +35,7 @@ from . import get_plugin_by_module_name
|
||||
from .manager import _current_plugin_chain
|
||||
|
||||
|
||||
def store_matcher(matcher: Type[Matcher]) -> None:
|
||||
def store_matcher(matcher: type[Matcher]) -> None:
|
||||
"""存储一个事件响应器到插件。
|
||||
|
||||
参数:
|
||||
@ -109,14 +109,14 @@ def on(
|
||||
rule: Optional[Union[Rule, T_RuleChecker]] = None,
|
||||
permission: Optional[Union[Permission, T_PermissionChecker]] = None,
|
||||
*,
|
||||
handlers: Optional[List[Union[T_Handler, Dependent]]] = None,
|
||||
handlers: Optional[list[Union[T_Handler, Dependent]]] = None,
|
||||
temp: bool = False,
|
||||
expire_time: Optional[Union[datetime, timedelta]] = None,
|
||||
priority: int = 1,
|
||||
block: bool = False,
|
||||
state: Optional[T_State] = None,
|
||||
_depth: int = 0,
|
||||
) -> Type[Matcher]:
|
||||
) -> type[Matcher]:
|
||||
"""注册一个基础事件响应器,可自定义类型。
|
||||
|
||||
参数:
|
||||
@ -146,7 +146,7 @@ def on(
|
||||
return matcher
|
||||
|
||||
|
||||
def on_metaevent(*args, _depth: int = 0, **kwargs) -> Type[Matcher]:
|
||||
def on_metaevent(*args, _depth: int = 0, **kwargs) -> type[Matcher]:
|
||||
"""注册一个元事件响应器。
|
||||
|
||||
参数:
|
||||
@ -162,7 +162,7 @@ def on_metaevent(*args, _depth: int = 0, **kwargs) -> Type[Matcher]:
|
||||
return on("meta_event", *args, **kwargs, _depth=_depth + 1)
|
||||
|
||||
|
||||
def on_message(*args, _depth: int = 0, **kwargs) -> Type[Matcher]:
|
||||
def on_message(*args, _depth: int = 0, **kwargs) -> type[Matcher]:
|
||||
"""注册一个消息事件响应器。
|
||||
|
||||
参数:
|
||||
@ -179,7 +179,7 @@ def on_message(*args, _depth: int = 0, **kwargs) -> Type[Matcher]:
|
||||
return on("message", *args, **kwargs, _depth=_depth + 1)
|
||||
|
||||
|
||||
def on_notice(*args, _depth: int = 0, **kwargs) -> Type[Matcher]:
|
||||
def on_notice(*args, _depth: int = 0, **kwargs) -> type[Matcher]:
|
||||
"""注册一个通知事件响应器。
|
||||
|
||||
参数:
|
||||
@ -195,7 +195,7 @@ def on_notice(*args, _depth: int = 0, **kwargs) -> Type[Matcher]:
|
||||
return on("notice", *args, **kwargs, _depth=_depth + 1)
|
||||
|
||||
|
||||
def on_request(*args, _depth: int = 0, **kwargs) -> Type[Matcher]:
|
||||
def on_request(*args, _depth: int = 0, **kwargs) -> type[Matcher]:
|
||||
"""注册一个请求事件响应器。
|
||||
|
||||
参数:
|
||||
@ -212,12 +212,12 @@ def on_request(*args, _depth: int = 0, **kwargs) -> Type[Matcher]:
|
||||
|
||||
|
||||
def on_startswith(
|
||||
msg: Union[str, Tuple[str, ...]],
|
||||
msg: Union[str, tuple[str, ...]],
|
||||
rule: Optional[Union[Rule, T_RuleChecker]] = None,
|
||||
ignorecase: bool = False,
|
||||
_depth: int = 0,
|
||||
**kwargs,
|
||||
) -> Type[Matcher]:
|
||||
) -> type[Matcher]:
|
||||
"""注册一个消息事件响应器,并且当消息的**文本部分**以指定内容开头时响应。
|
||||
|
||||
参数:
|
||||
@ -236,12 +236,12 @@ def on_startswith(
|
||||
|
||||
|
||||
def on_endswith(
|
||||
msg: Union[str, Tuple[str, ...]],
|
||||
msg: Union[str, tuple[str, ...]],
|
||||
rule: Optional[Union[Rule, T_RuleChecker]] = None,
|
||||
ignorecase: bool = False,
|
||||
_depth: int = 0,
|
||||
**kwargs,
|
||||
) -> Type[Matcher]:
|
||||
) -> type[Matcher]:
|
||||
"""注册一个消息事件响应器,并且当消息的**文本部分**以指定内容结尾时响应。
|
||||
|
||||
参数:
|
||||
@ -260,12 +260,12 @@ def on_endswith(
|
||||
|
||||
|
||||
def on_fullmatch(
|
||||
msg: Union[str, Tuple[str, ...]],
|
||||
msg: Union[str, tuple[str, ...]],
|
||||
rule: Optional[Union[Rule, T_RuleChecker]] = None,
|
||||
ignorecase: bool = False,
|
||||
_depth: int = 0,
|
||||
**kwargs,
|
||||
) -> Type[Matcher]:
|
||||
) -> type[Matcher]:
|
||||
"""注册一个消息事件响应器,并且当消息的**文本部分**与指定内容完全一致时响应。
|
||||
|
||||
参数:
|
||||
@ -284,11 +284,11 @@ def on_fullmatch(
|
||||
|
||||
|
||||
def on_keyword(
|
||||
keywords: Set[str],
|
||||
keywords: set[str],
|
||||
rule: Optional[Union[Rule, T_RuleChecker]] = None,
|
||||
_depth: int = 0,
|
||||
**kwargs,
|
||||
) -> Type[Matcher]:
|
||||
) -> type[Matcher]:
|
||||
"""注册一个消息事件响应器,并且当消息纯文本部分包含关键词时响应。
|
||||
|
||||
参数:
|
||||
@ -306,13 +306,13 @@ def on_keyword(
|
||||
|
||||
|
||||
def on_command(
|
||||
cmd: Union[str, Tuple[str, ...]],
|
||||
cmd: Union[str, tuple[str, ...]],
|
||||
rule: Optional[Union[Rule, T_RuleChecker]] = None,
|
||||
aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = None,
|
||||
aliases: Optional[set[Union[str, tuple[str, ...]]]] = None,
|
||||
force_whitespace: Optional[Union[str, bool]] = None,
|
||||
_depth: int = 0,
|
||||
**kwargs,
|
||||
) -> Type[Matcher]:
|
||||
) -> type[Matcher]:
|
||||
"""注册一个消息事件响应器,并且当消息以指定命令开头时响应。
|
||||
|
||||
命令匹配规则参考: `命令形式匹配 <rule.md#command-command>`_
|
||||
@ -341,13 +341,13 @@ def on_command(
|
||||
|
||||
|
||||
def on_shell_command(
|
||||
cmd: Union[str, Tuple[str, ...]],
|
||||
cmd: Union[str, tuple[str, ...]],
|
||||
rule: Optional[Union[Rule, T_RuleChecker]] = None,
|
||||
aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = None,
|
||||
aliases: Optional[set[Union[str, tuple[str, ...]]]] = None,
|
||||
parser: Optional[ArgumentParser] = None,
|
||||
_depth: int = 0,
|
||||
**kwargs,
|
||||
) -> Type[Matcher]:
|
||||
) -> type[Matcher]:
|
||||
"""注册一个支持 `shell_like` 解析参数的命令消息事件响应器。
|
||||
|
||||
与普通的 `on_command` 不同的是,在添加 `parser` 参数时, 响应器会自动处理消息。
|
||||
@ -383,7 +383,7 @@ def on_regex(
|
||||
rule: Optional[Union[Rule, T_RuleChecker]] = None,
|
||||
_depth: int = 0,
|
||||
**kwargs,
|
||||
) -> Type[Matcher]:
|
||||
) -> type[Matcher]:
|
||||
"""注册一个消息事件响应器,并且当消息匹配正则表达式时响应。
|
||||
|
||||
命令匹配规则参考: `正则匹配 <rule.md#regex-regex-flags-0>`_
|
||||
@ -404,12 +404,12 @@ def on_regex(
|
||||
|
||||
|
||||
def on_type(
|
||||
types: Union[Type[Event], Tuple[Type[Event], ...]],
|
||||
types: Union[type[Event], tuple[type[Event], ...]],
|
||||
rule: Optional[Union[Rule, T_RuleChecker]] = None,
|
||||
*,
|
||||
_depth: int = 0,
|
||||
**kwargs,
|
||||
) -> Type[Matcher]:
|
||||
) -> type[Matcher]:
|
||||
"""注册一个事件响应器,并且当事件为指定类型时响应。
|
||||
|
||||
参数:
|
||||
@ -430,14 +430,14 @@ def on_type(
|
||||
class _Group:
|
||||
def __init__(self, **kwargs):
|
||||
"""创建一个事件响应器组合,参数为默认值,与 `on` 一致"""
|
||||
self.matchers: List[Type[Matcher]] = []
|
||||
self.matchers: list[type[Matcher]] = []
|
||||
"""组内事件响应器列表"""
|
||||
self.base_kwargs: Dict[str, Any] = kwargs
|
||||
self.base_kwargs: dict[str, Any] = kwargs
|
||||
"""其他传递给 `on` 的参数默认值"""
|
||||
|
||||
def _get_final_kwargs(
|
||||
self, update: Dict[str, Any], *, exclude: Optional[Set[str]] = None
|
||||
) -> Dict[str, Any]:
|
||||
self, update: dict[str, Any], *, exclude: Optional[set[str]] = None
|
||||
) -> dict[str, Any]:
|
||||
"""获取最终传递给 `on` 的参数
|
||||
|
||||
参数:
|
||||
@ -470,18 +470,18 @@ class CommandGroup(_Group):
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self, cmd: Union[str, Tuple[str, ...]], prefix_aliases: bool = False, **kwargs
|
||||
self, cmd: Union[str, tuple[str, ...]], prefix_aliases: bool = False, **kwargs
|
||||
):
|
||||
"""命令前缀"""
|
||||
super().__init__(**kwargs)
|
||||
self.basecmd: Tuple[str, ...] = (cmd,) if isinstance(cmd, str) else cmd
|
||||
self.basecmd: tuple[str, ...] = (cmd,) if isinstance(cmd, str) else cmd
|
||||
self.base_kwargs.pop("aliases", None)
|
||||
self.prefix_aliases = prefix_aliases
|
||||
|
||||
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]:
|
||||
def command(self, cmd: Union[str, tuple[str, ...]], **kwargs) -> type[Matcher]:
|
||||
"""注册一个新的命令。新参数将会覆盖命令组默认值
|
||||
|
||||
参数:
|
||||
@ -509,8 +509,8 @@ class CommandGroup(_Group):
|
||||
return matcher
|
||||
|
||||
def shell_command(
|
||||
self, cmd: Union[str, Tuple[str, ...]], **kwargs
|
||||
) -> Type[Matcher]:
|
||||
self, cmd: Union[str, tuple[str, ...]], **kwargs
|
||||
) -> type[Matcher]:
|
||||
"""注册一个新的 `shell_like` 命令。新参数将会覆盖命令组默认值
|
||||
|
||||
参数:
|
||||
@ -544,7 +544,7 @@ class MatcherGroup(_Group):
|
||||
def __repr__(self) -> str:
|
||||
return f"MatcherGroup(matchers={len(self.matchers)})"
|
||||
|
||||
def on(self, **kwargs) -> Type[Matcher]:
|
||||
def on(self, **kwargs) -> type[Matcher]:
|
||||
"""注册一个基础事件响应器,可自定义类型。
|
||||
|
||||
参数:
|
||||
@ -562,7 +562,7 @@ class MatcherGroup(_Group):
|
||||
self.matchers.append(matcher)
|
||||
return matcher
|
||||
|
||||
def on_metaevent(self, **kwargs) -> Type[Matcher]:
|
||||
def on_metaevent(self, **kwargs) -> type[Matcher]:
|
||||
"""注册一个元事件响应器。
|
||||
|
||||
参数:
|
||||
@ -580,7 +580,7 @@ class MatcherGroup(_Group):
|
||||
self.matchers.append(matcher)
|
||||
return matcher
|
||||
|
||||
def on_message(self, **kwargs) -> Type[Matcher]:
|
||||
def on_message(self, **kwargs) -> type[Matcher]:
|
||||
"""注册一个消息事件响应器。
|
||||
|
||||
参数:
|
||||
@ -598,7 +598,7 @@ class MatcherGroup(_Group):
|
||||
self.matchers.append(matcher)
|
||||
return matcher
|
||||
|
||||
def on_notice(self, **kwargs) -> Type[Matcher]:
|
||||
def on_notice(self, **kwargs) -> type[Matcher]:
|
||||
"""注册一个通知事件响应器。
|
||||
|
||||
参数:
|
||||
@ -616,7 +616,7 @@ class MatcherGroup(_Group):
|
||||
self.matchers.append(matcher)
|
||||
return matcher
|
||||
|
||||
def on_request(self, **kwargs) -> Type[Matcher]:
|
||||
def on_request(self, **kwargs) -> type[Matcher]:
|
||||
"""注册一个请求事件响应器。
|
||||
|
||||
参数:
|
||||
@ -635,8 +635,8 @@ class MatcherGroup(_Group):
|
||||
return matcher
|
||||
|
||||
def on_startswith(
|
||||
self, msg: Union[str, Tuple[str, ...]], **kwargs
|
||||
) -> Type[Matcher]:
|
||||
self, msg: Union[str, tuple[str, ...]], **kwargs
|
||||
) -> type[Matcher]:
|
||||
"""注册一个消息事件响应器,并且当消息的**文本部分**以指定内容开头时响应。
|
||||
|
||||
参数:
|
||||
@ -656,7 +656,7 @@ class MatcherGroup(_Group):
|
||||
self.matchers.append(matcher)
|
||||
return matcher
|
||||
|
||||
def on_endswith(self, msg: Union[str, Tuple[str, ...]], **kwargs) -> Type[Matcher]:
|
||||
def on_endswith(self, msg: Union[str, tuple[str, ...]], **kwargs) -> type[Matcher]:
|
||||
"""注册一个消息事件响应器,并且当消息的**文本部分**以指定内容结尾时响应。
|
||||
|
||||
参数:
|
||||
@ -676,7 +676,7 @@ class MatcherGroup(_Group):
|
||||
self.matchers.append(matcher)
|
||||
return matcher
|
||||
|
||||
def on_fullmatch(self, msg: Union[str, Tuple[str, ...]], **kwargs) -> Type[Matcher]:
|
||||
def on_fullmatch(self, msg: Union[str, tuple[str, ...]], **kwargs) -> type[Matcher]:
|
||||
"""注册一个消息事件响应器,并且当消息的**文本部分**与指定内容完全一致时响应。
|
||||
|
||||
参数:
|
||||
@ -696,7 +696,7 @@ class MatcherGroup(_Group):
|
||||
self.matchers.append(matcher)
|
||||
return matcher
|
||||
|
||||
def on_keyword(self, keywords: Set[str], **kwargs) -> Type[Matcher]:
|
||||
def on_keyword(self, keywords: set[str], **kwargs) -> type[Matcher]:
|
||||
"""注册一个消息事件响应器,并且当消息纯文本部分包含关键词时响应。
|
||||
|
||||
参数:
|
||||
@ -717,11 +717,11 @@ class MatcherGroup(_Group):
|
||||
|
||||
def on_command(
|
||||
self,
|
||||
cmd: Union[str, Tuple[str, ...]],
|
||||
aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = None,
|
||||
cmd: Union[str, tuple[str, ...]],
|
||||
aliases: Optional[set[Union[str, tuple[str, ...]]]] = None,
|
||||
force_whitespace: Optional[Union[str, bool]] = None,
|
||||
**kwargs,
|
||||
) -> Type[Matcher]:
|
||||
) -> type[Matcher]:
|
||||
"""注册一个消息事件响应器,并且当消息以指定命令开头时响应。
|
||||
|
||||
命令匹配规则参考: `命令形式匹配 <rule.md#command-command>`_
|
||||
@ -748,11 +748,11 @@ class MatcherGroup(_Group):
|
||||
|
||||
def on_shell_command(
|
||||
self,
|
||||
cmd: Union[str, Tuple[str, ...]],
|
||||
aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = None,
|
||||
cmd: Union[str, tuple[str, ...]],
|
||||
aliases: Optional[set[Union[str, tuple[str, ...]]]] = None,
|
||||
parser: Optional[ArgumentParser] = None,
|
||||
**kwargs,
|
||||
) -> Type[Matcher]:
|
||||
) -> type[Matcher]:
|
||||
"""注册一个支持 `shell_like` 解析参数的命令消息事件响应器。
|
||||
|
||||
与普通的 `on_command` 不同的是,在添加 `parser` 参数时, 响应器会自动处理消息。
|
||||
@ -780,7 +780,7 @@ class MatcherGroup(_Group):
|
||||
|
||||
def on_regex(
|
||||
self, pattern: str, flags: Union[int, re.RegexFlag] = 0, **kwargs
|
||||
) -> Type[Matcher]:
|
||||
) -> type[Matcher]:
|
||||
"""注册一个消息事件响应器,并且当消息匹配正则表达式时响应。
|
||||
|
||||
命令匹配规则参考: `正则匹配 <rule.md#regex-regex-flags-0>`_
|
||||
@ -803,8 +803,8 @@ class MatcherGroup(_Group):
|
||||
return matcher
|
||||
|
||||
def on_type(
|
||||
self, types: Union[Type[Event], Tuple[Type[Event]]], **kwargs
|
||||
) -> Type[Matcher]:
|
||||
self, types: Union[type[Event], tuple[type[Event]]], **kwargs
|
||||
) -> type[Matcher]:
|
||||
"""注册一个事件响应器,并且当事件为指定类型时响应。
|
||||
|
||||
参数:
|
||||
|
Reference in New Issue
Block a user