Allow runtime modification of timeout expiry at runtime

This is done through setting View.timeout while it's running
This commit is contained in:
Rapptz 2022-03-31 01:31:26 -04:00
parent e80be19c4d
commit b4fbb08818
2 changed files with 25 additions and 10 deletions

View File

@ -88,9 +88,6 @@ class Modal(View):
Attributes
------------
timeout: Optional[:class:`float`]
Timeout from last interaction with the UI before no longer accepting input.
If ``None`` then there is no timeout.
title: :class:`str`
The title of the modal.
children: List[:class:`Item`]
@ -184,9 +181,7 @@ class Modal(View):
async def _scheduled_task(self, interaction: Interaction):
try:
if self.timeout:
self.__timeout_expiry = time.monotonic() + self.timeout
self._refresh_timeout()
allow = await self.interaction_check(interaction)
if not allow:
return

View File

@ -154,9 +154,6 @@ class View:
Attributes
------------
timeout: Optional[:class:`float`]
Timeout from last interaction with the UI before no longer accepting input.
If ``None`` then there is no timeout.
children: List[:class:`Item`]
The list of children attached to this view.
"""
@ -188,7 +185,7 @@ class View:
return children
def __init__(self, *, timeout: Optional[float] = 180.0):
self.timeout = timeout
self.__timeout = timeout
self.children: List[Item[Self]] = self._init_children()
self.__weights = _ViewWeights(self.children)
self.id: str = os.urandom(16).hex()
@ -237,6 +234,29 @@ class View:
return components
def _refresh_timeout(self) -> None:
if self.__timeout:
self.__timeout_expiry = time.monotonic() + self.__timeout
@property
def timeout(self) -> Optional[float]:
"""Optional[:class:`float`]: The timeout in seconds from last interaction with the UI before no longer accepting input.
If ``None`` then there is no timeout.
"""
return self.__timeout
@timeout.setter
def timeout(self, value: Optional[float]) -> None:
# If the timeout task is already running this allows it to update
# the expiry while it's running
if self.__timeout_task is not None:
if value is not None:
self.__timeout_expiry = time.monotonic() + value
else:
self.__timeout_expiry = None
self.__timeout = value
@classmethod
def from_message(cls, message: Message, /, *, timeout: Optional[float] = 180.0) -> View:
"""Converts a message's components into a :class:`View`.