|
| 1 | +/* |
| 2 | + * Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package net.dv8tion.jda.api.components.attachmentupload; |
| 18 | + |
| 19 | +import net.dv8tion.jda.api.components.Component; |
| 20 | +import net.dv8tion.jda.api.components.attribute.ICustomId; |
| 21 | +import net.dv8tion.jda.api.components.label.LabelChildComponent; |
| 22 | +import net.dv8tion.jda.internal.components.attachmentupload.AttachmentUploadImpl; |
| 23 | +import net.dv8tion.jda.internal.utils.Checks; |
| 24 | + |
| 25 | +import javax.annotation.CheckReturnValue; |
| 26 | +import javax.annotation.Nonnull; |
| 27 | + |
| 28 | +/** |
| 29 | + * Component accepting files from users. |
| 30 | + * |
| 31 | + * <p>The user can send up to {@value #MAX_UPLOADS} files, the requested number of files can be adjusted or be completely optional. |
| 32 | + * |
| 33 | + * <p>Can only be used inside {@link net.dv8tion.jda.api.components.label.Label Labels}! |
| 34 | + */ |
| 35 | +public interface AttachmentUpload extends Component, ICustomId, LabelChildComponent |
| 36 | +{ |
| 37 | + /** The maximum number of files the user can upload at once ({@value}) */ |
| 38 | + int MAX_UPLOADS = 10; |
| 39 | + |
| 40 | + /** The maximum length a custom ID can have ({@value}) */ |
| 41 | + int ID_MAX_LENGTH = 100; |
| 42 | + |
| 43 | + /** |
| 44 | + * Creates a new {@link AttachmentUpload.Builder} with the provided custom ID. |
| 45 | + * <br>By default, the user will be <b>required</b> to submit a <b>single</b> attachment. |
| 46 | + * |
| 47 | + * @param customId |
| 48 | + * The custom ID of this component, can be used to pass data, then read in an interaction |
| 49 | + * |
| 50 | + * @throws IllegalArgumentException |
| 51 | + * If {@code customId} is {@code null}, blank, or longer than {@value #ID_MAX_LENGTH} characters |
| 52 | + * |
| 53 | + * @return The new {@link AttachmentUpload.Builder} |
| 54 | + */ |
| 55 | + @Nonnull |
| 56 | + static AttachmentUpload.Builder create(@Nonnull String customId) |
| 57 | + { |
| 58 | + return new AttachmentUpload.Builder(customId); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Creates a new {@link AttachmentUpload} with the provided custom ID. |
| 63 | + * <br>The user will be <b>required</b> to submit a <b>single</b> attachment. |
| 64 | + * |
| 65 | + * @param customId |
| 66 | + * The custom ID of this component, can be used to pass data, then read in an interaction |
| 67 | + * |
| 68 | + * @throws IllegalArgumentException |
| 69 | + * If {@code customId} is {@code null}, blank, or longer than {@value #ID_MAX_LENGTH} characters |
| 70 | + * |
| 71 | + * @return The new {@link AttachmentUpload} |
| 72 | + */ |
| 73 | + @Nonnull |
| 74 | + static AttachmentUpload of(@Nonnull String customId) |
| 75 | + { |
| 76 | + return create(customId).build(); |
| 77 | + } |
| 78 | + |
| 79 | + @Nonnull |
| 80 | + @Override |
| 81 | + @CheckReturnValue |
| 82 | + AttachmentUpload withUniqueId(int uniqueId); |
| 83 | + |
| 84 | + /** |
| 85 | + * The minimum amount of attachments the user must send. |
| 86 | + * |
| 87 | + * @return Minimum amount of attachments the user must send |
| 88 | + */ |
| 89 | + int getMinValues(); |
| 90 | + |
| 91 | + /** |
| 92 | + * The maximum amount of attachments the user can send. |
| 93 | + * |
| 94 | + * @return Maximum amount of attachments the user can send |
| 95 | + */ |
| 96 | + int getMaxValues(); |
| 97 | + |
| 98 | + /** |
| 99 | + * Whether the user must send attachments. |
| 100 | + * |
| 101 | + * <p>This attribute is completely separate from the value range, |
| 102 | + * for example you can have an optional {@link AttachmentUpload} with the range set to {@code [2 ; 2]}, |
| 103 | + * meaning you accept either 0 attachments, or 2. |
| 104 | + * |
| 105 | + * @return {@code true} if files must be uploaded, {@code false} if not |
| 106 | + */ |
| 107 | + boolean isRequired(); |
| 108 | + |
| 109 | + /** |
| 110 | + * Builder for {@link AttachmentUpload AttachmentUploads}. |
| 111 | + */ |
| 112 | + class Builder |
| 113 | + { |
| 114 | + protected int uniqueId = -1; |
| 115 | + protected String customId; |
| 116 | + protected int minValues = 1; |
| 117 | + protected int maxValues = 1; |
| 118 | + protected boolean required = true; |
| 119 | + |
| 120 | + protected Builder(@Nonnull String customId) |
| 121 | + { |
| 122 | + setCustomId(customId); |
| 123 | + } |
| 124 | + |
| 125 | + @Nonnull |
| 126 | + public Builder setUniqueId(int uniqueId) |
| 127 | + { |
| 128 | + Checks.positive(uniqueId, "Unique ID"); |
| 129 | + this.uniqueId = uniqueId; |
| 130 | + return this; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Changes the custom ID. |
| 135 | + * <br>This ID can be used to pass data, then read in an interaction. |
| 136 | + * |
| 137 | + * @param customId |
| 138 | + * The new custom ID |
| 139 | + * |
| 140 | + * @throws IllegalArgumentException |
| 141 | + * If {@code customId} is {@code null}, blank, or longer than {@value #ID_MAX_LENGTH} characters |
| 142 | + * |
| 143 | + * @return The same instance, for chaining purposes |
| 144 | + */ |
| 145 | + @Nonnull |
| 146 | + public Builder setCustomId(@Nonnull String customId) |
| 147 | + { |
| 148 | + Checks.notBlank(customId, "Custom ID"); |
| 149 | + Checks.notLonger(customId, ID_MAX_LENGTH, "Custom ID"); |
| 150 | + this.customId = customId; |
| 151 | + return this; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Changes the minimum amount of attachments the user has to send. |
| 156 | + * <br>Default: {@code 1} |
| 157 | + * |
| 158 | + * @param minValues |
| 159 | + * The new minimum amount of attachments the user has to send, must be >= 0 and less than {@value #MAX_UPLOADS} |
| 160 | + * |
| 161 | + * @throws IllegalArgumentException |
| 162 | + * If {@code minValues} is negative or larger than {@value #MAX_UPLOADS} |
| 163 | + * |
| 164 | + * @return The same instance, for chaining purposes |
| 165 | + */ |
| 166 | + @Nonnull |
| 167 | + public Builder setMinValues(int minValues) |
| 168 | + { |
| 169 | + Checks.notNegative(minValues, "Min values"); |
| 170 | + Checks.check(minValues <= MAX_UPLOADS, "Min values (%s) must be lower than %s", minValues, MAX_UPLOADS); |
| 171 | + this.minValues = minValues; |
| 172 | + return this; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Changes the maximum amount of attachments the user can send. |
| 177 | + * <br>Default: {@code 1} |
| 178 | + * |
| 179 | + * @param maxValues |
| 180 | + * The new maximum amount of attachments the user can send, must be positive and less than {@value #MAX_UPLOADS} |
| 181 | + * |
| 182 | + * @throws IllegalArgumentException |
| 183 | + * If {@code maxValues} is negative, zero, or larger than {@value #MAX_UPLOADS} |
| 184 | + * |
| 185 | + * @return The same instance, for chaining purposes |
| 186 | + */ |
| 187 | + @Nonnull |
| 188 | + public Builder setMaxValues(int maxValues) |
| 189 | + { |
| 190 | + Checks.positive(maxValues, "Max values"); |
| 191 | + Checks.check(maxValues <= MAX_UPLOADS, "Max values (%s) must be lower than %s", maxValues, MAX_UPLOADS); |
| 192 | + this.maxValues = maxValues; |
| 193 | + return this; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Changes the amounts of attachments the user can send. |
| 198 | + * <br>Default: {@code [1 ; 1]} |
| 199 | + * |
| 200 | + * @param min |
| 201 | + * The new minimum amount of attachments the user must send, must be >= 0 and less than {@value #MAX_UPLOADS} |
| 202 | + * @param max |
| 203 | + * The new maximum amount of attachments the user can send, must be positive and less than {@value #MAX_UPLOADS} |
| 204 | + * |
| 205 | + * @throws IllegalArgumentException |
| 206 | + * <ul> |
| 207 | + * <li>If {@code min} is negative or larger than {@value #MAX_UPLOADS}</li> |
| 208 | + * <li>If {@code max} is negative, zero, or larger than {@value #MAX_UPLOADS}</li> |
| 209 | + * </ul> |
| 210 | + * |
| 211 | + * @return The same instance, for chaining purposes |
| 212 | + */ |
| 213 | + @Nonnull |
| 214 | + public Builder setRequiredRange(int min, int max) |
| 215 | + { |
| 216 | + return setMinValues(min).setMaxValues(max); |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Changes whether the user must upload files. |
| 221 | + * <br>Default: {@code true} |
| 222 | + * |
| 223 | + * <p>This attribute is completely separate from the value range, |
| 224 | + * for example you can have an optional {@link AttachmentUpload} with the range set to {@code [2 ; 2]}, |
| 225 | + * meaning you accept either 0 attachments, or 2. |
| 226 | + * |
| 227 | + * @param required |
| 228 | + * The new required status |
| 229 | + * |
| 230 | + * @return The same instance, for chaining purposes |
| 231 | + */ |
| 232 | + @Nonnull |
| 233 | + public Builder setRequired(boolean required) |
| 234 | + { |
| 235 | + this.required = required; |
| 236 | + return this; |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * The unique, numeric identifier of this component. |
| 241 | + * <br>Can be set manually or automatically assigned by Discord (starting from {@code 1}). |
| 242 | + * If it has not been assigned yet, this will return {@code -1}. |
| 243 | + * |
| 244 | + * @return The unique identifier of this component, or {@code -1} if not assigned yet |
| 245 | + */ |
| 246 | + public int getUniqueId() |
| 247 | + { |
| 248 | + return uniqueId; |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * Returns the unique custom ID, this can be used to pass data, then read in an interaction. |
| 253 | + * |
| 254 | + * @return The custom ID |
| 255 | + */ |
| 256 | + @Nonnull |
| 257 | + public String getCustomId() |
| 258 | + { |
| 259 | + return customId; |
| 260 | + } |
| 261 | + |
| 262 | + /** |
| 263 | + * The minimum amount of attachments the user must send. |
| 264 | + * |
| 265 | + * @return Minimum amount of attachments the user must send |
| 266 | + */ |
| 267 | + public int getMinValues() |
| 268 | + { |
| 269 | + return minValues; |
| 270 | + } |
| 271 | + |
| 272 | + /** |
| 273 | + * The maximum amount of attachments the user can send. |
| 274 | + * |
| 275 | + * @return Maximum amount of attachments the user can send |
| 276 | + */ |
| 277 | + public int getMaxValues() |
| 278 | + { |
| 279 | + return maxValues; |
| 280 | + } |
| 281 | + |
| 282 | + /** |
| 283 | + * Whether the user must send attachments. |
| 284 | + * |
| 285 | + * <p>This attribute is completely separate from the value range, |
| 286 | + * for example you can have an optional {@link AttachmentUpload} with the range set to {@code [2 ; 2]}, |
| 287 | + * meaning you accept either 0 attachments, or 2. |
| 288 | + * |
| 289 | + * @return {@code true} if files must be uploaded, {@code false} if not |
| 290 | + */ |
| 291 | + public boolean isRequired() |
| 292 | + { |
| 293 | + return required; |
| 294 | + } |
| 295 | + |
| 296 | + /** |
| 297 | + * Builds a new {@link AttachmentUpload}. |
| 298 | + * |
| 299 | + * @throws IllegalArgumentException |
| 300 | + * If {@linkplain #setMinValues(int) min values} is larger than {@linkplain #setMaxValues(int) max values} |
| 301 | + * |
| 302 | + * @return The new {@link AttachmentUpload} |
| 303 | + */ |
| 304 | + @Nonnull |
| 305 | + public AttachmentUpload build() |
| 306 | + { |
| 307 | + Checks.check(maxValues >= minValues, "Max (%s) must be higher or equal to min (%s)", maxValues, minValues); |
| 308 | + return new AttachmentUploadImpl(uniqueId, customId, minValues, maxValues, required); |
| 309 | + } |
| 310 | + } |
| 311 | +} |
0 commit comments