Skip to content

Commit a8f6fcf

Browse files
authored
fix: hook onto add_command to propagate errors correctly (#1678)
* fix: hook onto add_command to propagate errors correctly * fix: re-add on_callback_added to add_hybrid_command
1 parent f815149 commit a8f6fcf

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

interactions/client/client.py

+5
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,8 @@ def __init__(
435435
self.async_startup_tasks: list[tuple[Callable[..., Coroutine], Iterable[Any], dict[str, Any]]] = []
436436
"""A list of coroutines to run during startup"""
437437

438+
self._add_command_hook: list[Callable[[Callable], Any]] = []
439+
438440
# callbacks
439441
if global_pre_run_callback:
440442
if asyncio.iscoroutinefunction(global_pre_run_callback):
@@ -1416,6 +1418,9 @@ def add_command(self, func: Callable) -> None:
14161418
else:
14171419
self.logger.debug(f"Added callback: {func.callback.__name__}")
14181420

1421+
for hook in self._add_command_hook:
1422+
hook(func)
1423+
14191424
self.dispatch(CallbackAdded(callback=func, extension=func.extension if hasattr(func, "extension") else None))
14201425

14211426
def _gather_callbacks(self) -> None:

interactions/ext/hybrid_commands/manager.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,22 @@ def __init__(
6565
self.client = cast(prefixed.PrefixedInjectedClient, client)
6666
self.ext_command_list: dict[str, list[str]] = {}
6767

68-
self.client.add_listener(self.add_hybrid_command.copy_with_binding(self))
6968
self.client.add_listener(self.handle_ext_unload.copy_with_binding(self))
7069

70+
self.client._add_command_hook.append(self._add_hybrid_command)
71+
7172
self.client.hybrid = self
7273

7374
@listen("on_callback_added")
74-
async def add_hybrid_command(self, event: CallbackAdded):
75-
if (
76-
not isinstance(event.callback, HybridSlashCommand)
77-
or not event.callback.callback
78-
or event.callback._dummy_base
79-
):
75+
async def add_hybrid_command(self, event: CallbackAdded) -> None:
76+
# just here for backwards compatability since it was accidentially public, don't rely on it
77+
self._add_hybrid_command(event.callback)
78+
79+
def _add_hybrid_command(self, callback: Callable):
80+
if not isinstance(callback, HybridSlashCommand) or not callback.callback or callback._dummy_base:
8081
return
8182

82-
cmd = event.callback
83+
cmd = callback
8384
prefixed_transform = slash_to_prefixed(cmd)
8485

8586
if self.use_slash_command_msg:

interactions/ext/prefixed_commands/manager.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from interactions.api.events.internal import (
1010
CommandError,
1111
CommandCompletion,
12-
CallbackAdded,
1312
ExtensionUnload,
1413
)
1514
from interactions.client.client import Client
@@ -90,13 +89,13 @@ def __init__(
9089
self.client.prefixed = self
9190

9291
self._dispatch_prefixed_commands = self._dispatch_prefixed_commands.copy_with_binding(self)
93-
self._register_command = self._register_command.copy_with_binding(self)
9492
self._handle_ext_unload = self._handle_ext_unload.copy_with_binding(self)
9593

9694
self.client.add_listener(self._dispatch_prefixed_commands)
97-
self.client.add_listener(self._register_command)
9895
self.client.add_listener(self._handle_ext_unload)
9996

97+
self.client._add_command_hook.append(self._register_command)
98+
10099
async def generate_prefixes(self, client: Client, msg: Message) -> str | list[str]:
101100
"""
102101
Generates a list of prefixes a prefixed command can have based on the client and message.
@@ -229,17 +228,13 @@ def remove_command(self, name: str, delete_parent_if_empty: bool = False) -> Opt
229228

230229
return command
231230

232-
@listen("callback_added")
233-
async def _register_command(self, event: CallbackAdded) -> None:
231+
def _register_command(self, callback: Callable) -> None:
234232
"""Registers a prefixed command, if there is one given."""
235-
if not isinstance(event.callback, PrefixedCommand):
233+
if not isinstance(callback, PrefixedCommand):
236234
return
237235

238-
cmd = event.callback
239-
cmd.extension = event.extension
240-
241-
if not cmd.is_subcommand:
242-
self.add_command(cmd)
236+
if not callback.is_subcommand:
237+
self.add_command(callback)
243238

244239
@listen("extension_unload")
245240
async def _handle_ext_unload(self, event: ExtensionUnload) -> None:

0 commit comments

Comments
 (0)