Skip to content

feat: File type filtering for Attachment options and FileUpload component - #3284

Open
NeloBlivion wants to merge 11 commits into
Pycord-Development:masterfrom
NeloBlivion:filetype
Open

feat: File type filtering for Attachment options and FileUpload component#3284
NeloBlivion wants to merge 11 commits into
Pycord-Development:masterfrom
NeloBlivion:filetype

Conversation

@NeloBlivion

Copy link
Copy Markdown
Member

Summary

Support file_types in Option and FileUpload.

Information

  • This PR fixes an issue.
  • This PR adds something new (e.g. new method or parameters).
  • This PR is a breaking change (e.g. methods or parameters removed/renamed).
  • This PR is not a code change (e.g. documentation, README, typehinting,
    examples, ...).

Checklist

  • I have searched the open pull requests for duplicates.
  • If code changes were made then they have been tested.
    • I have updated the documentation to reflect the changes.
  • If type: ignore comments were used, a comment is also left explaining why.
  • I have updated the changelog to include these changes.
  • AI Usage has been disclosed.
    • If AI has been used, I understand fully what the code does

@NeloBlivion
NeloBlivion requested review from a team and Paillat-dev June 23, 2026 04:23
@NeloBlivion
NeloBlivion requested a review from Lulalaby June 23, 2026 04:24
@github-project-automation github-project-automation Bot moved this to Todo in Pycord Jun 23, 2026
@pycord-app

pycord-app Bot commented Jun 23, 2026

Copy link
Copy Markdown

Thanks for opening this pull request!
Please make sure you have read the Contributing Guidelines and Code of Conduct.

This pull request can be checked-out with:

git fetch origin pull/3284/head:pr-3284
git checkout pr-3284

This pull request can be installed with:

pip install git+https://github.com/Pycord-Development/pycord@refs/pull/3284/head

@NeloBlivion NeloBlivion modified the milestones: 2.9.0, 2.9.0rc1 Jun 23, 2026
@Soheab Soheab added the priority: low Low Priority label Jul 26, 2026
@Soheab Soheab added feature Implements a feature upcoming discord feature Involves a feature not yet fully released by Discord undocumented discord feature This isn't documented, might not receive support for it! labels Jul 26, 2026
Comment thread docs/api/enums.rst
Comment on lines +2711 to +2721
.. attribute:: image

Allow image files. This includes ``.png``, ``.gif``, ``.jpg``, ``.jpeg``, ``.jfif``, ``.webp``, and ``.avif``.

.. attribute:: video

Allow video files. This includes ``.mp4``, ``.mov``, ``.qt``, and ``.webm``.

.. attribute:: audio

Allow audio files. This includes ``.mp3``, ``.m4a``, ``.wav``, ``.ogg``, ``.opus``, and ``.flac``.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented twice? Docstring in the class too. Not sure if that's even considered.

raise TypeError(
"items in file_types must be of type str or FileType"
)
if len(str(f)) > 16:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also checks the length of our enum values, which is a bit useless?

@Paillat-dev Paillat-dev added hold: testing This pull request requires further testing hold: changelog This pull request is missing a changelog entry labels Jul 26, 2026

@vmphase vmphase left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing changelog.

Comment thread discord/components.py
payload["required"] = self.required

if self.file_types:
payload["file_types"] = [str(f) for f in self.file_types]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

str(FileType) won't serialize correctly, since it subclasses Enum. Either make it subclass StrEnum, or explicitly serialize with .value when the item is a FileType.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it a string enum

if self.max_length is not None:
as_dict["max_length"] = self.max_length
if self.file_types:
as_dict["file_types"] = [str(f) for f in self.file_types]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

str(FileType) won't serialize correctly, since it subclasses Enum. Either make it subclass StrEnum, or explicitly serialize with .value when the item is a FileType.

Comment thread discord/components.py
Comment on lines +1424 to +1429
for f in data.get("file_types", []):
(
self.file_types.append(f)
if f not in FileType.__members__
else try_enum(FileType, f)
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure the logic here matches what you were planning, unless I missed something. Currently, when f is a known FileType member, it goes into the else branch and calls try_enum(FileType, f), but the result is never appended anywhere, so it's just discarded. Only unknown values are in self.file_types.

Comment thread discord/ui/file_upload.py
Whether the file upload field is required or not. Defaults to ``True``.
id: Optional[:class:`int`]
The file upload field's ID.
file_types: List[:class:`str`, :class:`FileType`]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List[X, Y] is not valid, because it only takes one type parameter.

Suggested change
file_types: List[:class:`str`, :class:`FileType`]
file_types: List[Union[:class:`str`, :class:`FileType`]]

Comment thread discord/components.py
The maximum number of files that can be uploaded.
required: Optional[:class:`bool`]
Whether the file upload field is required or not. Defaults to `True`.
file_types: List[:class:`str`, :class:`FileType`]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List[X, Y] is not valid, because it only takes one type parameter.

Suggested change
file_types: List[:class:`str`, :class:`FileType`]
file_types: List[Union[:class:`str`, :class:`FileType`]]

description_localizations: Dict[:class:`str`, :class:`str`]
The description localizations for this option. The values of this should be ``"locale": "description"``.
See `here <https://docs.discord.com/developers/reference#locales>`_ for a list of valid locales.
file_types: List[:class:`str`, :class:`FileType`]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List[X, Y] is not valid, because it only takes one type parameter.

Suggested change
file_types: List[:class:`str`, :class:`FileType`]
file_types: List[Union[:class:`str`, :class:`FileType`]]

@Lulalaby Lulalaby left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we potentially add a check if the provided file type string starts with .?

@vmphase

vmphase commented Jul 31, 2026

Copy link
Copy Markdown
Member

can we potentially add a check if the provided file type string starts with .?

Was just typing that, you were faster
=)
Currently garbage like "hello" up to 16 characters passes the validation, even though the docs say "dot-prefixed extensions".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Implements a feature hold: changelog This pull request is missing a changelog entry hold: testing This pull request requires further testing PA: All Contributors pending priority: low Low Priority undocumented discord feature This isn't documented, might not receive support for it! upcoming discord feature Involves a feature not yet fully released by Discord

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

5 participants