Skip to content

Commit 15898dc

Browse files
committed
✨ Add ModalInteraction.attachments with resolved attachments from modal file uploads
Signed-off-by: Paillat-dev <[email protected]>
1 parent e6b47e0 commit 15898dc

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

discord/interactions.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
import asyncio
2929
import datetime
3030
from collections.abc import Sequence
31-
from typing import TYPE_CHECKING, Any, Coroutine, Generic, Union, Unpack, cast
31+
from typing import TYPE_CHECKING, Any, Coroutine, Generic, cast
3232

33-
from typing_extensions import TypeVar, TypeVarTuple
33+
from typing_extensions import TypeVar, TypeVarTuple, Unpack
3434

3535
from . import utils
3636
from .channel import PartialMessageable, _threaded_channel_factory
@@ -45,6 +45,7 @@
4545
from .monetization import Entitlement
4646
from .object import Object
4747
from .permissions import Permissions
48+
from .role import Role
4849
from .user import User
4950
from .utils.private import cached_slot_property, delay_task, deprecated, get_as_snowflake
5051
from .webhook.async_ import (
@@ -92,6 +93,7 @@
9293
from .types.interactions import Interaction as InteractionPayload
9394
from .types.interactions import InteractionData
9495
from .types.interactions import InteractionMetadata as InteractionMetadataPayload
96+
from .types.interactions import ModalInteraction as ModalInteractionPayload
9597
from .types.partial_components import PartialComponent
9698

9799
InteractionChannel = (
@@ -664,7 +666,18 @@ def to_dict(self) -> dict[str, Any]:
664666

665667

666668
class ModalInteraction(Interaction, Generic[Unpack[Components_t]]):
667-
__slots__ = ("_components",)
669+
__slots__ = ("_components", "users", "attachments")
670+
671+
def __init__(self, *, data: ModalInteractionPayload, state: ConnectionState):
672+
super().__init__(data=data, state=state)
673+
resolved = data.get("data", {}).get("resolved", {})
674+
self.users: dict[int, User] = {int(user_id): User(state=state, data=user_data) for user_id, user_data in resolved.get("users", {}).items()}
675+
self.attachments: dict[int, Attachment] = {int(att_id): Attachment(state=state, data=att_data) for att_id, att_data in resolved.get("attachments", {}).items()}
676+
self.roles: dict[int, Role] = {
677+
int(role_id): Role(state=state, data=role_data, guild=self.guild) for role_id, role_data in resolved.get("roles", {}).items()
678+
}
679+
680+
# TODO: When we have better partial objects, add self.channels and self.members
668681

669682
@cached_slot_property("_components")
670683
def components(self) -> ComponentsHolder[Unpack[Components_t]]:

discord/types/interactions.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from __future__ import annotations
2727

28-
from typing import TYPE_CHECKING, Dict, Literal, Union
28+
from typing import TYPE_CHECKING, Dict, Generic, Literal, TypeAlias, TypeVar, Union
2929

3030
from ..permissions import Permissions
3131
from .channel import ChannelType
@@ -189,11 +189,17 @@ class ComponentInteractionData(TypedDict):
189189
component_type: ComponentType
190190

191191

192-
InteractionData = ApplicationCommandInteractionData | ComponentInteractionData
192+
class ModalInteractionData(TypedDict):
193+
custom_id: str
194+
components: list[Component]
195+
resolved: NotRequired[ApplicationCommandInteractionDataResolved]
193196

197+
InteractionData = ApplicationCommandInteractionData | ComponentInteractionData | ModalInteractionData
194198

195-
class Interaction(TypedDict):
196-
data: NotRequired[InteractionData]
199+
D = TypeVar("D", bound=InteractionData)
200+
201+
class Interaction(TypedDict, Generic[D]):
202+
data: NotRequired[D]
197203
guild_id: NotRequired[Snowflake]
198204
channel_id: NotRequired[Snowflake]
199205
channel: NotRequired[InteractionChannel]
@@ -213,6 +219,8 @@ class Interaction(TypedDict):
213219
authorizing_integration_owners: AuthorizingIntegrationOwners
214220
context: InteractionContextType
215221

222+
ModalInteraction: TypeAlias = Interaction[ModalInteractionData]
223+
216224

217225
class InteractionMetadata(TypedDict):
218226
id: Snowflake

discord/types/message.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class Attachment(TypedDict):
8181
waveform: NotRequired[str]
8282
flags: NotRequired[int]
8383
title: NotRequired[str]
84+
ephemeral: NotRequired[bool]
8485

8586

8687
MessageActivityType = Literal[1, 2, 3, 5]

0 commit comments

Comments
 (0)