diff --git a/discord/ui/action_row.py b/discord/ui/action_row.py index 4edf78b60..481337384 100644 --- a/discord/ui/action_row.py +++ b/discord/ui/action_row.py @@ -47,7 +47,7 @@ from .select import select as _select, Select, UserSelect, RoleSelect, ChannelSe from ..components import ActionRow as ActionRowComponent from ..enums import ButtonStyle, ComponentType, ChannelType from ..partial_emoji import PartialEmoji -from ..utils import MISSING +from ..utils import MISSING, get as _utils_get if TYPE_CHECKING: from typing_extensions import Self @@ -218,6 +218,26 @@ class ActionRow(Item[V]): pass return self + def get_item_by_id(self, id: str, /) -> Optional[Item[V]]: + """Gets an item with :attr:`Item.id` set as ``id``, or ``None`` if + not found. + + .. warning:: + + This is **not the same** as ``custom_id``. + + Parameters + ---------- + id: :class:`str` + The ID of the component. + + Returns + ------- + Optional[:class:`Item`] + The item found, or ``None``. + """ + return _utils_get(self._children, id=id) + def clear_items(self) -> Self: """Removes all items from the row. diff --git a/discord/ui/container.py b/discord/ui/container.py index 40583e17a..20aff903c 100644 --- a/discord/ui/container.py +++ b/discord/ui/container.py @@ -29,7 +29,7 @@ from .item import Item, ItemCallbackType from .view import _component_to_item, LayoutView from .dynamic import DynamicItem from ..enums import ComponentType -from ..utils import MISSING +from ..utils import MISSING, get as _utils_get if TYPE_CHECKING: from typing_extensions import Self @@ -287,6 +287,26 @@ class Container(Item[V]): pass return self + def get_item_by_id(self, id: str, /) -> Optional[Item[V]]: + """Gets an item with :attr:`Item.id` set as ``id``, or ``None`` if + not found. + + .. warning:: + + This is **not the same** as ``custom_id``. + + Parameters + ---------- + id: :class:`str` + The ID of the component. + + Returns + ------- + Optional[:class:`Item`] + The item found, or ``None``. + """ + return _utils_get(self._children, id=id) + def clear_items(self) -> Self: """Removes all the items from the container. diff --git a/discord/ui/section.py b/discord/ui/section.py index 88fe03e5d..5a3104af8 100644 --- a/discord/ui/section.py +++ b/discord/ui/section.py @@ -28,7 +28,7 @@ from typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, TypeVar, U from .item import Item from .text_display import TextDisplay from ..enums import ComponentType -from ..utils import MISSING +from ..utils import MISSING, get as _utils_get if TYPE_CHECKING: from typing_extensions import Self @@ -162,6 +162,26 @@ class Section(Item[V]): pass return self + def get_item_by_id(self, id: str, /) -> Optional[Item[V]]: + """Gets an item with :attr:`Item.id` set as ``id``, or ``None`` if + not found. + + .. warning:: + + This is **not the same** as ``custom_id``. + + Parameters + ---------- + id: :class:`str` + The ID of the component. + + Returns + ------- + Optional[:class:`Item`] + The item found, or ``None``. + """ + return _utils_get(self._children, id=id) + def clear_items(self) -> Self: """Removes all the items from the section.