Browse Source

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.
pull/9457/head
Rapptz 2 years ago
parent
commit
49e31e9e23
  1. 33
      discord/interactions.py

33
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.

Loading…
Cancel
Save