Skip to content

Commit efe8fe6

Browse files
authored
add Modal.get_item
1 parent fa77faa commit efe8fe6

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

discord/ui/modal.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import TYPE_CHECKING, Any, Callable
1010

1111
from ..enums import ComponentType
12+
from ..utils import find
1213
from .input_text import InputText
1314
from .select import Select
1415

@@ -267,6 +268,25 @@ def remove_item(self, item: InputText | Select) -> Self:
267268
pass
268269
return self
269270

271+
def get_item(self, id: str | int) -> Select | InputText | None:
272+
"""Gets an item from the modal. Roughly equal to `utils.get(modal.children, ...)`.
273+
If an :class:`int` is provided, the item will be retrieved by ``id``, otherwise by ``custom_id``.
274+
275+
Parameters
276+
----------
277+
id: Union[:class:`int`, :class:`str`]
278+
The id or custom_id of the item to get
279+
280+
Returns
281+
-------
282+
Optional[:class:`Item`]
283+
The item with the matching ``custom_id`` or ``id`` if it exists.
284+
"""
285+
if not id:
286+
return None
287+
attr = "id" if isinstance(id, int) else "custom_id"
288+
return find(lambda i: getattr(i, attr, None) == id, self.children)
289+
270290
def stop(self) -> None:
271291
"""Stops listening to interaction events from the modal dialog."""
272292
if not self._stopped.done():

0 commit comments

Comments
 (0)