Skip to content

Commit

Permalink
fix timeouts superpositions
Browse files Browse the repository at this point in the history
  • Loading branch information
AiroPi committed Mar 27, 2024
1 parent 66f792e commit 032ab34
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
33 changes: 25 additions & 8 deletions src/cogs/poll/edit.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from datetime import UTC, datetime, timedelta
from typing import TYPE_CHECKING, Any, Self, cast
from typing import TYPE_CHECKING, Any, Protocol, Self, cast

import discord
from discord import Interaction, ui
Expand Down Expand Up @@ -30,7 +30,7 @@ async def __init__(
poll_message: discord.Message | None = None,
inter: Interaction | None = None,
):
await super().__init__(bot=cog.bot, timeout=10, inter=inter)
await super().__init__(bot=cog.bot, inter=inter)

self.poll = poll
self.cog = cog
Expand Down Expand Up @@ -154,7 +154,7 @@ async def callback(self, inter: Interaction[MyBot]) -> None: # pyright: ignore
class EditPollMenus(ui.Select[EditPoll]):
def __init__(self, poll: db.Poll):
super().__init__(placeholder=_("Edit poll"))
self.menus: list[type[EditSubmenu]] = [
self.menus: list[type[EditSubmenu | EditSubmenuModal]] = [
EditTitleAndDescription,
EditEndingTime,
EditAllowedRoles,
Expand All @@ -177,28 +177,45 @@ async def callback(self, inter: Interaction[MyBot]) -> None: # pyright: ignore
await view.set_menu(inter, await self.menus[int(self.values[0])](view))


class EditSubmenu(SubMenu[EditPoll]):
class EditSubmenuProtocol(Protocol):
@property
def poll(self) -> db.Poll: ...

@property
def bot(self) -> MyBot: ...


class EditSubmenuMixin:
select_name: str
select_description: str = ""
select_emoji: str | None = None

async def update_poll_display(self: EditSubmenuProtocol, inter: discord.Interaction, view: ui.View | None = None):
self_as_view = cast(ui.View, self)
await inter.response.edit_message(**(await PollDisplay(self.poll, self.bot)), view=view or self_as_view)


class EditSubmenu(SubMenu[EditPoll], EditSubmenuMixin):
async def __init__(self, parent: EditPoll):
await super().__init__(parent)
self.poll = parent.poll

async def update_poll_display(self, inter: discord.Interaction, view: ui.View | None = None):
await inter.response.edit_message(**(await PollDisplay(self.poll, self.bot)), view=view or self)

class EditSubmenuModal(ModalSubMenu[EditPoll], EditSubmenuMixin):
async def __init__(self, parent: EditPoll):
await super().__init__(parent)
self.poll = parent.poll

class EditTitleAndDescription(EditSubmenu, ui.Modal):

class EditTitleAndDescription(EditSubmenuModal):
select_name = _("Edit title and description", _locale=None)
select_emoji = Emojis.pencil

async def __init__(self, parent: EditPoll):
self.title = _("Create a new poll")
self.custom_id = self.generate_custom_id()

await EditSubmenu.__init__(self, parent)
await super().__init__(parent)

self.question = ui.TextInput[Self](
label=_("Poll question"),
Expand Down
9 changes: 8 additions & 1 deletion src/core/view_menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,21 @@ async def __init__(
**kwargs: Any,
):
del kwargs # unused
self._default_timeout = timeout

super().__init__(timeout=timeout)
self.bot = bot
self.inter: Interaction | None = inter

async def set_menu(self, inter: Interaction, menu: Menu) -> None:
"""Set the display to a new menu."""
await menu.update()
self.inter = inter

if not isinstance(menu, ui.Modal):
self.timeout = None
menu.timeout = menu._default_timeout

await menu.update()
if isinstance(menu, ui.Modal):
await inter.response.send_modal(menu)
else:
Expand Down Expand Up @@ -104,6 +110,7 @@ def generate_custom_id() -> str:
return os.urandom(16).hex()


# TODO: try to implement the following two classes with some sort of Mixin ?
class SubMenuWithoutButtons(Menu, Generic[P]):
async def __init__(
self,
Expand Down

0 comments on commit 032ab34

Please sign in to comment.