|
39 | 39 | )
|
40 | 40 | from .flags import AttachmentFlags
|
41 | 41 | from .partial_emoji import PartialEmoji, _EmojiTag
|
42 |
| -from .utils import MISSING, get_slots |
| 42 | +from .utils import MISSING, find, get_slots |
43 | 43 |
|
44 | 44 | if TYPE_CHECKING:
|
45 | 45 | from .emoji import AppEmoji, GuildEmoji
|
@@ -189,6 +189,25 @@ def to_dict(self) -> ActionRowPayload:
|
189 | 189 | def walk_components(self) -> Iterator[Component]:
|
190 | 190 | yield from self.children
|
191 | 191 |
|
| 192 | + def get_component(self, id: str | int) -> Component | None: |
| 193 | + """Get a component from this action row. Roughly equivalent to `utils.get(row.children, ...)`. |
| 194 | + If an ``int`` is provided, the component will be retrieved by ``id``, otherwise by ``custom_id``. |
| 195 | +
|
| 196 | + Parameters |
| 197 | + ---------- |
| 198 | + id: Union[:class:`str`, :class:`int`] |
| 199 | + The custom_id or id of the component to get. |
| 200 | +
|
| 201 | + Returns |
| 202 | + ------- |
| 203 | + Optional[:class:`Component`] |
| 204 | + The component with the matching ``id`` or ``custom_id`` if it exists. |
| 205 | + """ |
| 206 | + if not id: |
| 207 | + return None |
| 208 | + attr = "id" if isinstance(id, int) else "custom_id" |
| 209 | + return find(lambda i: getattr(i, attr, None) == id, self.children) |
| 210 | + |
192 | 211 | @classmethod
|
193 | 212 | def with_components(cls, *components, id=None):
|
194 | 213 | return cls._raw_construct(
|
@@ -632,6 +651,28 @@ def walk_components(self) -> Iterator[Component]:
|
632 | 651 | yield from r + [self.accessory]
|
633 | 652 | yield from r
|
634 | 653 |
|
| 654 | + def get_component(self, id: str | int) -> Component | None: |
| 655 | + """Get a component from this section. Roughly equivalent to `utils.get(section.walk_components(), ...)`. |
| 656 | + If an ``int`` is provided, the component will be retrieved by ``id``, otherwise by ``custom_id``. |
| 657 | +
|
| 658 | + Parameters |
| 659 | + ---------- |
| 660 | + id: Union[:class:`str`, :class:`int`] |
| 661 | + The custom_id or id of the component to get. |
| 662 | +
|
| 663 | + Returns |
| 664 | + ------- |
| 665 | + Optional[:class:`Component`] |
| 666 | + The component with the matching ``id`` or ``custom_id`` if it exists. |
| 667 | + """ |
| 668 | + if not id: |
| 669 | + return None |
| 670 | + attr = "id" if isinstance(id, int) else "custom_id" |
| 671 | + if self.accessory and id == getattr(self.accessory, attr, None): |
| 672 | + return self.accessory |
| 673 | + component = find(lambda i: getattr(i, attr, None) == id, self.components) |
| 674 | + return component |
| 675 | + |
635 | 676 |
|
636 | 677 | class TextDisplay(Component):
|
637 | 678 | """Represents a Text Display from Components V2.
|
@@ -1048,6 +1089,32 @@ def walk_components(self) -> Iterator[Component]:
|
1048 | 1089 | else:
|
1049 | 1090 | yield c
|
1050 | 1091 |
|
| 1092 | + def get_component(self, id: str | int) -> Component | None: |
| 1093 | + """Get a component from this container. Roughly equivalent to `utils.get(container.components, ...)`. |
| 1094 | + If an ``int`` is provided, the component will be retrieved by ``id``, otherwise by ``custom_id``. |
| 1095 | + This method will also search for nested components. |
| 1096 | +
|
| 1097 | + Parameters |
| 1098 | + ---------- |
| 1099 | + id: Union[:class:`str`, :class:`int`] |
| 1100 | + The custom_id or id of the component to get. |
| 1101 | +
|
| 1102 | + Returns |
| 1103 | + ------- |
| 1104 | + Optional[:class:`Component`] |
| 1105 | + The component with the matching ``id`` or ``custom_id`` if it exists. |
| 1106 | + """ |
| 1107 | + if not id: |
| 1108 | + return None |
| 1109 | + attr = "id" if isinstance(id, int) else "custom_id" |
| 1110 | + for i in self.components: |
| 1111 | + if getattr(i, attr, None) == id: |
| 1112 | + return i |
| 1113 | + elif hasattr(i, "get_component"): |
| 1114 | + if component := i.get_component(id): |
| 1115 | + return component |
| 1116 | + return None |
| 1117 | + |
1051 | 1118 |
|
1052 | 1119 | class Label(Component):
|
1053 | 1120 | """Represents a Label used in modals as the top-level component.
|
|
0 commit comments