Skip to content

Commit 33d1744

Browse files
NeloBlivionpre-commit-ci[bot]Lulalaby
authored
feat: Move ctx.respond to Interaction and implement Interaction.edit (#2026)
Signed-off-by: Lala Sabathil <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Lala Sabathil <[email protected]>
1 parent 0f7b46e commit 33d1744

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ These changes are available on the `master` branch, but have not yet been releas
3737
- Added the `data` attribute to all
3838
[Raw Event payloads](https://docs.pycord.dev/en/master/api/models.html#events).
3939
([#2023](https://github.com/Pycord-Development/pycord/pull/2023))
40+
- Added `Interaction.respond` and `Interaction.edit` as shortcut responses.
41+
([#2026](https://github.com/Pycord-Development/pycord/pull/2026))
4042

4143
### Changed
4244

discord/commands/context.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -266,26 +266,12 @@ def unselected_options(self) -> list[Option] | None:
266266
def send_modal(self) -> Callable[..., Awaitable[Interaction]]:
267267
return self.interaction.response.send_modal
268268

269-
async def respond(self, *args, **kwargs) -> Interaction | WebhookMessage:
270-
"""|coro|
271-
272-
Sends either a response or a message using the followup webhook determined by whether the interaction
273-
has been responded to or not.
274-
275-
Returns
276-
-------
277-
Union[:class:`discord.Interaction`, :class:`discord.WebhookMessage`]:
278-
The response, its type depending on whether it's an interaction response or a followup.
279-
"""
280-
try:
281-
if not self.interaction.response.is_done():
282-
return await self.interaction.response.send_message(
283-
*args, **kwargs
284-
) # self.response
285-
else:
286-
return await self.followup.send(*args, **kwargs) # self.send_followup
287-
except discord.errors.InteractionResponded:
288-
return await self.followup.send(*args, **kwargs)
269+
@property
270+
@discord.utils.copy_doc(Interaction.respond)
271+
def respond(
272+
self, *args, **kwargs
273+
) -> Callable[..., Awaitable[Interaction | WebhookMessage]]:
274+
return self.interaction.respond
289275

290276
@property
291277
@discord.utils.copy_doc(InteractionResponse.send_message)

discord/interactions.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@
3838
from .object import Object
3939
from .permissions import Permissions
4040
from .user import User
41-
from .webhook.async_ import Webhook, async_context, handle_message_parameters
41+
from .webhook.async_ import (
42+
Webhook,
43+
WebhookMessage,
44+
async_context,
45+
handle_message_parameters,
46+
)
4247

4348
__all__ = (
4449
"Interaction",
@@ -519,6 +524,44 @@ async def delete_original_message(self, **kwargs):
519524
"""
520525
return await self.delete_original_response(**kwargs)
521526

527+
async def respond(self, *args, **kwargs) -> Interaction | WebhookMessage:
528+
"""|coro|
529+
530+
Sends either a response or a message using the followup webhook determined by whether the interaction
531+
has been responded to or not.
532+
533+
Returns
534+
-------
535+
Union[:class:`discord.Interaction`, :class:`discord.WebhookMessage`]:
536+
The response, its type depending on whether it's an interaction response or a followup.
537+
"""
538+
try:
539+
if not self.response.is_done():
540+
return await self.response.send_message(*args, **kwargs)
541+
else:
542+
return await self.followup.send(*args, **kwargs)
543+
except InteractionResponded:
544+
return await self.followup.send(*args, **kwargs)
545+
546+
async def edit(self, *args, **kwargs) -> InteractionMessage | None:
547+
"""|coro|
548+
549+
Either respond to the interaction with an edit_message or edits the existing response, determined by
550+
whether the interaction has been responded to or not.
551+
552+
Returns
553+
-------
554+
Union[:class:`discord.InteractionMessage`, :class:`discord.WebhookMessage`]:
555+
The response, its type depending on whether it's an interaction response or a followup.
556+
"""
557+
try:
558+
if not self.response.is_done():
559+
return await self.response.edit_message(*args, **kwargs)
560+
else:
561+
return await self.edit_original_response(*args, **kwargs)
562+
except InteractionResponded:
563+
return await self.edit_original_response(*args, **kwargs)
564+
522565
def to_dict(self) -> dict[str, Any]:
523566
"""
524567
Converts this interaction object into a dict.

0 commit comments

Comments
 (0)