Skip to content

Commit

Permalink
New discord.Thread additions (Pycord-Development#1447)
Browse files Browse the repository at this point in the history
* First additions

* Update state.py
  • Loading branch information
baronkobama authored Jun 30, 2022
1 parent 5fdbb3f commit 08d7f91
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
16 changes: 15 additions & 1 deletion discord/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,10 @@ class Message(Hashable):
The guild that the message belongs to, if applicable.
interaction: Optional[:class:`MessageInteraction`]
The interaction associated with the message, if applicable.
thread: Optional[:class:`Thread`]
The thread created from this message, if applicable.
.. versionadded:: 2.0
"""

__slots__ = (
Expand Down Expand Up @@ -691,6 +695,7 @@ class Message(Hashable):
"components",
"guild",
"interaction",
"thread",
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -766,6 +771,12 @@ def __init__(
except KeyError:
self.interaction = None

self.thread: Optional[Thread]
try:
self.thread = Thread(guild=self.guild, state=self._state, data=data["thread"])
except KeyError:
self.thread = None

for handler in ("author", "member", "mentions", "mention_roles"):
try:
getattr(self, f"_handle_{handler}")(data[handler])
Expand Down Expand Up @@ -1590,13 +1601,16 @@ async def create_thread(self, *, name: str, auto_archive_duration: ThreadArchive
default_auto_archive_duration: ThreadArchiveDuration = getattr(
self.channel, "default_auto_archive_duration", 1440
)

data = await self._state.http.start_thread_with_message(
self.channel.id,
self.id,
name=name,
auto_archive_duration=auto_archive_duration or default_auto_archive_duration,
)
return Thread(guild=self.guild, state=self._state, data=data)

self.thread = Thread(guild=self.guild, state=self._state, data=data)
return self.thread

async def reply(self, content: Optional[str] = None, **kwargs) -> Message:
"""|coro|
Expand Down
3 changes: 3 additions & 0 deletions discord/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,9 @@ def parse_thread_delete(self, data) -> None:
guild._remove_thread(thread) # type: ignore
self.dispatch("thread_delete", thread)

if (msg := thread.starting_message) is not None:
msg.thread = None

def parse_thread_list_sync(self, data) -> None:
guild_id = int(data["guild_id"])
guild: Optional[Guild] = self._get_guild(guild_id)
Expand Down
28 changes: 24 additions & 4 deletions discord/threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ class Thread(Messageable, Hashable):
The guild the thread belongs to.
id: :class:`int`
The thread ID.
.. note::
This ID is the same as the thread starting message ID.
parent_id: :class:`int`
The parent :class:`TextChannel` ID this thread belongs to.
owner_id: :class:`int`
Expand Down Expand Up @@ -260,7 +264,7 @@ def members(self) -> List[ThreadMember]:

@property
def last_message(self) -> Optional[Message]:
"""Fetches the last message from this channel in cache.
"""Returns the last message from this thread in cache.
The message might not be valid or point to an existing message.
Expand All @@ -273,7 +277,7 @@ def last_message(self) -> Optional[Message]:
attribute.
Returns
---------
--------
Optional[:class:`Message`]
The last message in this channel or ``None`` if not found.
"""
Expand All @@ -289,7 +293,7 @@ def category(self) -> Optional[CategoryChannel]:
The parent channel was not cached and returned ``None``.
Returns
-------
--------
Optional[:class:`CategoryChannel`]
The parent channel's category.
"""
Expand All @@ -309,7 +313,7 @@ def category_id(self) -> Optional[int]:
The parent channel was not cached and returned ``None``.
Returns
-------
--------
Optional[:class:`int`]
The parent channel's category ID.
"""
Expand All @@ -319,6 +323,22 @@ def category_id(self) -> Optional[int]:
raise ClientException("Parent channel not found")
return parent.category_id

@property
def starting_message(self) -> Optional[Message]:
"""Returns the message that started this thread.
The message might not be valid or point to an existing message.
.. note::
The ID for this message is the same as the thread ID.
Returns
--------
Optional[:class:`Message`]
The message that started this thread or ``None`` if not found in the cache.
"""
return self._state._get_message(self.id)

def is_private(self) -> bool:
""":class:`bool`: Whether the thread is a private thread.
Expand Down
2 changes: 2 additions & 0 deletions discord/types/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from .member import Member, UserWithMember
from .snowflake import Snowflake, SnowflakeList
from .sticker import StickerItem
from .threads import Thread
from .user import User

if TYPE_CHECKING:
Expand Down Expand Up @@ -110,6 +111,7 @@ class _MessageOptional(TypedDict, total=False):
referenced_message: Optional[Message]
interaction: MessageInteraction
components: List[Component]
thread: Optional[Thread]


MessageType = Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 18, 19, 20, 21]
Expand Down

0 comments on commit 08d7f91

Please sign in to comment.