|
27 | 27 | import net.dv8tion.jda.api.interactions.components.buttons.Button; |
28 | 28 | import net.dv8tion.jda.api.interactions.components.selections.SelectOption; |
29 | 29 | import net.dv8tion.jda.api.interactions.components.selections.StringSelectMenu; |
| 30 | +import net.dv8tion.jda.api.managers.channel.concrete.ThreadChannelManager; |
| 31 | +import net.dv8tion.jda.api.requests.RestAction; |
30 | 32 | import net.dv8tion.jda.api.requests.restaction.ForumPostAction; |
31 | 33 | import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder; |
32 | 34 | import net.dv8tion.jda.api.utils.messages.MessageCreateData; |
|
36 | 38 | import org.springframework.stereotype.Service; |
37 | 39 | import xyz.dynxsty.dih4jda.util.ComponentIdBuilder; |
38 | 40 |
|
| 41 | +import javax.annotation.CheckReturnValue; |
39 | 42 | import javax.annotation.Nonnull; |
40 | 43 |
|
41 | 44 | import java.sql.SQLException; |
@@ -113,18 +116,41 @@ public void execute() throws SQLException { |
113 | 116 | QOTWQuestion question = questionOptional.get(); |
114 | 117 | try (MessageCreateData data = new MessageCreateBuilder() |
115 | 118 | .setEmbeds(buildQuestionEmbed(question)).build()) { |
116 | | - ForumPostAction action = qotwConfig.getSubmissionsForumChannel() |
117 | | - .createForumPost(String.format("Week %s — %s", question.getQuestionNumber(), question.getText().replace("*", "")), data); |
118 | | - if (qotwConfig.getSubmissionsForumOngoingReviewTag() != null) { |
119 | | - action.setTags(qotwConfig.getSubmissionsForumOngoingReviewTag()); |
120 | | - } |
121 | | - action.queue(f -> f.getThreadChannel().getManager().setPinned(true).queue()); |
| 119 | + createPinnedAnswerForum(qotwConfig, question, data); |
122 | 120 | } |
123 | 121 | } |
124 | 122 | }); |
125 | 123 | } |
126 | 124 | } |
127 | 125 |
|
| 126 | + private void createPinnedAnswerForum(QOTWConfig qotwConfig, QOTWQuestion question, MessageCreateData data) { |
| 127 | + ForumPostAction createAnswerForumAction = qotwConfig.getSubmissionsForumChannel() |
| 128 | + .createForumPost(String.format("Week %s — %s", question.getQuestionNumber(), question.getText().replace("*", "")), data); |
| 129 | + if (qotwConfig.getSubmissionsForumOngoingReviewTag() != null) { |
| 130 | + createAnswerForumAction.setTags(qotwConfig.getSubmissionsForumOngoingReviewTag()); |
| 131 | + } |
| 132 | + |
| 133 | + createAnswerForumAction.flatMap(f -> changeForumPin(f.getThreadChannel())).queue(); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Removes the current pin in a forum channel and pins a new post. |
| 138 | + * @param toPin the forum channel to pin |
| 139 | + * @return a {@link RestAction} performing the pin change |
| 140 | + */ |
| 141 | + @CheckReturnValue |
| 142 | + private RestAction<Void> changeForumPin(ThreadChannel toPin) { |
| 143 | + ThreadChannelManager setPinned = toPin.getManager().setPinned(true); |
| 144 | + return toPin.getParentChannel() |
| 145 | + .getThreadChannels() |
| 146 | + .stream() |
| 147 | + .filter(ThreadChannel::isPinned) |
| 148 | + .findAny() |
| 149 | + .map(c->c.getManager().setPinned(false)) |
| 150 | + .map(a -> a.flatMap(result -> setPinned)) |
| 151 | + .orElse(setPinned); |
| 152 | + } |
| 153 | + |
128 | 154 | private @Nonnull StringSelectMenu buildSubmissionSelectMenu(JDA jda, long threadId) { |
129 | 155 | final SystemsConfig.EmojiConfig emojiConfig = botConfig.getSystems().getEmojiConfig(); |
130 | 156 | return StringSelectMenu.create(ComponentIdBuilder.build("qotw-submission-select", "review", threadId)) |
|
0 commit comments