mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-01-19 01:18:19 +08:00
Use stub file to keep class CommandGroup
clean
This commit is contained in:
parent
ae35c2e08a
commit
347318aaaa
@ -114,12 +114,18 @@ from .helpers import context_id
|
||||
|
||||
__all__ = [
|
||||
'NoneBot', 'scheduler', 'init', 'get_bot', 'run',
|
||||
|
||||
'CQHttpError',
|
||||
|
||||
'load_plugin', 'load_plugins', 'load_builtin_plugins',
|
||||
'get_loaded_plugins',
|
||||
|
||||
'message_preprocessor', 'Message', 'MessageSegment',
|
||||
|
||||
'on_command', 'CommandSession', 'CommandGroup',
|
||||
|
||||
'on_natural_language', 'NLPSession', 'NLPResult', 'IntentCommand',
|
||||
'on_notice', 'NoticeSession', 'on_request', 'RequestSession',
|
||||
|
||||
'context_id',
|
||||
]
|
||||
|
@ -156,55 +156,6 @@ def on_command(name: Union[str, CommandName_T], *,
|
||||
return deco
|
||||
|
||||
|
||||
class CommandGroup:
|
||||
"""
|
||||
Group a set of commands with same name prefix.
|
||||
"""
|
||||
__slots__ = ('basename', 'permission', 'only_to_me', 'privileged',
|
||||
'shell_like')
|
||||
|
||||
def __init__(self, name: Union[str, CommandName_T],
|
||||
permission: Optional[int] = None, *,
|
||||
only_to_me: Optional[bool] = None,
|
||||
privileged: Optional[bool] = None,
|
||||
shell_like: Optional[bool] = None):
|
||||
self.basename = (name,) if isinstance(name, str) else name
|
||||
self.permission = permission # TODO: use .pyi
|
||||
self.only_to_me = only_to_me
|
||||
self.privileged = privileged
|
||||
self.shell_like = shell_like
|
||||
|
||||
def command(self, name: Union[str, CommandName_T], *,
|
||||
aliases: Optional[Iterable[str]] = None,
|
||||
permission: Optional[int] = None,
|
||||
only_to_me: Optional[bool] = None,
|
||||
privileged: Optional[bool] = None,
|
||||
shell_like: Optional[bool] = None) -> Callable:
|
||||
sub_name = (name,) if isinstance(name, str) else name
|
||||
name = self.basename + sub_name
|
||||
|
||||
kwargs = {}
|
||||
if aliases is not None:
|
||||
kwargs['aliases'] = aliases
|
||||
if permission is not None:
|
||||
kwargs['permission'] = permission
|
||||
elif self.permission is not None:
|
||||
kwargs['permission'] = self.permission
|
||||
if only_to_me is not None:
|
||||
kwargs['only_to_me'] = only_to_me
|
||||
elif self.only_to_me is not None:
|
||||
kwargs['only_to_me'] = self.only_to_me
|
||||
if privileged is not None:
|
||||
kwargs['privileged'] = privileged
|
||||
elif self.privileged is not None:
|
||||
kwargs['privileged'] = self.privileged
|
||||
if shell_like is not None:
|
||||
kwargs['shell_like'] = shell_like
|
||||
elif self.shell_like is not None:
|
||||
kwargs['shell_like'] = self.shell_like
|
||||
return on_command(name, **kwargs)
|
||||
|
||||
|
||||
def _find_command(name: Union[str, CommandName_T]) -> Optional[Command]:
|
||||
cmd_name = (name,) if isinstance(name, str) else name
|
||||
if not cmd_name:
|
||||
@ -686,3 +637,6 @@ def kill_current_session(ctx: Context_T) -> None:
|
||||
ctx_id = context_id(ctx)
|
||||
if ctx_id in _sessions:
|
||||
del _sessions[ctx_id]
|
||||
|
||||
|
||||
from nonebot.command.group import CommandGroup
|
||||
|
26
nonebot/command/group.py
Normal file
26
nonebot/command/group.py
Normal file
@ -0,0 +1,26 @@
|
||||
from typing import Union, Callable
|
||||
|
||||
from nonebot.command import on_command
|
||||
from nonebot.typing import CommandName_T
|
||||
|
||||
|
||||
class CommandGroup:
|
||||
"""
|
||||
Group a set of commands with same name prefix.
|
||||
"""
|
||||
|
||||
__slots__ = ('basename', 'base_kwargs')
|
||||
|
||||
def __init__(self, name: Union[str, CommandName_T], **kwargs):
|
||||
self.basename = (name,) if isinstance(name, str) else name
|
||||
if 'aliases' in kwargs:
|
||||
del kwargs['aliases'] # ensure there is no aliases here
|
||||
self.base_kwargs = kwargs
|
||||
|
||||
def command(self, name: Union[str, CommandName_T], **kwargs) -> Callable:
|
||||
sub_name = (name,) if isinstance(name, str) else name
|
||||
name = self.basename + sub_name
|
||||
|
||||
final_kwargs = self.base_kwargs.copy()
|
||||
final_kwargs.update(kwargs)
|
||||
return on_command(name, **final_kwargs)
|
24
nonebot/command/group.pyi
Normal file
24
nonebot/command/group.pyi
Normal file
@ -0,0 +1,24 @@
|
||||
from typing import Union, Callable, Iterable
|
||||
|
||||
from nonebot.typing import CommandName_T
|
||||
|
||||
|
||||
class CommandGroup:
|
||||
"""
|
||||
Group a set of commands with same name prefix.
|
||||
"""
|
||||
|
||||
__slots__ = ('basename', 'base_kwargs')
|
||||
|
||||
def __init__(self, name: Union[str, CommandName_T], *,
|
||||
permission: int = ...,
|
||||
only_to_me: bool = ...,
|
||||
privileged: bool = ...,
|
||||
shell_like: bool = ...): ...
|
||||
|
||||
def command(self, name: Union[str, CommandName_T], *,
|
||||
aliases: Iterable[str] = ...,
|
||||
permission: int = ...,
|
||||
only_to_me: bool = ...,
|
||||
privileged: bool = ...,
|
||||
shell_like: bool = ...) -> Callable: ...
|
Loading…
Reference in New Issue
Block a user