fix(wormhole): make context menu registration idempotent

Problem: If bot crashes or shuts down ungracefully, context menus stay
registered in Discord's command tree. On restart, add_command() fails
with 'already loaded' error.

Solution: Remove existing context menus before registering new ones.
This makes registration idempotent (safe to run multiple times).

Changes:
- _register_context_menus() now removes existing menus first
- Wrapped in try/except to handle 'not found' gracefully
- Ensures clean reload even after ungraceful shutdown

Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
root 2026-05-26 16:33:09 +00:00
parent 15eb047d15
commit c8bc14fb42

View file

@ -169,6 +169,16 @@ class Wormhole(
Note: slash commands are handled automatically by Red's hybrid_command Note: slash commands are handled automatically by Red's hybrid_command
decorator on the mixin classes — do NOT manually register duplicates. decorator on the mixin classes — do NOT manually register duplicates.
""" """
# Remove any existing context menus first (idempotent registration)
menu_names = ("Report to Wormhole", "Bookmark (Wormhole)",
"Delete from Network", "Wormhole Profile")
for name in menu_names:
try:
self.bot.tree.remove_command(name, type=discord.AppCommandType.message)
except Exception:
pass # Already removed or never existed
# Register context menus
ctx_report = app_commands.ContextMenu(name="Report to Wormhole", callback=self._ctx_report_message) ctx_report = app_commands.ContextMenu(name="Report to Wormhole", callback=self._ctx_report_message)
ctx_bookmark = app_commands.ContextMenu(name="Bookmark (Wormhole)", callback=self._ctx_bookmark_message) ctx_bookmark = app_commands.ContextMenu(name="Bookmark (Wormhole)", callback=self._ctx_bookmark_message)
ctx_delete = app_commands.ContextMenu(name="Delete from Network", callback=self._ctx_delete_message) ctx_delete = app_commands.ContextMenu(name="Delete from Network", callback=self._ctx_delete_message)