Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for user's avatar decorations #2668

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/main/java/net/dv8tion/jda/api/entities/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public interface User extends UserSnowflake
String DEFAULT_AVATAR_URL = "https://cdn.discordapp.com/embed/avatars/%s.png";
/** Template for {@link Profile#getBannerUrl()} */
String BANNER_URL = "https://cdn.discordapp.com/banners/%s/%s.%s";
/** Template for {@link AvatarDecoration#getDecorationAvatarUrl()} */
String DECORATION_AVATAR_URL = "https://cdn.discordapp.com/avatar-decoration-presets/%s.png";

/** Used to keep consistency between color values used in the API */
int DEFAULT_ACCENT_COLOR_RAW = 0x1FFFFFFF; // java.awt.Color fills the MSB with FF, we just use 1F to provide better consistency
Expand Down Expand Up @@ -361,6 +363,14 @@ default ImageProxy getEffectiveAvatar()
*/
int getFlagsRaw();

/**
* Returns the possibly-null {@link AvatarDecoration} of this user. If the user has not set a decoration avatar, this will return null.
*
* @return The possibly-null avatar decoration of this user
*/
@Nullable
AvatarDecoration getAvatarDecoration();

/**
* Represents the information contained in a {@link User User}'s profile.
*
Expand Down Expand Up @@ -455,6 +465,70 @@ public String toString()
}
}

/**
* Represents the avatar decoration of a {@link User User}.
*/
class AvatarDecoration
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing equals() and hashCode(), since you rely on that for update checks.

{

private final String decorationAvatarId;
private final String skuId;

public AvatarDecoration(String decorationAvatarId, String skuId)
{
this.decorationAvatarId = decorationAvatarId;
this.skuId = skuId;
}

/**
* The never-null SKU id of the {@link User User} decoration avatar.
*
* @return The never-null SKU id of the {@link User User} decoration avatar.
*/
@Nonnull
public String getSkuId()
MinnDevelopment marked this conversation as resolved.
Show resolved Hide resolved
{
return skuId;
}

/**
* The never-null avatar ID for this user's decoration avatar image.
*
* @return The never-null avatar ID for this user's decoration avatar image.
*/
@Nonnull
public String getDecorationAvatarId()
{
return decorationAvatarId;
}

/**
* The URL for the user's decoration avatar image.
*
* @return The never-null String containing the {@link User User} decoration avatar url.
*
* @see User#DECORATION_AVATAR_URL
*/
@Nonnull
public String getDecorationAvatarUrl()
{
return String.format(DECORATION_AVATAR_URL, decorationAvatarId);
ItsTheSky marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Returns an {@link ImageProxy} for this user's decoration avatar.
*
* @return Never-null {@link ImageProxy} of this user's decoration avatar
*
* @see #getDecorationAvatarUrl()
*/
@Nonnull
public ImageProxy getDecorationAvatar()
{
return new ImageProxy(getDecorationAvatarUrl());
}
}

/**
* Represents the bit offsets used by Discord for public flags
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,13 +450,18 @@ public UserImpl createUser(DataObject user)
? new User.Profile(id, user.getString("banner", null), user.getInt("accent_color", User.DEFAULT_ACCENT_COLOR_RAW))
: null;

User.AvatarDecoration avatarDecoration = user.optObject("avatar_decoration_data")
.map(o -> new User.AvatarDecoration(o.getString("asset"), o.getString("sku_id")))
.orElse(null);

if (newUser)
{
// Initial creation
userObj.setName(user.getString("username"))
.setGlobalName(user.getString("global_name", null))
.setDiscriminator(Short.parseShort(user.getString("discriminator", "0")))
.setAvatarId(user.getString("avatar", null))
.setAvatarDecoration(avatarDecoration)
ItsTheSky marked this conversation as resolved.
Show resolved Hide resolved
.setBot(user.getBoolean("bot"))
.setSystem(user.getBoolean("system"))
.setFlags(user.getInt("public_flags", 0))
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/net/dv8tion/jda/internal/entities/UserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class UserImpl extends UserSnowflakeImpl implements User
protected String name;
protected String globalName;
protected String avatarId;
protected AvatarDecoration avatarDecoration;
protected Profile profile;
protected long privateChannelId = 0L;
protected boolean bot;
Expand Down Expand Up @@ -191,6 +192,13 @@ public int getFlagsRaw()
return flags;
}

@Nullable
@Override
public User.AvatarDecoration getAvatarDecoration()
{
return avatarDecoration;
}

@Override
public String toString()
{
Expand Down Expand Up @@ -225,6 +233,12 @@ public UserImpl setAvatarId(String avatarId)
return this;
}

public UserImpl setAvatarDecoration(AvatarDecoration avatarDecoration)
{
this.avatarDecoration = avatarDecoration;
return this;
}

public UserImpl setProfile(Profile profile)
{
this.profile = profile;
Expand Down
Loading