Browse Source

Add View.total_children_count

pull/10266/head
Rapptz 1 week ago
parent
commit
064cb7af71
  1. 33
      discord/ui/view.py

33
discord/ui/view.py

@ -327,14 +327,10 @@ class BaseView:
"""List[:class:`Item`]: The list of children attached to this view.""" """List[:class:`Item`]: The list of children attached to this view."""
return self._children.copy() return self._children.copy()
def content_length(self) -> int: @property
""":class:`int`: Returns the total length of all text content in the view's items. def total_children_count(self) -> int:
""":class:`int`: The total number of children in this view, including those from nested items."""
A view is allowed to have a maximum of 4000 display characters across all its items. return self._total_children
"""
from .text_display import TextDisplay
return sum(len(item.content) for item in self.walk_children() if isinstance(item, TextDisplay))
@classmethod @classmethod
def from_message(cls, message: Message, /, *, timeout: Optional[float] = 180.0) -> Union[View, LayoutView]: def from_message(cls, message: Message, /, *, timeout: Optional[float] = 180.0) -> Union[View, LayoutView]:
@ -693,12 +689,10 @@ class View(BaseView):
if TYPE_CHECKING: if TYPE_CHECKING:
@classmethod @classmethod
def from_dict(cls, data: List[ComponentPayload], *, timeout: Optional[float] = 180.0) -> View: def from_dict(cls, data: List[ComponentPayload], *, timeout: Optional[float] = 180.0) -> View: ...
...
@classmethod @classmethod
def from_message(cls, message: Message, /, *, timeout: Optional[float] = 180.0) -> View: def from_message(cls, message: Message, /, *, timeout: Optional[float] = 180.0) -> View: ...
...
def __init_subclass__(cls) -> None: def __init_subclass__(cls) -> None:
super().__init_subclass__() super().__init_subclass__()
@ -791,12 +785,10 @@ class LayoutView(BaseView):
if TYPE_CHECKING: if TYPE_CHECKING:
@classmethod @classmethod
def from_dict(cls, data: List[ComponentPayload], *, timeout: Optional[float] = 180.0) -> LayoutView: def from_dict(cls, data: List[ComponentPayload], *, timeout: Optional[float] = 180.0) -> LayoutView: ...
...
@classmethod @classmethod
def from_message(cls, message: Message, /, *, timeout: Optional[float] = 180.0) -> LayoutView: def from_message(cls, message: Message, /, *, timeout: Optional[float] = 180.0) -> LayoutView: ...
...
def __init__(self, *, timeout: Optional[float] = 180.0) -> None: def __init__(self, *, timeout: Optional[float] = 180.0) -> None:
super().__init__(timeout=timeout) super().__init__(timeout=timeout)
@ -846,6 +838,15 @@ class LayoutView(BaseView):
super().add_item(item) super().add_item(item)
return self return self
def content_length(self) -> int:
""":class:`int`: Returns the total length of all text content in the view's items.
A view is allowed to have a maximum of 4000 display characters across all its items.
"""
from .text_display import TextDisplay
return sum(len(item.content) for item in self.walk_children() if isinstance(item, TextDisplay))
class ViewStore: class ViewStore:
def __init__(self, state: ConnectionState): def __init__(self, state: ConnectionState):

Loading…
Cancel
Save