Browse Source

Add a swap method for dynamic items

pull/10271/head
DA-344 2 weeks ago
parent
commit
18a541d3d8
  1. 4
      discord/ui/container.py
  2. 4
      discord/ui/item.py
  3. 4
      discord/ui/section.py
  4. 10
      discord/ui/view.py

4
discord/ui/container.py

@ -198,6 +198,10 @@ class Container(Item[V]):
def _has_children(self): def _has_children(self):
return True return True
def _swap_item(self, base, new, custom_id) -> None:
child_index = self._children.index(base)
self._children[child_index] = new # type: ignore
@property @property
def children(self) -> List[Item[V]]: def children(self) -> List[Item[V]]:
"""List[:class:`Item`]: The children of this container.""" """List[:class:`Item`]: The children of this container."""

4
discord/ui/item.py

@ -44,6 +44,7 @@ if TYPE_CHECKING:
from ..components import Component from ..components import Component
from .action_row import ActionRow from .action_row import ActionRow
from .container import Container from .container import Container
from .dynamic import DynamicItem
I = TypeVar('I', bound='Item[Any]') I = TypeVar('I', bound='Item[Any]')
V = TypeVar('V', bound='BaseView', covariant=True) V = TypeVar('V', bound='BaseView', covariant=True)
@ -118,6 +119,9 @@ class Item(Generic[V]):
return self._provided_custom_id return self._provided_custom_id
return True return True
def _swap_item(self, base: Item, new: DynamicItem, custom_id: str) -> None:
raise ValueError
def __repr__(self) -> str: def __repr__(self) -> str:
attrs = ' '.join(f'{key}={getattr(self, key)!r}' for key in self.__item_repr_attributes__) attrs = ' '.join(f'{key}={getattr(self, key)!r}' for key in self.__item_repr_attributes__)
return f'<{self.__class__.__name__} {attrs}>' return f'<{self.__class__.__name__} {attrs}>'

4
discord/ui/section.py

@ -111,6 +111,10 @@ class Section(Item[V]):
def _is_v2(self) -> bool: def _is_v2(self) -> bool:
return True return True
def _swap_item(self, base, new, custom_id) -> None:
if self.accessory.is_dispatchable() and getattr(self.accessory, 'custom_id', None) == custom_id:
self.accessory = new # type: ignore
def walk_children(self) -> Generator[Item[V], None, None]: def walk_children(self) -> Generator[Item[V], None, None]:
"""An iterator that recursively walks through all the children of this section """An iterator that recursively walks through all the children of this section
and its children, if applicable. This includes the `accessory`. and its children, if applicable. This includes the `accessory`.

10
discord/ui/view.py

@ -300,6 +300,12 @@ class BaseView:
if self.__timeout: if self.__timeout:
self.__timeout_expiry = time.monotonic() + self.__timeout self.__timeout_expiry = time.monotonic() + self.__timeout
def _swap_item(self, base: Item, new: DynamicItem, custom_id: str) -> None:
# if an error is raised it is catched by the try/except block that calls
# this function
child_index = self._children.index(base)
self._children[child_index] = new # type: ignore
@property @property
def timeout(self) -> Optional[float]: def timeout(self) -> Optional[float]:
"""Optional[:class:`float`]: The timeout in seconds from last interaction with the UI before no longer accepting input. """Optional[:class:`float`]: The timeout in seconds from last interaction with the UI before no longer accepting input.
@ -954,11 +960,9 @@ class ViewStore:
parent = base_item._parent or view parent = base_item._parent or view
try: try:
child_index = parent._children.index(base_item) # type: ignore parent._swap_item(base_item, item, custom_id)
except ValueError: except ValueError:
return return
else:
parent._children[child_index] = item # type: ignore
item._view = view item._view = view
item._rendered_row = base_item._rendered_row item._rendered_row = base_item._rendered_row

Loading…
Cancel
Save