File: label.py

package info (click to toggle)
python-discord 2.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 8,476 kB
  • sloc: python: 49,910; javascript: 363; makefile: 154
file content (140 lines) | stat: -rw-r--r-- 4,325 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"""
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, Generator, Literal, Optional, Tuple, TypeVar

from ..components import LabelComponent
from ..enums import ComponentType
from ..utils import MISSING
from .item import Item

if TYPE_CHECKING:
    from typing_extensions import Self

    from ..types.components import LabelComponent as LabelComponentPayload
    from .view import View


# fmt: off
__all__ = (
    'Label',
)
# fmt: on

V = TypeVar('V', bound='View', covariant=True)


class Label(Item[V]):
    """Represents a UI label within a modal.

    .. versionadded:: 2.6

    Parameters
    ------------
    text: :class:`str`
        The text to display above the input field.
        Can only be up to 45 characters.
    description: Optional[:class:`str`]
        The description text to display right below the label text.
        Can only be up to 100 characters.
    component: Union[:class:`discord.ui.TextInput`, :class:`discord.ui.Select`]
        The component to display below the label.
    id: Optional[:class:`int`]
        The ID of the component. This must be unique across the view.

    Attributes
    ------------
    text: :class:`str`
        The text to display above the input field.
        Can only be up to 45 characters.
    description: Optional[:class:`str`]
        The description text to display right below the label text.
        Can only be up to 100 characters.
    component: :class:`Item`
        The component to display below the label. Currently only
        supports :class:`TextInput` and :class:`Select`.
    """

    __item_repr_attributes__: Tuple[str, ...] = (
        'text',
        'description',
        'component',
    )

    def __init__(
        self,
        *,
        text: str,
        component: Item[V],
        description: Optional[str] = None,
        id: Optional[int] = None,
    ) -> None:
        super().__init__()
        self.component: Item[V] = component
        self.text: str = text
        self.description: Optional[str] = description
        self.id = id

    @property
    def width(self) -> int:
        return 5

    def _has_children(self) -> bool:
        return True

    def walk_children(self) -> Generator[Item[V], None, None]:
        yield self.component

    def to_component_dict(self) -> LabelComponentPayload:
        payload: LabelComponentPayload = {
            'type': ComponentType.label.value,
            'label': self.text,
            'component': self.component.to_component_dict(),  # type: ignore
        }
        if self.description:
            payload['description'] = self.description
        if self.id is not None:
            payload['id'] = self.id
        return payload

    @classmethod
    def from_component(cls, component: LabelComponent) -> Self:
        from .view import _component_to_item

        self = cls(
            text=component.label,
            component=MISSING,
            description=component.description,
        )
        self.component = _component_to_item(component.component, self)
        return self

    @property
    def type(self) -> Literal[ComponentType.label]:
        return ComponentType.label

    def is_dispatchable(self) -> bool:
        return False