From 4044b2c97f6b1eaea32cf4c836eeb4cf1969910d Mon Sep 17 00:00:00 2001 From: DA-344 <108473820+DA-344@users.noreply.github.com> Date: Wed, 30 Apr 2025 22:20:23 +0200 Subject: [PATCH] chore: add *children param and validation for children --- discord/ui/action_row.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/discord/ui/action_row.py b/discord/ui/action_row.py index 946b6d3b9..e73d731de 100644 --- a/discord/ui/action_row.py +++ b/discord/ui/action_row.py @@ -134,9 +134,19 @@ class ActionRow(Item[V]): __discord_ui_action_row__: ClassVar[bool] = True __discord_ui_update_view__: ClassVar[bool] = True - def __init__(self, *, id: Optional[int] = None) -> None: + def __init__( + self, + *children: Item[V], + id: Optional[int] = None, + ) -> None: super().__init__() - self._children: List[Item[Any]] = self._init_children() + self._weight: int = 0 + self._children: List[Item[V]] = self._init_children() + self._children.extend(children) + self._weight += sum(i.width for i in children) + + if self._weight > 5: + raise ValueError('maximum number of children exceeded') self.id = id @@ -162,6 +172,7 @@ class ActionRow(Item[V]): item.callback = _ActionRowCallback(func, self, item) # type: ignore item._parent = getattr(func, '__discord_ui_parent__', self) setattr(self, func.__name__, item) + self._weight += item.width children.append(item) return children @@ -182,7 +193,7 @@ class ActionRow(Item[V]): def _update_children_view(self, view: LayoutView) -> None: for child in self._children: - child._view = view + child._view = view # pyright: ignore[reportAttributeAccessIssue] def _is_v2(self) -> bool: # although it is not really a v2 component the only usecase here is for