Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

[Bug] Discord slash command sync aborts with KeyError when a plugin handler is defined in a submodule

Đang mở Phù hợp với người mới
#10,626 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@yuexia5 đang làm issue này rồi.

Từ ngày 19/10/2026.

  • #10632 của @yuexia5 — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
77/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
api, backend

Hướng nghiên cứu

Bắt đầu tại _collect_and_register_commands trong astrbot/core/platform/sources/discord/discord_platform_adapter.py, khoảng dòng 475, và so sánh cách tra cứu star_map với các cách tra cứu có kiểm tra được mô tả trong issue. Tái hiện bằng một trình xử lý plugin từ mô-đun con và bật đăng ký lệnh Discord. Hoàn thành khi trình xử lý không xác định hoặc không hoạt động bị bỏ qua và quá trình đồng bộ lệnh kết thúc mà không bị dừng giữa chừng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Description

Discord slash commands are never synced when any plugin registers an event handler from a submodule (for example, a mixin class defined in plugin/foo.py and inherited by the plugin's main Star class).

Such a handler is registered with handler_module_path = handler.__module__ = data.plugins.<plugin>.foo, but star_map is only keyed by the plugin's main module (data.plugins.<plugin>.main). DiscordPlatformAdapter._collect_and_register_commands (astrbot/core/platform/sources/discord/discord_platform_adapter.py:475) does a direct lookup:

if not star_map[handler_md.handler_module_path].activated:
    continue

This raises KeyError inside on_ready, which aborts the loop before sync_commands() is called, so no plugin's slash commands get registered — not just the offending plugin's.

Other parts of the core already guard this lookup and skip unknown handlers (e.g. StarHandlerRegistry.get_handlers_by_event_type uses star_map.get(...), and the Telegram adapter checks handler_module_path not in star_map). The Discord adapter is the only one that crashes.

Suggested fix: use star_map.get() and skip handlers whose plugin is missing or inactive.

Reproduction Steps
  1. Enable a Discord platform adapter with discord_command_register: true.
  2. Install a plugin whose main class inherits a mixin from another module of the same plugin, where the mixin method is decorated with an event filter, e.g.:
    # plugin/foo.py
    class FooMixin:
        @filter.on_llm_response()
        async def on_resp(self, event, resp): ...
    
    # plugin/main.py
    from .foo import FooMixin
    class MyPlugin(FooMixin, Star): ...
    
    (Real-world example: astrbot_plugin_private_companion, atrelay.py.)
  3. Start AstrBot and wait for the Discord bot to become ready.
  4. on_ready_once_callback fails with KeyError and no slash commands are synced.
AstrBot Version

v4.29.1 (the same code is still present on current master)

Operating System

Linux

Deployment Method

Source (uv run main.py) managed by systemd

Message Platform Adapter

Discord

Error Log
[Core] [INFO] [discord.client:44]: [Discord] Logged in as <bot>
[Core] [INFO] [discord.discord_platform_adapter:471]: [Discord] Collecting and registering slash commands...
[Core] [ERRO] [v4.29.1] [discord.discord_platform_adapter:158]: [Discord] on_ready_once_callback err: 'data.plugins.astrbot_plugin_private_companion.atrelay'
Traceback (most recent call last):
  File ".../discord/client.py", line 465, in _run_event
  File "/opt/AstrBot/astrbot/core/platform/sources/discord/client.py", line 50, in on_ready
  File "/opt/AstrBot/astrbot/core/platform/sources/discord/discord_platform_adapter.py", line 151, in callback
  File "/opt/AstrBot/astrbot/core/platform/sources/discord/discord_platform_adapter.py", line 475, in _collect_and_register_commands
KeyError: 'data.plugins.astrbot_plugin_private_companion.atrelay'

After changing that line to star_map.get() locally:

[Discord] Ready to sync 49 commands: ...
[Discord] Command synchronization completed.
Supplementary Information

The plugin-side handler itself is also never invoked by the core (it is never bound to the plugin instance and is filtered out during dispatch); that part is a plugin issue and is being reported to the plugin author separately. This issue is only about the Discord adapter turning one unbound handler into a failure of the whole command sync.

Checklist
  • I have searched the Issue list for related issues and could not resolve the problem, or this issue has never been reported before.
  • I have tried disabling all plugins to rule out issues caused by plugins. (The trigger is a plugin's handler layout; the crash itself is in the Discord adapter, see above.)
  • The issue I am reporting is related to AstrBot itself, not to a specific plugin.
  • I have read and agree to the project's Code of Conduct.
  • (Optional) I am willing to submit a PR to help fix this issue.
Ngôn ngữ chính
Python
Star
42.1k
Fork
3.1k
Merge trung bình
1 ngày 50 phút
Pull request đã merge (30 ngày)
146

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của AstrBotDevs/AstrBot

Tất cả issue của AstrBotDevs/AstrBot

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.