|
13 | 13 | from ..api import WebSocketClient as WSClient |
14 | 14 | from ..api.error import LibraryException |
15 | 15 | from ..api.http.client import HTTPClient |
| 16 | +from ..api.models.channel import Channel |
16 | 17 | from ..api.models.flags import Intents, Permissions |
17 | 18 | from ..api.models.guild import Guild |
| 19 | +from ..api.models.member import Member |
18 | 20 | from ..api.models.message import Message |
19 | 21 | from ..api.models.misc import Image, Snowflake |
20 | 22 | from ..api.models.presence import ClientPresence |
| 23 | +from ..api.models.role import Role |
21 | 24 | from ..api.models.team import Application |
22 | 25 | from ..api.models.user import User |
23 | 26 | from ..base import get_logger |
@@ -1596,9 +1599,10 @@ async def wait_for_component( |
1596 | 1599 | Waits for a component to be interacted with, and returns the resulting context. |
1597 | 1600 |
|
1598 | 1601 | .. note:: |
1599 | | - If you are waiting for a select menu, you can find the selected values in ``ctx.data.values`` |
| 1602 | + If you are waiting for a select menu, you can find the selected values in ``ctx.data.values``. |
| 1603 | + Another possibility is using the :meth:`.Client.wait_for_select` method. |
1600 | 1604 |
|
1601 | | - :param Union[str, interactions.Button, interactions.SelectMenu, List[Union[str, interactions.Button, interactions.SelectMenu]]] components: The component(s) to wait for |
| 1605 | + :param Union[str, Button, SelectMenu, List[Union[str, Button, SelectMenu]]] components: The component(s) to wait for |
1602 | 1606 | :param Union[interactions.Message, int, List[Union[interactions.Message, int]]] messages: The message(s) to check for |
1603 | 1607 | :param Callable check: A function or coroutine to call, which should return a truthy value if the data should be returned |
1604 | 1608 | :param float timeout: How long to wait for the event before raising an error |
@@ -1653,6 +1657,57 @@ def _check(ctx: ComponentContext) -> bool: |
1653 | 1657 |
|
1654 | 1658 | return await self.wait_for("on_component", check=_check, timeout=timeout) |
1655 | 1659 |
|
| 1660 | + async def wait_for_select( |
| 1661 | + self, |
| 1662 | + components: Union[ |
| 1663 | + Union[str, SelectMenu], |
| 1664 | + List[Union[str, SelectMenu]], |
| 1665 | + ] = None, |
| 1666 | + messages: Union[Message, int, List[Union[Message, int]]] = None, |
| 1667 | + check: Optional[Callable[..., Union[bool, Awaitable[bool]]]] = None, |
| 1668 | + timeout: Optional[float] = None, |
| 1669 | + ) -> Tuple[ComponentContext, List[Union[str, Member, User, Role, Channel]]]: |
| 1670 | + """ |
| 1671 | + Waits for a select menu to be interacted with, and returns the resulting context and a list of the selected values. |
| 1672 | +
|
| 1673 | + The method can be used like this: |
| 1674 | +
|
| 1675 | + .. code-block:: python |
| 1676 | +
|
| 1677 | + ctx, values = await bot.wait_for_select(custom_id) |
| 1678 | +
|
| 1679 | + In this case ``ctx`` will be your normal context and ``values`` will be a list of :class:`str`, :class:`.Member`, :class:`.User`, :class:`.Channel` or :class:`.Role` objects, |
| 1680 | + depending on which select type you received. |
| 1681 | +
|
| 1682 | +
|
| 1683 | + :param Union[str, SelectMenu, List[Union[str, SelectMenu]]] components: The component(s) to wait for |
| 1684 | + :param Union[interactions.Message, int, List[Union[interactions.Message, int]]] messages: The message(s) to check for |
| 1685 | + :param Callable check: A function or coroutine to call, which should return a truthy value if the data should be returned |
| 1686 | + :param float timeout: How long to wait for the event before raising an error |
| 1687 | + :return: The ComponentContext and list of selections of the dispatched event |
| 1688 | + :rtype: Tuple[ComponentContext, Union[List[str], List[Member], List[User], List[Channel], List[Role]]] |
| 1689 | + """ |
| 1690 | + |
| 1691 | + def _check(_ctx: ComponentContext) -> bool: |
| 1692 | + if _ctx.data.component_type.value not in {4, 5, 6, 7, 8}: |
| 1693 | + return False |
| 1694 | + return check(_ctx) if check else True |
| 1695 | + |
| 1696 | + ctx: ComponentContext = await self.wait_for_component( |
| 1697 | + components, messages, check=_check, timeout=timeout |
| 1698 | + ) |
| 1699 | + |
| 1700 | + if ctx.data.component_type == 4: |
| 1701 | + return ctx, ctx.data.values |
| 1702 | + |
| 1703 | + _list = [] # temp storage for items |
| 1704 | + _data = self._websocket._WebSocketClient__select_option_type_context( |
| 1705 | + ctx, ctx.data.component_type.value |
| 1706 | + ) # resolved. |
| 1707 | + for value in ctx.data.values: |
| 1708 | + _list.append(_data[value]) |
| 1709 | + return ctx, _list |
| 1710 | + |
1656 | 1711 | async def wait_for_modal( |
1657 | 1712 | self, |
1658 | 1713 | modals: Union[Modal, str, List[Union[Modal, str]]], |
|
0 commit comments