Browse Source

Update total count tracking to always consider the wrapper object

pull/10278/head
Rapptz 6 days ago
parent
commit
9bda89b0d6
  1. 5
      discord/ui/action_row.py
  2. 16
      discord/ui/container.py
  3. 4
      discord/ui/dynamic.py
  4. 4
      discord/ui/item.py
  5. 5
      discord/ui/section.py
  6. 12
      discord/ui/view.py

5
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

16
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]]:

4
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], /

4
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."""

5
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."""

12
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

Loading…
Cancel
Save