Browse Source

chore: Add (View|Container|ActionRow|Section).walk_children methods

pull/10166/head
DA-344 2 months ago
parent
commit
7741166e86
  1. 14
      discord/ui/action_row.py
  2. 19
      discord/ui/container.py
  3. 21
      discord/ui/section.py
  4. 24
      discord/ui/view.py

14
discord/ui/action_row.py

@ -30,6 +30,7 @@ from typing import (
ClassVar,
Coroutine,
Dict,
Generator,
List,
Literal,
Optional,
@ -204,6 +205,19 @@ class ActionRow(Item[V]):
"""List[:class:`Item`]: The list of children attached to this action row."""
return self._children.copy()
def walk_children(self) -> Generator[Item[V], Any, None]:
"""An iterator that recursively walks through all the children of this view
and it's children, if applicable.
Yields
------
:class:`Item`
An item in the action row.
"""
for child in self.children:
yield child
def add_item(self, item: Item[Any]) -> Self:
"""Adds an item to this row.

19
discord/ui/container.py

@ -25,7 +25,7 @@ from __future__ import annotations
import copy
import os
from typing import TYPE_CHECKING, Any, ClassVar, Coroutine, Dict, List, Literal, Optional, Tuple, Type, TypeVar, Union
from typing import TYPE_CHECKING, Any, ClassVar, Coroutine, Dict, Generator, List, Literal, Optional, Tuple, Type, TypeVar, Union
from .item import Item, ItemCallbackType
from .view import _component_to_item, LayoutView
@ -297,6 +297,23 @@ class Container(Item[V]):
id=component.id,
)
def walk_children(self) -> Generator[Item[V], Any, None]:
"""An iterator that recursively walks through all the children of this container
and it's children, if applicable.
Yields
------
:class:`Item`
An item in the container.
"""
for child in self.children:
yield child
if getattr(child, '__discord_ui_update_view__', False):
# if it has this attribute then it can contain children
yield from child.walk_children() # type: ignore
def add_item(self, item: Item[Any]) -> Self:
"""Adds an item to this container.

21
discord/ui/section.py

@ -23,7 +23,7 @@ DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, TypeVar, Union, ClassVar
from typing import TYPE_CHECKING, Any, Dict, Generator, List, Literal, Optional, TypeVar, Union, ClassVar
from .item import Item
from .text_display import TextDisplay
@ -96,6 +96,11 @@ class Section(Item[V]):
def type(self) -> Literal[ComponentType.section]:
return ComponentType.section
@property
def children(self) -> List[Item[V]]:
"""List[:class:`Item`]: The list of children attached to this section."""
return self._children.copy()
@property
def width(self):
return 5
@ -110,6 +115,20 @@ class Section(Item[V]):
def is_dispatchable(self) -> bool:
return self.accessory.is_dispatchable()
def walk_children(self) -> Generator[Item[V], Any, None]:
"""An iterator that recursively walks through all the children of this section.
and it's children, if applicable.
Yields
------
:class:`Item`
An item in this section.
"""
for child in self.children:
yield child
yield self.accessory
def _update_children_view(self, view) -> None:
self.accessory._view = view

24
discord/ui/view.py

@ -638,6 +638,11 @@ class View(BaseView):
In order to modify and edit message components they must be
converted into a :class:`View` first.
.. warning::
This **will not** take into account every v2 component, if you
want to edit them, use :meth:`LayoutView.from_message` instead.
Parameters
-----------
message: :class:`discord.Message`
@ -770,7 +775,7 @@ class LayoutView(BaseView):
In order to modify and edit message components they must be
converted into a :class:`LayoutView` first.
Unlike :meth:`View.from_message` this works for
Unlike :meth:`View.from_message` this converts v2 components.
Parameters
-----------
@ -793,6 +798,23 @@ class LayoutView(BaseView):
return view
def walk_children(self):
"""An iterator that recursively walks through all the children of this view
and it's children, if applicable.
Yields
------
:class:`Item`
An item in the view.
"""
for child in self.children:
yield child
if getattr(child, '__discord_ui_update_view__', False):
# if it has this attribute then it can contain children
yield from child.walk_children() # type: ignore
class ViewStore:
def __init__(self, state: ConnectionState):

Loading…
Cancel
Save