|
| 1 | +package org.togetherjava.tjbot.features.utils; |
| 2 | + |
| 3 | +import net.dv8tion.jda.api.JDA; |
| 4 | +import net.dv8tion.jda.api.entities.channel.concrete.ForumChannel; |
| 5 | +import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; |
| 6 | +import net.dv8tion.jda.api.requests.restaction.ForumPostAction; |
| 7 | +import net.dv8tion.jda.api.requests.restaction.MessageCreateAction; |
| 8 | +import net.dv8tion.jda.api.utils.messages.MessageCreateData; |
| 9 | + |
| 10 | +import java.util.*; |
| 11 | + |
| 12 | +public final class ForumPoster { |
| 13 | + private final JDA jda; |
| 14 | + |
| 15 | + private ForumPoster(JDA jda) { |
| 16 | + this.jda = jda; |
| 17 | + } |
| 18 | + |
| 19 | + public static ForumPoster using(JDA jda) { |
| 20 | + return new ForumPoster(jda); |
| 21 | + } |
| 22 | + |
| 23 | + private ForumChannel findForumChannel(String channelId) { |
| 24 | + return Optional.ofNullable(jda.getForumChannelById(channelId)) |
| 25 | + .orElseThrow(() -> new IllegalStateException( |
| 26 | + "Did not find a forum channel with ID %s while trying to create a forum post. Make sure the config is setup properly.")); |
| 27 | + } |
| 28 | + |
| 29 | + private ThreadChannel findForumPost(String postId) { |
| 30 | + return Optional.ofNullable(jda.getThreadChannelById(postId)) |
| 31 | + .orElseThrow(() -> new IllegalStateException( |
| 32 | + "Did not find the forum post with ID %s while trying to reply to a post. Make sure the config is setup properly.")); |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + public MessageCreateAction sendPost(String postId, MessageCreateData messageData) { |
| 37 | + return sendPost(findForumPost(postId), messageData); |
| 38 | + } |
| 39 | + |
| 40 | + public MessageCreateAction sendPost(ThreadChannel post, MessageCreateData messageData) { |
| 41 | + return post.sendMessage(messageData); |
| 42 | + } |
| 43 | + |
| 44 | + public ForumPostAction createPost(String channelId, String title, MessageCreateData message) { |
| 45 | + return createPost(findForumChannel(channelId), title, message); |
| 46 | + } |
| 47 | + |
| 48 | + public ForumPostAction createPost(ForumChannel channel, String title, |
| 49 | + MessageCreateData message) { |
| 50 | + return channel.createForumPost(title, message); |
| 51 | + } |
| 52 | +} |
0 commit comments