Skip to content

Commit 29fa56e

Browse files
authored
Merge pull request #432 from danthe1st/threaded-links
fix message links and embeds in threads
2 parents 1df6230 + 52ddc2e commit 29fa56e

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/main/java/net/javadiscord/javabot/listener/MessageLinkListener.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
import net.dv8tion.jda.api.JDA;
66
import net.dv8tion.jda.api.entities.Guild;
77
import net.dv8tion.jda.api.entities.Message;
8+
import net.dv8tion.jda.api.entities.channel.ChannelType;
9+
import net.dv8tion.jda.api.entities.channel.attribute.IWebhookContainer;
810
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
911
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
12+
import net.dv8tion.jda.api.entities.channel.unions.IThreadContainerUnion;
13+
import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion;
1014
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
1115
import net.dv8tion.jda.api.hooks.ListenerAdapter;
1216
import net.dv8tion.jda.api.requests.RestAction;
@@ -32,13 +36,32 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
3236
if (event.getAuthor().isBot() || event.getAuthor().isSystem()) return;
3337
Matcher matcher = MESSAGE_URL_PATTERN.matcher(event.getMessage().getContentRaw());
3438
if (matcher.find()) {
35-
Optional<RestAction<Message>> optional = parseMessageUrl(matcher.group(), event.getJDA());
36-
optional.ifPresent(action -> action.queue(
37-
m -> WebhookUtil.ensureWebhookExists(event.getChannel().asTextChannel(),
38-
wh -> WebhookUtil.mirrorMessageToWebhook(wh, m, m.getContentRaw(), 0, List.of(ActionRow.of(Button.link(m.getJumpUrl(), "Jump to Message"))), null)
39-
), e -> ExceptionLogger.capture(e, getClass().getSimpleName())
40-
));
39+
MessageChannelUnion messageChannel = event.getChannel();
40+
IWebhookContainer webhookChannel = getWebhookChannel(messageChannel);
41+
if (webhookChannel != null) {
42+
Optional<RestAction<Message>> optional = parseMessageUrl(matcher.group(), event.getJDA());
43+
optional.ifPresent(action -> action.queue(m -> {
44+
WebhookUtil.ensureWebhookExists(webhookChannel,
45+
wh -> WebhookUtil.mirrorMessageToWebhook(wh, m, m.getContentRaw(), messageChannel.getType().isThread() ? messageChannel.getIdLong() : 0, List.of(ActionRow.of(Button.link(m.getJumpUrl(), "Jump to Message"))), null));
46+
}, e -> ExceptionLogger.capture(e, getClass().getSimpleName())));
47+
}
48+
}
49+
}
50+
51+
private IWebhookContainer getWebhookChannel(MessageChannelUnion channel) {
52+
return switch (channel.getType()) {
53+
case GUILD_PRIVATE_THREAD, GUILD_PUBLIC_THREAD -> getWebhookChannelFromParentChannel(channel);
54+
case TEXT -> channel.asTextChannel();
55+
default -> null;
56+
};
57+
}
58+
59+
private IWebhookContainer getWebhookChannelFromParentChannel(MessageChannelUnion childChannel) {
60+
IThreadContainerUnion parentChannel = childChannel.asThreadChannel().getParentChannel();
61+
if (parentChannel.getType() == ChannelType.FORUM) {
62+
return parentChannel.asForumChannel();
4163
}
64+
return parentChannel.asStandardGuildMessageChannel();
4265
}
4366

4467
/**

src/main/java/net/javadiscord/javabot/systems/staff_commands/embeds/CreateEmbedSubcommand.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import net.dv8tion.jda.api.entities.*;
99
import net.dv8tion.jda.api.entities.channel.Channel;
1010
import net.dv8tion.jda.api.entities.channel.ChannelType;
11-
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
1211
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
12+
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
1313
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
1414
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
1515
import net.dv8tion.jda.api.interactions.commands.OptionType;
@@ -78,7 +78,11 @@ public void handleModal(@NotNull ModalInteractionEvent event, @NotNull List<Moda
7878
return;
7979
}
8080
String[] id = ComponentIdBuilder.split(event.getModalId());
81-
TextChannel channel = event.getGuild().getTextChannelById(id[1]);
81+
String channelId = id[1];
82+
MessageChannel channel = event.getGuild().getTextChannelById(channelId);
83+
if (channel == null) {
84+
channel = event.getGuild().getThreadChannelById(channelId);
85+
}
8286
if (channel == null) {
8387
Responses.error(event.getHook(), "Please provide a valid text channel.").queue();
8488
return;

0 commit comments

Comments
 (0)