You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.3 KiB

"""
The MIT License (MIT)
Copyright (c) 2015-present Rapptz
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations
import sys
from typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, TypeVar, Union
from .item import Item
from .view import View, _component_to_item
from ..enums import ComponentType
if TYPE_CHECKING:
from typing_extensions import Self
from ..colour import Colour, Color
from ..components import Container as ContainerComponent
V = TypeVar('V', bound='View', covariant=True)
__all__ = ('Container',)
class Container(View, Item[V]):
"""Represents a Components V2 Container.
.. versionadded:: 2.6
Parameters
----------
children: List[Union[:class:`Item`, :class:`View`]]
The initial children or :class:`View`s of this container. Can have up to 10
items.
accent_colour: Optional[:class:`~discord.Colour`]
The colour of the container. Defaults to ``None``.
accent_color: Optional[:class:`~discord.Color`]
The color of the container. Defaults to ``None``.
spoiler: :class:`bool`
Whether to flag this container as a spoiler. Defaults
to ``False``.
"""
__discord_ui_container__ = True
def __init__(
self,
children: List[Union[Item[Any], View]],
*,
accent_colour: Optional[Colour] = None,
accent_color: Optional[Color] = None,
spoiler: bool = False,
timeout: Optional[float] = 180,
) -> None:
if len(children) > 10:
raise ValueError('maximum number of components exceeded')
self._children: List[Union[Item[Any], View]] = children
self.spoiler: bool = spoiler
self._colour = accent_colour or accent_color
super().__init__(timeout=timeout)
@property
def children(self) -> List[Union[Item[Any], View]]:
"""List[:class:`Item`]: The children of this container."""
return self._children.copy()
@children.setter
def children(self, value: List[Union[Item[Any], View]]) -> None:
self._children = value
@property
def accent_colour(self) -> Optional[Colour]:
"""Optional[:class:`~discord.Colour`]: The colour of the container, or ``None``."""
return self._colour
@accent_colour.setter
def accent_colour(self, value: Optional[Colour]) -> None:
self._colour = value
accent_color = accent_colour
"""Optional[:class:`~discord.Color`]: The color of the container, or ``None``."""
@property
def type(self) -> Literal[ComponentType.container]:
return ComponentType.container
@property
def _views(self) -> List[View]:
return [c for c in self._children if isinstance(c, View)]
def _is_v2(self) -> bool:
return True
def to_components(self) -> List[Dict[str, Any]]:
components = super().to_components()
return [{
'type': self.type.value,
'accent_color': self._colour.value if self._colour else None,
'spoiler': self.spoiler,
'components': components,
}]
@classmethod
def from_component(cls, component: ContainerComponent) -> Self:
return cls(
children=[_component_to_item(c) for c in component.children],
accent_colour=component.accent_colour,
spoiler=component.spoiler,
)