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)); } 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..a58d8b772d --- /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 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 IllegalArgumentException( + "Did not find the forum post with ID %s while trying to reply to a post.")); + + } + + 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); + } +}