diff --git a/discord/message.py b/discord/message.py index e1633aa97e..c9ce91088e 100644 --- a/discord/message.py +++ b/discord/message.py @@ -1201,13 +1201,16 @@ def __init__( except KeyError: self._poll = None - self.thread: Thread | None - try: - self.thread = Thread( - guild=self.guild, state=self._state, data=data["thread"] + self.thread: Thread | None = None + if thread_data := data.get("thread"): + # When fetching from a PartialMessageable we don't have a guild so we need to fallback here + guild = self.guild or state._get_guild( + utils._get_as_snowflake(thread_data, "guild_id") ) - except KeyError: - self.thread = None + if guild is not None: + self.thread = guild.get_thread(int(thread_data["id"])) or Thread( + guild=guild, state=self._state, data=thread_data + ) self.call: MessageCall | None try: diff --git a/discord/threads.py b/discord/threads.py index 7d73331c1f..5533a28a8e 100644 --- a/discord/threads.py +++ b/discord/threads.py @@ -208,7 +208,11 @@ def _from_data(self, data: ThreadPayload): ] # Here, we try to fill in potentially missing data - if thread := self.guild.get_thread(self.id) and data.pop("_invoke_flag", False): + if ( + self.guild + and (thread := self.guild.get_thread(self.id)) + and data.pop("_invoke_flag", False) + ): self.owner_id = thread.owner_id if self.owner_id is None else self.owner_id self.last_message_id = ( thread.last_message_id