Repository navigation
Discord 适配器增强功能 - #9124
Discord 适配器增强功能#9124NLKASHEI wants to merge 2 commits into
Conversation
# Discord 支持 Unicode 命令名(如中文),放宽匹配并确保全小写 Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- The updated regex
r"^[-_'\w]{1,32}$"now relies on\w, which in Python can match Unicode word characters; if Discord only accepts ASCII for slash command names, consider reverting to an explicit ASCII set like^[a-z0-9_\-']{1,32}$to avoid accidentally allowing unsupported characters.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The updated regex `r"^[-_'\w]{1,32}$"` now relies on `\w`, which in Python can match Unicode word characters; if Discord only accepts ASCII for slash command names, consider reverting to an explicit ASCII set like `^[a-z0-9_\-']{1,32}$` to avoid accidentally allowing unsupported characters.
## Individual Comments
### Comment 1
<location path="astrbot/core/platform/sources/discord/discord_platform_adapter.py" line_range="568" />
<code_context>
# Discord 斜杠指令名称规范
- if not re.match(r"^[a-z0-9_-]{1,32}$", cmd_name):
+ if cmd_name != cmd_name.lower() or not re.match(r"^[-_'\\w]{1,32}$", cmd_name):
logger.debug(f"[Discord] Skipping invalid slash command format: {cmd_name}")
return None
</code_context>
<issue_to_address>
**issue (bug_risk):** The regex character class appears to be incorrect and will reject digits and not treat `\w` as intended.
In a raw string, `r"\\w"` becomes the regex `\w` (a literal backslash + `w`), not the `\w` character class. So `[-_'\\w]` matches only `-`, `_`, `'`, `\` and `w`, and will reject digits and most letters. If you want word characters plus `-`/`'`, use `r"^[-_'\w]{1,32}$"` (single backslash in the raw string). If you prefer to keep an explicit range and you already enforce lowercase, `r"^[a-z0-9_'-]{1,32}$"` is also an option.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
|
||
| # Discord 斜杠指令名称规范 | ||
| if not re.match(r"^[a-z0-9_-]{1,32}$", cmd_name): | ||
| if cmd_name != cmd_name.lower() or not re.match(r"^[-_'\\w]{1,32}$", cmd_name): |
There was a problem hiding this comment.
issue (bug_risk): The regex character class appears to be incorrect and will reject digits and not treat \w as intended.
In a raw string, r"\\w" becomes the regex \w (a literal backslash + w), not the \w character class. So [-_'\\w] matches only -, _, ', \ and w, and will reject digits and most letters. If you want word characters plus -/', use r"^[-_'\w]{1,32}$" (single backslash in the raw string). If you prefer to keep an explicit range and you already enforce lowercase, r"^[a-z0-9_'-]{1,32}$" is also an option.
There was a problem hiding this comment.
Code Review
This pull request updates the Discord slash command name validation logic to ensure the command name is lowercase and matches a broader regex pattern. The reviewer identified a critical issue in the regular expression where using a double backslash \\w inside a raw string matches a literal backslash or the letter 'w' instead of the word character class \w, and provided a code suggestion to fix it.
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.
|
|
||
| # Discord 斜杠指令名称规范 | ||
| if not re.match(r"^[a-z0-9_-]{1,32}$", cmd_name): | ||
| if cmd_name != cmd_name.lower() or not re.match(r"^[-_'\\w]{1,32}$", cmd_name): |
There was a problem hiding this comment.
在 Python 的 re 模块中,使用原始字符串(raw string r"...")时,字符集 [] 内部的 \\w 会被解析为匹配字面量反斜杠 \\ 或字母 w,而不是预期的单词字符集 \w。
这会导致几乎所有不只包含 -、_、'、\\ 和 w 的斜杠指令(例如 help、start 等)都无法匹配该正则,从而被错误地跳过。
建议将 \\w 改为单反斜杠 \w。
| if cmd_name != cmd_name.lower() or not re.match(r"^[-_'\\w]{1,32}$", cmd_name): | |
| if cmd_name != cmd_name.lower() or not re.match(r"^[-_'\w]{1,32}$", cmd_name): |
解决的问题
主要修改discord_platform_adapter.py
尚未解决的问题
个人插件无法 import discord,如果想要斜杠指令实现attachment参数,需要 AstrBot 提供通用 AttachmentParam 类型或约定参数名。
Summary by Sourcery
Enhancements: