diff --git a/nonebot/plugin.py b/nonebot/plugin.py index d3db0631..736a34cf 100644 --- a/nonebot/plugin.py +++ b/nonebot/plugin.py @@ -242,17 +242,17 @@ def get_loaded_plugins() -> Set[Plugin]: class CommandGroup: - def __init__(self, name: Union[str, Tuple[str, ...]], **kwargs): - self.basename = (name,) if isinstance(name, str) else name + def __init__(self, cmd: Union[str, Tuple[str, ...]], **kwargs): + self.basecmd = (cmd,) if isinstance(cmd, str) else cmd if "aliases" in kwargs: del kwargs["aliases"] self.base_kwargs = kwargs - def command(self, name: Union[str, Tuple[str, ...]], + def command(self, cmd: Union[str, Tuple[str, ...]], **kwargs) -> Union[Type[Matcher], MatcherGroup]: - sub_name = (name,) if isinstance(name, str) else name - name = self.basename + sub_name + 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(name, **final_kwargs) + return on_command(cmd, **final_kwargs) diff --git a/nonebot/plugin.pyi b/nonebot/plugin.pyi new file mode 100644 index 00000000..cd737363 --- /dev/null +++ b/nonebot/plugin.pyi @@ -0,0 +1,165 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import re + +from nonebot.typing import Rule, Matcher, Handler, Permission, RuleChecker, MatcherGroup +from nonebot.typing import Set, List, Dict, Type, Tuple, Union, Optional, ModuleType + +plugins: Dict[str, "Plugin"] = ... + +_tmp_matchers: Set[Type[Matcher]] = ... + + +class Plugin(object): + name: str + module: ModuleType + matcher: Set[Type[Matcher]] + + +def on(rule: Optional[Union[Rule, RuleChecker]] = None, + permission: Optional[Permission] = None, + *, + handlers: Optional[List[Handler]] = None, + temp: bool = False, + priority: int = 1, + block: bool = False, + state: Optional[dict] = None) -> Type[Matcher]: + ... + + +def on_metaevent(rule: Optional[Union[Rule, RuleChecker]] = None, + *, + handlers: Optional[List[Handler]] = None, + temp: bool = False, + priority: int = 1, + block: bool = False, + state: Optional[dict] = None) -> Type[Matcher]: + ... + + +def on_message(rule: Optional[Union[Rule, RuleChecker]] = None, + permission: Optional[Permission] = None, + *, + handlers: Optional[List[Handler]] = None, + temp: bool = False, + priority: int = 1, + block: bool = True, + state: Optional[dict] = None) -> Type[Matcher]: + ... + + +def on_notice(rule: Optional[Union[Rule, RuleChecker]] = None, + *, + handlers: Optional[List[Handler]] = None, + temp: bool = False, + priority: int = 1, + block: bool = False, + state: Optional[dict] = None) -> Type[Matcher]: + ... + + +def on_request(rule: Optional[Union[Rule, RuleChecker]] = None, + *, + handlers: Optional[List[Handler]] = None, + temp: bool = False, + priority: int = 1, + block: bool = False, + state: Optional[dict] = None) -> Type[Matcher]: + ... + + +def on_startswith(msg: str, + rule: Optional[Optional[Union[Rule, RuleChecker]]] = None, + permission: Optional[Permission] = None, + *, + handlers: Optional[List[Handler]] = None, + temp: bool = False, + priority: int = 1, + block: bool = True, + state: Optional[dict] = None) -> Type[Matcher]: + ... + + +def on_endswith(msg: str, + rule: Optional[Optional[Union[Rule, RuleChecker]]] = None, + permission: Optional[Permission] = None, + *, + handlers: Optional[List[Handler]] = None, + temp: bool = False, + priority: int = 1, + block: bool = True, + state: Optional[dict] = None) -> Type[Matcher]: + ... + + +def on_command( + cmd: Union[str, Tuple[str, ...]], + rule: Optional[Union[Rule, RuleChecker]] = None, + aliases: Set[Union[str, Tuple[str, ...]]] = None, + permission: Optional[Permission] = None, + *, + handlers: Optional[List[Handler]] = None, + temp: bool = False, + priority: int = 1, + block: bool = True, + state: Optional[dict] = None) -> Union[Type[Matcher], MatcherGroup]: + ... + + +def on_regex(pattern: str, + flags: Union[int, re.RegexFlag] = 0, + rule: Optional[Rule] = None, + permission: Optional[Permission] = None, + *, + handlers: Optional[List[Handler]] = None, + temp: bool = False, + priority: int = 1, + block: bool = True, + state: Optional[dict] = None) -> Type[Matcher]: + ... + + +def load_plugin(module_path: str) -> Optional[Plugin]: + ... + + +def load_plugins(*plugin_dir: str) -> Set[Plugin]: + ... + + +def load_builtin_plugins(): + ... + + +def get_loaded_plugins() -> Set[Plugin]: + ... + + +class CommandGroup: + + def __init__(self, + cmd: Union[str, Tuple[str, ...]], + rule: Optional[Union[Rule, RuleChecker]] = None, + permission: Optional[Permission] = None, + *, + handlers: Optional[List[Handler]] = None, + temp: bool = False, + priority: int = 1, + block: bool = True, + state: Optional[dict] = None): + ... + + def command( + self, + cmd: Union[str, Tuple[str, ...]], + rule: Optional[Union[Rule, RuleChecker]] = None, + aliases: Set[Union[str, Tuple[str, ...]]] = None, + permission: Optional[Permission] = None, + *, + handlers: Optional[List[Handler]] = None, + temp: bool = False, + priority: int = 1, + block: bool = True, + state: Optional[dict] = None) -> Union[Type[Matcher], MatcherGroup]: + ... diff --git a/nonebot/typing.py b/nonebot/typing.py index 51834a08..5b722655 100644 --- a/nonebot/typing.py +++ b/nonebot/typing.py @@ -28,10 +28,10 @@ from typing import Union, TypeVar, Optional, Iterable, Callable, Awaitable # import some modules needed when checking types if TYPE_CHECKING: from nonebot.rule import Rule as RuleClass - from nonebot.matcher import Matcher as MatcherClass from nonebot.drivers import BaseDriver, BaseWebSocket from nonebot.permission import Permission as PermissionClass from nonebot.adapters import BaseBot, BaseEvent, BaseMessage, BaseMessageSegment + from nonebot.matcher import Matcher as MatcherClass, MatcherGroup as MatcherGroupClass def overrides(InterfaceClass: object): @@ -112,6 +112,14 @@ Matcher = TypeVar("Matcher", bound="MatcherClass") Matcher 即响应事件的处理类。通过 Rule 判断是否响应事件,运行 Handler。 """ +MatcherGroup = TypeVar("MatcherGroup", bound="MatcherGroupClass") +""" +:类型: ``MatcherGroup`` + +:说明: + + MatcherGroup 为 Matcher 的集合。可以共享 Handler。 +""" Rule = TypeVar("Rule", bound="RuleClass") """ :类型: ``Rule``