Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

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

Abierto Apto para principiantes
#10,626 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@yuexia5 ya está trabajando en esto.

Desde el 19/10/2026.

  • #10632 de @yuexia5 — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
77/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python
Área
api, backend

Línea de trabajo

Empieza en _collect_and_register_commands en astrbot/core/platform/sources/discord/discord_platform_adapter.py, alrededor de la línea 475, y compara la búsqueda en star_map con las búsquedas protegidas descritas en el issue. Reproduce el problema con un handler de plugin de un submódulo y el registro de comandos de Discord habilitado. Se considera terminado cuando se omite un handler desconocido o inactivo y la sincronización de comandos finaliza sin abortar.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.
Lenguaje dominante
Python
Estrellas
42.1k
Forks
3.1k
Merge medio
1 d 50 min
PR fusionados (30 d)
146

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de AstrBotDevs/AstrBot

Todos los issues de AstrBotDevs/AstrBot

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.