diff --git a/CHANGELOG.md b/CHANGELOG.md index 85a6af4a76..601ae796c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -84,6 +84,8 @@ These changes are available on the `master` branch, but have not yet been releas ([#2178](https://github.com/Pycord-Development/pycord/pull/2178)) - Added `applied_tags` parameter to `Webhook.send()` method. ([#2322](https://github.com/Pycord-Development/pycord/pull/2322)) +- Added properties `WebhookMessage.thread` and `SyncWebhookMessage.thread`. + ([#2314](https://github.com/Pycord-Development/pycord/pull/2314)) ### Changed @@ -215,6 +217,8 @@ These changes are available on the `master` branch, but have not yet been releas ([#2319](https://github.com/Pycord-Development/pycord/pull/2319)) - Fixed `AttributeError` when comparing application commands with non-command objects. ([#2299](https://github.com/Pycord-Development/pycord/issues/2299)) +- Fixed `TypeError` caused by `WebhookMessage._thread_id` being `None`. + ([#2314](https://github.com/Pycord-Development/pycord/pull/2314)) ## [2.4.1] - 2023-03-20 diff --git a/discord/webhook/async_.py b/discord/webhook/async_.py index 9c44c64d94..964e47e891 100644 --- a/discord/webhook/async_.py +++ b/discord/webhook/async_.py @@ -54,6 +54,7 @@ from ..object import Object from ..threads import Thread from ..user import BaseUser, User +from ..utils import cached_property __all__ = ( "Webhook", @@ -824,6 +825,13 @@ class WebhookMessage(Message): _state: _WebhookState + @cached_property + def thread(self) -> Thread | Object | None: + if isinstance(self.channel, Thread): + return self.channel + if self._thread_id: + return Object(self._thread_id) + async def edit( self, content: str | None = MISSING, @@ -897,13 +905,6 @@ async def edit( InvalidArgument There was no token associated with this webhook. """ - thread = MISSING - if hasattr(self, "_thread_id"): - thread = Object(self._thread_id) - elif isinstance(self.channel, Thread): - thread = Object(self.channel.id) - elif isinstance(self.channel, ForumChannel): - thread = Object(self.id) if attachments is MISSING: attachments = self.attachments or MISSING @@ -921,7 +922,7 @@ async def edit( attachments=attachments, view=view, allowed_mentions=allowed_mentions, - thread=thread, + thread=self.thread, suppress=suppress, ) @@ -945,11 +946,6 @@ async def delete(self, *, delay: float | None = None) -> None: HTTPException Deleting the message failed. """ - thread_id: int | None = None - if hasattr(self, "_thread_id"): - thread_id = self._thread_id - elif isinstance(self.channel, Thread): - thread_id = self.channel.id if delay is not None: @@ -957,14 +953,14 @@ async def inner_call(delay: float = delay): await asyncio.sleep(delay) try: await self._state._webhook.delete_message( - self.id, thread_id=thread_id + self.id, thread_id=self.thread.id ) except HTTPException: pass asyncio.create_task(inner_call()) else: - await self._state._webhook.delete_message(self.id, thread_id=thread_id) + await self._state._webhook.delete_message(self.id, thread_id=self.thread.id) class BaseWebhook(Hashable): @@ -1855,8 +1851,6 @@ async def fetch_message( thread_id=thread_id, ) msg = self._create_message(data) - if isinstance(msg.channel, PartialMessageable): - msg._thread_id = thread_id return msg diff --git a/discord/webhook/sync.py b/discord/webhook/sync.py index 9e812b5709..d095fc2a81 100644 --- a/discord/webhook/sync.py +++ b/discord/webhook/sync.py @@ -53,6 +53,7 @@ from ..message import Message from ..object import Object from ..threads import Thread +from ..utils import cached_property from .async_ import BaseWebhook, _WebhookState, handle_message_parameters __all__ = ( @@ -464,6 +465,13 @@ class SyncWebhookMessage(Message): _state: _WebhookState + @cached_property + def thread(self) -> Thread | Object | None: + if isinstance(self.channel, Thread): + return self.channel + if self._thread_id: + return Object(self._thread_id) + def edit( self, content: str | None = MISSING, @@ -514,11 +522,6 @@ def edit( InvalidArgument There was no token associated with this webhook. """ - thread = MISSING - if hasattr(self, "_thread_id"): - thread = Object(self._thread_id) - elif isinstance(self.channel, Thread): - thread = Object(self.channel.id) if suppress is MISSING: suppress = self.flags.suppress_embeds @@ -531,8 +534,8 @@ def edit( file=file, files=files, allowed_mentions=allowed_mentions, - thread=thread, suppress=suppress, + thread=self.thread, ) def delete(self, *, delay: float | None = None) -> None: @@ -554,16 +557,10 @@ def delete(self, *, delay: float | None = None) -> None: Deleting the message failed. """ - thread_id: int | None = None - if hasattr(self, "_thread_id"): - thread_id = self._thread_id - elif isinstance(self.channel, Thread): - thread_id = self.channel.id - if delay is not None: time.sleep(delay) - self._state._webhook.delete_message(self.id, thread_id=thread_id) + self._state._webhook.delete_message(self.id, thread_id=self.thread.id) class SyncWebhook(BaseWebhook): @@ -1147,8 +1144,6 @@ def fetch_message( thread_id=thread_id, ) msg = self._create_message(data) - if isinstance(msg.channel, PartialMessageable): - msg._thread_id = thread_id return msg