-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
ApplicationManagerUpdatable.java
366 lines (327 loc) · 15.3 KB
/
ApplicationManagerUpdatable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* Copyright 2015-2017 Austin Keener & Michael Ritter
*
* 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.client.managers;
import net.dv8tion.jda.client.entities.Application;
import net.dv8tion.jda.client.entities.impl.ApplicationImpl;
import net.dv8tion.jda.client.managers.fields.ApplicationField;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.entities.Icon;
import net.dv8tion.jda.core.requests.Request;
import net.dv8tion.jda.core.requests.Response;
import net.dv8tion.jda.core.requests.RestAction;
import net.dv8tion.jda.core.requests.Route;
import org.apache.http.util.Args;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
/**
* An {@link #update() updatable} manager that allows
* to modify role settings like the {@link #getNameField() name} and the {@link #getIconField() icon}.
*
* <p>This manager allows to modify multiple fields at once
* by getting the {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField} for specific
* properties and setting or resetting their values; followed by a call of {@link #update()}!
*
* <p>The {@link net.dv8tion.jda.client.managers.ApplicationManager ApplicationManager} implementation
* simplifies this process by giving simple setters that return the {@link #update() update} {@link net.dv8tion.jda.core.requests.RestAction RestAction}
*
* @since 3.0
* @author Aljoscha Grebe
*/
public class ApplicationManagerUpdatable
{
public final static Pattern URL_PATTERN = Pattern.compile("\\s*https?://.+\\..{2,}\\s*",
Pattern.CASE_INSENSITIVE);
private final ApplicationImpl application;
private ApplicationField<String> description;
private ApplicationField<Boolean> doesBotRequireCodeGrant;
private ApplicationField<Icon> icon;
private ApplicationField<Boolean> isBotPublic;
private ApplicationField<String> name;
private ApplicationField<List<String>> redirectUris;
public ApplicationManagerUpdatable(final ApplicationImpl application)
{
this.application = application;
this.setupFields();
}
/**
* The {@link net.dv8tion.jda.core.JDA JDA} instance of this Manager
*
* @return the corresponding JDA instance
*/
public JDA getJDA()
{
return this.application.getJDA();
}
/**
* The {@link net.dv8tion.jda.client.entities.Application Application} that will
* be modified by this Manager instance
*
* @return The {@link net.dv8tion.jda.client.entities.Application Application}
*/
public final Application getApplication()
{
return this.application;
}
/**
* An {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField}
* for the <b><u>description</u></b> of the selected {@link net.dv8tion.jda.client.entities.Application Application}.
*
* <p>To set the value use {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) setValue(String)}
* on the returned {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField} instance.
*
* <p>A description <b>must not</b> be more than 400 characters long!
* <br>Otherwise {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) Field.setValue(...)} will
* throw an {@link IllegalArgumentException IllegalArgumentException}.
*
* @return {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField} - Type: {@code String}
*/
public final ApplicationField<String> getDescriptionField()
{
return this.description;
}
/**
* An {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField}
* for the <b><u>code grant state</u></b> of the selected {@link net.dv8tion.jda.client.entities.Application Application's} bot.
*
* <p>To set the value use {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) setValue(Boolean)}
* on the returned {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField} instance.
*
* <p>A code grant state <b>must not</b> be {@code null}!
* <br>Otherwise {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) Field.setValue(...)} will
* throw an {@link IllegalArgumentException IllegalArgumentException}.
*
* @return {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField} - Type: {@code Boolean}
*/
public final ApplicationField<Boolean> getDoesBotRequireCodeGrantField()
{
return this.doesBotRequireCodeGrant;
}
/**
* An {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField}
* for the {@link net.dv8tion.jda.core.entities.Icon Icon} of the selected
* {@link net.dv8tion.jda.client.entities.Application Application}.
*
* <p>To set the value use {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) setValue(Icon)}
* on the returned {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField} instance.
*
* @return {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField}
* - Type: {@link net.dv8tion.jda.core.entities.Icon Icon}
*/
public final ApplicationField<Icon> getIconField()
{
return this.icon;
}
/**
* An {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField}
* for the <b><u>public state</u></b> of the selected {@link net.dv8tion.jda.client.entities.Application Application's} bot.
*
* <p>To set the value use {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) setValue(Boolean)}
* on the returned {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField} instance.
*
* <p>A public state <b>must not</b> be {@code null}!
* <br>Otherwise {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) Field.setValue(...)} will
* throw an {@link IllegalArgumentException IllegalArgumentException}.
*
* @return {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField} - Type: {@code Boolean}
*/
public final ApplicationField<Boolean> getIsBotPublicField()
{
return this.isBotPublic;
}
/**
* An {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField}
* for the <b><u>name</u></b> of the selected {@link net.dv8tion.jda.client.entities.Application Application}.
*
* <p>To set the value use {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) setValue(String)}
* on the returned {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField} instance.
*
* <p>A name <b>must not</b> be {@code null} nor less than 2 characters or more than 32 characters long!
* <br>Otherwise {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) Field.setValue(...)} will
* throw an {@link IllegalArgumentException IllegalArgumentException}.
*
* @return {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField} - Type: {@code String}
*/
public final ApplicationField<String> getNameField()
{
return this.name;
}
/**
* An {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField}
* for the <b><u>redirect uris</u></b> of the selected {@link net.dv8tion.jda.client.entities.Application Application}.
*
* <p>To set the value use {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) setValue(List)}
* on the returned {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField} instance.
*
* <p>Modification to the provided {@link java.util.List List} after passing it to this {@link ApplicationManagerUpdatable}
* will be ignored.
*
* <p>The {@link java.util.List List} as well as all redirect uris <b>must not</b> be {@code null}!
* <br>Otherwise {@link net.dv8tion.jda.core.managers.fields.Field#setValue(Object) Field.setValue(...)} will
* throw an {@link IllegalArgumentException IllegalArgumentException}.
*
* @return {@link net.dv8tion.jda.client.managers.fields.ApplicationField ApplicationField} - Type: {@code List<String>}
*/
public final ApplicationField<List<String>> getRedirectUrisField()
{
return this.redirectUris;
}
/**
* Resets all {@link net.dv8tion.jda.client.managers.fields.ApplicationField Fields}
* for this manager instance by calling {@link net.dv8tion.jda.core.managers.fields.Field#reset() Field.reset()} sequentially
* <br>This is automatically called by {@link #update()}
*/
public void reset()
{
this.description.reset();
this.doesBotRequireCodeGrant.reset();
this.icon.reset();
this.isBotPublic.reset();
this.name.reset();
this.redirectUris.reset();
}
protected boolean needsUpdate()
{
return this.description.shouldUpdate() || this.doesBotRequireCodeGrant.shouldUpdate()
|| this.icon.shouldUpdate() || this.isBotPublic.shouldUpdate() || this.name.shouldUpdate()
|| this.redirectUris.shouldUpdate();
}
protected void setupFields()
{
this.description = new ApplicationField<String>(this, this.application::getDescription)
{
@Override
public void checkValue(final String value)
{
if (value != null && value.length() > 400)
throw new IllegalArgumentException("application description must not be more than 400 characters long");
}
};
this.doesBotRequireCodeGrant = new ApplicationField<Boolean>(this, this.application::doesBotRequireCodeGrant)
{
@Override
public void checkValue(final Boolean value)
{
Args.notNull(value, "doesBotRequireCodeGrant");
}
};
this.icon = new ApplicationField<Icon>(this, null)
{
@Override
public void checkValue(final Icon value)
{}
@Override
public Icon getOriginalValue()
{
throw new UnsupportedOperationException(
"Cannot easily provide the original Avatar. Use Application#getIconUrl() and download it yourself.");
}
@Override
public boolean shouldUpdate()
{
return this.isSet();
}
};
this.isBotPublic = new ApplicationField<Boolean>(this, this.application::isBotPublic)
{
@Override
public void checkValue(final Boolean value)
{
Args.notNull(value, "isBotPublic");
}
};
this.name = new ApplicationField<String>(this, this.application::getName)
{
@Override
public void checkValue(final String value)
{
Args.notNull(value, "application name");
if (value.length() < 2 || value.length() > 32)
throw new IllegalArgumentException("Application name must be 2 to 32 characters in length");
}
};
this.redirectUris = new ApplicationField<List<String>>(this, this.application::getRedirectUris)
{
@Override
public void checkValue(final List<String> value)
{
Args.notNull(value, "redirect uris");
for (final String url : value)
{
Args.notNull(url, "redirect uri");
if (!ApplicationManagerUpdatable.URL_PATTERN.matcher(url).matches())
throw new IllegalArgumentException("URL must be a valid http or https url.");
}
}
@Override
public ApplicationManagerUpdatable setValue(List<String> value)
{
checkValue(value);
this.value = Collections.unmodifiableList(new ArrayList<>(value));
this.set = true;
return manager;
}
};
}
/**
* Creates a new {@link net.dv8tion.jda.core.requests.RestAction RestAction} instance
* that will apply <b>all</b> changes that have been made to this manager instance (one per runtime per JDA instance).
*
* <p>Before applying new changes it is recommended to call {@link #reset()} to reset previous changes.
* <br>This is automatically called if this method returns successfully.
*
* @return {@link net.dv8tion.jda.core.requests.RestAction RestAction} - Type: Void
* <br>Updates all modified fields or does nothing if none of the {@link net.dv8tion.jda.core.managers.fields.Field Fields}
* have been modified. ({@link net.dv8tion.jda.core.requests.RestAction.EmptyRestAction EmptyRestAction})
*/
public RestAction<Void> update()
{
if (!this.needsUpdate())
return new RestAction.EmptyRestAction<>(null);
final JSONObject body = new JSONObject();
// All fields are required or they are resetted to default
body.put("description",
this.description.shouldUpdate() ? this.description.getValue() : this.description.getOriginalValue());
body.put("bot_require_code_grant", this.doesBotRequireCodeGrant.shouldUpdate()
? this.doesBotRequireCodeGrant.getValue() : this.doesBotRequireCodeGrant.getOriginalValue());
body.put("icon",
this.icon.shouldUpdate()
? this.icon.getValue() == null ? JSONObject.NULL : this.icon.getValue().getEncoding()
: this.application.getIconUrl());
body.put("bot_public",
this.isBotPublic.shouldUpdate() ? this.isBotPublic.getValue() : this.isBotPublic.getOriginalValue());
body.put("name", this.name.shouldUpdate() ? this.name.getValue() : this.name.getOriginalValue());
body.put("redirect_uris", this.redirectUris.shouldUpdate() ? this.redirectUris.getValue() : this.redirectUris.getOriginalValue());
reset(); //now that we've built our JSON object, reset the manager back to the non-modified state
Route.CompiledRoute route = Route.Applications.MODIFY_APPLICATION.compile(this.application.getId());
return new RestAction<Void>(this.getJDA(), route, body)
{
@Override
protected void handleResponse(final Response response, final Request request)
{
if (response.isOk())
{
ApplicationManagerUpdatable.this.application.updateFromJson(response.getObject());
request.onSuccess(null);
}
else
request.onFailure(response);
}
};
}
}