Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
2 changes: 1 addition & 1 deletion buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ repositories {
}

dependencies {
implementation "gradle.plugin.org.flywaydb:gradle-plugin-publishing:10.0.0"
implementation "gradle.plugin.org.flywaydb:gradle-plugin-publishing:10.1.0"
implementation 'nu.studer:gradle-jooq-plugin:8.2'
}
2 changes: 1 addition & 1 deletion database/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var sqliteVersion = "3.44.0.0"
dependencies {
implementation 'com.google.code.findbugs:jsr305:3.0.2'
implementation "org.xerial:sqlite-jdbc:${sqliteVersion}"
implementation 'org.flywaydb:flyway-core:10.0.0'
implementation 'org.flywaydb:flyway-core:10.1.0'
implementation "org.jooq:jooq:$jooqVersion"

implementation project(':utils')
Expand Down