From cd9f7768fb37d537586f6815c423efd575444472 Mon Sep 17 00:00:00 2001 From: DA-344 <108473820+DA-344@users.noreply.github.com> Date: Fri, 18 Apr 2025 22:53:35 +0200 Subject: [PATCH] some fixes and typings --- discord/ui/action_row.py | 6 +++--- discord/ui/container.py | 7 ++++--- discord/ui/item.py | 7 +++++++ discord/ui/section.py | 12 +++++++++--- discord/ui/view.py | 4 ++-- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/discord/ui/action_row.py b/discord/ui/action_row.py index 481337384..70384cf9a 100644 --- a/discord/ui/action_row.py +++ b/discord/ui/action_row.py @@ -140,7 +140,7 @@ class ActionRow(Item[V]): pattern = item.__discord_ui_compiled_template__ dynamic_items[pattern] = item.__class__ elif item.is_dispatchable(): - dispatch_info[(item.type.value, item.custom_id)] = item # type: ignore + dispatch_info[(item.type.value, item.custom_id)] = item is_fully_dynamic = False return is_fully_dynamic @@ -218,7 +218,7 @@ class ActionRow(Item[V]): pass return self - def get_item_by_id(self, id: str, /) -> Optional[Item[V]]: + def get_item_by_id(self, id: int, /) -> Optional[Item[V]]: """Gets an item with :attr:`Item.id` set as ``id``, or ``None`` if not found. @@ -228,7 +228,7 @@ class ActionRow(Item[V]): Parameters ---------- - id: :class:`str` + id: :class:`int` The ID of the component. Returns diff --git a/discord/ui/container.py b/discord/ui/container.py index 20aff903c..198973dbe 100644 --- a/discord/ui/container.py +++ b/discord/ui/container.py @@ -97,6 +97,7 @@ class Container(Item[V]): row: Optional[int] = None, id: Optional[int] = None, ) -> None: + super().__init__() self.__dispatchable: List[Item[V]] = [] self._children: List[Item[V]] = self._init_children() @@ -196,7 +197,7 @@ class Container(Item[V]): def to_components(self) -> List[Dict[str, Any]]: components = [] - for child in self._children: + for child in sorted(self._children, key=lambda i: i._rendered_row or 0): components.append(child.to_component_dict()) return components @@ -287,7 +288,7 @@ class Container(Item[V]): pass return self - def get_item_by_id(self, id: str, /) -> Optional[Item[V]]: + def get_item_by_id(self, id: int, /) -> Optional[Item[V]]: """Gets an item with :attr:`Item.id` set as ``id``, or ``None`` if not found. @@ -297,7 +298,7 @@ class Container(Item[V]): Parameters ---------- - id: :class:`str` + id: :class:`int` The ID of the component. Returns diff --git a/discord/ui/item.py b/discord/ui/item.py index 597be4dab..614859d72 100644 --- a/discord/ui/item.py +++ b/discord/ui/item.py @@ -24,6 +24,7 @@ DEALINGS IN THE SOFTWARE. from __future__ import annotations +import os from typing import Any, Callable, Coroutine, Dict, Generic, Optional, TYPE_CHECKING, Tuple, Type, TypeVar from ..interactions import Interaction @@ -81,6 +82,9 @@ class Item(Generic[V]): self._id: Optional[int] = None self._max_row: int = 5 if not self._is_v2() else 10 + if self._is_v2(): + self.custom_id: str = os.urandom(16).hex() + def to_component_dict(self) -> Dict[str, Any]: raise NotImplementedError @@ -124,6 +128,9 @@ class Item(Generic[V]): else: raise ValueError(f'row cannot be negative or greater than or equal to {self._max_row}') + if self._rendered_row is None: + self._rendered_row = value + @property def width(self) -> int: return 1 diff --git a/discord/ui/section.py b/discord/ui/section.py index 5a3104af8..bacda7883 100644 --- a/discord/ui/section.py +++ b/discord/ui/section.py @@ -162,7 +162,7 @@ class Section(Item[V]): pass return self - def get_item_by_id(self, id: str, /) -> Optional[Item[V]]: + def get_item_by_id(self, id: int, /) -> Optional[Item[V]]: """Gets an item with :attr:`Item.id` set as ``id``, or ``None`` if not found. @@ -172,7 +172,7 @@ class Section(Item[V]): Parameters ---------- - id: :class:`str` + id: :class:`int` The ID of the component. Returns @@ -204,7 +204,13 @@ class Section(Item[V]): def to_component_dict(self) -> Dict[str, Any]: data = { 'type': self.type.value, - 'components': [c.to_component_dict() for c in self._children], + 'components': [ + c.to_component_dict() for c in + sorted( + self._children, + key=lambda i: i._rendered_row or 0, + ) + ], '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 4a3e2ac8d..e9bd6f773 100644 --- a/discord/ui/view.py +++ b/discord/ui/view.py @@ -372,7 +372,7 @@ class BaseView: self._children.clear() return self - def get_item_by_id(self, id: str, /) -> Optional[Item[Self]]: + def get_item_by_id(self, id: int, /) -> Optional[Item[Self]]: """Gets an item with :attr:`Item.id` set as ``id``, or ``None`` if not found. @@ -384,7 +384,7 @@ class BaseView: Parameters ---------- - id: :class:`str` + id: :class:`int` The ID of the component. Returns