-
-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathfile_upload.py
More file actions
200 lines (169 loc) · 7.27 KB
/
Copy pathfile_upload.py
File metadata and controls
200 lines (169 loc) · 7.27 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""
The MIT License (MIT)
Copyright (c) 2021-present Pycord Development
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 os
from typing import TYPE_CHECKING
from ..components import FileUpload as FileUploadComponent
from ..enums import ComponentType
from ..message import Attachment
from .item import ModalItem
__all__ = ("FileUpload",)
if TYPE_CHECKING:
from ..interactions import Interaction
from ..types.components import FileUploadComponent as FileUploadComponentPayload
class FileUpload(ModalItem):
"""Represents a UI File Upload component.
.. versionadded:: 2.7
Parameters
----------
custom_id: Optional[:class:`str`]
The ID of the file upload field that gets received during an interaction.
min_values: Optional[:class:`int`]
The minimum number of files that must be uploaded.
Defaults to 0 and must be between 0 and 10, inclusive.
max_values: Optional[:class:`int`]
The maximum number of files that can be uploaded.
Must be between 1 and 10, inclusive.
required: :class:`bool`
Whether the file upload field is required or not. Defaults to ``True``.
id: Optional[:class:`int`]
The file upload field's ID.
"""
__item_repr_attributes__: tuple[str, ...] = (
"required",
"min_values",
"max_values",
"custom_id",
"id",
)
def __init__(
self,
*,
custom_id: str | None = None,
min_values: int | None = None,
max_values: int | None = None,
required: bool = True,
id: int | None = None,
):
super().__init__()
if min_values and (min_values < 0 or min_values > 10):
raise ValueError("min_values must be between 0 and 10")
if max_values and (max_values < 1 or max_values > 10):
raise ValueError("max_values must be between 1 and 10")
if custom_id is not None and not isinstance(custom_id, str):
raise TypeError(
f"expected custom_id to be str, not {custom_id.__class__.__name__}"
)
if not isinstance(required, bool):
raise TypeError(f"required must be bool not {required.__class__.__name__}")
custom_id = os.urandom(16).hex() if custom_id is None else custom_id
self._attachments: list[Attachment] | None = None
self._underlying: FileUploadComponent = self._generate_underlying(
custom_id=custom_id,
min_values=min_values,
max_values=max_values,
required=required,
id=id,
)
def _generate_underlying(
self,
custom_id: str | None = None,
min_values: int | None = None,
max_values: int | None = None,
required: bool | None = None,
id: int | None = None,
) -> FileUploadComponent:
super()._generate_underlying(FileUploadComponent)
return FileUploadComponent._raw_construct(
type=ComponentType.file_upload,
custom_id=custom_id or self.custom_id,
min_values=min_values if min_values is not None else self.min_values,
max_values=max_values if max_values is not None else self.max_values,
required=required if required is not None else self.required,
id=id or self.id,
)
@property
def custom_id(self) -> str:
"""The custom id that gets received during an interaction."""
return self.underlying.custom_id
@custom_id.setter
def custom_id(self, value: str):
if not isinstance(value, str):
raise TypeError(f"custom_id must be str not {value.__class__.__name__}")
if len(value) > 100:
raise ValueError("custom_id must be 100 characters or fewer")
self.underlying.custom_id = value
@property
def min_values(self) -> int | None:
"""The minimum number of files that must be uploaded. Defaults to 0."""
return self.underlying.min_values
@min_values.setter
def min_values(self, value: int | None):
if value and not isinstance(value, int):
raise TypeError(f"min_values must be None or int not {value.__class__.__name__}") # type: ignore
if value and (value < 0 or value > 10):
raise ValueError("min_values must be between 0 and 10")
self.underlying.min_values = value
@property
def max_values(self) -> int | None:
"""The maximum number of files that can be uploaded."""
return self.underlying.max_values
@max_values.setter
def max_values(self, value: int | None):
if value and not isinstance(value, int):
raise TypeError(f"max_values must be None or int not {value.__class__.__name__}") # type: ignore
if value and (value < 1 or value > 10):
raise ValueError("max_values must be between 1 and 10")
self.underlying.max_values = value
@property
def required(self) -> bool:
"""Whether the input file upload is required or not. Defaults to ``True``."""
return self.underlying.required
@required.setter
def required(self, value: bool):
if not isinstance(value, bool):
raise TypeError(f"required must be bool not {value.__class__.__name__}") # type: ignore
self.underlying.required = bool(value)
@property
def values(self) -> list[Attachment] | None:
"""The files that were uploaded to the field. This will be ``None`` if the file upload has not been submitted via a modal yet."""
return self._attachments
def to_component_dict(self) -> FileUploadComponentPayload:
return self.underlying.to_dict()
def refresh_from_modal(self, interaction: Interaction, data: dict) -> None:
values = data.get("values", [])
self._attachments = [
Attachment(
state=interaction._state,
data=interaction.data["resolved"]["attachments"][attachment_id],
)
for attachment_id in values
]
@classmethod
def from_component(
cls: type[FileUpload], component: FileUploadComponent
) -> FileUpload:
return cls(
custom_id=component.custom_id,
min_values=component.min_values,
max_values=component.max_values,
required=component.required,
id=component.id,
)