/
WidgetImpl.java
601 lines (517 loc) · 15.2 KB
/
WidgetImpl.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/*
* 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.internal.entities;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import gnu.trove.map.TLongObjectMap;
import gnu.trove.map.hash.TLongObjectHashMap;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.Widget;
import net.dv8tion.jda.api.utils.ImageProxy;
import net.dv8tion.jda.api.utils.MiscUtil;
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.utils.EntityString;
public class WidgetImpl implements Widget
{
private final boolean isAvailable;
private final long id;
private final String name;
private final String invite;
private final TLongObjectMap<VoiceChannelImpl> channels;
private final TLongObjectMap<Member> members;
/**
* Constructs an unavailable Widget
*/
public WidgetImpl(long guildId)
{
isAvailable = false;
id = guildId;
name = null;
invite = null;
channels = new TLongObjectHashMap<>();
members = new TLongObjectHashMap<>();
}
/**
* Constructs an available Widget
*
* @param json
* The {@link net.dv8tion.jda.api.utils.data.DataObject DataObject} to construct the Widget from
*/
public WidgetImpl(@Nonnull DataObject json)
{
String inviteCode = json.getString("instant_invite", null);
if (inviteCode != null)
inviteCode = inviteCode.substring(inviteCode.lastIndexOf("/") + 1);
isAvailable = true;
id = json.getLong("id");
name = json.getString("name");
invite = inviteCode;
channels = MiscUtil.newLongMap();
members = MiscUtil.newLongMap();
DataArray channelsJson = json.getArray("channels");
for (int i = 0; i < channelsJson.length(); i++)
{
DataObject channel = channelsJson.getObject(i);
channels.put(channel.getLong("id"), new VoiceChannelImpl(channel, this));
}
DataArray membersJson = json.getArray("members");
for (int i = 0; i<membersJson.length(); i++)
{
DataObject memberJson = membersJson.getObject(i);
MemberImpl member = new MemberImpl(memberJson, this);
if (!memberJson.isNull("channel_id")) // voice state
{
VoiceChannelImpl channel = channels.get(memberJson.getLong("channel_id"));
member.setVoiceState(new VoiceStateImpl(channel,
memberJson.getBoolean("mute"),
memberJson.getBoolean("deaf"),
memberJson.getBoolean("suppress"),
memberJson.getBoolean("self_mute"),
memberJson.getBoolean("self_deaf"),
member,
this));
channel.addMember(member);
}
members.put(member.getIdLong(), member);
}
}
@Override
public boolean isAvailable()
{
return isAvailable;
}
@Override
public long getIdLong()
{
return id;
}
@Override
@Nonnull
public String getName()
{
checkAvailable();
return name;
}
@Override
@Nullable
public String getInviteCode()
{
checkAvailable();
return invite;
}
@Override
@Nonnull
public List<VoiceChannel> getVoiceChannels()
{
checkAvailable();
return Collections.unmodifiableList(new ArrayList<>(channels.valueCollection()));
}
@Override
@Nullable
public VoiceChannel getVoiceChannelById(String id)
{
checkAvailable();
return channels.get(MiscUtil.parseSnowflake(id));
}
@Override
@Nullable
public VoiceChannel getVoiceChannelById(long id)
{
checkAvailable();
return channels.get(id);
}
@Override
@Nonnull
public List<Member> getMembers()
{
checkAvailable();
return Collections.unmodifiableList(new ArrayList<>(members.valueCollection()));
}
@Override
@Nullable
public Member getMemberById(String id)
{
checkAvailable();
return members.get(MiscUtil.parseSnowflake(id));
}
@Override
@Nullable
public Member getMemberById(long id)
{
checkAvailable();
return members.get(id);
}
@Override
public int hashCode() {
return Long.hashCode(id);
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof WidgetImpl))
return false;
WidgetImpl oWidget = (WidgetImpl) obj;
return this == oWidget || this.id == oWidget.getIdLong();
}
@Override
public String toString()
{
final EntityString entityString = new EntityString(this);
if (isAvailable())
entityString.setName(getName());
return entityString.toString();
}
private void checkAvailable()
{
if (!isAvailable)
throw new IllegalStateException("The widget for this Guild is unavailable!");
}
public class MemberImpl implements Member
{
private final boolean bot;
private final long id;
private final String username;
private final String discriminator;
private final String avatar;
private final String nickname;
private final OnlineStatus status;
private final Activity game;
private final WidgetImpl widget;
private VoiceState state;
private MemberImpl(@Nonnull DataObject json, @Nonnull WidgetImpl widget)
{
this.widget = widget;
this.bot = json.getBoolean("bot");
this.id = json.getLong("id");
this.username = json.getString("username");
this.discriminator = json.getString("discriminator");
this.avatar = json.getString("avatar", null);
this.nickname = json.getString("nick", null);
this.status = OnlineStatus.fromKey(json.getString("status"));
this.game = json.isNull("game") ? null : EntityBuilder.createActivity(json.getObject("game"));
}
private void setVoiceState(VoiceState voiceState)
{
state = voiceState;
}
@Override
public boolean isBot()
{
return bot;
}
@Override
@Nonnull
public String getName()
{
return username;
}
@Override
public long getIdLong()
{
return id;
}
@Nonnull
@Override
public String getAsMention()
{
return "<@" + getId() + ">";
}
@Override
@Nonnull
public String getDiscriminator()
{
return discriminator;
}
@Override
@Nullable
public String getAvatarId()
{
return avatar;
}
@Override
@Nullable
public String getAvatarUrl()
{
String avatarId = getAvatarId();
return avatarId == null ? null : String.format(User.AVATAR_URL, getId(), avatarId, avatarId.startsWith("a_") ? ".gif" : ".png");
}
@Override
@Nullable
public ImageProxy getAvatar()
{
final String avatarUrl = getAvatarUrl();
return avatarUrl == null ? null : new ImageProxy(avatarUrl);
}
@Override
@Nonnull
public String getDefaultAvatarId()
{
return String.valueOf(Integer.parseInt(getDiscriminator()) % 5);
}
@Override
@Nonnull
public String getDefaultAvatarUrl()
{
return String.format(User.DEFAULT_AVATAR_URL, getDefaultAvatarId());
}
@Override
@Nonnull
public ImageProxy getDefaultAvatar()
{
return new ImageProxy(getDefaultAvatarUrl());
}
@Override
@Nonnull
public String getEffectiveAvatarUrl()
{
String avatarUrl = getAvatarUrl();
return avatarUrl == null ? getDefaultAvatarUrl() : avatarUrl;
}
@Override
@Nonnull
public ImageProxy getEffectiveAvatar()
{
return new ImageProxy(getEffectiveAvatarUrl());
}
@Override
@Nullable
public String getNickname()
{
return nickname;
}
@Override
@Nonnull
public String getEffectiveName()
{
return nickname == null ? username : nickname;
}
@Override
@Nonnull
public OnlineStatus getOnlineStatus()
{
return status;
}
@Override
@Nullable
public Activity getActivity()
{
return game;
}
@Override
@Nonnull
public VoiceState getVoiceState()
{
return state == null ? new VoiceStateImpl(this, widget) : state;
}
@Override
@Nonnull
public WidgetImpl getWidget()
{
return widget;
}
@Override
public int hashCode() {
return (widget.getId() + ' ' + id).hashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Member))
return false;
Member oMember = (Member) obj;
return this == oMember || (this.id == oMember.getIdLong() && this.widget.getIdLong() == oMember.getWidget().getIdLong());
}
@Override
public String toString()
{
return new EntityString(this)
.setName(getName())
.toString();
}
}
public class VoiceChannelImpl implements VoiceChannel
{
private final int position;
private final long id;
private final String name;
private final List<Member> members;
private final Widget widget;
private VoiceChannelImpl(@Nonnull DataObject json, @Nonnull Widget widget)
{
this.widget = widget;
this.position = json.getInt("position");
this.id = json.getLong("id");
this.name = json.getString("name");
this.members = new ArrayList<>();
}
private void addMember(@Nonnull Member member)
{
members.add(member);
}
@Override
public int getPosition()
{
return position;
}
@Override
public long getIdLong()
{
return id;
}
@Override
@Nonnull
public String getName()
{
return name;
}
@Override
@Nonnull
public List<Member> getMembers()
{
return members;
}
@Override
@Nonnull
public Widget getWidget()
{
return widget;
}
@Override
public int hashCode() {
return Long.hashCode(id);
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof VoiceChannel))
return false;
VoiceChannel oVChannel = (VoiceChannel) obj;
return this == oVChannel || this.id == oVChannel.getIdLong();
}
@Override
public String toString()
{
return new EntityString(this)
.setName(getName())
.toString();
}
}
public class VoiceStateImpl implements VoiceState
{
private final VoiceChannel channel;
private final boolean muted;
private final boolean deafened;
private final boolean suppress;
private final boolean selfMute;
private final boolean selfDeaf;
private final Member member;
private final Widget widget;
private VoiceStateImpl(@Nonnull Member member, @Nonnull Widget widget)
{
this(null, false, false, false, false, false, member, widget);
}
private VoiceStateImpl(@Nullable VoiceChannel channel, boolean muted, boolean deafened, boolean suppress, boolean selfMute, boolean selfDeaf, @Nonnull Member member, @Nonnull Widget widget)
{
this.channel = channel;
this.muted = muted;
this.deafened = deafened;
this.suppress = suppress;
this.selfMute = selfMute;
this.selfDeaf = selfDeaf;
this.member = member;
this.widget = widget;
}
@Override
@Nullable
public VoiceChannel getChannel()
{
return channel;
}
@Override
public boolean inVoiceChannel()
{
return channel != null;
}
@Override
public boolean isGuildMuted()
{
return muted;
}
@Override
public boolean isGuildDeafened()
{
return deafened;
}
@Override
public boolean isSuppressed()
{
return suppress;
}
@Override
public boolean isSelfMuted()
{
return selfMute;
}
@Override
public boolean isSelfDeafened()
{
return selfDeaf;
}
@Override
public boolean isMuted()
{
return selfMute || muted;
}
@Override
public boolean isDeafened()
{
return selfDeaf || deafened;
}
@Override
@Nonnull
public Member getMember()
{
return member;
}
@Override
@Nonnull
public Widget getWidget()
{
return widget;
}
@Override
public int hashCode() {
return member.hashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof VoiceState))
return false;
VoiceState oState = (VoiceState) obj;
return this == oState || (this.member.equals(oState.getMember()) && this.widget.equals(oState.getWidget()));
}
@Override
public String toString() {
return new EntityString(this)
.setName(widget.getName())
.addMetadata("memberName", member.getEffectiveName())
.toString();
}
}
}