Add support for dynamic items that parse custom_id for state

This commit is contained in:
Rapptz
2023-07-20 22:49:38 -04:00
parent 78ce5f2331
commit a852f90358
8 changed files with 393 additions and 4 deletions

View File

@ -72,6 +72,7 @@ from .backoff import ExponentialBackoff
from .webhook import Webhook
from .appinfo import AppInfo
from .ui.view import View
from .ui.dynamic import DynamicItem
from .stage_instance import StageInstance
from .threads import Thread
from .sticker import GuildSticker, StandardSticker, StickerPack, _sticker_factory
@ -111,6 +112,7 @@ if TYPE_CHECKING:
from .scheduled_event import ScheduledEvent
from .threads import ThreadMember
from .types.guild import Guild as GuildPayload
from .ui.item import Item
from .voice_client import VoiceProtocol
from .audit_logs import AuditLogEntry
@ -2678,6 +2680,30 @@ class Client:
data = await state.http.start_private_message(user.id)
return state.add_dm_channel(data)
def add_dynamic_items(self, *items: Type[DynamicItem[Item[Any]]]) -> None:
r"""Registers a :class:`~discord.ui.DynamicItem` class for persistent listening.
This method accepts *class types* rather than instances.
.. versionadded:: 2.4
Parameters
-----------
\*items: Type[:class:`~discord.ui.DynamicItem`]
The classes of dynamic items to add.
Raises
-------
TypeError
The class is not a subclass of :class:`~discord.ui.DynamicItem`.
"""
for item in items:
if not issubclass(item, DynamicItem):
raise TypeError(f'expected subclass of DynamicItem not {item.__name__}')
self._connection.store_dynamic_items(*items)
def add_view(self, view: View, *, message_id: Optional[int] = None) -> None:
"""Registers a :class:`~discord.ui.View` for persistent listening.