Skip to content

Commit 6a72ff0

Browse files
NeloBlivionpre-commit-ci[bot]Lulalaby
authored
feat: Received Interactions come with a channel object (#2025)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Lala Sabathil <[email protected]>
1 parent 33d1744 commit 6a72ff0

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ These changes are available on the `master` branch, but have not yet been releas
4444

4545
- Changed file-upload size limit from 8 MB to 25 MB accordingly.
4646
([#2014](https://github.com/Pycord-Development/pycord/pull/2014))
47+
- `Interaction.channel` is received from the gateway, so it can now be `DMChannel` and
48+
`GroupChannel`. ([#2025](https://github.com/Pycord-Development/pycord/pull/2025))
49+
- `DMChannel.recipients` can now be `None`
50+
([#2025](https://github.com/Pycord-Development/pycord/pull/2025))
4751

4852
### Removed
4953

discord/channel.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2838,7 +2838,9 @@ def __init__(
28382838
self, *, me: ClientUser, state: ConnectionState, data: DMChannelPayload
28392839
):
28402840
self._state: ConnectionState = state
2841-
self.recipient: User | None = state.store_user(data["recipients"][0])
2841+
self.recipient: User | None = None
2842+
if r := data.get("recipients"):
2843+
self.recipient: state.store_user(r[0])
28422844
self.me: ClientUser = me
28432845
self.id: int = int(data["id"])
28442846

discord/interactions.py

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from typing import TYPE_CHECKING, Any, Coroutine, Union
3030

3131
from . import utils
32-
from .channel import ChannelType, PartialMessageable
32+
from .channel import ChannelType, PartialMessageable, _threaded_channel_factory
3333
from .enums import InteractionResponseType, InteractionType, try_enum
3434
from .errors import ClientException, InteractionResponded, InvalidArgument
3535
from .file import File
@@ -57,7 +57,9 @@
5757

5858
from .channel import (
5959
CategoryChannel,
60+
DMChannel,
6061
ForumChannel,
62+
GroupChannel,
6163
StageChannel,
6264
TextChannel,
6365
VoiceChannel,
@@ -82,6 +84,8 @@
8284
ForumChannel,
8385
CategoryChannel,
8486
Thread,
87+
DMChannel,
88+
GroupChannel,
8589
PartialMessageable,
8690
]
8791

@@ -104,8 +108,10 @@ class Interaction:
104108
The interaction type.
105109
guild_id: Optional[:class:`int`]
106110
The guild ID the interaction was sent from.
111+
channel: Optional[Union[:class:`abc.GuildChannel`, :class:`abc.PrivateChannel`, :class:`Thread`]]
112+
The channel the interaction was sent from.
107113
channel_id: Optional[:class:`int`]
108-
The channel ID the interaction was sent from.
114+
The ID of the channel the interaction was sent from.
109115
application_id: :class:`int`
110116
The application ID that the interaction was for.
111117
user: Optional[Union[:class:`User`, :class:`Member`]]
@@ -129,6 +135,7 @@ class Interaction:
129135
"id",
130136
"type",
131137
"guild_id",
138+
"channel",
132139
"channel_id",
133140
"data",
134141
"application_id",
@@ -139,6 +146,7 @@ class Interaction:
139146
"token",
140147
"version",
141148
"custom_id",
149+
"_channel_data",
142150
"_message_data",
143151
"_permissions",
144152
"_app_permissions",
@@ -174,13 +182,7 @@ def _from_data(self, data: InteractionPayload):
174182
self._app_permissions: int = int(data.get("app_permissions", 0))
175183

176184
self.message: Message | None = None
177-
178-
if message_data := data.get("message"):
179-
self.message = Message(
180-
state=self._state, channel=self.channel, data=message_data
181-
)
182-
183-
self._message_data = message_data
185+
self.channel = None
184186

185187
self.user: User | Member | None = None
186188
self._permissions: int = 0
@@ -211,6 +213,30 @@ def _from_data(self, data: InteractionPayload):
211213
except KeyError:
212214
pass
213215

216+
if channel := data.get("channel"):
217+
if (ch_type := channel.get("type")) is not None:
218+
factory, ch_type = _threaded_channel_factory(ch_type)
219+
220+
if ch_type in (ChannelType.group, ChannelType.private):
221+
self.channel = factory(
222+
me=self.user, data=channel, state=self._state
223+
)
224+
elif self.guild:
225+
self.channel = factory(
226+
guild=self.guild, state=self._state, data=channel
227+
)
228+
else:
229+
self.channel = self.cached_channel
230+
231+
self._channel_data = channel
232+
233+
if message_data := data.get("message"):
234+
self.message = Message(
235+
state=self._state, channel=self.channel, data=message_data
236+
)
237+
238+
self._message_data = message_data
239+
214240
@property
215241
def client(self) -> Client:
216242
"""Returns the client that sent the interaction."""
@@ -230,7 +256,7 @@ def is_component(self) -> bool:
230256
return self.type == InteractionType.component
231257

232258
@utils.cached_slot_property("_cs_channel")
233-
def channel(self) -> InteractionChannel | None:
259+
def cached_channel(self) -> InteractionChannel | None:
234260
"""The channel the
235261
interaction was sent from.
236262

discord/types/interactions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
if TYPE_CHECKING:
4141
from .message import AllowedMentions, Message
42+
from ..interactions import InteractionChannel
4243

4344
from .._typed_dict import NotRequired, TypedDict
4445

@@ -196,6 +197,7 @@ class Interaction(TypedDict):
196197
data: NotRequired[InteractionData]
197198
guild_id: NotRequired[Snowflake]
198199
channel_id: NotRequired[Snowflake]
200+
channel: NotRequired[InteractionChannel]
199201
member: NotRequired[Member]
200202
user: NotRequired[User]
201203
message: NotRequired[Message]

0 commit comments

Comments
 (0)