Skip to content

Commit 6cccfdc

Browse files
committed
feat💥: make ModalContext able to handle new component types
This requires breaking the assumption that responses always has string keys. That being said, since this is Discord breaking things, not us, this should be fine for a non-breaking release.
1 parent 3c968e6 commit 6cccfdc

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

interactions/models/internal/context.py

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -925,19 +925,68 @@ def component(self) -> typing.Optional[BaseComponent]:
925925

926926

927927
class ModalContext(InteractionContext[ClientT]):
928-
responses: dict[str, str]
928+
responses: dict[str, typing.Any]
929929
"""The responses of the modal. The key is the `custom_id` of the component."""
930930
custom_id: str
931931
"""The developer defined custom ID of this modal"""
932932
edit_origin: bool
933933
"""Whether to edit the original message instead of sending a new one."""
934934

935935
@classmethod
936-
def from_dict(cls, client: "ClientT", payload: dict) -> Self:
936+
def from_dict(cls, client: "ClientT", payload: dict) -> Self: # noqa: C901
937937
instance = super().from_dict(client, payload)
938-
instance.responses = {
939-
comp["components"][0]["custom_id"]: comp["components"][0]["value"] for comp in payload["data"]["components"]
940-
}
938+
instance.responses = {}
939+
940+
for component in payload["data"]["components"]:
941+
if component["type"] == ComponentType.ACTION_ROW:
942+
instance.responses[component["components"][0]["custom_id"]] = component["components"][0]["value"]
943+
elif component["type"] == ComponentType.LABEL:
944+
held_component = component["component"]
945+
946+
if held_component["type"] == ComponentType.INPUT_TEXT:
947+
instance.responses[held_component["custom_id"]] = held_component["value"]
948+
elif held_component["type"] == ComponentType.STRING_SELECT:
949+
instance.responses[held_component["custom_id"]] = held_component["values"]
950+
elif held_component["type"] in (
951+
ComponentType.USER_SELECT,
952+
ComponentType.CHANNEL_SELECT,
953+
ComponentType.ROLE_SELECT,
954+
ComponentType.MENTIONABLE_SELECT,
955+
):
956+
searches = {
957+
"users": held_component["type"]
958+
in (ComponentType.USER_SELECT, ComponentType.MENTIONABLE_SELECT),
959+
"members": instance.guild_id
960+
and held_component["type"] in (ComponentType.USER_SELECT, ComponentType.MENTIONABLE_SELECT),
961+
"channels": held_component["type"]
962+
in (ComponentType.CHANNEL_SELECT, ComponentType.MENTIONABLE_SELECT),
963+
"roles": instance.guild_id
964+
and held_component["type"] in (ComponentType.ROLE_SELECT, ComponentType.MENTIONABLE_SELECT),
965+
}
966+
967+
values = held_component["values"]
968+
969+
for i, value in enumerate(held_component["values"]):
970+
if re.match(r"\d{17,}", value):
971+
key = Snowflake(value)
972+
973+
if resolved := instance.resolved.get(key):
974+
values[i] = resolved
975+
elif searches["members"] and (
976+
member := instance.client.cache.get_member(instance.guild_id, key)
977+
):
978+
values[i] = member
979+
elif searches["users"] and (user := instance.client.cache.get_user(key)):
980+
values[i] = user
981+
elif searches["roles"] and (role := instance.client.cache.get_role(key)):
982+
values[i] = role
983+
elif searches["channels"] and (channel := instance.client.cache.get_channel(key)):
984+
values[i] = channel
985+
986+
instance.responses[held_component["custom_id"]] = values
987+
else:
988+
raise ValueError(f"Unknown component type in modal: {held_component['type']}")
989+
941990
instance.kwargs = instance.responses
942991
instance.custom_id = payload["data"]["custom_id"]
943992
instance.edit_origin = False

0 commit comments

Comments
 (0)