Browse Source

fix: rows being set weirdly because of sorted

pull/10166/head
DA-344 2 weeks ago
parent
commit
8f39bf5731
  1. 5
      discord/ui/action_row.py
  2. 5
      discord/ui/container.py
  3. 15
      discord/ui/section.py
  4. 8
      discord/ui/view.py

5
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,

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

15
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:

8
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

Loading…
Cancel
Save