From 9bda89b0d6df93a68afd97314070b8d0413623bf Mon Sep 17 00:00:00 2001 From: Rapptz Date: Thu, 21 Aug 2025 22:20:12 -0400 Subject: [PATCH] Update total count tracking to always consider the wrapper object --- discord/ui/action_row.py | 5 +++++ discord/ui/container.py | 16 ++++++++-------- discord/ui/dynamic.py | 4 ++++ discord/ui/item.py | 4 ++++ discord/ui/section.py | 5 +++++ discord/ui/view.py | 12 ++---------- 6 files changed, 28 insertions(+), 18 deletions(-) diff --git a/discord/ui/action_row.py b/discord/ui/action_row.py index a8da7e67d..b0598db20 100644 --- a/discord/ui/action_row.py +++ b/discord/ui/action_row.py @@ -203,6 +203,11 @@ class ActionRow(Item[V]): def width(self): return 5 + @property + def _total_count(self) -> int: + # 1 for self and all children + return 1 + len(self._children) + @property def type(self) -> Literal[ComponentType.action_row]: return ComponentType.action_row diff --git a/discord/ui/container.py b/discord/ui/container.py index 67a58a6b4..43f3ec1ee 100644 --- a/discord/ui/container.py +++ b/discord/ui/container.py @@ -234,6 +234,11 @@ class Container(Item[V]): def width(self): return 5 + @property + def _total_count(self) -> int: + # 1 for self and all children + return 1 + len(tuple(self.walk_children())) + def _is_v2(self) -> bool: return True @@ -313,10 +318,8 @@ class Container(Item[V]): if not isinstance(item, Item): raise TypeError(f'expected Item not {item.__class__.__name__}') - if item._has_children() and self._view: - self._view._add_count(len(tuple(item.walk_children()))) # type: ignore - elif self._view: - self._view._add_count(1) + if self._view: + self._view._add_count(item._total_count) self._children.append(item) item._update_view(self.view) @@ -341,10 +344,7 @@ class Container(Item[V]): pass else: if self._view: - if item._has_children(): - self._view._add_count(-len(tuple(item.walk_children()))) # type: ignore - else: - self._view._add_count(-1) + self._view._add_count(-item._total_count) return self def find_item(self, id: int, /) -> Optional[Item[V]]: diff --git a/discord/ui/dynamic.py b/discord/ui/dynamic.py index fb38b4b2e..faed8c370 100644 --- a/discord/ui/dynamic.py +++ b/discord/ui/dynamic.py @@ -168,6 +168,10 @@ class DynamicItem(Generic[BaseT], Item[Union[View, LayoutView]]): def width(self) -> int: return self.item.width + @property + def _total_count(self) -> int: + return self.item._total_count + @classmethod async def from_custom_id( cls: Type[Self], interaction: Interaction[ClientT], item: Item[Any], match: re.Match[str], / diff --git a/discord/ui/item.py b/discord/ui/item.py index 3bcb4ad96..5498dc20f 100644 --- a/discord/ui/item.py +++ b/discord/ui/item.py @@ -147,6 +147,10 @@ class Item(Generic[V]): def width(self) -> int: return 1 + @property + def _total_count(self) -> int: + return 1 + @property def view(self) -> Optional[V]: """Optional[Union[:class:`View`, :class:`LayoutView`]]: The underlying view for this item.""" diff --git a/discord/ui/section.py b/discord/ui/section.py index 847ac1e19..6a4026e22 100644 --- a/discord/ui/section.py +++ b/discord/ui/section.py @@ -102,6 +102,11 @@ class Section(Item[V]): def width(self): return 5 + @property + def _total_count(self) -> int: + # Count the accessory, ourselves, and all children + return 2 + len(self._children) + @property def accessory(self) -> Item[V]: """:class:`Item`: The section's accessory.""" diff --git a/discord/ui/view.py b/discord/ui/view.py index c66fdf189..cf6b495f0 100644 --- a/discord/ui/view.py +++ b/discord/ui/view.py @@ -428,12 +428,7 @@ class BaseView: raise ValueError('v2 items cannot be added to this view') item._update_view(self) - added = 1 - - if item._has_children(): - added += len(tuple(item.walk_children())) # type: ignore - - self._add_count(added) + self._add_count(item._total_count) self._children.append(item) return self @@ -454,10 +449,7 @@ class BaseView: except ValueError: pass else: - removed = 1 - if item._has_children(): - removed += len(tuple(item.walk_children())) # type: ignore - self._add_count(-removed) + self._add_count(-item._total_count) return self