aboutsummaryrefslogtreecommitdiff
path: root/discord/ui/view.py
diff options
context:
space:
mode:
authorRapptz <[email protected]>2021-06-30 03:06:51 -0400
committerRapptz <[email protected]>2021-06-30 03:06:51 -0400
commitd8075d54125013cb6873b3ffd93944a72679fe09 (patch)
tree967f7ab9b765613d87676f4564fe4cf8f469672f /discord/ui/view.py
parentAdd conversion routine for SelectMenu to ui.Select (diff)
downloaddiscord.py-d8075d54125013cb6873b3ffd93944a72679fe09.tar.xz
discord.py-d8075d54125013cb6873b3ffd93944a72679fe09.zip
Add View.from_message to convert message components to a View
Diffstat (limited to 'discord/ui/view.py')
-rw-r--r--discord/ui/view.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/discord/ui/view.py b/discord/ui/view.py
index 42dc8da3..78ae552b 100644
--- a/discord/ui/view.py
+++ b/discord/ui/view.py
@@ -48,6 +48,7 @@ __all__ = (
if TYPE_CHECKING:
from ..interactions import Interaction
+ from ..message import Message
from ..types.components import Component as ComponentPayload
from ..state import ConnectionState
@@ -113,6 +114,7 @@ class _ViewWeights:
def clear(self) -> None:
self.weights = [0, 0, 0, 0, 0]
+
class View:
"""Represents a UI view.
@@ -188,6 +190,33 @@ class View:
return components
+ @classmethod
+ def from_message(cls, message: Message, /, timeout: Optional[float] = 180.0) -> View:
+ """Converts a message's components into a :class:`View`.
+
+ The :attr:`Message.components` of a message are read-only
+ and separate types from those in the ``discord.ui`` namespace.
+ In order to modify and edit message components they must be
+ converted into a :class:`View` first.
+
+ Parameters
+ -----------
+ message: :class:`discord.Message`
+ The message with components to convert into a view.
+ timeout: Optional[:class:`float`]
+ The timeout of the converted view.
+
+ Returns
+ --------
+ :class:`View`
+ The converted view. This always returns a :class:`View` and not
+ one of its subclasses.
+ """
+ view = View(timeout=timeout)
+ for component in _walk_all_components(message.components):
+ view.add_item(_component_to_item(component))
+ return view
+
@property
def _expires_at(self) -> Optional[float]:
if self.timeout:
@@ -404,11 +433,13 @@ class ViewStore:
@property
def persistent_views(self) -> Sequence[View]:
+ # fmt: off
views = {
view.id: view
for (_, (view, _, _)) in self._views.items()
if view.is_persistent()
}
+ # fmt: on
return list(views.values())
def __verify_integrity(self):