From 4197f4e22b4b6c3eb2e6397e53daff8c222b79a8 Mon Sep 17 00:00:00 2001 From: Khakers <22665282+khakers@users.noreply.github.com> Date: Fri, 25 Aug 2023 22:54:34 -0700 Subject: [PATCH] Rewrite _find_from_channel method to use database as source instead of parsing channel topics --- core/thread.py | 52 ++++++++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/core/thread.py b/core/thread.py index 1e8adf0b89..9e04d7619f 100644 --- a/core/thread.py +++ b/core/thread.py @@ -1375,44 +1375,34 @@ def check(topic): return thread - async def _find_from_channel(self, channel): + async def _find_from_channel(self, channel) -> typing.Optional[Thread]: """ - Tries to find a thread from a channel channel topic, - if channel topic doesnt exist for some reason, falls back to + Tries to find a thread from a channel topic, + if channel topic doesn't exist for some reason, falls back to searching channel history for genesis embed and extracts user_id from that. """ - - if not channel.topic: - return None - - _, user_id, other_ids = parse_channel_topic(channel.topic) - - if user_id == -1: + + logger.debug("_find_from_channel") + logger.debug(f"channel: {channel}") + + # TODO cache thread for channel ID + + log = await self.bot.api.get_log(channel.id) + + if log is None: return None - if user_id in self.cache: - return self.cache[user_id] - - try: - recipient = await self.bot.get_or_fetch_user(user_id) - except discord.NotFound: - recipient = None - - other_recipients = [] - for uid in other_ids: - try: - other_recipient = await self.bot.get_or_fetch_user(uid) - except discord.NotFound: - continue - other_recipients.append(other_recipient) - - if recipient is None: - thread = Thread(self, user_id, channel, other_recipients) - else: - self.cache[user_id] = thread = Thread(self, recipient, channel, other_recipients) + logger.debug("This is a thread channel") + + recipients = log["other_recipients"] + # Create a list of tasks to fetch the users + tasks = [self.bot.get_or_fetch_user(user_data["id"]) for user_data in recipients] + # Fetch the users + recipient_users: list[discord.Member] = await asyncio.gather(*tasks) + + thread = Thread(self, recipient=log["creator"]["id"], channel=channel, other_recipients=recipient_users) thread.ready = True - return thread async def create(