From 49e31e9e23517471252cd3f992fa3c88607741ff Mon Sep 17 00:00:00 2001 From: Rapptz Date: Sat, 17 Jun 2023 01:24:10 -0400 Subject: [PATCH] Use cache data if available for Interaction.channel The data from Discord does not contain all the attributes that the cached data has. There may be a slight chance that the interaction data is more up to date than the cached data or vice versa but more information tends to trump over this slight chance. --- discord/interactions.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/discord/interactions.py b/discord/interactions.py index 44645396e..1cf029451 100644 --- a/discord/interactions.py +++ b/discord/interactions.py @@ -184,22 +184,6 @@ class Interaction(Generic[ClientT]): self.version: int = data['version'] self.guild_id: Optional[int] = utils._get_as_snowflake(data, 'guild_id') self.channel: Optional[InteractionChannel] = None - - raw_channel = data.get('channel', {}) - raw_ch_type = raw_channel.get('type') - if raw_ch_type is not None: - factory, ch_type = _threaded_channel_factory(raw_ch_type) # type is never None - if factory is None: - logging.info('Unknown channel type {type} for channel ID {id}.'.format_map(raw_channel)) - else: - if ch_type in (ChannelType.group, ChannelType.private): - channel = factory(me=self._client.user, data=raw_channel, state=self._state) # type: ignore - else: - guild = self._state._get_or_create_unavailable_guild(self.guild_id) # type: ignore - channel = factory(guild=guild, state=self._state, data=raw_channel) # type: ignore - - self.channel = channel - self.application_id: int = int(data['application_id']) self.locale: Locale = try_enum(Locale, data.get('locale', 'en-US')) @@ -220,6 +204,7 @@ class Interaction(Generic[ClientT]): self._permissions: int = 0 self._app_permissions: int = int(data.get('app_permissions', 0)) + guild = None if self.guild_id: guild = self._state._get_or_create_unavailable_guild(self.guild_id) @@ -240,6 +225,22 @@ class Interaction(Generic[ClientT]): except KeyError: pass + raw_channel = data.get('channel', {}) + channel_id = utils._get_as_snowflake(raw_channel, 'id') + if channel_id is not None and guild is not None: + self.channel = guild and guild._resolve_channel(channel_id) + + raw_ch_type = raw_channel.get('type') + if self.channel is None and raw_ch_type is not None: + factory, ch_type = _threaded_channel_factory(raw_ch_type) # type is never None + if factory is None: + logging.info('Unknown channel type {type} for channel ID {id}.'.format_map(raw_channel)) + else: + if ch_type in (ChannelType.group, ChannelType.private): + self.channel = factory(me=self._client.user, data=raw_channel, state=self._state) # type: ignore + elif guild is not None: + self.channel = factory(guild=guild, state=self._state, data=raw_channel) # type: ignore + @property def client(self) -> ClientT: """:class:`Client`: The client that is handling this interaction.