|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +from ..message import Attachment |
| 7 | +from ..components import FileUpload as FileUploadComponent |
| 8 | +from ..enums import ComponentType |
| 9 | + |
| 10 | +__all__ = ("FileUpload",) |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from ..interactions import Interaction |
| 14 | + from ..types.components import FileUploadComponent as FileUploadComponentPayload |
| 15 | + |
| 16 | + |
| 17 | +class FileUpload: |
| 18 | + """Represents a UI file upload field. |
| 19 | +
|
| 20 | + .. versionadded:: 2.7 |
| 21 | +
|
| 22 | + Parameters |
| 23 | + ---------- |
| 24 | + custom_id: Optional[:class:`str`] |
| 25 | + The ID of the input text field that gets received during an interaction. |
| 26 | + label: :class:`str` |
| 27 | + The label for the file upload field. |
| 28 | + Must be 45 characters or fewer. |
| 29 | + description: Optional[:class:`str`] |
| 30 | + The description for the file upload field. |
| 31 | + Must be 100 characters or fewer. |
| 32 | + min_values: Optional[:class:`int`] |
| 33 | + The minimum number of files that must be uploaded. |
| 34 | + Defaults to 0 and must be between 0 and 10, inclusive. |
| 35 | + max_values: Optional[:class:`int`] |
| 36 | + The maximum number of files that can be uploaded. |
| 37 | + Must be between 1 and 10, inclusive. |
| 38 | + required: Optional[:class:`bool`] |
| 39 | + Whether the file upload field is required or not. Defaults to ``True``. |
| 40 | + row: Optional[:class:`int`] |
| 41 | + The relative row this file upload field belongs to. A modal dialog can only have 5 |
| 42 | + rows. By default, items are arranged automatically into those 5 rows. If you'd |
| 43 | + like to control the relative positioning of the row then passing an index is advised. |
| 44 | + For example, row=1 will show up before row=2. Defaults to ``None``, which is automatic |
| 45 | + ordering. The row number must be between 0 and 4 (i.e. zero indexed). |
| 46 | + """ |
| 47 | + |
| 48 | + __item_repr_attributes__: tuple[str, ...] = ( |
| 49 | + "label", |
| 50 | + "required", |
| 51 | + "min_values", |
| 52 | + "max_values", |
| 53 | + "custom_id", |
| 54 | + "id", |
| 55 | + "description", |
| 56 | + ) |
| 57 | + |
| 58 | + def __init__( |
| 59 | + self, |
| 60 | + *, |
| 61 | + custom_id: str | None = None, |
| 62 | + label: str, |
| 63 | + min_values: int | None = None, |
| 64 | + max_values: int | None = None, |
| 65 | + required: bool | None = True, |
| 66 | + row: int | None = None, |
| 67 | + id: int | None = None, |
| 68 | + description: str | None = None, |
| 69 | + ): |
| 70 | + super().__init__() |
| 71 | + if len(str(label)) > 45: |
| 72 | + raise ValueError("label must be 45 characters or fewer") |
| 73 | + if description and len(description) > 100: |
| 74 | + raise ValueError("description must be 100 characters or fewer") |
| 75 | + if min_values and (min_values < 0 or min_values > 10): |
| 76 | + raise ValueError("min_values must be between 0 and 10") |
| 77 | + if max_values and (max_values < 1 or max_values > 10): |
| 78 | + raise ValueError("max_length must be between 1 and 10") |
| 79 | + if not isinstance(custom_id, str) and custom_id is not None: |
| 80 | + raise TypeError( |
| 81 | + f"expected custom_id to be str, not {custom_id.__class__.__name__}" |
| 82 | + ) |
| 83 | + custom_id = os.urandom(16).hex() if custom_id is None else custom_id |
| 84 | + self.label: str = str(label) |
| 85 | + self.description: str | None = description |
| 86 | + |
| 87 | + self._underlying: FileUploadComponent = FileUploadComponent._raw_construct( |
| 88 | + type=ComponentType.file_upload, |
| 89 | + custom_id=custom_id, |
| 90 | + label=label, |
| 91 | + min_values=min_values, |
| 92 | + max_values=max_values, |
| 93 | + required=required, |
| 94 | + id=id, |
| 95 | + ) |
| 96 | + self._interaction: Interaction | None = None |
| 97 | + self._values: list[str] | None = None |
| 98 | + self.row = row |
| 99 | + self._rendered_row: int | None = None |
| 100 | + |
| 101 | + def __repr__(self) -> str: |
| 102 | + attrs = " ".join( |
| 103 | + f"{key}={getattr(self, key)!r}" for key in self.__item_repr_attributes__ |
| 104 | + ) |
| 105 | + return f"<{self.__class__.__name__} {attrs}>" |
| 106 | + |
| 107 | + @property |
| 108 | + def type(self) -> ComponentType: |
| 109 | + return self._underlying.type |
| 110 | + |
| 111 | + @property |
| 112 | + def id(self) -> int | None: |
| 113 | + """The file upload's ID. If not provided by the user, it is set sequentially by Discord.""" |
| 114 | + return self._underlying.id |
| 115 | + |
| 116 | + @property |
| 117 | + def custom_id(self) -> str: |
| 118 | + """The ID of the file upload field that gets received during an interaction.""" |
| 119 | + return self._underlying.custom_id |
| 120 | + |
| 121 | + @custom_id.setter |
| 122 | + def custom_id(self, value: str): |
| 123 | + if not isinstance(value, str): |
| 124 | + raise TypeError( |
| 125 | + f"custom_id must be None or str not {value.__class__.__name__}" |
| 126 | + ) |
| 127 | + self._underlying.custom_id = value |
| 128 | + |
| 129 | + @property |
| 130 | + def min_values(self) -> int | None: |
| 131 | + """The minimum number of files that must be uploaded. Defaults to 0.""" |
| 132 | + return self._underlying.min_values |
| 133 | + |
| 134 | + @min_values.setter |
| 135 | + def min_values(self, value: int | None): |
| 136 | + if value and not isinstance(value, int): |
| 137 | + raise TypeError(f"min_values must be None or int not {value.__class__.__name__}") # type: ignore |
| 138 | + if value and (value < 0 or value) > 4000: |
| 139 | + raise ValueError("min_values must be between 0 and 10") |
| 140 | + self._underlying.min_values = value |
| 141 | + |
| 142 | + @property |
| 143 | + def max_values(self) -> int | None: |
| 144 | + """The maximum number of files that can be uploaded.""" |
| 145 | + return self._underlying.max_values |
| 146 | + |
| 147 | + @max_values.setter |
| 148 | + def max_values(self, value: int | None): |
| 149 | + if value and not isinstance(value, int): |
| 150 | + raise TypeError(f"max_values must be None or int not {value.__class__.__name__}") # type: ignore |
| 151 | + if value and (value <= 0 or value > 4000): |
| 152 | + raise ValueError("max_values must be between 1 and 10") |
| 153 | + self._underlying.max_values = value |
| 154 | + |
| 155 | + @property |
| 156 | + def required(self) -> bool | None: |
| 157 | + """Whether the input file upload is required or not. Defaults to ``True``.""" |
| 158 | + return self._underlying.required |
| 159 | + |
| 160 | + @required.setter |
| 161 | + def required(self, value: bool | None): |
| 162 | + if not isinstance(value, bool): |
| 163 | + raise TypeError(f"required must be bool not {value.__class__.__name__}") # type: ignore |
| 164 | + self._underlying.required = bool(value) |
| 165 | + |
| 166 | + @property |
| 167 | + def values(self) -> list[Attachment] | None: |
| 168 | + """The files that were uploaded to the field.""" |
| 169 | + if self._interaction is None: |
| 170 | + return None |
| 171 | + attachments = [] |
| 172 | + for attachment_id in self._values: |
| 173 | + print(attachment_id) |
| 174 | + attachment_data = self._interaction.data["resolved"]["attachments"][attachment_id] |
| 175 | + attachments.append(Attachment(state=self._interaction._state, data=attachment_data)) |
| 176 | + return attachments |
| 177 | + |
| 178 | + @property |
| 179 | + def width(self) -> int: |
| 180 | + return 5 |
| 181 | + |
| 182 | + def to_component_dict(self) -> FileUploadComponentPayload: |
| 183 | + return self._underlying.to_dict() |
| 184 | + |
| 185 | + def refresh_from_modal(self, interaction: Interaction, data: dict) -> None: |
| 186 | + print(data) |
| 187 | + self._interaction = interaction |
| 188 | + self._values = data.get("values", []) |
| 189 | + |
| 190 | + @staticmethod |
| 191 | + def uses_label() -> bool: |
| 192 | + return True |
| 193 | + |
0 commit comments