Skip to content

Commit

Permalink
Handle new channel field on interactions (#2436)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment authored Apr 6, 2023
1 parent 2eac320 commit 3d0ae90
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package net.dv8tion.jda.internal.handle;

import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
Expand All @@ -33,14 +34,14 @@
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.interactions.InteractionImpl;
import net.dv8tion.jda.internal.interactions.modal.ModalInteractionImpl;
import net.dv8tion.jda.internal.interactions.command.CommandAutoCompleteInteractionImpl;
import net.dv8tion.jda.internal.interactions.command.MessageContextInteractionImpl;
import net.dv8tion.jda.internal.interactions.command.SlashCommandInteractionImpl;
import net.dv8tion.jda.internal.interactions.command.UserContextInteractionImpl;
import net.dv8tion.jda.internal.interactions.component.ButtonInteractionImpl;
import net.dv8tion.jda.internal.interactions.component.EntitySelectInteractionImpl;
import net.dv8tion.jda.internal.interactions.component.StringSelectInteractionImpl;
import net.dv8tion.jda.internal.interactions.modal.ModalInteractionImpl;
import net.dv8tion.jda.internal.requests.WebSocketClient;

public class InteractionCreateHandler extends SocketHandler
Expand All @@ -67,15 +68,28 @@ protected Long handleInternally(DataObject content)
return guildId;
if (guildId != 0 && guild == null)
return null; // discard event if it is not from a guild we are currently in

// Check channel type
DataObject channelJson = content.getObject("channel");
if (guild != null)
{
GuildChannel channel = guild.getGuildChannelById(content.getUnsignedLong("channel_id", 0));
if (channel == null || !channel.getType().isMessage()) // TODO: This might break when interactions can be used outside of message channels in the future, not the case right now though!
long channelId = channelJson.getUnsignedLong("id", 0);
GuildChannel channel = guild.getGuildChannelById(channelId);
if (channel == null || !channel.getType().isMessage())
{
WebSocketClient.LOG.debug("Discarding INTERACTION_CREATE event from unexpected channel type. Channel: {}", channel);
return null;
}
}
else
{
ChannelType channelType = ChannelType.fromId(channelJson.getInt("type"));
if (!channelType.isMessage() || channelType == ChannelType.GROUP)
{
WebSocketClient.LOG.debug("Discarding INTERACTION_CREATE event from unexpected channel type. Channel: {}", channelJson);
return null;
}
}

switch (InteractionType.fromKey(type))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.dv8tion.jda.api.entities.Member;
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.interactions.DiscordLocale;
import net.dv8tion.jda.api.interactions.Interaction;
Expand Down Expand Up @@ -58,17 +59,22 @@ public InteractionImpl(JDAImpl jda, DataObject data)
this.type = data.getInt("type");
this.guild = jda.getGuildById(data.getUnsignedLong("guild_id", 0L));
this.userLocale = DiscordLocale.from(data.getString("locale", "en-US"));

DataObject channelJson = data.getObject("channel");
if (guild != null)
{
member = jda.getEntityBuilder().createMember((GuildImpl) guild, data.getObject("member"));
jda.getEntityBuilder().updateMemberCache((MemberImpl) member);
user = member.getUser();
channel = guild.getGuildChannelById(data.getUnsignedLong("channel_id"));
channel = guild.getGuildChannelById(channelJson.getUnsignedLong("id"));
}
else
{
member = null;
long channelId = data.getUnsignedLong("channel_id");
long channelId = channelJson.getUnsignedLong("id");
ChannelType type = ChannelType.fromId(channelJson.getInt("type"));
if (type != ChannelType.PRIVATE)
throw new IllegalArgumentException("Received interaction in unexpected channel type! Type " + type + " is not supported yet!");
PrivateChannel channel = jda.getPrivateChannelById(channelId);
if (channel == null)
{
Expand Down

0 comments on commit 3d0ae90

Please sign in to comment.