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

Fix handling of wrong length discriminators #2478

Merged
merged 2 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public SelfUser createSelfUser(DataObject self)
.setMfaEnabled(self.getBoolean("mfa_enabled"))
.setName(self.getString("username"))
.setGlobalName(self.getString("global_name", null))
.setDiscriminator(self.getString("discriminator", "0"))
.setDiscriminator(Short.parseShort(self.getString("discriminator", "0")))
.setAvatarId(self.getString("avatar", null))
.setBot(self.getBoolean("bot"))
.setSystem(false);
Expand Down Expand Up @@ -453,7 +453,7 @@ public UserImpl createUser(DataObject user)
// Initial creation
userObj.setName(user.getString("username"))
.setGlobalName(user.getString("global_name", null))
.setDiscriminator(user.getString("discriminator", "0"))
.setDiscriminator(Short.parseShort(user.getString("discriminator", "0")))
.setAvatarId(user.getString("avatar", null))
.setBot(user.getBoolean("bot"))
.setSystem(user.getBoolean("system"))
Expand All @@ -475,8 +475,8 @@ public void updateUser(UserImpl userObj, DataObject user)
String newName = user.getString("username");
String oldGlobalName = userObj.getGlobalName();
String newGlobalName = user.getString("global_name", null);
String oldDiscriminator = userObj.getDiscriminator();
String newDiscriminator = user.getString("discriminator", "0");
short oldDiscriminator = userObj.getDiscriminatorInt();
short newDiscriminator = Short.parseShort(user.getString("discriminator", "0"));
String oldAvatar = userObj.getAvatarId();
String newAvatar = user.getString("avatar", null);
int oldFlags = userObj.getFlagsRaw();
Expand All @@ -502,13 +502,14 @@ public void updateUser(UserImpl userObj, DataObject user)
userObj, oldGlobalName));
}

if (!oldDiscriminator.equals(newDiscriminator))
if (oldDiscriminator != newDiscriminator)
{
String oldDiscrimString = userObj.getDiscriminator();
userObj.setDiscriminator(newDiscriminator);
jda.handleEvent(
new UserUpdateDiscriminatorEvent(
jda, responseNumber,
userObj, oldDiscriminator));
userObj, oldDiscrimString));
}

if (!Objects.equals(oldAvatar, newAvatar))
Expand Down Expand Up @@ -548,7 +549,6 @@ public boolean updateMemberCache(MemberImpl member, boolean forceRemove)
if (user.getMutualGuilds().isEmpty())
{
// we no longer share any guilds/channels with this user so remove it from cache
user.setFake(true);
getJDA().getUsersView().remove(user.getIdLong());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static SelfUserImpl copyOf(SelfUserImpl other, JDAImpl jda)
selfUser.setName(other.name)
.setGlobalName(other.globalName)
.setAvatarId(other.avatarId)
.setDiscriminator(other.getDiscriminator())
.setDiscriminator(other.getDiscriminatorInt())
.setBot(other.bot);
return selfUser
.setVerified(other.verified)
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/net/dv8tion/jda/internal/entities/UserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public class UserImpl extends UserSnowflakeImpl implements User
protected long privateChannelId = 0L;
protected boolean bot;
protected boolean system;
protected boolean fake = false;
protected int flags;

public UserImpl(long id, JDAImpl api)
Expand Down Expand Up @@ -214,9 +213,9 @@ public UserImpl setGlobalName(String globalName)
return this;
}

public UserImpl setDiscriminator(String discriminator)
public UserImpl setDiscriminator(short discriminator)
{
this.discriminator = Short.parseShort(discriminator);
this.discriminator = discriminator;
return this;
}

Expand Down Expand Up @@ -251,18 +250,17 @@ public UserImpl setSystem(boolean system)
return this;
}

public UserImpl setFake(boolean fake)
{
this.fake = fake;
return this;
}

public UserImpl setFlags(int flags)
{
this.flags = flags;
return this;
}

public short getDiscriminatorInt()
{
return discriminator;
}

@Override
public void formatTo(Formatter formatter, int flags, int width, int precision)
{
Expand Down