Skip to content

Commit

Permalink
bug/archive flow moved inside retrieve member action (#964)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ankitsmt211 authored Nov 25, 2023
1 parent 5fc8f0a commit 5498c6a
Showing 1 changed file with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.channel.concrete.ForumChannel;
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
import net.dv8tion.jda.api.utils.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -19,6 +19,7 @@
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

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

Instant archiveAfterMoment = computeArchiveAfterMoment();
activeThreads.forEach(activeThread -> autoArchiveForThread(activeThread, archiveAfterMoment,
activeThread.getOwner()));
activeThreads
.forEach(activeThread -> autoArchiveForThread(activeThread, archiveAfterMoment));
}

private Instant computeArchiveAfterMoment() {
return Instant.now().minus(ARCHIVE_AFTER_INACTIVITY_OF);
}

private void autoArchiveForThread(ThreadChannel threadChannel, Instant archiveAfterMoment,
Member author) {
private void autoArchiveForThread(ThreadChannel threadChannel, Instant archiveAfterMoment) {
if (shouldBeArchived(threadChannel, archiveAfterMoment)) {
logger.debug("Auto archiving help thread {}", threadChannel.getId());

Expand Down Expand Up @@ -110,10 +110,7 @@ private void autoArchiveForThread(ThreadChannel threadChannel, Instant archiveAf
.setColor(HelpSystemHelper.AMBIENT_COLOR)
.build();

threadChannel.sendMessage(author.getAsMention())
.addEmbeds(embed)
.flatMap(any -> threadChannel.getManager().setArchived(true))
.queue();
handleArchiveFlow(threadChannel, embed);
}
}

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

return lastActivity.isBefore(archiveAfterMoment);
}

private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) {
Consumer<Throwable> handleFailure = error -> {
if (error instanceof ErrorResponseException) {
logger.warn("Unknown error occurred during help thread auto archive routine",
error);
}
};

threadChannel.getGuild()
.retrieveMemberById(threadChannel.getOwnerIdLong())
.flatMap(author -> threadChannel.sendMessage(author.getAsMention()).addEmbeds(embed))
.flatMap(any -> threadChannel.getManager().setArchived(true))
.queue(any -> {
}, handleFailure);
}
}

0 comments on commit 5498c6a

Please sign in to comment.