From 8f39bf5731544183a3211e5382ec5e17261555dd Mon Sep 17 00:00:00 2001 From: DA-344 <108473820+DA-344@users.noreply.github.com> Date: Wed, 14 May 2025 16:31:04 +0200 Subject: [PATCH] fix: rows being set weirdly because of sorted --- discord/ui/action_row.py | 5 +++-- discord/ui/container.py | 5 +++-- discord/ui/section.py | 15 ++++++++------- discord/ui/view.py | 8 ++++---- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/discord/ui/action_row.py b/discord/ui/action_row.py index 524644472..cd7b4c75e 100644 --- a/discord/ui/action_row.py +++ b/discord/ui/action_row.py @@ -24,6 +24,7 @@ DEALINGS IN THE SOFTWARE. from __future__ import annotations import sys +from itertools import groupby from typing import ( TYPE_CHECKING, Any, @@ -335,8 +336,8 @@ class ActionRow(Item[V]): components = [] key = lambda i: i._rendered_row or i._row or sys.maxsize - for child in sorted(self._children, key=key): - components.append(child.to_component_dict()) + for _, cmps in groupby(self._children, key=key): + components.extend(c.to_component_dict() for c in cmps) base = { 'type': self.type.value, diff --git a/discord/ui/container.py b/discord/ui/container.py index 439f49a85..e9c7494d8 100644 --- a/discord/ui/container.py +++ b/discord/ui/container.py @@ -26,6 +26,7 @@ from __future__ import annotations import copy import os import sys +from itertools import groupby from typing import ( TYPE_CHECKING, Any, @@ -269,8 +270,8 @@ class Container(Item[V]): components = [] key = lambda i: i._rendered_row or i._row or sys.maxsize - for child in sorted(self._children, key=key): - components.append(child.to_component_dict()) + for _, comps in groupby(self._children, key=key): + components.extend(c.to_component_dict() for c in comps) return components def to_component_dict(self) -> Dict[str, Any]: diff --git a/discord/ui/section.py b/discord/ui/section.py index 2653e7469..c38040346 100644 --- a/discord/ui/section.py +++ b/discord/ui/section.py @@ -24,6 +24,7 @@ DEALINGS IN THE SOFTWARE. from __future__ import annotations import sys +from itertools import groupby from typing import TYPE_CHECKING, Any, Dict, Generator, List, Literal, Optional, TypeVar, Union, ClassVar from .item import Item @@ -236,16 +237,16 @@ class Section(Item[V]): id=component.id, ) + def to_components(self) -> List[Dict[str, Any]]: + components = [] + for _, comps in groupby(self._children, key=lambda i: i._rendered_row or i._row or sys.maxsize): + components.extend(c.to_component_dict() for c in comps) + return components + def to_component_dict(self) -> Dict[str, Any]: data = { 'type': self.type.value, - 'components': [ - c.to_component_dict() - for c in sorted( - self._children, - key=lambda i: i._rendered_row or sys.maxsize, - ) - ], + 'components': self.to_components(), 'accessory': self.accessory.to_component_dict(), } if self.id is not None: diff --git a/discord/ui/view.py b/discord/ui/view.py index dfa4a2388..65d54c7da 100644 --- a/discord/ui/view.py +++ b/discord/ui/view.py @@ -761,7 +761,7 @@ class LayoutView(BaseView): super().__init__(timeout=timeout) if self._total_children > 40: - raise ValueError('maximum number of children exceeded') + raise ValueError('maximum number of children exceeded (40)') def __init_subclass__(cls) -> None: super().__init_subclass__() @@ -792,9 +792,9 @@ class LayoutView(BaseView): # sorted by row, which in LayoutView indicates the position of the component in the # payload instead of in which ActionRow it should be placed on. key = lambda i: i._rendered_row or i._row or sys.maxsize - for child in sorted(self._children, key=key): - components.append( - child.to_component_dict(), + for _, cmps in groupby(self._children, key=key): + components.extend( + c.to_component_dict() for c in cmps ) return components