8 changed files with 358 additions and 42 deletions
@ -0,0 +1,151 @@ |
|||||
|
""" |
||||
|
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 |
||||
|
|
||||
|
from typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, TypeVar, Union |
||||
|
|
||||
|
from .item import Item |
||||
|
from .text_display import TextDisplay |
||||
|
from ..enums import ComponentType |
||||
|
|
||||
|
if TYPE_CHECKING: |
||||
|
from typing_extensions import Self |
||||
|
|
||||
|
from .view import View |
||||
|
from ..components import SectionComponent |
||||
|
|
||||
|
V = TypeVar('V', bound='View', covariant=True) |
||||
|
|
||||
|
|
||||
|
class Section(Item[V]): |
||||
|
"""Represents a UI section. |
||||
|
|
||||
|
.. versionadded:: 2.6 |
||||
|
|
||||
|
Parameters |
||||
|
---------- |
||||
|
children: List[Union[:class:`str`, :class:`TextDisplay`]] |
||||
|
The text displays of this section. Up to 3. |
||||
|
accessory: Optional[:class:`Item`] |
||||
|
The section accessory. Defaults to ``None``. |
||||
|
""" |
||||
|
|
||||
|
__slots__ = ( |
||||
|
'_children', |
||||
|
'accessory', |
||||
|
) |
||||
|
|
||||
|
def __init__( |
||||
|
self, |
||||
|
children: List[Union[TextDisplay[Any], str]], |
||||
|
*, |
||||
|
accessory: Optional[Item[Any]] = None, |
||||
|
) -> None: |
||||
|
if len(children) > 3: |
||||
|
raise ValueError('maximum number of children exceeded') |
||||
|
self._children: List[TextDisplay[Any]] = [ |
||||
|
c if isinstance(c, TextDisplay) else TextDisplay(c) for c in children |
||||
|
] |
||||
|
self.accessory: Optional[Item[Any]] = accessory |
||||
|
|
||||
|
@property |
||||
|
def type(self) -> Literal[ComponentType.section]: |
||||
|
return ComponentType.section |
||||
|
|
||||
|
def _is_v2(self) -> bool: |
||||
|
return True |
||||
|
|
||||
|
def add_item(self, item: Union[str, TextDisplay[Any]]) -> Self: |
||||
|
"""Adds an item to this section. |
||||
|
|
||||
|
This function returns the class instance to allow for fluent-style |
||||
|
chaining. |
||||
|
|
||||
|
Parameters |
||||
|
---------- |
||||
|
item: Union[:class:`str`, :class:`TextDisplay`] |
||||
|
The text display to add. |
||||
|
|
||||
|
Raises |
||||
|
------ |
||||
|
TypeError |
||||
|
A :class:`TextDisplay` was not passed. |
||||
|
ValueError |
||||
|
Maximum number of children has been exceeded (3). |
||||
|
""" |
||||
|
|
||||
|
if len(self._children) >= 3: |
||||
|
raise ValueError('maximum number of children exceeded') |
||||
|
|
||||
|
if not isinstance(item, (TextDisplay, str)): |
||||
|
raise TypeError(f'expected TextDisplay or str not {item.__class__.__name__}') |
||||
|
|
||||
|
self._children.append( |
||||
|
item if isinstance(item, TextDisplay) else TextDisplay(item), |
||||
|
) |
||||
|
return self |
||||
|
|
||||
|
def remove_item(self, item: TextDisplay[Any]) -> Self: |
||||
|
"""Removes an item from this section. |
||||
|
|
||||
|
This function returns the class instance to allow for fluent-style |
||||
|
chaining. |
||||
|
|
||||
|
Parameters |
||||
|
---------- |
||||
|
item: :class:`TextDisplay` |
||||
|
The item to remove from the section. |
||||
|
""" |
||||
|
|
||||
|
try: |
||||
|
self._children.remove(item) |
||||
|
except ValueError: |
||||
|
pass |
||||
|
return self |
||||
|
|
||||
|
def clear_items(self) -> Self: |
||||
|
"""Removes all the items from the section. |
||||
|
|
||||
|
This function returns the class instance to allow for fluent-style |
||||
|
chaining. |
||||
|
""" |
||||
|
self._children.clear() |
||||
|
return self |
||||
|
|
||||
|
@classmethod |
||||
|
def from_component(cls, component: SectionComponent) -> Self: |
||||
|
from .view import _component_to_item # >circular import< |
||||
|
return cls( |
||||
|
children=[_component_to_item(c) for c in component.components], |
||||
|
accessory=_component_to_item(component.accessory) if component.accessory else None, |
||||
|
) |
||||
|
|
||||
|
def to_component_dict(self) -> Dict[str, Any]: |
||||
|
data = { |
||||
|
'components': [c.to_component_dict() for c in self._children], |
||||
|
'type': self.type.value, |
||||
|
} |
||||
|
if self.accessory: |
||||
|
data['accessory'] = self.accessory.to_component_dict() |
||||
|
return data |
@ -0,0 +1,86 @@ |
|||||
|
""" |
||||
|
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 |
||||
|
|
||||
|
from typing import TYPE_CHECKING, Any, Dict, Literal, Optional, TypeVar |
||||
|
|
||||
|
from .item import Item |
||||
|
from ..enums import ComponentType |
||||
|
|
||||
|
if TYPE_CHECKING: |
||||
|
from typing_extensions import Self |
||||
|
|
||||
|
from .view import View |
||||
|
from ..components import ThumbnailComponent |
||||
|
|
||||
|
V = TypeVar('V', bound='View', covariant=True) |
||||
|
|
||||
|
__all__ = ( |
||||
|
'Thumbnail', |
||||
|
) |
||||
|
|
||||
|
class Thumbnail(Item[V]): |
||||
|
"""Represents a UI Thumbnail. |
||||
|
|
||||
|
.. versionadded:: 2.6 |
||||
|
|
||||
|
Parameters |
||||
|
---------- |
||||
|
url: :class:`str` |
||||
|
The URL of the thumbnail. This can only point to a local attachment uploaded |
||||
|
within this item. URLs must match the ``attachment://file-name.extension`` |
||||
|
structure. |
||||
|
description: Optional[:class:`str`] |
||||
|
The description of this thumbnail. Defaults to ``None``. |
||||
|
spoiler: :class:`bool` |
||||
|
Whether to flag this thumbnail as a spoiler. Defaults to ``False``. |
||||
|
""" |
||||
|
|
||||
|
def __init__(self, url: str, *, description: Optional[str] = None, spoiler: bool = False) -> None: |
||||
|
self.url: str = url |
||||
|
self.description: Optional[str] = description |
||||
|
self.spoiler: bool = spoiler |
||||
|
|
||||
|
@property |
||||
|
def type(self) -> Literal[ComponentType.thumbnail]: |
||||
|
return ComponentType.thumbnail |
||||
|
|
||||
|
def _is_v2(self) -> bool: |
||||
|
return True |
||||
|
|
||||
|
def to_component_dict(self) -> Dict[str, Any]: |
||||
|
return { |
||||
|
'type': self.type.value, |
||||
|
'spoiler': self.spoiler, |
||||
|
'media': {'url': self.url}, |
||||
|
'description': self.description, |
||||
|
} |
||||
|
|
||||
|
@classmethod |
||||
|
def from_component(cls, component: ThumbnailComponent) -> Self: |
||||
|
return cls( |
||||
|
url=component.media.url, |
||||
|
description=component.description, |
||||
|
spoiler=component.spoiler, |
||||
|
) |
Loading…
Reference in new issue