From b4e39668fbb69b717aee417d55d5da68c7268365 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Thu, 8 Jul 2021 21:51:21 -0400 Subject: [PATCH] Change CHANNEL_PINS_UPDATE to use guild information from gateway This changes the channel lookup from O(n) to two amortised O(1) lookups --- discord/state.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/discord/state.py b/discord/state.py index f51c50372..e7150426f 100644 --- a/discord/state.py +++ b/discord/state.py @@ -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)