Skip to content

Commit 478cc32

Browse files
authored
Feat/telegram command alias register #5233 (#5234)
* feat: support registering command aliases for Telegram Now when registering commands with aliases, all aliases will be registered as Telegram bot commands in addition to the main command. Example: @register_command(command_name="draw", alias={"画", "gen"}) Now /draw, /画, and /gen will all appear in the Telegram command menu. * feat(telegram): add duplicate command name warning when registering commands Log a warning when duplicate command names are detected during Telegram command registration to help identify configuration conflicts.
1 parent 7b30244 commit 478cc32

1 file changed

Lines changed: 31 additions & 20 deletions

File tree

astrbot/core/platform/sources/telegram/tg_adapter.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,19 @@ def collect_commands(self) -> list[BotCommand]:
174174
if not handler_metadata.enabled:
175175
continue
176176
for event_filter in handler_metadata.event_filters:
177-
cmd_info = self._extract_command_info(
177+
cmd_info_list = self._extract_command_info(
178178
event_filter,
179179
handler_metadata,
180180
skip_commands,
181181
)
182-
if cmd_info:
183-
cmd_name, description = cmd_info
184-
command_dict.setdefault(cmd_name, description)
182+
if cmd_info_list:
183+
for cmd_name, description in cmd_info_list:
184+
if cmd_name in command_dict:
185+
logger.warning(
186+
f"命令名 '{cmd_name}' 重复注册,将使用首次注册的定义: "
187+
f"'{command_dict[cmd_name]}'"
188+
)
189+
command_dict.setdefault(cmd_name, description)
185190

186191
commands_a = sorted(command_dict.keys())
187192
return [BotCommand(cmd, command_dict[cmd]) for cmd in commands_a]
@@ -191,36 +196,42 @@ def _extract_command_info(
191196
event_filter,
192197
handler_metadata,
193198
skip_commands: set,
194-
) -> tuple[str, str] | None:
195-
"""从事件过滤器中提取指令信息"""
196-
cmd_name = None
199+
) -> list[tuple[str, str]] | None:
200+
"""从事件过滤器中提取指令信息,包括所有别名"""
201+
cmd_names = []
197202
is_group = False
198203
if isinstance(event_filter, CommandFilter) and event_filter.command_name:
199204
if (
200205
event_filter.parent_command_names
201206
and event_filter.parent_command_names != [""]
202207
):
203208
return None
204-
cmd_name = event_filter.command_name
209+
# 收集主命令名和所有别名
210+
cmd_names = [event_filter.command_name]
211+
if event_filter.alias:
212+
cmd_names.extend(event_filter.alias)
205213
elif isinstance(event_filter, CommandGroupFilter):
206214
if event_filter.parent_group:
207215
return None
208-
cmd_name = event_filter.group_name
216+
cmd_names = [event_filter.group_name]
209217
is_group = True
210218

211-
if not cmd_name or cmd_name in skip_commands:
212-
return None
219+
result = []
220+
for cmd_name in cmd_names:
221+
if not cmd_name or cmd_name in skip_commands:
222+
continue
223+
if not re.match(r"^[a-z0-9_]+$", cmd_name) or len(cmd_name) > 32:
224+
continue
213225

214-
if not re.match(r"^[a-z0-9_]+$", cmd_name) or len(cmd_name) > 32:
215-
return None
226+
# Build description.
227+
description = handler_metadata.desc or (
228+
f"Command group: {cmd_name}" if is_group else f"Command: {cmd_name}"
229+
)
230+
if len(description) > 30:
231+
description = description[:30] + "..."
232+
result.append((cmd_name, description))
216233

217-
# Build description.
218-
description = handler_metadata.desc or (
219-
f"指令组: {cmd_name} (包含多个子指令)" if is_group else f"指令: {cmd_name}"
220-
)
221-
if len(description) > 30:
222-
description = description[:30] + "..."
223-
return cmd_name, description
234+
return result if result else None
224235

225236
async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
226237
if not update.effective_chat:

0 commit comments

Comments
 (0)