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.
This commit is contained in:
Rapptz 2023-06-17 01:24:10 -04:00
parent dc4ed438a5
commit 49e31e9e23

View File

@ -184,22 +184,6 @@ class Interaction(Generic[ClientT]):
self.version: int = data['version'] self.version: int = data['version']
self.guild_id: Optional[int] = utils._get_as_snowflake(data, 'guild_id') self.guild_id: Optional[int] = utils._get_as_snowflake(data, 'guild_id')
self.channel: Optional[InteractionChannel] = None 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.application_id: int = int(data['application_id'])
self.locale: Locale = try_enum(Locale, data.get('locale', 'en-US')) self.locale: Locale = try_enum(Locale, data.get('locale', 'en-US'))
@ -220,6 +204,7 @@ class Interaction(Generic[ClientT]):
self._permissions: int = 0 self._permissions: int = 0
self._app_permissions: int = int(data.get('app_permissions', 0)) self._app_permissions: int = int(data.get('app_permissions', 0))
guild = None
if self.guild_id: if self.guild_id:
guild = self._state._get_or_create_unavailable_guild(self.guild_id) guild = self._state._get_or_create_unavailable_guild(self.guild_id)
@ -240,6 +225,22 @@ class Interaction(Generic[ClientT]):
except KeyError: except KeyError:
pass 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 @property
def client(self) -> ClientT: def client(self) -> ClientT:
""":class:`Client`: The client that is handling this interaction. """:class:`Client`: The client that is handling this interaction.