Skip to content

Commit

Permalink
Add wait() method to Modal class (Pycord-Development#1013)
Browse files Browse the repository at this point in the history
* add `wait()` and `stop()` methods to `Modal` class

* clarify docstring
  • Loading branch information
krittick authored Feb 15, 2022
1 parent c695387 commit 1d315a2
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion discord/ui/modal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import asyncio
import os
import sys
from itertools import groupby
Expand Down Expand Up @@ -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|
Expand All @@ -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:
Expand Down Expand Up @@ -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 submitted."""
return await self._stopped

def to_dict(self):
return {
"title": self.title,
Expand Down

0 comments on commit 1d315a2

Please sign in to comment.