Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions astrbot/core/star/filter/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,10 @@ def filter(self, event: AstrMessageEvent, cfg: AstrBotConfig) -> bool:
if not ok:
return False

# 分割为列表
ls = message_str.split(" ")
# 分割为列表,同时保护 @昵称(ID) 格式不被空格拆坏
# 例如 "@Heaven Whisper(488267082) 64" 应拆为 ["@Heaven Whisper(488267082)", "64"]
# 而非 ["@Heaven", "Whisper(488267082)", "64"]
ls = re.findall(r"@[^@]+?\(\d+\)|[^\s]+", message_str)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

这里的正则表达式 r"@[^@]+?\(\d+\)|[^\s]+" 使用了 \d+ 来匹配括号中的用户 ID。虽然这对于 QQ 等使用纯数字 QQ 号作为 ID 的平台非常有效,但 AstrBot 作为一个多平台机器人框架,可能会接入其他平台的适配器(例如微信 wxid、Slack、Discord 或使用 UUID 的平台),这些平台的非数字用户 ID(可能包含字母、下划线、连字符等)将无法被该正则匹配,从而导致在这些平台上包含空格的昵称仍然会被拆分。

为了提高多平台兼容性,建议将 \d+ 替换为 [\w\-]+。这样既能支持包含字母、下划线、连字符等非数字字符的 ID,又因为不允许空格而避免了误匹配普通的带空格括号文本。

Suggested change
ls = re.findall(r"@[^@]+?\(\d+\)|[^\s]+", message_str)
ls = re.findall(r"@[^@]+?\([\w\-]+\)|[^\s]+", message_str)

# 去除空字符串
ls = [param for param in ls if param]
params = {}
Expand Down