mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-27 18:45:05 +08:00
Add CommandGroup class
This commit is contained in:
parent
e52aa34f85
commit
fd49a272ae
@ -106,6 +106,24 @@ def on_command(name: Union[str, Tuple[str]], *,
|
||||
return deco
|
||||
|
||||
|
||||
class CommandGroup:
|
||||
__slots__ = ('basename', 'permission')
|
||||
|
||||
def __init__(self, name: Union[str, Tuple[str]], permission: int = None):
|
||||
self.basename = name if isinstance(name, tuple) else (name,)
|
||||
self.permission = permission
|
||||
|
||||
def command(self, name: Union[str, Tuple[str]], *,
|
||||
aliases: Iterable = None, permission: int = None) -> Callable:
|
||||
name = self.basename + (name if isinstance(name, tuple) else (name,))
|
||||
kwargs = {}
|
||||
if aliases is not None:
|
||||
kwargs['aliases'] = aliases
|
||||
if permission is not None:
|
||||
kwargs['permission'] = permission
|
||||
return on_command(name, **kwargs)
|
||||
|
||||
|
||||
def _find_command(name: Union[str, Tuple[str]]) -> Optional[Command]:
|
||||
cmd_name = name if isinstance(name, tuple) else (name,)
|
||||
|
||||
|
@ -1,9 +1,11 @@
|
||||
import none
|
||||
from none.command import Session
|
||||
from none.command import Session, CommandGroup
|
||||
from none.expressions import weather as expr
|
||||
|
||||
w = CommandGroup('weather')
|
||||
|
||||
@none.on_command(('weather', 'weather'), aliases=('天气', '天气预报'))
|
||||
|
||||
@w.command('weather', aliases=('天气', '天气预报'))
|
||||
async def weather(session: Session):
|
||||
city = session.require_arg('city', prompt_expr=expr.WHICH_CITY)
|
||||
await session.send_expr(expr.REPORT, city=city)
|
||||
@ -15,7 +17,6 @@ async def _(session: Session):
|
||||
session.args[session.current_key] = session.current_arg.strip()
|
||||
|
||||
|
||||
@none.on_command(('weather', 'suggestion'),
|
||||
aliases=('生活指数', '生活建议', '生活提示'))
|
||||
@w.command('suggestion', aliases=('生活指数', '生活建议', '生活提示'))
|
||||
async def suggestion(session: Session):
|
||||
await session.send('suggestion')
|
||||
|
Loading…
Reference in New Issue
Block a user