Browse Source

Allow runtime modification of timeout expiry at runtime

This is done through setting View.timeout while it's running
pull/7821/head
Rapptz 3 years ago
parent
commit
b4fbb08818
  1. 7
      discord/ui/modal.py
  2. 28
      discord/ui/view.py

7
discord/ui/modal.py

@ -88,9 +88,6 @@ class Modal(View):
Attributes 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` title: :class:`str`
The title of the modal. The title of the modal.
children: List[:class:`Item`] children: List[:class:`Item`]
@ -184,9 +181,7 @@ class Modal(View):
async def _scheduled_task(self, interaction: Interaction): async def _scheduled_task(self, interaction: Interaction):
try: try:
if self.timeout: self._refresh_timeout()
self.__timeout_expiry = time.monotonic() + self.timeout
allow = await self.interaction_check(interaction) allow = await self.interaction_check(interaction)
if not allow: if not allow:
return return

28
discord/ui/view.py

@ -154,9 +154,6 @@ class View:
Attributes 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`] children: List[:class:`Item`]
The list of children attached to this view. The list of children attached to this view.
""" """
@ -188,7 +185,7 @@ class View:
return children return children
def __init__(self, *, timeout: Optional[float] = 180.0): def __init__(self, *, timeout: Optional[float] = 180.0):
self.timeout = timeout self.__timeout = timeout
self.children: List[Item[Self]] = self._init_children() self.children: List[Item[Self]] = self._init_children()
self.__weights = _ViewWeights(self.children) self.__weights = _ViewWeights(self.children)
self.id: str = os.urandom(16).hex() self.id: str = os.urandom(16).hex()
@ -237,6 +234,29 @@ class View:
return components 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 @classmethod
def from_message(cls, message: Message, /, *, timeout: Optional[float] = 180.0) -> View: def from_message(cls, message: Message, /, *, timeout: Optional[float] = 180.0) -> View:
"""Converts a message's components into a :class:`View`. """Converts a message's components into a :class:`View`.

Loading…
Cancel
Save