Skip to content

Commit

Permalink
Make components expire on timeout
Browse files Browse the repository at this point in the history
There is still an issue with timeout superpositions.
  • Loading branch information
AiroPi committed Mar 27, 2024
1 parent 70cfdc8 commit 66f792e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/cogs/poll/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

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

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

Expand Down
10 changes: 8 additions & 2 deletions src/cogs/poll/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/cogs/poll/vote_menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, {})
Expand Down Expand Up @@ -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]):
Expand Down
30 changes: 11 additions & 19 deletions src/core/view_menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -123,6 +113,7 @@ async def __init__(
await super().__init__(
bot=parent.bot,
timeout=timeout,
inter=parent.inter,
)
self.parent: P = parent

Expand All @@ -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")
Expand Down

0 comments on commit 66f792e

Please sign in to comment.