Skip to content

Discord 适配器增强功能 - #9124

Closed
NLKASHEI wants to merge 2 commits into
AstrBotDevs:masterfrom
NLKASHEI:master
Closed

NLKASHEI wants to merge 2 commits into
AstrBotDevs:masterfrom
NLKASHEI:master

Conversation

@NLKASHEI

@NLKASHEI NLKASHEI commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

解决的问题

主要修改discord_platform_adapter.py

  1. 所有命令参数统一显示为 params → 用 inspect.signature() 按函数签名生成独立 Option
  2. 命令描述用插件 desc → 改用 handler.doc(docstring)
  3. star_map 无异常保护 → 加 try/except KeyError
  4. 重复 import inspect → 移除
  5. Option description 优化 → "请输入 {name}"

尚未解决的问题
个人插件无法 import discord,如果想要斜杠指令实现attachment参数,需要 AstrBot 提供通用 AttachmentParam 类型或约定参数名。

Summary by Sourcery

Enhancements:

  • Broaden Discord slash command name pattern to allow additional safe characters such as apostrophes, while still rejecting non-lowercase names.

NLKASHEI and others added 2 commits July 1, 2026 18:22
# Discord 支持 Unicode 命令名(如中文),放宽匹配并确保全小写

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@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 3, 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 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>

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.


# 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):

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.

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.

@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 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):

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.

critical

在 Python 的 re 模块中,使用原始字符串(raw string r"...")时,字符集 [] 内部的 \\w 会被解析为匹配字面量反斜杠 \\ 或字母 w,而不是预期的单词字符集 \w。

这会导致几乎所有不只包含 -、_、'、\\ 和 w 的斜杠指令(例如 help、start 等)都无法匹配该正则,从而被错误地跳过。

建议将 \\w 改为单反斜杠 \w。

Suggested change
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):

@NLKASHEI NLKASHEI closed this Jul 3, 2026
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.

1 participant