Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<RestAction<Message>> 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<RestAction<Message>> 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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,7 +78,11 @@ public void handleModal(@NotNull ModalInteractionEvent event, @NotNull List<Moda
return;
}
String[] id = ComponentIdBuilder.split(event.getModalId());
TextChannel channel = event.getGuild().getTextChannelById(id[1]);
String channelId = id[1];
MessageChannel channel = event.getGuild().getTextChannelById(channelId);
if (channel == null) {
channel = event.getGuild().getThreadChannelById(channelId);
}
if (channel == null) {
Responses.error(event.getHook(), "Please provide a valid text channel.").queue();
return;
Expand Down