/
User.java
495 lines (453 loc) · 17.1 KB
/
User.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
/*
* 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.api.entities;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.utils.MiscUtil;
import net.dv8tion.jda.internal.entities.UserById;
import net.dv8tion.jda.internal.utils.Checks;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.regex.Pattern;
/**
* Represents a Discord User.
* Contains all publicly available information about a specific Discord User.
*
* <h1>Formattable</h1>
* This interface extends {@link java.util.Formattable Formattable} and can be used with a {@link java.util.Formatter Formatter}
* such as used by {@link String#format(String, Object...) String.format(String, Object...)}
* or {@link java.io.PrintStream#printf(String, Object...) PrintStream.printf(String, Object...)}.
*
* <p>This will use {@link #getAsMention()} rather than {@link Object#toString()}!
* <br>Supported Features:
* <ul>
* <li><b>Alternative</b>
* <br> - Uses the <u>Discord Tag</u> (Username#Discriminator) instead
* (Example: {@code %#s} - results in <code>{@link User#getName()}#{@link User#getDiscriminator()}
* {@literal ->} Minn#6688</code>)</li>
*
* <li><b>Width/Left-Justification</b>
* <br> - Ensures the size of a format
* (Example: {@code %20s} - uses at minimum 20 chars;
* {@code %-10s} - uses left-justified padding)</li>
*
* <li><b>Precision</b>
* <br> - Cuts the content to the specified size
* (Example: {@code %.20s})</li>
* </ul>
*
* <p>More information on formatting syntax can be found in the {@link java.util.Formatter format syntax documentation}!
*
* @see User#openPrivateChannel()
*
* @see JDA#getUserCache()
* @see JDA#getUserById(long)
* @see JDA#getUserByTag(String)
* @see JDA#getUserByTag(String, String)
* @see JDA#getUsersByName(String, boolean)
* @see JDA#getUsers()
*
* @see JDA#retrieveUserById(String)
*/
public interface User extends IMentionable, IFakeable
{
/**
* Compiled pattern for a Discord Tag: {@code (.{2,32})#(\d{4})}
*/
Pattern USER_TAG = Pattern.compile("(.{2,32})#(\\d{4})");
/** Template for {@link #getAvatarUrl()}. */
String AVATAR_URL = "https://cdn.discordapp.com/avatars/%s/%s.%s";
/** Template for {@link #getDefaultAvatarUrl()} */
String DEFAULT_AVATAR_URL = "https://cdn.discordapp.com/embed/avatars/%s.png";
/**
* Creates a User instance which only wraps an ID.
* <br>All other methods beside {@link #getIdLong()} and {@link #getId()} will throw {@link UnsupportedOperationException}.
*
* @param id
* The user id
*
* @return A user instance
*
* @see JDA#retrieveUserById(long)
*/
@Nonnull
static User fromId(long id)
{
return new UserById(id);
}
/**
* Creates a User instance which only wraps an ID.
* <br>All other methods beside {@link #getIdLong()} and {@link #getId()} will throw {@link UnsupportedOperationException}.
*
* @param id
* The user id
*
* @throws IllegalArgumentException
* If the provided ID is not a valid snowflake
*
* @return A user instance
*
* @see JDA#retrieveUserById(String)
*/
@Nonnull
static User fromId(@Nonnull String id)
{
return fromId(MiscUtil.parseSnowflake(id));
}
/**
* The username of the {@link net.dv8tion.jda.api.entities.User User}. Length is between 2 and 32 characters (inclusive).
*
* @throws UnsupportedOperationException
* If this User was created with {@link #fromId(long)}
*
* @return Never-null String containing the {@link net.dv8tion.jda.api.entities.User User}'s username.
*/
@Nonnull
String getName();
/**
* <br>The discriminator of the {@link net.dv8tion.jda.api.entities.User User}. Used to differentiate between users with the same usernames.
* <br>This only contains the 4 digits after the username and the #.
*
* @throws UnsupportedOperationException
* If this User was created with {@link #fromId(long)}
* Ex: 6297
*
* @return Never-null String containing the {@link net.dv8tion.jda.api.entities.User User} discriminator.
*/
@Nonnull
String getDiscriminator();
/**
* The Discord Id for this user's avatar image.
* If the user has not set an image, this will return null.
*
* @throws UnsupportedOperationException
* If this User was created with {@link #fromId(long)}
*
* @return Possibly-null String containing the {@link net.dv8tion.jda.api.entities.User User} avatar id.
*/
@Nullable
String getAvatarId();
/**
* The URL for the user's avatar image.
* If the user has not set an image, this will return null.
*
* @throws UnsupportedOperationException
* If this User was created with {@link #fromId(long)}
*
* @return Possibly-null String containing the {@link net.dv8tion.jda.api.entities.User User} avatar url.
*/
@Nullable
default String getAvatarUrl()
{
String avatarId = getAvatarId();
return avatarId == null ? null : String.format(AVATAR_URL, getId(), avatarId, avatarId.startsWith("a_") ? "gif" : "png");
}
/**
* The Discord Id for this user's default avatar image.
*
* @throws UnsupportedOperationException
* If this User was created with {@link #fromId(long)}
*
* @return Never-null String containing the {@link net.dv8tion.jda.api.entities.User User} default avatar id.
*/
@Nonnull
String getDefaultAvatarId();
/**
* The URL for the for the user's default avatar image.
*
* @throws UnsupportedOperationException
* If this User was created with {@link #fromId(long)}
*
* @return Never-null String containing the {@link net.dv8tion.jda.api.entities.User User} default avatar url.
*/
@Nonnull
default String getDefaultAvatarUrl()
{
return String.format(DEFAULT_AVATAR_URL, getDefaultAvatarId());
}
/**
* The URL for the user's avatar image.
* If they do not have an avatar set, this will return the URL of their
* default avatar
*
* @throws UnsupportedOperationException
* If this User was created with {@link #fromId(long)}
*
* @return Never-null String containing the {@link net.dv8tion.jda.api.entities.User User} effective avatar url.
*/
@Nonnull
default String getEffectiveAvatarUrl()
{
String avatarUrl = getAvatarUrl();
return avatarUrl == null ? getDefaultAvatarUrl() : avatarUrl;
}
/**
* The "tag" for this user
* <p>This is the equivalent of calling {@link java.lang.String#format(String, Object...) String.format}("%#s", user)
*
* @throws UnsupportedOperationException
* If this User was created with {@link #fromId(long)}
*
* @return Never-null String containing the tag for this user, for example DV8FromTheWorld#6297
*/
@Nonnull
String getAsTag();
/**
* Whether or not the currently logged in user and this user have a currently open
* {@link net.dv8tion.jda.api.entities.PrivateChannel PrivateChannel} or not.
*
* @throws UnsupportedOperationException
* If this User was created with {@link #fromId(long)}
*
* @return True if the logged in account shares a PrivateChannel with this user.
*/
boolean hasPrivateChannel();
/**
* Opens a {@link net.dv8tion.jda.api.entities.PrivateChannel PrivateChannel} with this User.
* <br>If a channel has already been opened with this user, it is immediately returned in the RestAction's
* success consumer without contacting the Discord API.
*
* <h2>Examples</h2>
* <pre>{@code
* // Send message without response handling
* public void sendMessage(User user, String content) {
* user.openPrivateChannel()
* .flatMap(channel -> channel.sendMessage(content))
* .queue();
* }
*
* // Send message and delete 30 seconds later
* public RestAction<Void> sendSecretMessage(User user, String content) {
* return user.openPrivateChannel() // RestAction<PrivateChannel>
* .flatMap(channel -> channel.sendMessage(content)) // RestAction<Message>
* .delay(30, TimeUnit.SECONDS) // RestAction<Message> with delayed response
* .flatMap(Message::delete); // RestAction<Void> (executed 30 seconds after sending)
* }
* }</pre>
*
* @throws UnsupportedOperationException
* If the recipient User is the currently logged in account (represented by {@link net.dv8tion.jda.api.entities.SelfUser SelfUser})
* or if the user was created with {@link #fromId(long)}
*
* @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: {@link net.dv8tion.jda.api.entities.PrivateChannel PrivateChannel}
* <br>Retrieves the PrivateChannel to use to directly message this User.
*
* @see JDA#openPrivateChannelById(long)
*/
@Nonnull
@CheckReturnValue
RestAction<PrivateChannel> openPrivateChannel();
/**
* Finds and collects all {@link net.dv8tion.jda.api.entities.Guild Guild} instances that contain this {@link net.dv8tion.jda.api.entities.User User} within the current {@link net.dv8tion.jda.api.JDA JDA} instance.<br>
* <p>This method is a shortcut for {@link net.dv8tion.jda.api.JDA#getMutualGuilds(User...) JDA.getMutualGuilds(User)}.</p>
*
* @throws UnsupportedOperationException
* If this User was created with {@link #fromId(long)}
*
* @return Immutable list of all {@link net.dv8tion.jda.api.entities.Guild Guilds} that this user is a member of.
*/
@Nonnull
List<Guild> getMutualGuilds();
/**
* Returns whether or not the given user is a Bot-Account (special badge in client, some different behaviour)
*
* @throws UnsupportedOperationException
* If this User was created with {@link #fromId(long)}
*
* @return If the User's Account is marked as Bot
*/
boolean isBot();
/**
* Returns the {@link net.dv8tion.jda.api.JDA JDA} instance of this User
*
* @throws UnsupportedOperationException
* If this User was created with {@link #fromId(long)}
*
* @return the corresponding JDA instance
*/
@Nonnull
JDA getJDA();
/**
* Returns the {@link net.dv8tion.jda.api.entities.User.UserFlag UserFlags} of this user.
*
* @throws UnsupportedOperationException
* If this User was created with {@link #fromId(long)}
*
* @return EnumSet containing the flags of the user.
*/
@Nonnull
EnumSet<UserFlag> getFlags();
/**
* Returns the bitmask representation of the {@link net.dv8tion.jda.api.entities.User.UserFlag UserFlags} of this user.
*
* @throws UnsupportedOperationException
* If this User was created with {@link #fromId(long)}
*
* @return bitmask representation of the user's flags.
*/
int getFlagsRaw();
/**
* Represents the bit offsets used by Discord for public flags
*/
enum UserFlag
{
STAFF( 0, "Discord Employee"),
PARTNER( 1, "Partnered Server Owner"),
HYPESQUAD( 2, "HypeSquad Events"),
BUG_HUNTER_LEVEL_1(3, "Bug Hunter Level 1"),
// HypeSquad
HYPESQUAD_BRAVERY( 6, "HypeSquad Bravery"),
HYPESQUAD_BRILLIANCE(7, "HypeSquad Brilliance"),
HYPESQUAD_BALANCE( 8, "HypeSquad Balance"),
EARLY_SUPPORTER( 9, "Early Supporter"),
TEAM_USER( 10, "Team User"),
SYSTEM( 12, "System User"),
BUG_HUNTER_LEVEL_2(14, "Bug Hunter Level 2"),
VERIFIED_BOT( 16, "Verified Bot"),
VERIFIED_DEVELOPER(17, "Early Verified Bot Developer"),
UNKNOWN(-1, "Unknown");
/**
* Empty array of UserFlag enum, useful for optimized use in {@link java.util.Collection#toArray(Object[])}.
*/
public static final UserFlag[] EMPTY_FLAGS = new UserFlag[0];
private final int offset;
private final int raw;
private final String name;
UserFlag(int offset, @Nonnull String name)
{
this.offset = offset;
this.raw = 1 << offset;
this.name = name;
}
/**
* The readable name as used in the Discord Client.
*
* @return The readable name of this UserFlag.
*/
@Nonnull
public String getName()
{
return this.name;
}
/**
* The binary offset of the flag.
*
* @return The offset that represents this UserFlag.
*/
public int getOffset()
{
return offset;
}
/**
* The value of this flag when viewed as raw value.
* <br>This is equivalent to: <code>1 {@literal <<} {@link #getOffset()}</code>
*
* @return The raw value of this specific flag.
*/
public int getRawValue()
{
return raw;
}
/**
* Gets the first UserFlag relating to the provided offset.
* <br>If there is no UserFlag that matches the provided offset,
* {@link #UNKNOWN} is returned.
*
* @param offset
* The offset to match a UserFlag to.
*
* @return UserFlag relating to the provided offset.
*/
@Nonnull
public static UserFlag getFromOffset(int offset)
{
for (UserFlag flag : values())
{
if (flag.offset == offset)
return flag;
}
return UNKNOWN;
}
/**
* A set of all UserFlags that are specified by this raw int representation of
* flags.
*
* @param flags
* The raw {@code int} representation if flags.
*
* @return Possibly-empty EnumSet of UserFlags.
*/
@Nonnull
public static EnumSet<UserFlag> getFlags(int flags)
{
final EnumSet<UserFlag> foundFlags = EnumSet.noneOf(UserFlag.class);
if (flags == 0)
return foundFlags; //empty
for (UserFlag flag : values())
{
if (flag != UNKNOWN && (flags & flag.raw) == flag.raw)
foundFlags.add(flag);
}
return foundFlags;
}
/**
* This is effectively the opposite of {@link #getFlags(int)}, this takes 1 or more UserFlags
* and returns the bitmask representation of the flags.
*
* @param flags
* The array of flags of which to form into the raw int representation.
*
* @throws java.lang.IllegalArgumentException
* When the provided UserFlags are null.
*
* @return bitmask representing the provided flags.
*/
public static int getRaw(@Nonnull UserFlag... flags){
Checks.noneNull(flags, "UserFlags");
int raw = 0;
for (UserFlag flag : flags)
{
if (flag != null && flag != UNKNOWN)
raw |= flag.raw;
}
return raw;
}
/**
* This is effectively the opposite of {@link #getFlags(int)}. This takes a collection of UserFlags
* and returns the bitmask representation of the flags.
* <br>Example: {@code getRaw(EnumSet.of(UserFlag.STAFF, UserFlag.HYPESQUAD))}
*
* @param flags
* The flags to convert
*
* @throws java.lang.IllegalArgumentException
* When the provided UserFLags are null.
*
* @return bitmask representing the provided flags.
*
* @see java.util.EnumSet EnumSet
*/
public static int getRaw(@Nonnull Collection<UserFlag> flags)
{
Checks.notNull(flags, "Flag Collection");
return getRaw(flags.toArray(EMPTY_FLAGS));
}
}
}