From c8bc14fb42f215bf65609545f0ec025d32da3fe9 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 26 May 2026 16:33:09 +0000 Subject: [PATCH] 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) --- wormhole/core.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/wormhole/core.py b/wormhole/core.py index 133e369..4637301 100644 --- a/wormhole/core.py +++ b/wormhole/core.py @@ -169,6 +169,16 @@ class Wormhole( Note: slash commands are handled automatically by Red's hybrid_command 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_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)