Skip to content

Commit

Permalink
Fixes NPE from uncached users (#2035)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver276 committed Feb 22, 2022
1 parent 5cd34d3 commit ac3104d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/main/java/net/dv8tion/jda/internal/entities/EntityBuilder.java
Expand Up @@ -1217,7 +1217,7 @@ public PrivateChannel createPrivateChannel(DataObject json, UserImpl user)
if (recipient != null)
{
//update the channel if we have found the user
channel.setUser(user);
channel.setUser(recipient);
}
}
if (recipient != null)
Expand Down Expand Up @@ -1363,17 +1363,34 @@ public ReceivedMessage createMessage(DataObject jsonObject, @Nullable MessageCha
final long authorId = author.getLong("id");
MemberImpl member = null;

if (channel == null && jsonObject.isNull("guild_id"))
if (jsonObject.isNull("guild_id"))
{
DataObject channelData = DataObject.empty()
.put("id", channelId);
//if we see an author that isn't us, we can assume that is the other side of this private channel
//if the author is us, we learn no information about the user at the other end
if (authorId != getJDA().getSelfUser().getIdLong())
//we know it's a private channel
PrivateChannelImpl priv = (PrivateChannelImpl) channel;

boolean isAuthorSelfUser = authorId == getJDA().getSelfUser().getIdLong();
if (priv == null)
{
DataObject channelData = DataObject.empty()
.put("id", channelId);

//if we see an author that isn't us, we can assume that is the other side of this private channel
//if the author is us, we learn no information about the user at the other end
if (!isAuthorSelfUser)
channelData.put("recipient", author);
//even without knowing the user at the other end, we can still construct a minimal channel
channel = createPrivateChannel(channelData);

//even without knowing the user at the other end, we can still construct a minimal channel
channel = createPrivateChannel(channelData);
}
else if (priv.getUser() == null && !isAuthorSelfUser)
{
//if we see an author that isn't us, we can assume that is the other side of this private channel
//if the author is us, we learn no information about the user at the other end
priv.setUser(createUser(author));
}

}

else if (channel == null)
throw new IllegalArgumentException(MISSING_CHANNEL);

Expand Down
Expand Up @@ -184,6 +184,9 @@ public String toString()

private void updateUser()
{
//if the user is null then we don't even know their ID, and so we have to check that first
if (user == null)
return;
// Load user from cache if one exists, otherwise we might have an outdated user instance
User realUser = getJDA().getUserById(user.getIdLong());
if (realUser != null)
Expand Down
Expand Up @@ -25,6 +25,7 @@
import net.dv8tion.jda.internal.entities.EmoteImpl;
import net.dv8tion.jda.internal.entities.GuildImpl;
import net.dv8tion.jda.internal.entities.MemberImpl;
import net.dv8tion.jda.internal.entities.PrivateChannelImpl;
import net.dv8tion.jda.internal.requests.WebSocketClient;
import net.dv8tion.jda.internal.utils.JDALogger;

Expand Down Expand Up @@ -168,7 +169,15 @@ protected Long handleInternally(DataObject content)
MessageReaction reaction = new MessageReaction(channel, rEmote, messageId, userId == api.getSelfUser().getIdLong(), -1);

if (channel.getType() == ChannelType.PRIVATE)
{
api.usedPrivateChannel(reaction.getChannel().getIdLong());
PrivateChannelImpl priv = (PrivateChannelImpl) channel;
//try to add the user here if we need to, as we have their ID
if (priv.getUser() == null && user != null)
{
priv.setUser(user);
}
}

if (add)
{
Expand Down

0 comments on commit ac3104d

Please sign in to comment.