/
ApplicationImpl.java
425 lines (367 loc) · 11.3 KB
/
ApplicationImpl.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
* Copyright 2015-2018 Austin Keener & Michael Ritter & Florian Spieß
*
* 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.entities.impl;
import net.dv8tion.jda.client.entities.Application;
import net.dv8tion.jda.client.managers.ApplicationManager;
import net.dv8tion.jda.client.managers.ApplicationManagerUpdatable;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.Permission;
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.json.JSONArray;
import org.json.JSONObject;
import java.util.*;
public class ApplicationImpl implements Application
{
private final JDA api;
private final Object mngLock = new Object();
private ApplicationManager manager;
private ApplicationManagerUpdatable managerUpdatable;
private BotImpl bot;
private String description;
private boolean doesBotRequireCodeGrant;
private int flags;
private String iconId;
private long id;
private boolean isBotPublic;
private String name;
private List<String> redirectUris;
private int rpcApplicationState;
private String secret;
public ApplicationImpl(final JDA api, final JSONObject object)
{
this.api = api;
this.updateFromJson(object);
}
@Override
public RestAction<Application.Bot> createBot()
{
if (this.hasBot())
return new RestAction.EmptyRestAction<>(getJDA(), this.bot);
return new RestAction<Application.Bot>(this.api, Route.Applications.CREATE_BOT.compile(getId()))
{
@Override
protected void handleResponse(final Response response, final Request<Application.Bot> request)
{
if (response.isOk())
request.onSuccess(ApplicationImpl.this.bot = new BotImpl(response.getObject()));
else
request.onFailure(response);
}
};
}
@Override
public RestAction<Void> delete()
{
return new RestAction<Void>(this.api, Route.Applications.DELETE_APPLICATION.compile(getId()))
{
@Override
protected void handleResponse(final Response response, final Request<Void> request)
{
if (response.isOk())
request.onSuccess(null);
else
request.onFailure(response);
}
};
}
@Override
public boolean doesBotRequireCodeGrant()
{
return this.doesBotRequireCodeGrant;
}
@Override
public boolean equals(final Object obj)
{
return obj instanceof ApplicationImpl && this.id == ((ApplicationImpl) obj).id;
}
@Override
public Bot getBot()
{
return this.bot;
}
@Override
public String getDescription()
{
return this.description;
}
@Override
public int getFlags()
{
return this.flags;
}
@Override
public String getIconId()
{
return this.iconId;
}
@Override
public String getIconUrl()
{
return this.iconId == null ? null
: "https://cdn.discordapp.com/app-icons/" + this.id + '/' + this.iconId + ".jpg";
}
@Override
public long getIdLong()
{
return this.id;
}
@Override
public JDA getJDA()
{
return this.api;
}
@Override
public ApplicationManager getManager()
{
ApplicationManager mng = manager;
if (mng == null)
{
synchronized (mngLock)
{
mng = manager;
if (mng == null)
mng = manager = new ApplicationManager(this);
}
}
return mng;
}
@Override
public ApplicationManagerUpdatable getManagerUpdatable()
{
ApplicationManagerUpdatable mng = managerUpdatable;
if (mng == null)
{
synchronized (mngLock)
{
mng = managerUpdatable;
if (mng == null)
mng = managerUpdatable = new ApplicationManagerUpdatable(this);
}
}
return mng;
}
@Override
public String getName()
{
return this.name;
}
@Override
public List<String> getRedirectUris()
{
return Collections.unmodifiableList(this.redirectUris);
}
@Override
public int getRpcApplicationState()
{
return this.rpcApplicationState;
}
@Override
public String getSecret()
{
return this.secret;
}
@Override
public boolean hasBot()
{
return this.bot != null;
}
@Override
public boolean isBotPublic()
{
return this.isBotPublic;
}
@Override
public RestAction<Application> resetSecret()
{
Route.CompiledRoute route = Route.Applications.RESET_BOT_TOKEN.compile(getId());
return new RestAction<Application>(this.api, route)
{
@Override
protected void handleResponse(final Response response, final Request<Application> request)
{
if (response.isOk())
request.onSuccess(ApplicationImpl.this.updateFromJson(response.getObject()));
else
request.onFailure(response);
}
};
}
@Override
public String toString()
{
return "Application(" + this.id + ")";
}
public ApplicationImpl updateFromJson(final JSONObject object)
{
if (object.has("bot"))
{
final JSONObject botObject = object.getJSONObject("bot");
if (this.bot == null)
this.bot = new BotImpl(botObject);
else
this.bot.updateFromJson(botObject);
}
else
{
this.bot = null;
}
this.isBotPublic = object.getBoolean("bot_public");
this.doesBotRequireCodeGrant = object.getBoolean("bot_require_code_grant");
this.description = object.getString("description");
this.flags = object.getInt("flags");
this.iconId = object.has("icon") ? object.getString("icon") : null;
this.id = object.getLong("id");
this.name = object.getString("name");
final JSONArray redirectUriArray = object.getJSONArray("redirect_uris");
if (this.redirectUris == null)
this.redirectUris = new ArrayList<>(redirectUriArray.length());
else
this.redirectUris.clear();
for (int i = 0; i < redirectUriArray.length(); i++)
this.redirectUris.add(redirectUriArray.getString(i));
this.rpcApplicationState = object.getInt("rpc_application_state");
this.secret = object.getString("secret");
return this;
}
public class BotImpl implements Application.Bot
{
private long id;
private String avatarId;
private String discriminator;
private String name;
private String token;
private BotImpl(final JSONObject object)
{
this.updateFromJson(object);
}
@Override
public boolean equals(final Object obj)
{
return obj instanceof BotImpl && this.id == ((BotImpl) obj).id;
}
@Override
public Application getApplication()
{
return ApplicationImpl.this;
}
@Override
public String getAvatarId()
{
return this.avatarId;
}
@Override
public String getAvatarUrl()
{
return this.avatarId == null ? null
: "https://cdn.discordapp.com/avatars/" + this.id + "/" + this.avatarId + ".jpg";
}
@Override
public String getDiscriminator()
{
return this.discriminator;
}
@Override
public long getIdLong()
{
return this.id;
}
@Override
public String getInviteUrl(final Collection<Permission> permissions)
{
return this.getInviteUrl(null, permissions);
}
@Override
public String getInviteUrl(final Permission... permissions)
{
return this.getInviteUrl(null, permissions);
}
@Override
public String getInviteUrl(final String guildId, final Collection<Permission> permissions)
{
StringBuilder builder = new StringBuilder("https://discordapp.com/oauth2/authorize?client_id=");
builder.append(this.getId());
builder.append("&scope=bot");
if (permissions != null && !permissions.isEmpty())
{
builder.append("&permissions=");
builder.append(Permission.getRaw(permissions));
}
if (guildId != null)
{
builder.append("&guild_id=");
builder.append(guildId);
}
return builder.toString();
}
@Override
public String getInviteUrl(final String guildId, final Permission... permissions)
{
return this.getInviteUrl(guildId, permissions == null ? null : Arrays.asList(permissions));
}
public JDA getJDA()
{
return ApplicationImpl.this.getJDA();
}
@Override
public String getName()
{
return this.name;
}
@Override
public String getToken()
{
return this.token;
}
@Override
public int hashCode()
{
return Long.hashCode(this.id);
}
@Override
public RestAction<Bot> resetToken()
{
Route.CompiledRoute route = Route.Applications.RESET_BOT_TOKEN.compile(getId());
return new RestAction<Bot>(getJDA(), route)
{
@Override
protected void handleResponse(final Response response, final Request<Bot> request)
{
if (response.isOk())
request.onSuccess(BotImpl.this.updateFromJson(response.getObject()));
else
request.onFailure(response);
}
};
}
@Override
public String toString()
{
return "Application.Bot(" + this.id + ")";
}
public BotImpl updateFromJson(final JSONObject object)
{
this.name = object.getString("username");
this.discriminator = object.getString("discriminator");
this.token = object.getString("token");
this.id = object.getLong("id");
this.avatarId = object.has("avatar") ? object.getString("avatar") : null;
return this;
}
}
}