55import net .dv8tion .jda .api .JDA ;
66import net .dv8tion .jda .api .entities .Guild ;
77import 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 ;
810import net .dv8tion .jda .api .entities .channel .middleman .GuildChannel ;
911import 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 ;
1014import net .dv8tion .jda .api .events .message .MessageReceivedEvent ;
1115import net .dv8tion .jda .api .hooks .ListenerAdapter ;
1216import 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 /**
0 commit comments