Skip to content

修复了DISCORD适配器注册命令正则过于严格的问题#9102

Merged
zouyonghe merged 2 commits into
AstrBotDevs:masterfrom
NLKASHEI:master
Jul 2, 2026
Merged

修复了DISCORD适配器注册命令正则过于严格的问题#9102
zouyonghe merged 2 commits into
AstrBotDevs:masterfrom
NLKASHEI:master

Conversation

@NLKASHEI

@NLKASHEI NLKASHEI commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

针对:[Bug]Discord适配器中,注册命令匹配正则 [a-z0-9_-] 应当改成 [\w-] #9101
修复了discord斜杠的中文命令无法注册的问题
我就改了一行代码

改前

if not re.match(r"^[a-z0-9_-]{1,32}$", cmd_name):

改后

if not re.match(r"^[-_'\w]{1,32}$", cmd_name, re.UNICODE):

再无任何破坏性改动和其他改动

Summary by Sourcery

Bug Fixes:

  • Allow Discord slash commands with non-ASCII characters (e.g., Chinese) to be registered by broadening the command name validation regex.

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. labels Jul 1, 2026

@sourcery-ai sourcery-ai Bot left a comment

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.

Hey - I've left some high level feedback:

  • Consider adding a brief inline comment explaining why the regex intentionally diverges from Discord’s official lowercase a-z0-9_- naming rule to allow Unicode/Chinese, so future maintainers don’t “fix” it back.
  • Using \w with re.UNICODE will allow a broad set of Unicode letters (e.g., Cyrillic, Greek) and digits; if the intent is specifically Chinese (or a limited set of locales), you may want to restrict the character classes more narrowly.
  • Double-check whether including the apostrophe ' in the allowed characters is intentional for Discord slash command names; if not, removing it from the regex will avoid accepting potentially invalid names.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider adding a brief inline comment explaining why the regex intentionally diverges from Discord’s official lowercase `a-z0-9_-` naming rule to allow Unicode/Chinese, so future maintainers don’t “fix” it back.
- Using `\w` with `re.UNICODE` will allow a broad set of Unicode letters (e.g., Cyrillic, Greek) and digits; if the intent is specifically Chinese (or a limited set of locales), you may want to restrict the character classes more narrowly.
- Double-check whether including the apostrophe `'` in the allowed characters is intentional for Discord slash command names; if not, removing it from the regex will avoid accepting potentially invalid names.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request updates the regular expression used to validate Discord slash command names to allow a broader set of characters. The reviewer noted that Discord slash commands do not allow uppercase letters, which the new \w pattern would match, potentially causing API errors. They suggested adding a lowercase check and removing the redundant re.UNICODE flag.

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.

Comment thread astrbot/core/platform/sources/discord/discord_platform_adapter.py Outdated
# Discord 支持 Unicode 命令名(如中文),放宽匹配并确保全小写

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@zouyonghe zouyonghe merged commit 029e9c8 into AstrBotDevs:master Jul 2, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants