From 3d0ae900fdf8be4cedd1ecb7c166c8fd51cf66e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Spie=C3=9F?= Date: Thu, 6 Apr 2023 20:05:43 +0200 Subject: [PATCH] Handle new channel field on interactions (#2436) --- .../handle/InteractionCreateHandler.java | 20 ++++++++++++++++--- .../interactions/InteractionImpl.java | 10 ++++++++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/internal/handle/InteractionCreateHandler.java b/src/main/java/net/dv8tion/jda/internal/handle/InteractionCreateHandler.java index 85770a37a8..3407426be9 100644 --- a/src/main/java/net/dv8tion/jda/internal/handle/InteractionCreateHandler.java +++ b/src/main/java/net/dv8tion/jda/internal/handle/InteractionCreateHandler.java @@ -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; @@ -33,7 +34,6 @@ 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; @@ -41,6 +41,7 @@ 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 @@ -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)) { diff --git a/src/main/java/net/dv8tion/jda/internal/interactions/InteractionImpl.java b/src/main/java/net/dv8tion/jda/internal/interactions/InteractionImpl.java index 7f2e2ca0ab..b2da3e74b3 100644 --- a/src/main/java/net/dv8tion/jda/internal/interactions/InteractionImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/interactions/InteractionImpl.java @@ -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; @@ -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) {