Skip to content

Commit 6ee59af

Browse files
authored
Merge branch 'discord-jda:master' into master
2 parents ae8cf2d + fc4d1ef commit 6ee59af

34 files changed

Lines changed: 1160 additions & 68 deletions

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ plugins {
4747
////////////////////////////////////
4848

4949
projectEnvironment {
50-
version = Version(major = "6", minor = "0", revision = "0", classifier = null)
50+
version = Version(major = "6", minor = "1", revision = "0", classifier = null)
5151
}
5252

5353
artifactFilters {

src/main/java/net/dv8tion/jda/api/components/Component.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ enum Type
134134
SEPARATOR(14, true, false),
135135
CONTAINER(17, true, false),
136136
LABEL(18, false, true),
137+
FILE_UPLOAD(19, false, true),
137138
;
138139

139140
private final int key;
Lines changed: 311 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,311 @@
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+
}

src/main/java/net/dv8tion/jda/api/components/label/LabelChildComponent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* <li>{@link net.dv8tion.jda.api.components.textinput.TextInput TextInput}</li>
2727
* <li>{@link net.dv8tion.jda.api.components.selections.StringSelectMenu StringSelectMenu}</li>
2828
* <li>{@link net.dv8tion.jda.api.components.selections.EntitySelectMenu EntitySelectMenu}</li>
29+
* <li>{@link net.dv8tion.jda.api.components.attachmentupload.AttachmentUpload AttachmentUpload}</li>
2930
* </ul>
3031
*/
3132
public interface LabelChildComponent extends Component

src/main/java/net/dv8tion/jda/api/components/label/LabelChildComponentUnion.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import net.dv8tion.jda.api.components.Component;
2020
import net.dv8tion.jda.api.components.IComponentUnion;
21+
import net.dv8tion.jda.api.components.attachmentupload.AttachmentUpload;
2122
import net.dv8tion.jda.api.components.selections.EntitySelectMenu;
2223
import net.dv8tion.jda.api.components.selections.StringSelectMenu;
2324
import net.dv8tion.jda.api.components.textinput.TextInput;
@@ -30,6 +31,7 @@
3031
* <li>{@link TextInput}</li>
3132
* <li>{@link StringSelectMenu}</li>
3233
* <li>{@link EntitySelectMenu}</li>
34+
* <li>{@link AttachmentUpload}</li>
3335
* </ul>
3436
*/
3537
public interface LabelChildComponentUnion extends LabelChildComponent, IComponentUnion
@@ -106,6 +108,28 @@ public interface LabelChildComponentUnion extends LabelChildComponent, IComponen
106108
@Nonnull
107109
EntitySelectMenu asEntitySelectMenu();
108110

111+
/**
112+
* Casts this union to a {@link AttachmentUpload}.
113+
* This method exists for developer discoverability.
114+
*
115+
* <p>Note: This is effectively equivalent to using the cast operator:
116+
* <pre><code>
117+
* //These are the same!
118+
* AttachmentUpload menu = union.asAttachmentUpload();
119+
* AttachmentUpload menu2 = (AttachmentUpload) union;
120+
* </code></pre>
121+
*
122+
* You can use {@link #getType()} to see if the component is of type {@link Component.Type#FILE_UPLOAD} to validate
123+
* whether you can call this method in addition to normal instanceof checks: <code>component instanceof AttachmentUpload</code>
124+
*
125+
* @throws IllegalStateException
126+
* If the component represented by this union is not actually a {@link AttachmentUpload}.
127+
*
128+
* @return The component as a {@link AttachmentUpload}
129+
*/
130+
@Nonnull
131+
AttachmentUpload asAttachmentUpload();
132+
109133
@Nonnull
110134
@Override
111135
LabelChildComponentUnion withUniqueId(int uniqueId);

src/main/java/net/dv8tion/jda/api/components/utils/ComponentDeserializer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import net.dv8tion.jda.api.utils.data.DataObject;
3434
import net.dv8tion.jda.internal.components.UnknownComponentImpl;
3535
import net.dv8tion.jda.internal.components.actionrow.ActionRowImpl;
36+
import net.dv8tion.jda.internal.components.attachmentupload.AttachmentUploadImpl;
3637
import net.dv8tion.jda.internal.components.buttons.ButtonImpl;
3738
import net.dv8tion.jda.internal.components.container.ContainerImpl;
3839
import net.dv8tion.jda.internal.components.filedisplay.FileDisplayFileUpload;
@@ -280,6 +281,8 @@ private IComponentUnion parseComponent(@Nonnull DataObject data)
280281
return new ContainerImpl(this, data);
281282
case LABEL:
282283
return new LabelImpl(this, data);
284+
case FILE_UPLOAD:
285+
return new AttachmentUploadImpl(data);
283286
default:
284287
return new UnknownComponentImpl(data);
285288
}

0 commit comments

Comments
 (0)