Browse Source

some fixes and typings

pull/10166/head
DA-344 2 months ago
parent
commit
cd9f7768fb
  1. 6
      discord/ui/action_row.py
  2. 7
      discord/ui/container.py
  3. 7
      discord/ui/item.py
  4. 12
      discord/ui/section.py
  5. 4
      discord/ui/view.py

6
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

7
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

7
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

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

4
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

Loading…
Cancel
Save