From 5b087df7bffe7cfd8ab0ed84c5570ac27254d1ab Mon Sep 17 00:00:00 2001 From: discorddioxin <72288822+discorddioxin@users.noreply.github.com> Date: Mon, 25 Aug 2025 00:07:34 -0700 Subject: [PATCH 1/3] Introduced ForumPoster Allows easier access & management of forum posting. See #1307 --- .../tjbot/features/utils/ForumPoster.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 application/src/main/java/org/togetherjava/tjbot/features/utils/ForumPoster.java diff --git a/application/src/main/java/org/togetherjava/tjbot/features/utils/ForumPoster.java b/application/src/main/java/org/togetherjava/tjbot/features/utils/ForumPoster.java new file mode 100644 index 0000000000..1303d62f3b --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/features/utils/ForumPoster.java @@ -0,0 +1,52 @@ +package org.togetherjava.tjbot.features.utils; + +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.entities.channel.concrete.ForumChannel; +import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; +import net.dv8tion.jda.api.requests.restaction.ForumPostAction; +import net.dv8tion.jda.api.requests.restaction.MessageCreateAction; +import net.dv8tion.jda.api.utils.messages.MessageCreateData; + +import java.util.*; + +public final class ForumPoster { + private final JDA jda; + + private ForumPoster(JDA jda) { + this.jda = jda; + } + + public static ForumPoster using(JDA jda) { + return new ForumPoster(jda); + } + + private ForumChannel findForumChannel(String channelId) { + return Optional.ofNullable(jda.getForumChannelById(channelId)) + .orElseThrow(() -> new IllegalStateException( + "Did not find a forum channel with ID %s while trying to create a forum post. Make sure the config is setup properly.")); + } + + private ThreadChannel findForumPost(String postId) { + return Optional.ofNullable(jda.getThreadChannelById(postId)) + .orElseThrow(() -> new IllegalStateException( + "Did not find the forum post with ID %s while trying to reply to a post. Make sure the config is setup properly.")); + + } + + public MessageCreateAction sendPost(String postId, MessageCreateData messageData) { + return sendPost(findForumPost(postId), messageData); + } + + public MessageCreateAction sendPost(ThreadChannel post, MessageCreateData messageData) { + return post.sendMessage(messageData); + } + + public ForumPostAction createPost(String channelId, String title, MessageCreateData message) { + return createPost(findForumChannel(channelId), title, message); + } + + public ForumPostAction createPost(ForumChannel channel, String title, + MessageCreateData message) { + return channel.createForumPost(title, message); + } +} From a65e39388d969edd8c09c4307b24bae5688014be Mon Sep 17 00:00:00 2001 From: discorddioxin <72288822+discorddioxin@users.noreply.github.com> Date: Mon, 25 Aug 2025 00:10:09 -0700 Subject: [PATCH 2/3] TransferQuestionCommand now makes use of ForumPoster Introduced a forum management API. TransferQuestionCommand now makes use of it. See #1307 --- .../tjbot/features/moderation/TransferQuestionCommand.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java b/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java index 69e59f9358..ef8899354d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java @@ -32,6 +32,7 @@ import org.togetherjava.tjbot.features.CommandVisibility; import org.togetherjava.tjbot.features.MessageContextCommand; import org.togetherjava.tjbot.features.chatgpt.ChatGptService; +import org.togetherjava.tjbot.features.utils.ForumPoster; import org.togetherjava.tjbot.features.utils.StringDistances; import java.awt.Color; @@ -233,7 +234,8 @@ private RestAction createForumPost(ModalInteractionEvent event, ForumTag tag = getTagOrDefault(questionsForum.getAvailableTagsByName(queryTag, true), () -> questionsForum.getAvailableTagsByName(mostCommonTag, true).getFirst()); - return questionsForum.createForumPost(forumTitle, forumMessage) + return ForumPoster.using(event.getJDA()) + .createPost(questionsForum, forumTitle, forumMessage) .setTags(ForumTagSnowflake.fromId(tag.getId())) .map(createdPost -> new ForumPostData(createdPost, originalUser)); } From e329d815ff125bcc8f5648b49ab6a3c0f42e40e1 Mon Sep 17 00:00:00 2001 From: discorddioxin <72288822+discorddioxin@users.noreply.github.com> Date: Mon, 25 Aug 2025 00:48:19 -0700 Subject: [PATCH 3/3] Updated ForumPoster to improve error descriptions --- .../org/togetherjava/tjbot/features/utils/ForumPoster.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/utils/ForumPoster.java b/application/src/main/java/org/togetherjava/tjbot/features/utils/ForumPoster.java index 1303d62f3b..a58d8b772d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/utils/ForumPoster.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/utils/ForumPoster.java @@ -22,14 +22,14 @@ public static ForumPoster using(JDA jda) { private ForumChannel findForumChannel(String channelId) { return Optional.ofNullable(jda.getForumChannelById(channelId)) - .orElseThrow(() -> new IllegalStateException( + .orElseThrow(() -> new IllegalArgumentException( "Did not find a forum channel with ID %s while trying to create a forum post. Make sure the config is setup properly.")); } private ThreadChannel findForumPost(String postId) { return Optional.ofNullable(jda.getThreadChannelById(postId)) - .orElseThrow(() -> new IllegalStateException( - "Did not find the forum post with ID %s while trying to reply to a post. Make sure the config is setup properly.")); + .orElseThrow(() -> new IllegalArgumentException( + "Did not find the forum post with ID %s while trying to reply to a post.")); }