diff --git a/discord/ui/action_row.py b/discord/ui/action_row.py index a72d4db1e..47903a4be 100644 --- a/discord/ui/action_row.py +++ b/discord/ui/action_row.py @@ -23,6 +23,7 @@ DEALINGS IN THE SOFTWARE. """ from __future__ import annotations +import sys from typing import ( TYPE_CHECKING, Any, @@ -202,6 +203,9 @@ class ActionRow(Item[V]): def is_dispatchable(self) -> bool: return any(c.is_dispatchable() for c in self.children) + def is_persistent(self) -> bool: + return self.is_dispatchable() and all(c.is_persistent() for c in self.children) + def _update_children_view(self, view: LayoutView) -> None: for child in self._children: child._view = view # pyright: ignore[reportAttributeAccessIssue] @@ -330,8 +334,9 @@ class ActionRow(Item[V]): def to_component_dict(self) -> Dict[str, Any]: components = [] - for item in self._children: - components.append(item.to_component_dict()) + 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()) base = { 'type': self.type.value, diff --git a/discord/ui/container.py b/discord/ui/container.py index dab8f58ee..174a2ec02 100644 --- a/discord/ui/container.py +++ b/discord/ui/container.py @@ -25,6 +25,7 @@ from __future__ import annotations import copy import os +import sys from typing import ( TYPE_CHECKING, Any, @@ -210,6 +211,9 @@ class Container(Item[V]): def is_dispatchable(self) -> bool: return bool(self.__dispatchable) + def is_persistent(self) -> bool: + return self.is_dispatchable() and all(c.is_persistent() for c in self.children) + def __init_subclass__(cls) -> None: super().__init_subclass__() @@ -263,7 +267,9 @@ class Container(Item[V]): def to_components(self) -> List[Dict[str, Any]]: components = [] - for child in sorted(self._children, key=lambda i: i._rendered_row or 0): + + 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()) return components diff --git a/discord/ui/section.py b/discord/ui/section.py index 70c5a778c..28688152f 100644 --- a/discord/ui/section.py +++ b/discord/ui/section.py @@ -23,6 +23,7 @@ DEALINGS IN THE SOFTWARE. """ from __future__ import annotations +import sys from typing import TYPE_CHECKING, Any, Dict, Generator, List, Literal, Optional, TypeVar, Union, ClassVar from .item import Item @@ -115,6 +116,9 @@ class Section(Item[V]): def is_dispatchable(self) -> bool: return self.accessory.is_dispatchable() + def is_persistent(self) -> bool: + return self.is_dispatchable() and self.accessory.is_persistent() + def walk_children(self) -> Generator[Item[V], None, None]: """An iterator that recursively walks through all the children of this section. and it's children, if applicable. @@ -239,7 +243,7 @@ class Section(Item[V]): c.to_component_dict() for c in sorted( self._children, - key=lambda i: i._rendered_row or 0, + key=lambda i: i._rendered_row or sys.maxsize, ) ], 'accessory': self.accessory.to_component_dict(),