From d9af1e99d1dfb4c1a3d33ed11a40975adfeae7a5 Mon Sep 17 00:00:00 2001 From: danthe1st Date: Mon, 28 Aug 2023 11:53:32 +0200 Subject: [PATCH 1/2] message links in threads --- .../javabot/listener/MessageLinkListener.java | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/javadiscord/javabot/listener/MessageLinkListener.java b/src/main/java/net/javadiscord/javabot/listener/MessageLinkListener.java index 6759beaa7..2113ca8a8 100644 --- a/src/main/java/net/javadiscord/javabot/listener/MessageLinkListener.java +++ b/src/main/java/net/javadiscord/javabot/listener/MessageLinkListener.java @@ -5,8 +5,12 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.channel.ChannelType; +import net.dv8tion.jda.api.entities.channel.attribute.IWebhookContainer; import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import net.dv8tion.jda.api.entities.channel.unions.IThreadContainerUnion; +import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.requests.RestAction; @@ -32,13 +36,32 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) { if (event.getAuthor().isBot() || event.getAuthor().isSystem()) return; Matcher matcher = MESSAGE_URL_PATTERN.matcher(event.getMessage().getContentRaw()); if (matcher.find()) { - Optional> optional = parseMessageUrl(matcher.group(), event.getJDA()); - optional.ifPresent(action -> action.queue( - m -> WebhookUtil.ensureWebhookExists(event.getChannel().asTextChannel(), - wh -> WebhookUtil.mirrorMessageToWebhook(wh, m, m.getContentRaw(), 0, List.of(ActionRow.of(Button.link(m.getJumpUrl(), "Jump to Message"))), null) - ), e -> ExceptionLogger.capture(e, getClass().getSimpleName()) - )); + MessageChannelUnion messageChannel = event.getChannel(); + IWebhookContainer webhookChannel = getWebhookChannel(messageChannel); + if (webhookChannel != null) { + Optional> optional = parseMessageUrl(matcher.group(), event.getJDA()); + optional.ifPresent(action -> action.queue(m -> { + WebhookUtil.ensureWebhookExists(webhookChannel, + 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)); + }, e -> ExceptionLogger.capture(e, getClass().getSimpleName()))); + } + } + } + + private IWebhookContainer getWebhookChannel(MessageChannelUnion channel) { + return switch (channel.getType()) { + case GUILD_PRIVATE_THREAD, GUILD_PUBLIC_THREAD -> getWebhookChannelFromParentChannel(channel); + case TEXT -> channel.asTextChannel(); + default -> null; + }; + } + + private IWebhookContainer getWebhookChannelFromParentChannel(MessageChannelUnion childChannel) { + IThreadContainerUnion parentChannel = childChannel.asThreadChannel().getParentChannel(); + if (parentChannel.getType() == ChannelType.FORUM) { + return parentChannel.asForumChannel(); } + return parentChannel.asStandardGuildMessageChannel(); } /** From f4f4171e12449c29286d2b402e559516f43ddb2f Mon Sep 17 00:00:00 2001 From: danthe1st Date: Mon, 28 Aug 2023 12:46:11 +0200 Subject: [PATCH 2/2] allow using /embed create in threads --- .../staff_commands/embeds/CreateEmbedSubcommand.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/javadiscord/javabot/systems/staff_commands/embeds/CreateEmbedSubcommand.java b/src/main/java/net/javadiscord/javabot/systems/staff_commands/embeds/CreateEmbedSubcommand.java index f605dd292..441e7c957 100644 --- a/src/main/java/net/javadiscord/javabot/systems/staff_commands/embeds/CreateEmbedSubcommand.java +++ b/src/main/java/net/javadiscord/javabot/systems/staff_commands/embeds/CreateEmbedSubcommand.java @@ -8,8 +8,8 @@ import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.entities.channel.Channel; import net.dv8tion.jda.api.entities.channel.ChannelType; -import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel; +import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.interactions.commands.OptionType; @@ -78,7 +78,11 @@ public void handleModal(@NotNull ModalInteractionEvent event, @NotNull List