Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement (Sync)WebhookMessage.thread #2314

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
28 changes: 11 additions & 17 deletions discord/webhook/async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
from ..object import Object
from ..threads import Thread
from ..user import BaseUser, User
from ..utils import cached_property

__all__ = (
"Webhook",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -921,7 +922,7 @@ async def edit(
attachments=attachments,
view=view,
allowed_mentions=allowed_mentions,
thread=thread,
thread=self.thread,
suppress=suppress,
)

Expand All @@ -945,26 +946,21 @@ 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:

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):
Expand Down Expand Up @@ -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

Expand Down
25 changes: 10 additions & 15 deletions discord/webhook/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = (
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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

Expand Down
Loading