Skip to content

Commit

Permalink
Add thread getters to Message
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed Dec 10, 2023
1 parent 2b51e5e commit 29344b9
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
79 changes: 79 additions & 0 deletions discord/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,20 @@ def jump_url(self) -> str:
guild_id = getattr(self.guild, 'id', '@me')
return f'https://discord.com/channels/{guild_id}/{self.channel.id}/{self.id}'

@property
def thread(self) -> Optional[Thread]:
"""Optional[:class:`Thread`]: The public thread created from this message, if it exists.
.. note::
This does not retrieve archived threads, as they are not retained in the internal
cache. Use :meth:`fetch_thread` instead.
.. versionadded:: 2.4
"""
if self.guild is not None:
return self.guild.get_thread(self.id)

async def fetch(self) -> Message:
"""|coro|
Expand Down Expand Up @@ -1280,6 +1294,40 @@ async def create_thread(
)
return Thread(guild=self.guild, state=self._state, data=data)

async def fetch_thread(self) -> Thread:
"""|coro|
Retrieves the public thread attached to this message.
.. note::
This method is an API call. For general usage, consider :attr:`thread` instead.
.. versionadded:: 2.4
Raises
-------
InvalidData
An unknown channel type was received from Discord
or the guild the thread belongs to is not the same
as the one in this object points to.
HTTPException
Retrieving the thread failed.
NotFound
There is no thread attached to this message.
Forbidden
You do not have permission to fetch this channel.
Returns
--------
:class:`.Thread`
The public thread attached to this message.
"""
if self.guild is None:
raise ValueError('This message does not have guild info attached.')

return await self.guild.fetch_channel(self.id) # type: ignore # Can only be Thread in this case

@overload
async def reply(
self,
Expand Down Expand Up @@ -1572,6 +1620,7 @@ class Message(PartialMessage, Hashable):
'_cs_raw_channel_mentions',
'_cs_raw_role_mentions',
'_cs_system_content',
'_thread',
'tts',
'content',
'webhook_id',
Expand Down Expand Up @@ -1640,6 +1689,21 @@ def __init__(
except AttributeError:
self.guild = state._get_guild(utils._get_as_snowflake(data, 'guild_id'))

self._thread: Optional[Thread] = None

if self.guild is not None:
try:
thread = data['thread']
except KeyError:
pass
else:
self._thread = self.guild.get_thread(int(thread['id']))

if self._thread is not None:
self._thread._update(thread)
else:
self._thread = Thread(guild=self.guild, state=state, data=thread)

self.interaction: Optional[MessageInteraction] = None

try:
Expand Down Expand Up @@ -1982,6 +2046,21 @@ def edited_at(self) -> Optional[datetime.datetime]:
"""Optional[:class:`datetime.datetime`]: An aware UTC datetime object containing the edited time of the message."""
return self._edited_timestamp

@property
def thread(self) -> Optional[Thread]:
"""Optional[:class:`Thread`]: The public thread created from this message, if it exists.
.. note::
For messages received via the gateway this does not retrieve archived threads, as they
are not retained in the internal cache. Use :meth:`fetch_thread` instead.
.. versionadded:: 2.4
"""
if self.guild is not None:
# Fall back to guild threads in case one was created after the message
return self._thread or self.guild.get_thread(self.id)

def is_system(self) -> bool:
""":class:`bool`: Whether the message is a system message.
Expand Down
2 changes: 2 additions & 0 deletions discord/types/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from .components import Component
from .interactions import MessageInteraction
from .sticker import StickerItem
from .threads import Thread


class PartialMessage(TypedDict):
Expand Down Expand Up @@ -146,6 +147,7 @@ class Message(PartialMessage):
components: NotRequired[List[Component]]
position: NotRequired[int]
role_subscription_data: NotRequired[RoleSubscriptionData]
thread: NotRequired[Thread]


AllowedMentionType = Literal['roles', 'users', 'everyone']
Expand Down

0 comments on commit 29344b9

Please sign in to comment.