Browse Source

fix attr error things

pull/10166/head
DA-344 2 weeks ago
parent
commit
176d0a4182
  1. 6
      discord/ui/action_row.py
  2. 13
      discord/ui/container.py
  3. 6
      discord/ui/section.py
  4. 22
      discord/ui/view.py

6
discord/ui/action_row.py

@ -274,7 +274,7 @@ class ActionRow(Item[V]):
self._children.append(item)
if self._view and getattr(self._view, '__discord_ui_layout_view__', False):
self._view.__total_children += 1
self._view._total_children += 1
return self
@ -296,7 +296,7 @@ class ActionRow(Item[V]):
pass
else:
if self._view and getattr(self._view, '__discord_ui_layout_view__', False):
self._view.__total_children -= 1
self._view._total_children -= 1
return self
@ -327,7 +327,7 @@ class ActionRow(Item[V]):
chaining.
"""
if self._view and getattr(self._view, '__discord_ui_layout_view__', False):
self._view.__total_children -= len(self._children)
self._view._total_children -= len(self._children)
self._children.clear()
return self

13
discord/ui/container.py

@ -366,10 +366,9 @@ class Container(Item[V]):
item._update_children_view(self.view) # type: ignore
if is_layout_view:
self._view.__total_children += len(tuple(item.walk_children())) # type: ignore
else:
if is_layout_view:
self._view.__total_children += 1 # type: ignore
self._view._total_children += sum(1 for _ in item.walk_children()) # type: ignore
elif is_layout_view:
self._view._total_children += 1 # type: ignore
item._view = self.view
item._parent = self
@ -394,9 +393,9 @@ class Container(Item[V]):
else:
if self._view and getattr(self._view, '__discord_ui_layout_view__', False):
if getattr(item, '__discord_ui_update_view__', False):
self._view.__total_children -= len(tuple(item.walk_children())) # type: ignore
self._view._total_children -= len(tuple(item.walk_children())) # type: ignore
else:
self._view.__total_children -= 1
self._view._total_children -= 1
return self
def get_item_by_id(self, id: int, /) -> Optional[Item[V]]:
@ -427,6 +426,6 @@ class Container(Item[V]):
"""
if self._view and getattr(self._view, '__discord_ui_layout_view__', False):
self._view.__total_children -= len(tuple(self.walk_children()))
self._view._total_children -= sum(1 for _ in self.walk_children())
self._children.clear()
return self

6
discord/ui/section.py

@ -168,7 +168,7 @@ class Section(Item[V]):
self._children.append(item)
if self._view and getattr(self._view, '__discord_ui_layout_view__', False):
self._view.__total_children += 1
self._view._total_children += 1
return self
@ -190,7 +190,7 @@ class Section(Item[V]):
pass
else:
if self._view and getattr(self._view, '__discord_ui_layout_view__', False):
self._view.__total_children -= 1
self._view._total_children -= 1
return self
@ -221,7 +221,7 @@ class Section(Item[V]):
chaining.
"""
if self._view and getattr(self._view, '__discord_ui_layout_view__', False):
self._view.__total_children -= len(self._children) + 1 # the + 1 is the accessory
self._view._total_children -= len(self._children) # we don't count the accessory because it is required
self._children.clear()
return self

22
discord/ui/view.py

@ -198,7 +198,6 @@ class _ViewCallback:
class BaseView:
__discord_ui_view__: ClassVar[bool] = False
__discord_ui_modal__: ClassVar[bool] = False
__discord_ui_container__: ClassVar[bool] = False
__view_children_items__: ClassVar[Dict[str, ItemLike]] = {}
def __init__(self, *, timeout: Optional[float] = 180.0) -> None:
@ -210,7 +209,7 @@ class BaseView:
self.__timeout_expiry: Optional[float] = None
self.__timeout_task: Optional[asyncio.Task[None]] = None
self.__stopped: asyncio.Future[bool] = asyncio.get_running_loop().create_future()
self.__total_children: int = len(tuple(self.walk_children()))
self._total_children: int = sum(1 for _ in self.walk_children())
def _is_v2(self) -> bool:
return False
@ -354,7 +353,7 @@ class BaseView:
item._update_children_view(self) # type: ignore
added += len(tuple(item.walk_children())) # type: ignore
if self._is_v2() and self.__total_children + added > 40:
if self._is_v2() and self._total_children + added > 40:
raise ValueError('maximum number of children exceeded')
self._children.append(item)
@ -381,10 +380,10 @@ class BaseView:
if getattr(item, '__discord_ui_update_view__', False):
removed += len(tuple(item.walk_children())) # type: ignore
if self.__total_children - removed < 0:
self.__total_children = 0
if self._total_children - removed < 0:
self._total_children = 0
else:
self.__total_children -= removed
self._total_children -= removed
return self
@ -395,7 +394,7 @@ class BaseView:
chaining.
"""
self._children.clear()
self.__total_children = 0
self._total_children = 0
return self
def get_item_by_id(self, id: int, /) -> Optional[Item[Self]]:
@ -760,9 +759,8 @@ class LayoutView(BaseView):
def __init__(self, *, timeout: Optional[float] = 180.0) -> None:
super().__init__(timeout=timeout)
self.__total_children: int = len(list(self.walk_children()))
if self.__total_children > 40:
if self._total_children > 40:
raise ValueError('maximum number of children exceeded')
def __init_subclass__(cls) -> None:
@ -776,9 +774,7 @@ class LayoutView(BaseView):
for base in reversed(cls.__mro__):
for name, member in base.__dict__.items():
if isinstance(member, Item):
if member._row is None:
member._row = row
member._rendered_row = member._row
member._rendered_row = member._row or row
children[name] = member
row += 1
elif hasattr(member, '__discord_ui_model_type__') and getattr(member, '__discord_ui_parent__', None):
@ -804,7 +800,7 @@ class LayoutView(BaseView):
return components
def add_item(self, item: Item[Any]) -> Self:
if self.__total_children >= 40:
if self._total_children >= 40:
raise ValueError('maximum number of children exceeded (40)')
super().add_item(item)
return self

Loading…
Cancel
Save