Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,24 @@
import net.dv8tion.jda.api.components.Component;
import net.dv8tion.jda.api.components.attribute.ICustomId;
import net.dv8tion.jda.api.components.label.LabelChildComponent;
import net.dv8tion.jda.api.interactions.FileType;
import net.dv8tion.jda.api.interactions.IFilterableFileTypes;
import net.dv8tion.jda.internal.components.attachmentupload.AttachmentUploadImpl;
import net.dv8tion.jda.internal.interactions.FileTypesImpl;
import net.dv8tion.jda.internal.utils.Checks;
import org.jetbrains.annotations.UnmodifiableView;

import java.util.Collection;
import java.util.List;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;

/**
* Component accepting files from users.
*
* <p>The user can send up to {@value #MAX_UPLOADS} files, the requested number of files can be adjusted or be completely optional.
* <p>The user can send up to {@value #MAX_UPLOADS} files, the requested number of files can be adjusted or be completely optional,
* additionally, the accepted file types can be configured.
*
* <p>Can only be used inside {@link net.dv8tion.jda.api.components.label.Label Labels}!
*/
Expand Down Expand Up @@ -92,6 +100,16 @@ static AttachmentUpload of(@Nonnull String customId) {
*/
int getMaxValues();

/**
* The <b>unmodifiable</b> list <b>view</b> of file types to filter for.
* Returns an empty list if any file is accepted.
*
* @return Unmodifiable list view of file types
*/
@Nonnull
@UnmodifiableView
List<FileType> getFileTypes();

/**
* Whether the user must send attachments.
*
Expand All @@ -106,11 +124,12 @@ static AttachmentUpload of(@Nonnull String customId) {
/**
* Builder for {@link AttachmentUpload AttachmentUploads}.
*/
class Builder {
class Builder implements IFilterableFileTypes<Builder> {
protected int uniqueId = -1;
protected String customId;
protected int minValues = 1;
protected int maxValues = 1;
protected final FileTypesImpl fileTypes = FileTypesImpl.empty();
protected boolean required = true;

protected Builder(@Nonnull String customId) {
Expand Down Expand Up @@ -206,6 +225,20 @@ public Builder setRequiredRange(int min, int max) {
return setMinValues(min).setMaxValues(max);
}

@Nonnull
@Override
public Builder addFileTypes(@Nonnull Collection<FileType> fileTypes) {
this.fileTypes.addAll(fileTypes);
return this;
}

@Nonnull
@Override
public Builder setFileTypes(@Nonnull Collection<FileType> fileTypes) {
this.fileTypes.setAll(fileTypes);
return this;
}

/**
* Changes whether the user must upload files.
* <br>Default: {@code true}
Expand Down Expand Up @@ -264,6 +297,18 @@ public int getMaxValues() {
return maxValues;
}

/**
* The <b>unmodifiable</b> list <b>view</b> of file types to filter for.
* Returns an empty list if any file is accepted.
*
* @return Unmodifiable list view of file types
*/
@Nonnull
@UnmodifiableView
public List<FileType> getFileTypes() {
return fileTypes.asView();
}

/**
* Whether the user must send attachments.
*
Expand All @@ -288,7 +333,7 @@ public boolean isRequired() {
@Nonnull
public AttachmentUpload build() {
Checks.check(maxValues >= minValues, "Max (%s) must be higher or equal to min (%s)", maxValues, minValues);
return new AttachmentUploadImpl(uniqueId, customId, minValues, maxValues, required);
return new AttachmentUploadImpl(uniqueId, customId, minValues, maxValues, fileTypes, required);
}
}
}
133 changes: 133 additions & 0 deletions src/main/java/net/dv8tion/jda/api/interactions/FileType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dv8tion.jda.api.interactions;

import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.EntityString;
import org.jetbrains.annotations.ApiStatus;

import java.util.Objects;
import java.util.regex.Pattern;

import javax.annotation.Nonnull;

/**
* Represents a type of file, you can use presets such as {@link #IMAGE},
* or specify an extension using {@link #ofExtension(String)}.
*
* <p>Remember that this only checks the extension, Discord does not check for any file signature/MIME type,
* and thus does not guarantee receiving a valid file.
*/
public final class FileType {
private static final Pattern EXTENSION_PATTERN = Pattern.compile("[\\w\\-.]+");

/** Matches any image supported by the Discord client. */
public static final FileType IMAGE = new FileType("image");
/** Matches any video supported by the Discord client. */
public static final FileType VIDEO = new FileType("video");
/** Matches any audio supported by the Discord client. */
public static final FileType AUDIO = new FileType("audio");

private final String value;

@ApiStatus.Internal

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.

Why this instead of just making it private?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's used in FileTypesImpl#fromArray where the raw value is passed.

public FileType(String value) {
this.value = value;
}

/**
* Creates a {@link FileType} matching the provided extension.
*
* @param extension
* The extension to match against.
*
* @throws IllegalArgumentException
* <ul>
* <li>If the extension is {@code null} or empty</li>
* <li>If the extension does not match {@code [\w\-.]+} (latin letters, digits, dashes or dots)</li>
* </ul>
*
* @return The new {@link FileType}
*/
@Nonnull
public static FileType ofExtension(@Nonnull String extension) {
Checks.matches(extension, EXTENSION_PATTERN, "Extension");
return new FileType("." + extension);
}

/**
* Whether this file type accepts all image formats supported by Discord.
*
* @return {@code true} if this file type accepts all image formats supported by Discord, {@code false} if not
*/
public boolean isImage() {
return value.equals("image");
}

/**
* Whether this file type accepts all video formats supported by Discord.
*
* @return {@code true} if this file type accepts all video formats supported by Discord, {@code false} if not
*/
public boolean isVideo() {
return value.equals("video");
}

/**
* Whether this file type accepts all audio formats supported by Discord.
*
* @return {@code true} if this file type accepts all audio formats supported by Discord, {@code false} if not
*/
public boolean isAudio() {
return value.equals("audio");
}

/**
* The raw value of this file type.
*
* @return The raw value
*/
@Nonnull
public String getValue() {
return value;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof FileType)) {
return false;
}
FileType that = (FileType) o;
return Objects.equals(value, that.value);
}

@Override
public int hashCode() {
return Objects.hashCode(value);
}

@Override
public String toString() {
return new EntityString(this)
.setName("FileType")
.addMetadata("value", value)
.toString();
}
}
Loading