mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-09-06 12:06:45 +00:00
✨ Feature: CommandGroup 支持命令别名添加前缀选项 (#2134)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@ -429,6 +429,7 @@ class CommandGroup(_Group):
|
||||
|
||||
参数:
|
||||
cmd: 指定命令内容
|
||||
prefix_aliases: 是否影响命令别名,给命令别名加前缀
|
||||
rule: 事件响应规则
|
||||
permission: 事件响应权限
|
||||
handlers: 事件处理函数列表
|
||||
@ -439,11 +440,14 @@ class CommandGroup(_Group):
|
||||
state: 默认 state
|
||||
"""
|
||||
|
||||
def __init__(self, cmd: Union[str, Tuple[str, ...]], **kwargs):
|
||||
def __init__(
|
||||
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.base_kwargs.pop("aliases", None)
|
||||
self.prefix_aliases = prefix_aliases
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"CommandGroup(cmd={self.basecmd}, matchers={len(self.matchers)})"
|
||||
@ -466,6 +470,11 @@ class CommandGroup(_Group):
|
||||
"""
|
||||
sub_cmd = (cmd,) if isinstance(cmd, str) else cmd
|
||||
cmd = self.basecmd + sub_cmd
|
||||
if self.prefix_aliases and (aliases := kwargs.get("aliases")):
|
||||
kwargs["aliases"] = {
|
||||
self.basecmd + ((alias,) if isinstance(alias, str) else alias)
|
||||
for alias in aliases
|
||||
}
|
||||
matcher = on_command(cmd, **self._get_final_kwargs(kwargs))
|
||||
self.matchers.append(matcher)
|
||||
return matcher
|
||||
@ -490,6 +499,11 @@ class CommandGroup(_Group):
|
||||
"""
|
||||
sub_cmd = (cmd,) if isinstance(cmd, str) else cmd
|
||||
cmd = self.basecmd + sub_cmd
|
||||
if self.prefix_aliases and (aliases := kwargs.get("aliases")):
|
||||
kwargs["aliases"] = {
|
||||
self.basecmd + ((alias,) if isinstance(alias, str) else alias)
|
||||
for alias in aliases
|
||||
}
|
||||
matcher = on_shell_command(cmd, **self._get_final_kwargs(kwargs))
|
||||
self.matchers.append(matcher)
|
||||
return matcher
|
||||
|
@ -179,6 +179,7 @@ class CommandGroup:
|
||||
def __init__(
|
||||
self,
|
||||
cmd: str | tuple[str, ...],
|
||||
prefix_aliases: bool = ...,
|
||||
*,
|
||||
rule: Rule | T_RuleChecker | None = ...,
|
||||
permission: Permission | T_PermissionChecker | None = ...,
|
||||
|
Reference in New Issue
Block a user