Change CHANNEL_PINS_UPDATE to use guild information from gateway

This changes the channel lookup from O(n) to two amortised O(1) lookups
This commit is contained in:
Rapptz 2021-07-08 21:51:21 -04:00
parent af8742a911
commit b4e39668fb

View File

@ -702,19 +702,21 @@ class ConnectionState:
def parse_channel_pins_update(self, data):
channel_id = int(data['channel_id'])
channel = self.get_channel(channel_id)
try:
guild = self._get_guild(int(data['guild_id']))
except KeyError:
guild = None
channel = self._get_private_channel(channel_id)
else:
channel = guild and guild._resolve_channel(channel_id)
if channel is None:
log.debug('CHANNEL_PINS_UPDATE referencing an unknown channel ID: %s. Discarding.', channel_id)
return
last_pin = utils.parse_time(data['last_pin_timestamp']) if data['last_pin_timestamp'] else None
try:
# I have not imported discord.abc in this file
# the isinstance check is also 2x slower than just checking this attribute
# so we're just gonna check it since it's easier and faster and lazier
channel.guild
except AttributeError:
if guild is None:
self.dispatch('private_channel_pins_update', channel, last_pin)
else:
self.dispatch('guild_channel_pins_update', channel, last_pin)