Browse Source

fix: Container.__dispatchable not having new dispatchable nested items added after a dispatchable item was added in add_item

pull/10166/head
DA-344 2 weeks ago
parent
commit
eb38195e80
  1. 3
      discord/ui/action_row.py
  2. 32
      discord/ui/container.py

3
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): if self._view and getattr(self._view, '__discord_ui_layout_view__', False):
self._view._total_children += 1 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 return self
def remove_item(self, item: Item[Any]) -> Self: def remove_item(self, item: Item[Any]) -> Self:

32
discord/ui/container.py

@ -160,6 +160,15 @@ class Container(Item[V]):
self.row = row self.row = row
self.id = id 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]]: def _init_children(self) -> List[Item[Any]]:
children = [] children = []
parents = {} parents = {}
@ -393,20 +402,15 @@ class Container(Item[V]):
pass pass
else: else:
if item.is_dispatchable(): if item.is_dispatchable():
# none of this should error, but wrap in a try/except block if getattr(item, '__discord_ui_section__', False):
# anyways. self._remove_dispatchable(item.accessory) # type: ignore
try: elif getattr(item, '__discord_ui_action_row__', False):
if getattr(item, '__discord_ui_section__', False): for c in item._children: # type: ignore
self.__dispatchable.remove(item.accessory) # type: ignore if not c.is_dispatchable():
elif getattr(item, '__discord_ui_action_row__', False): continue
for c in item._children: # type: ignore self._remove_dispatchable(c)
if not c.is_dispatchable(): else:
continue self._remove_dispatchable(item)
self.__dispatchable.remove(c)
else:
self.__dispatchable.remove(item)
except ValueError:
pass
if self._view and getattr(self._view, '__discord_ui_layout_view__', False): if self._view and getattr(self._view, '__discord_ui_layout_view__', False):
if getattr(item, '__discord_ui_update_view__', False): if getattr(item, '__discord_ui_update_view__', False):

Loading…
Cancel
Save