Skip to content
1 change: 1 addition & 0 deletions discord/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ async def send_message(
if ephemeral and view.timeout is None:
view.timeout = 15 * 60.0

view.message = await self._parent.original_message()
self._parent._state.store_view(view)

self._responded = True
Expand Down
15 changes: 11 additions & 4 deletions discord/ui/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
Optional,
Sequence,
Tuple,
Union,
)

from ..components import ActionRow as ActionRowComponent
Expand All @@ -56,7 +57,7 @@


if TYPE_CHECKING:
from ..interactions import Interaction
from ..interactions import Interaction, InteractionMessage
from ..message import Message
from ..state import ConnectionState
from ..types.components import Component as ComponentPayload
Expand Down Expand Up @@ -144,6 +145,8 @@ class View:
If ``None`` then there is no timeout.
children: List[:class:`Item`]
The list of children attached to this view.
disable_on_timeout: :class:`bool`
Whether to disable the view when the timeout is reached. Defaults to ``False``.
message: Optional[:class:`Message`]
The message that this view is attached to.
If ``None`` then the view has not been sent with a message.
Expand All @@ -164,8 +167,9 @@ def __init_subclass__(cls) -> None:

cls.__view_children_items__ = children

def __init__(self, *items: Item, timeout: Optional[float] = 180.0):
def __init__(self, *items: Item, timeout: Optional[float] = 180.0, disable_on_timeout: bool = False):
self.timeout = timeout
self.disable_on_timeout = disable_on_timeout
self.children: List[Item] = []
for func in self.__view_children_items__:
item: Item = func.__discord_ui_model_type__(**func.__discord_ui_model_kwargs__)
Expand All @@ -184,7 +188,7 @@ def __init__(self, *items: Item, timeout: Optional[float] = 180.0):
self.__timeout_expiry: Optional[float] = None
self.__timeout_task: Optional[asyncio.Task[None]] = None
self.__stopped: asyncio.Future[bool] = loop.create_future()
self._message: Optional[Message] = None
self._message: Optional[Union[Message, InteractionMessage]] = None

def __repr__(self) -> str:
return f"<{self.__class__.__name__} timeout={self.timeout} children={len(self.children)}>"
Expand Down Expand Up @@ -341,7 +345,10 @@ async def on_timeout(self) -> None:

A callback that is called when a view's timeout elapses without being explicitly stopped.
"""
pass
if self.disable_on_timeout:
if self._message:
self.disable_all_items()
await self._message.edit(view=self)

async def on_error(self, error: Exception, item: Item, interaction: Interaction) -> None:
"""|coro|
Expand Down