From 97eb9737d7583f6c011ace91f672955760f57342 Mon Sep 17 00:00:00 2001 From: Krittick Date: Mon, 14 Feb 2022 14:39:15 -0800 Subject: [PATCH 1/2] add `wait()` and `stop()` methods to `Modal` class --- discord/ui/modal.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/discord/ui/modal.py b/discord/ui/modal.py index d909488823..26c381abe0 100644 --- a/discord/ui/modal.py +++ b/discord/ui/modal.py @@ -1,5 +1,6 @@ from __future__ import annotations +import asyncio import os import sys from itertools import groupby @@ -29,6 +30,8 @@ def __init__(self, title: str, custom_id: Optional[str] = None) -> None: self.title = title self.children: List[InputText] = [] self.__weights = _ModalWeights(self.children) + loop = asyncio.get_running_loop() + self._stopped: asyncio.Future[bool] = loop.create_future() async def callback(self, interaction: Interaction): """|coro| @@ -40,7 +43,7 @@ async def callback(self, interaction: Interaction): interaction: :class:`~discord.Interaction` The interaction that submitted the modal dialog. """ - pass + self.stop() def to_components(self) -> List[Dict[str, Any]]: def key(item: InputText) -> int: @@ -93,6 +96,15 @@ def remove_item(self, item: InputText): except ValueError: pass + def stop(self) -> None: + """Stops listening to interaction events from the modal dialog.""" + if not self._stopped.done(): + self._stopped.set_result(True) + + async def wait(self) -> bool: + """Waits for the modal dialog to be closed.""" + return await self._stopped + def to_dict(self): return { "title": self.title, From 54b1d3169ffd846a768846a95800fb38a45b5a60 Mon Sep 17 00:00:00 2001 From: Krittick Date: Mon, 14 Feb 2022 15:03:21 -0800 Subject: [PATCH 2/2] clarify docstring --- discord/ui/modal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/ui/modal.py b/discord/ui/modal.py index 26c381abe0..96e2f334be 100644 --- a/discord/ui/modal.py +++ b/discord/ui/modal.py @@ -102,7 +102,7 @@ def stop(self) -> None: self._stopped.set_result(True) async def wait(self) -> bool: - """Waits for the modal dialog to be closed.""" + """Waits for the modal dialog to be submitted.""" return await self._stopped def to_dict(self):