Skip to content

Commit f629ad6

Browse files
committed
Squashed 'astrbot-sdk/' changes from 67b101992..956f52dd8
956f52dd8 chore: refresh vendor snapshot [skip ci] a9a953aef Merge pull request #93 from united-pooh:dev 8fe640dac fix: 优化 on_command 中别名展开逻辑,提升代码可读性;更新测试文档以反映最新变更 47ef5c1a4 feat: 添加插件写入功能,支持 group 和多级 group 路径的命令分发测试 e0d9f409d feat: 添加 on_command 和 conversation_command 的 group 与 group_help 参数支持 85c8b4028 docs: 精简架构文档,移除能力清单和API速查,聚焦架构描述 7139af7a7 Merge branch 'dev' of https://github.com/united-pooh/astrbot-sdk into dev 8485d68ec docs: 更新项目架构文档,添加最后更新日期,修正协议优先特性描述,调整客户端架构和能力注册流程 994e8b77a Merge pull request #92 from united-pooh/main 1aa39faac Update README.md 28cdf0e23 Update README.md 61fd6fce7 Update README.md be1d306f5 Merge pull request #90 from united-pooh/fix/dev-skill-plugin-version-constraint a810b2235 Refine plugin dev dependency version guidance REVERT: 67b101992 chore: refresh vendor snapshot [skip ci] git-subtree-dir: astrbot-sdk git-subtree-split: 956f52dd8a236519e12b96207327002f581b3c8e
1 parent 2446b6c commit f629ad6

1 file changed

Lines changed: 40 additions & 2 deletions

File tree

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)