fix: 保护 @昵称(ID) 格式在命令参数解析时不被空格拆坏#9307
Conversation
当 @mention 的昵称包含空格时(如 @heaven Whisper(488267082)), 原有的 message_str.split(' ') 会将其拆为多个参数, 导致后续参数类型转换失败(如 value 参数收到 'Whisper(488267082)' 无法转 int)。 使用正则 re.findall(r'@[^@]+?\(\d+\)|[^\s]+', ...) 替代简单 split, 将 @昵称(ID) 作为整体 token 保留,不受昵称中空格影响。 影响范围:仅 CommandFilter.filter() 中参数分割逻辑, 不影响无 @mention 的普通命令解析。
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Consider extracting the mention-parsing regex into a named constant with a brief comment about its intent, so future changes to the mention format or additional patterns can be updated in one place.
- The regex currently assumes the ID is purely numeric (
\d+); if non-numeric IDs or different identifier formats are possible now or in the future, you may want to relax this part of the pattern to avoid silently mis-parsing those mentions. - If messages can contain multiple
@segments or nested parentheses, double-check that@[^@]+?\(\d+\)behaves as expected and does not accidentally consume too much text; a slightly more constrained character class before the(might make the matching more robust.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider extracting the mention-parsing regex into a named constant with a brief comment about its intent, so future changes to the mention format or additional patterns can be updated in one place.
- The regex currently assumes the ID is purely numeric (`\d+`); if non-numeric IDs or different identifier formats are possible now or in the future, you may want to relax this part of the pattern to avoid silently mis-parsing those mentions.
- If messages can contain multiple `@` segments or nested parentheses, double-check that `@[^@]+?\(\d+\)` behaves as expected and does not accidentally consume too much text; a slightly more constrained character class before the `(` might make the matching more robust.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Code Review
This pull request updates the command parameter splitting logic in astrbot/core/star/filter/command.py to prevent splitting @Nickname(ID) formats containing spaces. The reviewer suggested improving the regular expression to support non-numeric user IDs from other platforms (such as Slack or Discord) by replacing \d+ with [\w\-]+.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| # 分割为列表,同时保护 @昵称(ID) 格式不被空格拆坏 | ||
| # 例如 "@Heaven Whisper(488267082) 64" 应拆为 ["@Heaven Whisper(488267082)", "64"] | ||
| # 而非 ["@Heaven", "Whisper(488267082)", "64"] | ||
| ls = re.findall(r"@[^@]+?\(\d+\)|[^\s]+", message_str) |
There was a problem hiding this comment.
这里的正则表达式 r"@[^@]+?\(\d+\)|[^\s]+" 使用了 \d+ 来匹配括号中的用户 ID。虽然这对于 QQ 等使用纯数字 QQ 号作为 ID 的平台非常有效,但 AstrBot 作为一个多平台机器人框架,可能会接入其他平台的适配器(例如微信 wxid、Slack、Discord 或使用 UUID 的平台),这些平台的非数字用户 ID(可能包含字母、下划线、连字符等)将无法被该正则匹配,从而导致在这些平台上包含空格的昵称仍然会被拆分。
为了提高多平台兼容性,建议将 \d+ 替换为 [\w\-]+。这样既能支持包含字母、下划线、连字符等非数字字符的 ID,又因为不允许空格而避免了误匹配普通的带空格括号文本。
| ls = re.findall(r"@[^@]+?\(\d+\)|[^\s]+", message_str) | |
| ls = re.findall(r"@[^@]+?\([\w\-]+\)|[^\s]+", message_str) |
问题说明 / Motivation
当
@mention的昵称包含空格时(例如@Heaven Whisper(488267082)),原先的message_str.split(" ")会将其拆成多个参数,导致后续参数转换失败(例如value收到Whisper(488267082)而无法转换为int)。Modifications / 改动点
@昵称(ID)保留为单个命令参数,不受昵称中空格影响。CommandFilter回归测试,覆盖:影响范围仅为
CommandFilter.filter()的参数分割逻辑;无新依赖、无破坏性变更。Test Results / 验证结果
Checklist / 检查清单