Skip to content

Commit 25a04d4

Browse files
authored
Merge pull request #468 from danthe1st/pin-qotw-answer-forum
fix pinning of QOTW answer posts
2 parents 2b5af8c + 802c571 commit 25a04d4

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

src/main/java/net/discordjug/javabot/systems/qotw/jobs/QOTWCloseSubmissionsJob.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import net.dv8tion.jda.api.interactions.components.buttons.Button;
2828
import net.dv8tion.jda.api.interactions.components.selections.SelectOption;
2929
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;
3032
import net.dv8tion.jda.api.requests.restaction.ForumPostAction;
3133
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
3234
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
@@ -36,6 +38,7 @@
3638
import org.springframework.stereotype.Service;
3739
import xyz.dynxsty.dih4jda.util.ComponentIdBuilder;
3840

41+
import javax.annotation.CheckReturnValue;
3942
import javax.annotation.Nonnull;
4043

4144
import java.sql.SQLException;
@@ -113,18 +116,41 @@ public void execute() throws SQLException {
113116
QOTWQuestion question = questionOptional.get();
114117
try (MessageCreateData data = new MessageCreateBuilder()
115118
.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);
122120
}
123121
}
124122
});
125123
}
126124
}
127125

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+
128154
private @Nonnull StringSelectMenu buildSubmissionSelectMenu(JDA jda, long threadId) {
129155
final SystemsConfig.EmojiConfig emojiConfig = botConfig.getSystems().getEmojiConfig();
130156
return StringSelectMenu.create(ComponentIdBuilder.build("qotw-submission-select", "review", threadId))

0 commit comments

Comments
 (0)