Skip to content

Commit 5498c6a

Browse files
authored
bug/archive flow moved inside retrieve member action (#964)
* archive flow moved inside retrieve member action * improve var name * refactor auto archive flow * remove unnecessary handling of failures & extract archive flow to seperate method for clarity * reverting back to original values for timers, last ones meant to be for debugging * unnecessary line of space
1 parent 5fc8f0a commit 5498c6a

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import net.dv8tion.jda.api.EmbedBuilder;
44
import net.dv8tion.jda.api.JDA;
55
import net.dv8tion.jda.api.entities.Guild;
6-
import net.dv8tion.jda.api.entities.Member;
76
import net.dv8tion.jda.api.entities.MessageEmbed;
87
import net.dv8tion.jda.api.entities.channel.concrete.ForumChannel;
98
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
109
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
10+
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
1111
import net.dv8tion.jda.api.utils.TimeUtil;
1212
import org.slf4j.Logger;
1313
import org.slf4j.LoggerFactory;
@@ -19,6 +19,7 @@
1919
import java.util.List;
2020
import java.util.Optional;
2121
import java.util.concurrent.TimeUnit;
22+
import java.util.function.Consumer;
2223

2324
/**
2425
* Routine, which periodically checks all help threads and archives them if there has not been any
@@ -66,16 +67,15 @@ private void autoArchiveForGuild(Guild guild) {
6667
logger.debug("Found {} active questions", activeThreads.size());
6768

6869
Instant archiveAfterMoment = computeArchiveAfterMoment();
69-
activeThreads.forEach(activeThread -> autoArchiveForThread(activeThread, archiveAfterMoment,
70-
activeThread.getOwner()));
70+
activeThreads
71+
.forEach(activeThread -> autoArchiveForThread(activeThread, archiveAfterMoment));
7172
}
7273

7374
private Instant computeArchiveAfterMoment() {
7475
return Instant.now().minus(ARCHIVE_AFTER_INACTIVITY_OF);
7576
}
7677

77-
private void autoArchiveForThread(ThreadChannel threadChannel, Instant archiveAfterMoment,
78-
Member author) {
78+
private void autoArchiveForThread(ThreadChannel threadChannel, Instant archiveAfterMoment) {
7979
if (shouldBeArchived(threadChannel, archiveAfterMoment)) {
8080
logger.debug("Auto archiving help thread {}", threadChannel.getId());
8181

@@ -110,10 +110,7 @@ private void autoArchiveForThread(ThreadChannel threadChannel, Instant archiveAf
110110
.setColor(HelpSystemHelper.AMBIENT_COLOR)
111111
.build();
112112

113-
threadChannel.sendMessage(author.getAsMention())
114-
.addEmbeds(embed)
115-
.flatMap(any -> threadChannel.getManager().setArchived(true))
116-
.queue();
113+
handleArchiveFlow(threadChannel, embed);
117114
}
118115
}
119116

@@ -123,4 +120,20 @@ private static boolean shouldBeArchived(MessageChannel channel, Instant archiveA
123120

124121
return lastActivity.isBefore(archiveAfterMoment);
125122
}
123+
124+
private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) {
125+
Consumer<Throwable> handleFailure = error -> {
126+
if (error instanceof ErrorResponseException) {
127+
logger.warn("Unknown error occurred during help thread auto archive routine",
128+
error);
129+
}
130+
};
131+
132+
threadChannel.getGuild()
133+
.retrieveMemberById(threadChannel.getOwnerIdLong())
134+
.flatMap(author -> threadChannel.sendMessage(author.getAsMention()).addEmbeds(embed))
135+
.flatMap(any -> threadChannel.getManager().setArchived(true))
136+
.queue(any -> {
137+
}, handleFailure);
138+
}
126139
}

0 commit comments

Comments
 (0)