Skip to content

Commit b80701d

Browse files
committed
Merge commit 'f629ad68f5b2083b77051d074650b826d905fa4d' into feat/sdk-integration
优化指令组的装饰器,方便插件作者使用
2 parents 87579db + f629ad6 commit b80701d

1 file changed

Lines changed: 40 additions & 2 deletions

File tree

astrbot-sdk/src/astrbot_sdk/decorators.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,8 @@ def on_command(
454454
*,
455455
aliases: list[str] | None = None,
456456
description: str | None = None,
457+
group: str | typing.Sequence[str] | None = None,
458+
group_help: str | None = None,
457459
) -> Callable[[HandlerCallable], HandlerCallable]:
458460
"""注册命令处理方法。
459461
@@ -464,6 +466,9 @@ def on_command(
464466
command: 命令名称(不包含前缀符)
465467
aliases: 命令别名列表
466468
description: 命令描述,用于帮助信息
469+
group: 指令组路径。传入 "admin" 表示一级组;传入 ["admin", "user"] 表示多级组
470+
设置后实际命令为 ``"admin command"`` 或 ``"admin user command"``
471+
group_help: 指令组描述,用于帮助信息
467472
468473
Returns:
469474
装饰器函数
@@ -472,6 +477,10 @@ def on_command(
472477
@on_command("echo", aliases=["repeat"], description="重复消息")
473478
async def echo(self, event: MessageEvent, ctx: Context):
474479
await event.reply(event.text)
480+
481+
@on_command("ban", group="admin", description="封禁用户")
482+
async def admin_ban(self, event: MessageEvent, ctx: Context):
483+
await event.reply("已封禁")
475484
"""
476485

477486
commands = (
@@ -482,22 +491,45 @@ async def echo(self, event: MessageEvent, ctx: Context):
482491
commands = [item for item in commands if item]
483492
if not commands:
484493
raise ValueError("on_command requires at least one non-empty command name")
494+
495+
group_path: list[str] = []
496+
if group is not None:
497+
group_path = (
498+
[str(group).strip()]
499+
if isinstance(group, str)
500+
else [str(item).strip() for item in group]
501+
)
502+
group_path = [item for item in group_path if item]
503+
485504
canonical = commands[0]
505+
display_command = " ".join([*group_path, canonical]) if group_path else canonical
486506
merged_aliases: list[str] = [
487507
item
488508
for item in dict.fromkeys([*commands[1:], *(aliases or [])])
489509
if isinstance(item, str) and item and item != canonical
490510
]
511+
expanded_aliases: list[str] = (
512+
[" ".join([*group_path, alias]) for alias in merged_aliases]
513+
if group_path
514+
else merged_aliases
515+
)
491516

492517
def decorator(func: HandlerCallable) -> HandlerCallable:
493518
meta = _get_or_create_meta(func)
494519
normalized_description = _normalize_description(description)
520+
trigger_command = display_command if group_path else canonical
495521
meta.trigger = CommandTrigger(
496-
command=canonical,
497-
aliases=merged_aliases,
522+
command=trigger_command,
523+
aliases=expanded_aliases if group_path else merged_aliases,
498524
description=normalized_description,
499525
)
500526
meta.description = normalized_description
527+
if group_path:
528+
meta.command_route = CommandRouteSpec(
529+
group_path=group_path,
530+
display_command=display_command,
531+
group_help=_normalize_description(group_help),
532+
)
501533
_validate_message_trigger_compatibility(meta)
502534
return func
503535

@@ -1003,6 +1035,8 @@ def conversation_command(
10031035
*,
10041036
aliases: list[str] | None = None,
10051037
description: str | None = None,
1038+
group: str | typing.Sequence[str] | None = None,
1039+
group_help: str | None = None,
10061040
timeout: int = 60,
10071041
mode: ConversationMode = "replace",
10081042
busy_message: str | None = None,
@@ -1016,6 +1050,8 @@ def conversation_command(
10161050
command: 命令名称或序列(首项为正式名,其余视为别名)
10171051
aliases: 额外别名列表
10181052
description: 命令描述
1053+
group: 指令组路径,例如 ``"admin"`` 或 ``["admin", "user"]``
1054+
group_help: 指令组描述,用于帮助信息
10191055
timeout: 会话超时时间(秒),必须为正整数
10201056
mode: 会话冲突时的行为:
10211057
- ``"replace"``: 替换当前会话
@@ -1046,6 +1082,8 @@ async def chat(self, event: MessageEvent, ctx: Context):
10461082
command,
10471083
aliases=aliases,
10481084
description=description,
1085+
group=group,
1086+
group_help=group_help,
10491087
)
10501088

10511089
def decorator(func: HandlerCallable) -> HandlerCallable:

0 commit comments

Comments
 (0)