diff --git a/src/cogs/poll/__init__.py b/src/cogs/poll/__init__.py index 39ad71e..b62de0c 100644 --- a/src/cogs/poll/__init__.py +++ b/src/cogs/poll/__init__.py @@ -103,7 +103,7 @@ async def edit_poll(self, inter: Interaction, message: discord.Message) -> None: raise NonSpecificError(_("You are not the author of this poll. You can't edit it.", _l=256)) await inter.response.send_message( **(await PollDisplay(poll, self.bot)), - view=await EditPoll(self, poll, message), + view=await EditPoll(self, poll, message, inter), ephemeral=True, ) @@ -134,7 +134,7 @@ async def on_submit(self, inter: discord.Interaction): self.poll.description = self.description.value await inter.response.send_message( **(await PollDisplay(self.poll, self.bot)), - view=await EditPoll(self.cog, self.poll, inter.message), + view=await EditPoll(self.cog, self.poll, inter.message, inter), ephemeral=True, ) @@ -174,7 +174,7 @@ async def on_submit(self, inter: discord.Interaction): await inter.response.send_message( **(await PollDisplay(self.poll, self.bot)), - view=await EditPoll(self.cog, self.poll, inter.message), + view=await EditPoll(self.cog, self.poll, inter.message, inter), ephemeral=True, ) diff --git a/src/cogs/poll/edit.py b/src/cogs/poll/edit.py index f14e66f..83c2a35 100644 --- a/src/cogs/poll/edit.py +++ b/src/cogs/poll/edit.py @@ -23,8 +23,14 @@ class EditPoll(Menu): - async def __init__(self, cog: PollCog, poll: db.Poll, poll_message: discord.Message | None = None): - await super().__init__(bot=cog.bot, timeout=600) + async def __init__( + self, + cog: PollCog, + poll: db.Poll, + poll_message: discord.Message | None = None, + inter: Interaction | None = None, + ): + await super().__init__(bot=cog.bot, timeout=10, inter=inter) self.poll = poll self.cog = cog diff --git a/src/cogs/poll/vote_menus.py b/src/cogs/poll/vote_menus.py index 79716d0..c201438 100644 --- a/src/cogs/poll/vote_menus.py +++ b/src/cogs/poll/vote_menus.py @@ -32,7 +32,8 @@ async def __init__(self, cog: PollCog, poll: db.Poll | None = None): self.cog = cog self.poll = poll - self.vote.label = _("Vote") + # _silent because this view is added to persistent views. + self.vote.label = _("Vote", _silent=True) def get_current_votes(self, poll: db.Poll) -> dict[int, tuple[Interaction, ui.View]]: return self.cog.current_votes.setdefault(poll.id, {}) @@ -174,7 +175,7 @@ async def update(self): @ui.select(cls=ui.Select[Self]) async def choice(self, inter: Interaction, select: ui.Select[Self]): del select # unused - await self.edit_message(inter, False) + await self.edit_message(inter) @ui.button(style=discord.ButtonStyle.red) async def remove_vote(self, inter: Interaction, button: ui.Button[Self]): diff --git a/src/core/view_menus.py b/src/core/view_menus.py index d351e70..57c0478 100644 --- a/src/core/view_menus.py +++ b/src/core/view_menus.py @@ -45,38 +45,28 @@ class Menu(ui.View, AsyncInitMixin): async def __init__( self, bot: MyBot, - message_attached_to: discord.Message | None = None, timeout: float | None = 600, + inter: Interaction | None = None, **kwargs: Any, ): del kwargs # unused super().__init__(timeout=timeout) self.bot = bot - self.message_attached_to: discord.Message | None = message_attached_to + self.inter: Interaction | None = inter - async def set_menu(self, inter: Interaction, menu: Menu, view_only: bool = False) -> None: + async def set_menu(self, inter: Interaction, menu: Menu) -> None: """Set the display to a new menu.""" await menu.update() + self.inter = inter if isinstance(menu, ui.Modal): await inter.response.send_modal(menu) else: await inter.response.edit_message(**await menu.message_display(), view=menu) - async def edit_message(self, inter: Interaction, view_only: bool = False): - """This edit the message with the view and the message content. - - It is very similar to `set_menu`, and can be considered as a "re-set" of the menu (`menu.set_menu(inter, menu)`) - - Args: - inter: the Interaction object - refresh_display: when set to False, only the view in updated, not the message content. Defaults to True. - """ - await self.update() - if view_only: - await inter.response.edit_message(view=self) - else: - await inter.response.edit_message(view=self, **await self.message_display()) + async def edit_message(self, inter: Interaction): + """A convenient await to update a message with self.""" + await self.set_menu(inter, self) async def update(self) -> None: """Update the components to match the datas. @@ -96,10 +86,10 @@ def disable_view(self): item.disabled = True async def on_timeout(self) -> None: - if self.message_attached_to: + if self.inter is not None: self.disable_view() with contextlib.suppress(discord.NotFound, discord.HTTPException): - await self.message_attached_to.edit(view=self) + await self.inter.edit_original_response(view=self) async def message_display(self) -> MessageDisplay | UneditedMessageDisplay: """This function can be defined and used in order to add a message content (embeds, etc...) within the menu. @@ -123,6 +113,7 @@ async def __init__( await super().__init__( bot=parent.bot, timeout=timeout, + inter=parent.inter, ) self.parent: P = parent @@ -136,6 +127,7 @@ async def __init__( await super().__init__( bot=parent.bot, timeout=timeout, + inter=parent.inter, ) self.parent: P = parent self.cancel_btn.label = _("Cancel")