Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 🐛 Fix Interaction.channel incorrectly set #2658

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ These changes are available on the `master` branch, but have not yet been releas
apps. ([#2650](https://github.com/Pycord-Development/pycord/pull/2650))
- Fixed type annotations of cached properties.
([#2635](https://github.com/Pycord-Development/pycord/issues/2635))
- Fixed malformed properties in `Interaction.channel`.
([#2658](https://github.com/Pycord-Development/pycord/pull/2658))

### Changed

Expand Down
48 changes: 30 additions & 18 deletions discord/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class Interaction:
The interaction type.
guild_id: Optional[:class:`int`]
The guild ID the interaction was sent from.
channel: Optional[Union[:class:`abc.GuildChannel`, :class:`abc.PrivateChannel`, :class:`Thread`]]
channel: Optional[Union[:class:`abc.GuildChannel`, :class:`abc.PrivateChannel`, :class:`Thread`, :class:`PartialMessageable`]]
The channel the interaction was sent from.
channel_id: Optional[:class:`int`]
The ID of the channel the interaction was sent from.
Expand Down Expand Up @@ -261,20 +261,32 @@ def _from_data(self, data: InteractionPayload):
except KeyError:
pass

if channel := data.get("channel"):
if (ch_type := channel.get("type")) is not None:
factory, ch_type = _threaded_channel_factory(ch_type)
channel = data.get("channel")
data_ch_type: int | None = None
if channel:
data_ch_type = channel.get("type")

if data_ch_type is not None:
factory, ch_type = _threaded_channel_factory(data_ch_type)
if ch_type in (ChannelType.group, ChannelType.private):
self.channel = factory(me=self.user, data=channel, state=self._state)
elif self.guild:
self.channel = self.guild._resolve_channel(self.channel_id) or factory(
guild=self.guild, state=self._state, data=channel
)

if ch_type in (ChannelType.group, ChannelType.private):
self.channel = factory(
me=self.user, data=channel, state=self._state
)
elif self.guild:
self.channel = factory(
guild=self.guild, state=self._state, data=channel
)
else:
self.channel = self.cached_channel
if self.channel is None and self.guild:
self.channel = self.guild._resolve_channel(self.channel_id)
if self.channel is None:
if self.channel_id is not None:
ch_type = (
ChannelType.text
if self.guild_id is not None
else ChannelType.private
)
return PartialMessageable(
state=self._state, id=self.channel_id, type=ch_type
)

self._channel_data = channel

Expand Down Expand Up @@ -306,12 +318,12 @@ def is_component(self) -> bool:
return self.type == InteractionType.component

@utils.cached_slot_property("_cs_channel")
@utils.deprecated("Interaction.channel", "2.7", stacklevel=4)
def cached_channel(self) -> InteractionChannel | None:
"""The channel the
interaction was sent from.
"""The cached channel from which the interaction was sent.
DM channels are not resolved. These are :class:`PartialMessageable` instead.

Note that due to a Discord limitation, DM channels are not resolved since there is
no data to complete them. These are :class:`PartialMessageable` instead.
.. deprecated:: 2.7
"""
guild = self.guild
channel = guild and guild._resolve_channel(self.channel_id)
Expand Down
Loading