Skip to content

Commit b4a1bc9

Browse files
feat!: Add view.parent and store view.message on receiving Interaction for a component (#2036)
Signed-off-by: Om <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 71944ef commit b4a1bc9

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ These changes are available on the `master` branch, but have not yet been releas
4646
([#2030](https://github.com/Pycord-Development/pycord/pull/2030))
4747
- Added `Interaction.respond` and `Interaction.edit` as shortcut responses.
4848
([#2026](https://github.com/Pycord-Development/pycord/pull/2026))
49+
- Added `view.parent` which is set when the view was sent by
50+
`interaction.response.send_message`.
51+
([#2036](https://github.com/Pycord-Development/pycord/pull/2036))
4952

5053
### Changed
5154

@@ -57,11 +60,16 @@ These changes are available on the `master` branch, but have not yet been releas
5760
`GroupChannel`. ([#2025](https://github.com/Pycord-Development/pycord/pull/2025))
5861
- `DMChannel.recipients` can now be `None`
5962
([#2025](https://github.com/Pycord-Development/pycord/pull/2025))
63+
- Store `view.message` on receiving Interaction for a component.
64+
([#2036](https://github.com/Pycord-Development/pycord/pull/2036))
6065

6166
### Removed
6267

6368
- Removed `@client.once()` in favour of `@client.listen(once=True)`.
6469
([#1957](https://github.com/Pycord-Development/pycord/pull/1957))
70+
- Removed `view.message` being set when the view was sent by
71+
`interaction.response.send_message`.
72+
([#2036](https://github.com/Pycord-Development/pycord/pull/2036))
6573

6674
### Fixed
6775

discord/interactions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ async def send_message(
913913
if ephemeral and view.timeout is None:
914914
view.timeout = 15 * 60.0
915915

916-
view.message = await self._parent.original_response()
916+
view.parent = self._parent
917917
self._parent._state.store_view(view)
918918

919919
self._responded = True

discord/ui/view.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ class View:
141141
message: Optional[:class:`.Message`]
142142
The message that this view is attached to.
143143
If ``None`` then the view has not been sent with a message.
144+
parent: Optional[:class:`.Interaction`]
145+
The parent interaction which this view was sent from.
146+
If ``None`` then the view was not sent using :meth:`InteractionResponse.send_message`.
144147
"""
145148

146149
__discord_ui_view__: ClassVar[bool] = True
@@ -187,6 +190,7 @@ def __init__(
187190
self.__timeout_task: asyncio.Task[None] | None = None
188191
self.__stopped: asyncio.Future[bool] = loop.create_future()
189192
self._message: Message | InteractionMessage | None = None
193+
self.parent: Interaction | None = None
190194

191195
def __repr__(self) -> str:
192196
return f"<{self.__class__.__name__} timeout={self.timeout} children={len(self.children)}>"
@@ -363,9 +367,12 @@ async def on_timeout(self) -> None:
363367
A callback that is called when a view's timeout elapses without being explicitly stopped.
364368
"""
365369
if self.disable_on_timeout:
366-
if self._message:
367-
self.disable_all_items()
368-
await self._message.edit(view=self)
370+
self.disable_all_items()
371+
message = self._message or self.parent
372+
if message:
373+
m = await message.edit(view=self)
374+
if m:
375+
self._message = m
369376

370377
async def on_check_failure(self, interaction: Interaction) -> None:
371378
"""|coro|
@@ -438,6 +445,9 @@ def _dispatch_item(self, item: Item, interaction: Interaction):
438445
if self.__stopped.done():
439446
return
440447

448+
if interaction.message:
449+
self.message = interaction.message
450+
441451
asyncio.create_task(
442452
self._scheduled_task(item, interaction),
443453
name=f"discord-ui-view-dispatch-{self.id}",

0 commit comments

Comments
 (0)