From eb38195e8044ea6c11647d0e8110a088f086fd1f Mon Sep 17 00:00:00 2001 From: DA-344 <108473820+DA-344@users.noreply.github.com> Date: Thu, 15 May 2025 15:41:02 +0200 Subject: [PATCH] fix: Container.__dispatchable not having new dispatchable nested items added after a dispatchable item was added in add_item --- discord/ui/action_row.py | 3 +++ discord/ui/container.py | 32 ++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/discord/ui/action_row.py b/discord/ui/action_row.py index cd7b4c75e..764ce1dc0 100644 --- a/discord/ui/action_row.py +++ b/discord/ui/action_row.py @@ -277,6 +277,9 @@ class ActionRow(Item[V]): if self._view and getattr(self._view, '__discord_ui_layout_view__', False): self._view._total_children += 1 + if item.is_dispatchable() and self._parent and getattr(self._parent, '__discord_ui_container__', False): + self._parent._add_dispatchable(item) # type: ignore + return self def remove_item(self, item: Item[Any]) -> Self: diff --git a/discord/ui/container.py b/discord/ui/container.py index 199835179..633af8360 100644 --- a/discord/ui/container.py +++ b/discord/ui/container.py @@ -160,6 +160,15 @@ class Container(Item[V]): self.row = row self.id = id + def _add_dispatchable(self, item: Item[Any]) -> None: + self.__dispatchable.append(item) + + def _remove_dispatchable(self, item: Item[Any]) -> None: + try: + self.__dispatchable.remove(item) + except ValueError: + pass + def _init_children(self) -> List[Item[Any]]: children = [] parents = {} @@ -393,20 +402,15 @@ class Container(Item[V]): pass else: if item.is_dispatchable(): - # none of this should error, but wrap in a try/except block - # anyways. - try: - if getattr(item, '__discord_ui_section__', False): - self.__dispatchable.remove(item.accessory) # type: ignore - elif getattr(item, '__discord_ui_action_row__', False): - for c in item._children: # type: ignore - if not c.is_dispatchable(): - continue - self.__dispatchable.remove(c) - else: - self.__dispatchable.remove(item) - except ValueError: - pass + if getattr(item, '__discord_ui_section__', False): + self._remove_dispatchable(item.accessory) # type: ignore + elif getattr(item, '__discord_ui_action_row__', False): + for c in item._children: # type: ignore + if not c.is_dispatchable(): + continue + self._remove_dispatchable(c) + else: + self._remove_dispatchable(item) if self._view and getattr(self._view, '__discord_ui_layout_view__', False): if getattr(item, '__discord_ui_update_view__', False):