Skip to content

Commit

Permalink
Create a PrivateChannel on every interaction
Browse files Browse the repository at this point in the history
Fixes the returned (cached) channel having the wrong user
  • Loading branch information
freya022 committed May 18, 2024
1 parent c1a27d3 commit 244cce2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.entities.channel.concrete.PrivateChannelImpl;
import net.dv8tion.jda.internal.entities.channel.mixin.attribute.IPostContainerMixin;
import net.dv8tion.jda.internal.entities.channel.mixin.concrete.*;
import net.dv8tion.jda.internal.entities.mixin.MemberMixin;
Expand Down Expand Up @@ -196,6 +197,11 @@ public ForumTagImpl createForumTag(IPostContainerMixin<?> channel, DataObject js
return tag;
}

protected void configurePrivateChannel(DataObject json, PrivateChannelImpl channel)
{
channel.setLatestMessageIdLong(json.getLong("last_message_id", 0));
}

protected void configureMember(DataObject memberJson, MemberMixin<?> member)
{
member.setNickname(memberJson.getString("nick", null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1368,10 +1368,8 @@ public PrivateChannel createPrivateChannel(DataObject json, UserImpl user)
final long channelId = json.getUnsignedLong("id");
PrivateChannelImpl channel = (PrivateChannelImpl) api.getPrivateChannelById(channelId);
if (channel == null)
{
channel = new PrivateChannelImpl(getJDA(), channelId, user)
.setLatestMessageIdLong(json.getLong("last_message_id", 0));
}
channel = new PrivateChannelImpl(getJDA(), channelId, user);
configurePrivateChannel(json, channel);
UserImpl recipient = user;
if (channel.getUser() == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.entities.channel.concrete.PrivateChannelImpl;
import net.dv8tion.jda.internal.entities.channel.concrete.detached.*;
import net.dv8tion.jda.internal.entities.channel.mixin.attribute.IInteractionPermissionMixin;
import net.dv8tion.jda.internal.entities.detached.DetachedGuildImpl;
Expand Down Expand Up @@ -245,4 +246,11 @@ public Role createRole(@Nonnull Guild guild, DataObject roleJson)
configureRole(roleJson, role, id);
return role;
}

public PrivateChannel createPrivateChannel(DataObject json, User user)
{
final PrivateChannelImpl channel = new PrivateChannelImpl(getJDA(), json.getUnsignedLong("id"), user);
configurePrivateChannel(json, channel);
return channel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.channel.Channel;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.concrete.PrivateChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.interactions.DiscordLocale;
import net.dv8tion.jda.api.interactions.IntegrationOwners;
Expand All @@ -35,8 +34,6 @@
import net.dv8tion.jda.internal.entities.GuildImpl;
import net.dv8tion.jda.internal.entities.InteractionEntityBuilder;
import net.dv8tion.jda.internal.entities.MemberImpl;
import net.dv8tion.jda.internal.entities.UserImpl;
import net.dv8tion.jda.internal.entities.channel.concrete.PrivateChannelImpl;
import net.dv8tion.jda.internal.entities.detached.DetachedGuildImpl;
import net.dv8tion.jda.internal.utils.Helpers;

Expand Down Expand Up @@ -108,30 +105,14 @@ else if (guild instanceof DetachedGuildImpl)
}
else
{
//(G)DMs
user = jda.getEntityBuilder().createUser(userObj);
member = null;
long channelId = channelJson.getUnsignedLong("id");
ChannelType type = ChannelType.fromId(channelJson.getInt("type"));
if (type == ChannelType.PRIVATE) {
PrivateChannel channel = jda.getPrivateChannelById(channelId);
if (channel == null) {
channel = jda.getEntityBuilder().createPrivateChannel(
DataObject.empty()
.put("id", channelId)
.put("recipient", data.getObject("user"))
);
}
this.channel = channel;

User user = channel.getUser();
if (user == null) {
user = jda.getEntityBuilder().createUser(data.getObject("user"));
((PrivateChannelImpl) channel).setUser(user);
((UserImpl) user).setPrivateChannel(channel);
}
this.user = user;
this.channel = interactionEntityBuilder.createPrivateChannel(channelJson, user);
} else if (type == ChannelType.GROUP) {
this.channel = interactionEntityBuilder.createGroupChannel(channelJson);
this.user = jda.getEntityBuilder().createUser(data.getObject("user"));
} else {
throw new IllegalArgumentException("Received interaction in unexpected channel type! Type " + type + " is not supported yet!");
}
Expand Down

0 comments on commit 244cce2

Please sign in to comment.