diff --git a/discord/client.py b/discord/client.py index cc793c0b7..27ec55c1a 100644 --- a/discord/client.py +++ b/discord/client.py @@ -29,7 +29,7 @@ import logging import signal import sys import traceback -from typing import Any, Generator, List, Optional, TYPE_CHECKING, TypeVar, Union +from typing import Any, Generator, List, Optional, Sequence, TYPE_CHECKING, TypeVar, Union import aiohttp @@ -1463,3 +1463,8 @@ class Client: raise ValueError('View is not persistent. Items need to have a custom_id set and View must have no timeout') self._connection.store_view(view, message_id) + + @property + def persistent_views(self) -> Sequence[View]: + """Sequence[:class:`View`]: A sequence of persistent views added to the client.""" + return self._connection.persistent_views diff --git a/discord/state.py b/discord/state.py index 80272cd64..71892fd85 100644 --- a/discord/state.py +++ b/discord/state.py @@ -287,6 +287,10 @@ class ConnectionState: def prevent_view_updates_for(self, message_id): return self._view_store.remove_message_tracking(message_id) + @property + def persistent_views(self): + return self._view_store.persistent_views + @property def guilds(self): return list(self._guilds.values()) diff --git a/discord/ui/view.py b/discord/ui/view.py index b6ddf4e6f..f6dac2414 100644 --- a/discord/ui/view.py +++ b/discord/ui/view.py @@ -23,7 +23,7 @@ DEALINGS IN THE SOFTWARE. """ from __future__ import annotations -from typing import Any, Callable, ClassVar, Dict, Iterator, List, Optional, TYPE_CHECKING, Tuple +from typing import Any, Callable, ClassVar, Dict, Iterator, List, Optional, Sequence, TYPE_CHECKING, Tuple from functools import partial from itertools import groupby @@ -370,6 +370,15 @@ class ViewStore: self._synced_message_views: Dict[int, View] = {} self._state: ConnectionState = state + @property + def persistent_views(self) -> Sequence[View]: + views = { + view.id: view + for (_, (view, _, _)) in self._views.items() + if view.is_persistent() + } + return list(views.values()) + def __verify_integrity(self): to_remove: List[Tuple[int, str]] = [] now = time.monotonic()