Add timeout to client.wait_for

This commit is contained in:
rockboy987
2021-09-02 16:25:15 +05:30
parent f37eb6bbc1
commit d352b04c85
+9 -2
View File
@@ -129,7 +129,7 @@ class Client:
"""
self.state.get_server(id)
async def wait_for(self, event: str, *, check: Optional[Callable[..., bool]] = None) -> Any:
async def wait_for(self, event: str, *, check: Optional[Callable[..., bool]] = None, timeout: Optional[float] = None) -> Any:
"""Waits for an event
Parameters
@@ -138,6 +138,13 @@ class Client:
The name of the event to wait for, without the `on_`
check: Optional[Callable[..., :class:`bool`]]
A function that says what event to wait_for, this function takes the same parameters as the event you are waiting for and should return a bool saying if that is the event you want
timeout: Optional[:class:`float`]
Time in seconds to wait for the event. By default it waits forever
Raises
-------
asyncio.TimeoutError
If timeout is provided and it was reached
Returns
--------
@@ -150,4 +157,4 @@ class Client:
future = asyncio.get_running_loop().create_future()
self.listeners.setdefault(event, []).append((check, future))
return await future
return await asyncio.wait_for(future, timeout)