|
| 1 | +""" |
| 2 | +The MIT License (MIT) |
| 3 | +
|
| 4 | +Copyright (c) 2021-present Pycord Development |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | +copy of this software and associated documentation files (the "Software"), |
| 8 | +to deal in the Software without restriction, including without limitation |
| 9 | +the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | +and/or sell copies of the Software, and to permit persons to whom the |
| 11 | +Software is furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in |
| 14 | +all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 17 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | +DEALINGS IN THE SOFTWARE. |
| 23 | +""" |
| 24 | + |
| 25 | +from __future__ import annotations |
| 26 | + |
| 27 | +from typing import TYPE_CHECKING, ClassVar, Literal |
| 28 | + |
| 29 | +from typing_extensions import override |
| 30 | + |
| 31 | +from ..enums import ComponentType |
| 32 | +from ..types.components import FileUpload as FileUploadPayload |
| 33 | +from .component import Component, ModalComponentMixin |
| 34 | + |
| 35 | +if TYPE_CHECKING: |
| 36 | + from typing_extensions import Self |
| 37 | + |
| 38 | + |
| 39 | +class FileUpload(ModalComponentMixin[FileUploadPayload], Component[FileUploadPayload]): |
| 40 | + """Represents a File Upload Component. |
| 41 | +
|
| 42 | + This component displays a file upload box in a :class:`Modal`. |
| 43 | +
|
| 44 | + This inherits from :class:`Component`. |
| 45 | +
|
| 46 | + .. versionadded:: 3.0 |
| 47 | +
|
| 48 | + Attributes |
| 49 | + ---------- |
| 50 | + type: Literal[:data:`ComponentType.file_upload`] |
| 51 | + The type of component. |
| 52 | + custom_id: :class:`str` |
| 53 | + The custom ID of the file upload component that gets received during an interaction. |
| 54 | + min_values: :class:`int` |
| 55 | + The minimum number of files that must be uploaded. |
| 56 | + max_values: :class:`int` |
| 57 | + The maximum number of files that can be uploaded. |
| 58 | + required: :class:`bool` |
| 59 | + Whether the file upload is required to submit the modal. |
| 60 | + id: :class:`int` | :data:`None` |
| 61 | + The section's ID. |
| 62 | +
|
| 63 | + Parameters |
| 64 | + ---------- |
| 65 | + id: |
| 66 | + The component's ID. If not provided by the user, it is set sequentially by Discord. |
| 67 | + The ID `0` is treated as if no ID was provided. |
| 68 | + """ |
| 69 | + |
| 70 | + __slots__: tuple[str, ...] = ( |
| 71 | + "file", |
| 72 | + "spoiler", |
| 73 | + "name", |
| 74 | + "size", |
| 75 | + ) |
| 76 | + |
| 77 | + __repr_info__: ClassVar[tuple[str, ...]] = __slots__ |
| 78 | + versions: tuple[int, ...] = (2,) |
| 79 | + type: Literal[ComponentType.file_upload] = ComponentType.file_upload # pyright: ignore[reportIncompatibleVariableOverride] |
| 80 | + |
| 81 | + def __init__( |
| 82 | + self, |
| 83 | + custom_id: str, |
| 84 | + id: int | None = None, |
| 85 | + min_values: int = 1, |
| 86 | + max_values: int = 1, |
| 87 | + required: bool = True, |
| 88 | + ) -> None: |
| 89 | + self.custom_id: str = custom_id |
| 90 | + self.min_values: int = min_values |
| 91 | + self.max_values: int = max_values |
| 92 | + self.required: bool = required |
| 93 | + super().__init__(id=id) |
| 94 | + |
| 95 | + @classmethod |
| 96 | + @override |
| 97 | + def from_payload(cls, payload: FileUploadPayload) -> Self: |
| 98 | + return cls( |
| 99 | + custom_id=payload["custom_id"], |
| 100 | + id=payload["id"], |
| 101 | + min_values=payload.get("min_values", 1), |
| 102 | + max_values=payload.get("max_values", 1), |
| 103 | + required=payload.get("required", True), |
| 104 | + ) |
| 105 | + |
| 106 | + @override |
| 107 | + def to_dict(self, modal: bool = True) -> FileUploadPayload: |
| 108 | + payload: FileUploadPayload = { |
| 109 | + "type": int(self.type), |
| 110 | + "custom_id": self.custom_id, |
| 111 | + "min_values": self.min_values, |
| 112 | + "max_values": self.max_values, |
| 113 | + "required": self.required, |
| 114 | + "id": self.id, |
| 115 | + } |
| 116 | + return payload |
0 commit comments