|
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, get_slots, find |
43 | 43 |
|
44 | 44 | if TYPE_CHECKING:
|
45 | 45 | from .emoji import AppEmoji, GuildEmoji
|
@@ -187,6 +187,25 @@ def to_dict(self) -> ActionRowPayload:
|
187 | 187 | def walk_components(self) -> Iterator[Component]:
|
188 | 188 | yield from self.children
|
189 | 189 |
|
| 190 | + def get_component(self, id: str | int) -> Component | None: |
| 191 | + """Get a component from this action row. Roughly equivalent to `utils.get(row.children, ...)`. |
| 192 | + If an ``int`` is provided, the component will be retrieved by ``id``, otherwise by ``custom_id``. |
| 193 | +
|
| 194 | + Parameters |
| 195 | + ---------- |
| 196 | + id: Union[:class:`str`, :class:`int`] |
| 197 | + The custom_id or id of the component to get. |
| 198 | +
|
| 199 | + Returns |
| 200 | + ------- |
| 201 | + Optional[:class:`Component`] |
| 202 | + The component with the matching ``id`` or ``custom_id`` if it exists. |
| 203 | + """ |
| 204 | + if not id: |
| 205 | + return None |
| 206 | + attr = "id" if isinstance(id, int) else "custom_id" |
| 207 | + return find(lambda i: getattr(i, attr, None) == id, self.children) |
| 208 | + |
190 | 209 | @classmethod
|
191 | 210 | def with_components(cls, *components, id=None):
|
192 | 211 | return cls._raw_construct(
|
@@ -620,6 +639,28 @@ def walk_components(self) -> Iterator[Component]:
|
620 | 639 | yield from r + [self.accessory]
|
621 | 640 | yield from r
|
622 | 641 |
|
| 642 | + def get_component(self, id: str | int) -> Component | None: |
| 643 | + """Get a component from this section. Roughly equivalent to `utils.get(section.walk_components(), ...)`. |
| 644 | + If an ``int`` is provided, the component will be retrieved by ``id``, otherwise by ``custom_id``. |
| 645 | +
|
| 646 | + Parameters |
| 647 | + ---------- |
| 648 | + id: Union[:class:`str`, :class:`int`] |
| 649 | + The custom_id or id of the component to get. |
| 650 | +
|
| 651 | + Returns |
| 652 | + ------- |
| 653 | + Optional[:class:`Component`] |
| 654 | + The component with the matching ``id`` or ``custom_id`` if it exists. |
| 655 | + """ |
| 656 | + if not id: |
| 657 | + return None |
| 658 | + attr = "id" if isinstance(id, int) else "custom_id" |
| 659 | + if self.accessory and id == getattr(self.accessory, attr, None): |
| 660 | + return self.accessory |
| 661 | + component = find(lambda i: getattr(i, attr, None) == id, self.components) |
| 662 | + return component |
| 663 | + |
623 | 664 |
|
624 | 665 | class TextDisplay(Component):
|
625 | 666 | """Represents a Text Display from Components V2.
|
@@ -1036,6 +1077,32 @@ def walk_components(self) -> Iterator[Component]:
|
1036 | 1077 | else:
|
1037 | 1078 | yield c
|
1038 | 1079 |
|
| 1080 | + def get_component(self, id: str | int) -> Component | None: |
| 1081 | + """Get a component from this container. Roughly equivalent to `utils.get(container.components, ...)`. |
| 1082 | + If an ``int`` is provided, the component will be retrieved by ``id``, otherwise by ``custom_id``. |
| 1083 | + This method will also search for nested components. |
| 1084 | +
|
| 1085 | + Parameters |
| 1086 | + ---------- |
| 1087 | + id: Union[:class:`str`, :class:`int`] |
| 1088 | + The custom_id or id of the component to get. |
| 1089 | +
|
| 1090 | + Returns |
| 1091 | + ------- |
| 1092 | + Optional[:class:`Component`] |
| 1093 | + The component with the matching ``id`` or ``custom_id`` if it exists. |
| 1094 | + """ |
| 1095 | + if not id: |
| 1096 | + return None |
| 1097 | + attr = "id" if isinstance(id, int) else "custom_id" |
| 1098 | + component = find(lambda i: getattr(i, attr, None) == id, self.components) |
| 1099 | + if not component: |
| 1100 | + for i in self.components: |
| 1101 | + if hasattr(i, "get_component"): |
| 1102 | + if component := i.get_component(id): |
| 1103 | + return component |
| 1104 | + return component |
| 1105 | + |
1039 | 1106 |
|
1040 | 1107 | COMPONENT_MAPPINGS = {
|
1041 | 1108 | 1: ActionRow,
|
|
0 commit comments