Skip to content

Commit ff11b8f

Browse files
authored
proper file upload support
1 parent c07f016 commit ff11b8f

File tree

3 files changed

+52
-34
lines changed

3 files changed

+52
-34
lines changed

discord/ui/file_upload.py

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from ..components import FileUpload as FileUploadComponent
77
from ..enums import ComponentType
88
from ..message import Attachment
9+
from .item import ModalItem
910

1011
__all__ = ("FileUpload",)
1112

@@ -14,21 +15,15 @@
1415
from ..types.components import FileUploadComponent as FileUploadComponentPayload
1516

1617

17-
class FileUpload:
18+
class FileUpload(ModalItem):
1819
"""Represents a UI File Upload component.
1920
2021
.. versionadded:: 2.7
2122
2223
Parameters
2324
----------
24-
label: :class:`str`
25-
The label for this component
26-
Must be 45 characters or fewer.
2725
custom_id: Optional[:class:`str`]
2826
The ID of the input text field that gets received during an interaction.
29-
description: Optional[:class:`str`]
30-
The description for the file upload field.
31-
Must be 100 characters or fewer.
3227
min_values: Optional[:class:`int`]
3328
The minimum number of files that must be uploaded.
3429
Defaults to 0 and must be between 0 and 10, inclusive.
@@ -37,41 +32,28 @@ class FileUpload:
3732
Must be between 1 and 10, inclusive.
3833
required: Optional[:class:`bool`]
3934
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).
35+
id: Optional[:class:`int`]
36+
The file upload field's ID.
4637
"""
4738

4839
__item_repr_attributes__: tuple[str, ...] = (
49-
"label",
5040
"required",
5141
"min_values",
5242
"max_values",
5343
"custom_id",
5444
"id",
55-
"description",
5645
)
5746

5847
def __init__(
5948
self,
6049
*,
61-
label: str,
6250
custom_id: str | None = None,
6351
min_values: int | None = None,
6452
max_values: int | None = None,
6553
required: bool = True,
66-
row: int | None = None,
6754
id: int | None = None,
68-
description: str | None = None,
6955
):
7056
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")
7557
if min_values and (min_values < 0 or min_values > 10):
7658
raise ValueError("min_values must be between 0 and 10")
7759
if max_values and (max_values < 1 or max_values > 10):
@@ -83,8 +65,6 @@ def __init__(
8365
if not isinstance(required, bool):
8466
raise TypeError(f"required must be bool not {required.__class__.__name__}") # type: ignore
8567
custom_id = os.urandom(16).hex() if custom_id is None else custom_id
86-
self.label: str = str(label)
87-
self.description: str | None = description
8868

8969
self._underlying: FileUploadComponent = FileUploadComponent._raw_construct(
9070
type=ComponentType.file_upload,
@@ -168,10 +148,6 @@ def values(self) -> list[Attachment] | None:
168148
"""The files that were uploaded to the field."""
169149
return self._attachments
170150

171-
@property
172-
def width(self) -> int:
173-
return 5
174-
175151
def to_component_dict(self) -> FileUploadComponentPayload:
176152
return self._underlying.to_dict()
177153

@@ -184,7 +160,3 @@ def refresh_from_modal(self, interaction: Interaction, data: dict) -> None:
184160
)
185161
for attachment_id in values
186162
]
187-
188-
@staticmethod
189-
def uses_label() -> bool:
190-
return True

discord/ui/label.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from .input_text import InputText
3636
from .item import ItemCallbackType, ModalItem
3737
from .select import Select
38+
from .file_upload import FileUpload
3839

3940
__all__ = ("Label",)
4041

@@ -59,6 +60,7 @@ class Label(ModalItem[M]):
5960
6061
- :class:`discord.ui.Select`
6162
- :class:`discord.ui.InputText`
63+
- :class:`discord.ui.FileUpload`
6264
6365
.. versionadded:: 2.7
6466
@@ -118,9 +120,8 @@ def set_item(self, item: ModalItem) -> Self:
118120
119121
Parameters
120122
----------
121-
item: Union[:class:`ModalItem`, :class:`InputText`]
123+
item: Union[:class:`ModalItem`]
122124
The item to set.
123-
Currently only supports :class:`~discord.ui.Select` and :class:`~discord.ui.InputText`.
124125
125126
Raises
126127
------
@@ -328,6 +329,46 @@ def set_select(
328329

329330
return self.set_item(select)
330331

332+
def set_file_upload(
333+
self,
334+
*,
335+
custom_id: str | None = None,
336+
min_values: int | None = None,
337+
max_values: int | None = None,
338+
required: bool | None = True,
339+
id: int | None = None,
340+
) -> Self:
341+
"""Set this label's item to a file upload.
342+
343+
To set a pre-existing :class:`FileUpload`, use the
344+
:meth:`set_item` method, instead.
345+
346+
Parameters
347+
----------
348+
custom_id: Optional[:class:`str`]
349+
The ID of the input text field that gets received during an interaction.
350+
min_values: Optional[:class:`int`]
351+
The minimum number of files that must be uploaded.
352+
Defaults to 0 and must be between 0 and 10, inclusive.
353+
max_values: Optional[:class:`int`]
354+
The maximum number of files that can be uploaded.
355+
Must be between 1 and 10, inclusive.
356+
required: Optional[:class:`bool`]
357+
Whether the file upload field is required or not. Defaults to ``True``.
358+
id: Optional[:class:`int`]
359+
The file upload field's ID.
360+
"""
361+
362+
upload = FileUpload(
363+
custom_id=custom_id,
364+
min_values=min_values,
365+
max_values=max_values,
366+
required=required,
367+
id=id,
368+
)
369+
370+
return self.set_item(upload)
371+
331372
@property
332373
def label(self) -> str:
333374
"""The label text. Must be 45 characters or fewer."""

docs/api/models.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,11 @@ UI Components
445445
:members:
446446
:inherited-members:
447447

448+
.. attributetable:: Label
449+
450+
.. autoclass:: Label()
451+
:members:
452+
448453
.. attributetable:: FileUpload
449454

450455
.. autoclass:: FileUpload()

0 commit comments

Comments
 (0)