fix: prevent KeyError in Telegram collect_commands when plugin handler not in star_map#7405
Open
michealmachine wants to merge 1 commit intoAstrBotDevs:masterfrom
Open
Conversation
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- To avoid repeated dictionary lookups and keep the intent clearer, consider using
star_map.get(handler_metadata.handler_module_path)once, assign it to a local variable, and then check both its existence and.activatedstate. - It might be useful to add a debug log when
handler_module_pathis not found instar_mapso that unexpected plugin registrations can be detected and diagnosed more easily.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- To avoid repeated dictionary lookups and keep the intent clearer, consider using `star_map.get(handler_metadata.handler_module_path)` once, assign it to a local variable, and then check both its existence and `.activated` state.
- It might be useful to add a debug log when `handler_module_path` is not found in `star_map` so that unexpected plugin registrations can be detected and diagnosed more easily.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the Telegram adapter to include a safety check for the existence of a handler module path within the star map before accessing its activation status. This modification prevents potential KeyErrors during command collection. I have no feedback to provide as no review comments were submitted.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation / 动机
When using the astrbot_plugin_proactive_chat plugin (and potentially other plugins), the Telegram adapter logs the following error every 5 minutes repeatedly:
\
[ERRO] [telegram.tg_adapter:222]: 向 Telegram 注册指令时发生错误: 'data.plugins.astrbot_plugin_proactive_chat.core.message_events'
\\
The root cause is in \collect_commands()\ inside \ g_adapter.py. It iterates over \star_handlers_registry\ and accesses \star_map[handler_metadata.handler_module_path]\ directly using [], which raises a \KeyError\ when a plugin registers a handler whose module path is not yet present in \star_map\ (e.g. internal event modules that are scanned but not added to \star_map).
Confirmed on AstrBot v4.22.3 with \�strbot_plugin_proactive_chat\ v1.2.2.
Modifications / 改动点
Before:
\\python
if not star_map[handler_metadata.handler_module_path].activated:
\\
After:
\\python
if handler_metadata.handler_module_path not in star_map or not star_map[handler_metadata.handler_module_path].activated:
\\
Screenshots or Test Results / 运行截图或测试结果
After applying the fix and restarting the container, the repeated error is gone:
\
[INFO] [telegram.tg_adapter:170]: Starting Telegram polling...
[INFO] [telegram.tg_adapter:174]: Telegram Platform Adapter is running.
\\
Checklist / 检查清单
Summary by Sourcery
Bug Fixes: