Add file type filtering in slash command attachment options and modal attachment uploads - #3113
Add file type filtering in slash command attachment options and modal attachment uploads#3113freya022 wants to merge 16 commits into
Conversation
Sharing with attachment uploads and slash command options
…ns and attachment uploads
This is the regex used by Discord, it allows for multiple extensions (like `tar.gz`)
|
|
||
| private final String value; | ||
|
|
||
| @ApiStatus.Internal |
There was a problem hiding this comment.
Why this instead of just making it private?
There was a problem hiding this comment.
It's used in FileTypesImpl#fromArray where the raw value is passed.
| this.maxLength = json.getInt("max_length"); | ||
| } | ||
| this.fileTypes = | ||
| json.optArray("file_types").map(FileTypesImpl::fromArray).orElse(FileTypesImpl.empty()); |
There was a problem hiding this comment.
| json.optArray("file_types").map(FileTypesImpl::fromArray).orElse(FileTypesImpl.empty()); | |
| json.optArray("file_types").map(FileTypesImpl::fromArray).orElseGet(FileTypesImpl::empty); |
There was a problem hiding this comment.
I changed it to keep the orElse but replaced the value to be a constant, empty & immutable list.
| } | ||
|
|
||
| /** | ||
| * The <b>immutable</b> list of file types accepted by this option. |
There was a problem hiding this comment.
Immutable and unmodifiable view are different implications. Since the unmodifiable list can still change and thus throw exceptions when modified by a different thread.
There was a problem hiding this comment.
Even if a view is returned, Command is an immutable object, so the returned list is also immutable since there is no code that can change it, that's why it's documented and annotated as immutable/unmodifiable, and not as a view.
| } | ||
| } | ||
| if (type == OptionType.ATTACHMENT) { | ||
| json.put("file_types", fileTypes.toData()); |
There was a problem hiding this comment.
So what happens here when fileTypes is still empty (initial state)?
There was a problem hiding this comment.
It sends an empty array, which is the same as if the field was absent, accepting any attachment.
Co-authored-by: Florian Spieß <business@minn.dev>
Co-authored-by: Florian Spieß <business@minn.dev>
Makes the payload smaller and avoids (third party) command cache flushes Co-authored-by: Florian Spieß <business@minn.dev>
Pull Request Etiquette
./gradlew formatChanges
Closes Issue: NaN
Description
This PR enables slash commands options, and modal attachment uploads, to accept attachments with specific extensions or media type (image, video, audio ; the list of extensions implied by these is not documented on purpose).
Docs PR: discord/discord-api-docs#8506
Changelog: https://docs.discord.com/developers/change-log#filter-file-types-in-file-uploads-and-attachment-options
Examples
Slash commands
Modals
The same methods exist on
AttachmentUpload.Builder, with the same limit (10) and same behavior.