From 09d147de9d1f3109392bf0e568b0e117c67c5f54 Mon Sep 17 00:00:00 2001 From: Paillat-dev Date: Sat, 29 Mar 2025 16:24:37 +0100 Subject: [PATCH] :sparkles: Update view to support ephemeral responses --- examples/counter.py | 4 ++-- src/pycord_reactive_views/view.py | 17 ++++++++++------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/examples/counter.py b/examples/counter.py index 774b28c..458a103 100644 --- a/examples/counter.py +++ b/examples/counter.py @@ -50,7 +50,7 @@ class Counter(ReactiveView): @bot.slash_command() async def counter(ctx: discord.ApplicationContext) -> None: """Send the counter view.""" - await Counter().send(ctx) + await Counter().send(ctx, ephemeral=True) -bot.run(os.getenv("TOKEN")) +bot.run(os.getenv("TOKEN_2")) diff --git a/src/pycord_reactive_views/view.py b/src/pycord_reactive_views/view.py index 06519fb..87e73ce 100644 --- a/src/pycord_reactive_views/view.py +++ b/src/pycord_reactive_views/view.py @@ -20,6 +20,7 @@ class ReactiveView(discord.ui.View): ) -> None: super().__init__(timeout=timeout, disable_on_timeout=disable_on_timeout) # pyright: ignore[reportUnknownMemberType] self._reactives: list[Reactive] = [] + self.ctx: discord.ApplicationContext | discord.Interaction | None = None @override def add_item(self, item: discord.ui.Item[Self]) -> None: @@ -48,16 +49,18 @@ class ReactiveView(discord.ui.View): """ for reactive in self._reactives: await reactive.refresh() - if not self.message: - raise ValueError("View has no message (not yet sent?), can't refresh") + editable = self.ctx or self.message + if not editable: + raise ValueError("View has no editable (not yet sent?), can't refresh") if embeds := await self._get_embeds(): - await self.message.edit(content=await self._get_content(), embeds=embeds, view=self) + await editable.edit(content=await self._get_content(), embeds=embeds, view=self) else: - await self.message.edit(content=await self._get_content(), view=self) + await editable.edit(content=await self._get_content(), view=self) - async def send(self, ctx: discord.ApplicationContext | discord.Interaction) -> None: + async def send(self, ctx: discord.ApplicationContext | discord.Interaction, ephemeral: bool = False) -> None: """Send the view to a context.""" + self.ctx = ctx if embeds := await self._get_embeds(): - await ctx.respond(content=await self._get_content(), embeds=embeds, view=self) # pyright: ignore [reportUnknownMemberType] + await ctx.respond(content=await self._get_content(), embeds=embeds, view=self, ephemeral=ephemeral) # pyright: ignore [reportUnknownMemberType] else: - await ctx.respond(content=await self._get_content(), view=self) # pyright: ignore [reportUnknownMemberType] + await ctx.respond(content=await self._get_content(), view=self, ephemeral=ephemeral) # pyright: ignore [reportUnknownMemberType]