From 7b01d6da9de1278b2f42328999004e0e87f207ca Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Mon, 11 Dec 2023 22:57:24 +0530 Subject: [PATCH 01/19] handling error when member left guild --- .../features/help/HelpThreadAutoArchiver.java | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java index f1bdf20c2a..236be9ce53 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java @@ -3,11 +3,15 @@ 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.Message; 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.requests.RestAction; +import net.dv8tion.jda.api.utils.Result; import net.dv8tion.jda.api.utils.TimeUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,6 +24,7 @@ import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; +import java.util.function.Function; /** * Routine, which periodically checks all help threads and archives them if there has not been any @@ -27,8 +32,8 @@ */ public final class HelpThreadAutoArchiver implements Routine { private static final Logger logger = LoggerFactory.getLogger(HelpThreadAutoArchiver.class); - private static final int SCHEDULE_MINUTES = 60; - private static final Duration ARCHIVE_AFTER_INACTIVITY_OF = Duration.ofHours(12); + private static final int SCHEDULE_MINUTES = 6; + private static final Duration ARCHIVE_AFTER_INACTIVITY_OF = Duration.ofSeconds(12); private final HelpSystemHelper helper; @@ -43,7 +48,7 @@ public HelpThreadAutoArchiver(HelpSystemHelper helper) { @Override public Schedule createSchedule() { - return new Schedule(ScheduleMode.FIXED_RATE, 0, SCHEDULE_MINUTES, TimeUnit.MINUTES); + return new Schedule(ScheduleMode.FIXED_RATE, 0, SCHEDULE_MINUTES, TimeUnit.SECONDS); } @Override @@ -122,6 +127,7 @@ private static boolean shouldBeArchived(MessageChannel channel, Instant archiveA } private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) { + Consumer handleFailure = error -> { if (error instanceof ErrorResponseException) { logger.warn("Unknown error occurred during help thread auto archive routine", @@ -129,9 +135,22 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) } }; + Function, RestAction> sendEmbedWithMention = + (member) -> threadChannel.sendMessage(member.get().getAsMention()).addEmbeds(embed); + + Function, RestAction> sendEmbedWithoutMention = + (member) -> threadChannel.sendMessageEmbeds(embed); + threadChannel.getGuild() .retrieveMemberById(threadChannel.getOwnerIdLong()) - .flatMap(author -> threadChannel.sendMessage(author.getAsMention()).addEmbeds(embed)) + .mapToResult() + .flatMap(member -> { + if (member.isSuccess()) { + return sendEmbedWithMention.apply(member); + } + logger.debug("Member already left server, archiving thread without mention"); + return sendEmbedWithoutMention.apply(member); + }) .flatMap(any -> threadChannel.getManager().setArchived(true)) .queue(any -> { }, handleFailure); From 6e753a4d3ba0c0c08744b883ca2534821b43da0d Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Mon, 11 Dec 2023 23:41:22 +0530 Subject: [PATCH 02/19] taking carre of debugging leftovers --- .../tjbot/features/help/HelpThreadAutoArchiver.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java index 236be9ce53..9d4c263781 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java @@ -32,8 +32,8 @@ */ public final class HelpThreadAutoArchiver implements Routine { private static final Logger logger = LoggerFactory.getLogger(HelpThreadAutoArchiver.class); - private static final int SCHEDULE_MINUTES = 6; - private static final Duration ARCHIVE_AFTER_INACTIVITY_OF = Duration.ofSeconds(12); + private static final int SCHEDULE_MINUTES = 60; + private static final Duration ARCHIVE_AFTER_INACTIVITY_OF = Duration.ofHours(12); private final HelpSystemHelper helper; @@ -48,7 +48,7 @@ public HelpThreadAutoArchiver(HelpSystemHelper helper) { @Override public Schedule createSchedule() { - return new Schedule(ScheduleMode.FIXED_RATE, 0, SCHEDULE_MINUTES, TimeUnit.SECONDS); + return new Schedule(ScheduleMode.FIXED_RATE, 0, SCHEDULE_MINUTES, TimeUnit.MINUTES); } @Override From f82821ad14f8e4714e6adf891d88405c1de645c6 Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Tue, 12 Dec 2023 00:42:05 +0530 Subject: [PATCH 03/19] sonar fix --- .../tjbot/features/help/HelpThreadAutoArchiver.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java index 9d4c263781..7525eadf6e 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java @@ -136,10 +136,10 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) }; Function, RestAction> sendEmbedWithMention = - (member) -> threadChannel.sendMessage(member.get().getAsMention()).addEmbeds(embed); + member -> threadChannel.sendMessage(member.get().getAsMention()).addEmbeds(embed); Function, RestAction> sendEmbedWithoutMention = - (member) -> threadChannel.sendMessageEmbeds(embed); + member -> threadChannel.sendMessageEmbeds(embed); threadChannel.getGuild() .retrieveMemberById(threadChannel.getOwnerIdLong()) From eb166a8249502fdabfa85ac8247f4e4ec29c2978 Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Tue, 12 Dec 2023 13:30:29 +0530 Subject: [PATCH 04/19] refactor loggers, archiving thread incase of failure --- .../tjbot/features/help/HelpThreadAutoArchiver.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java index 7525eadf6e..76098432c4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java @@ -130,8 +130,11 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) Consumer handleFailure = error -> { if (error instanceof ErrorResponseException) { - logger.warn("Unknown error occurred during help thread auto archive routine", + logger.warn( + "Unknown error occurred during help thread auto archive routine, archiving thread", error); + + threadChannel.getManager().setArchived(true).queue(); } }; @@ -148,7 +151,8 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) if (member.isSuccess()) { return sendEmbedWithMention.apply(member); } - logger.debug("Member already left server, archiving thread without mention"); + + logger.debug("Unable to mention user", member.getFailure()); return sendEmbedWithoutMention.apply(member); }) .flatMap(any -> threadChannel.getManager().setArchived(true)) From 3541c1301e2604c0ce661a7fb54bef31e6ddb949 Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Wed, 13 Dec 2023 21:13:38 +0530 Subject: [PATCH 05/19] fallback archiving in same queue --- .../tjbot/features/help/HelpThreadAutoArchiver.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java index 76098432c4..739db271e0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java @@ -133,8 +133,6 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) logger.warn( "Unknown error occurred during help thread auto archive routine, archiving thread", error); - - threadChannel.getManager().setArchived(true).queue(); } }; @@ -156,7 +154,10 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) return sendEmbedWithoutMention.apply(member); }) .flatMap(any -> threadChannel.getManager().setArchived(true)) - .queue(any -> { - }, handleFailure); + .onErrorFlatMap(error -> { + handleFailure.accept(error); + return threadChannel.getManager().setArchived(true); + }) + .queue(); } } From 2ec755e19d0b9062115c440de9f671f577652932 Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Wed, 13 Dec 2023 21:35:35 +0530 Subject: [PATCH 06/19] archive logic in supplier --- .../tjbot/features/help/HelpThreadAutoArchiver.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java index 739db271e0..b55fac7442 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java @@ -25,6 +25,7 @@ import java.util.concurrent.TimeUnit; import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.Supplier; /** * Routine, which periodically checks all help threads and archives them if there has not been any @@ -136,6 +137,9 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) } }; + Supplier> archiveThread = + () -> threadChannel.getManager().setArchived(true); + Function, RestAction> sendEmbedWithMention = member -> threadChannel.sendMessage(member.get().getAsMention()).addEmbeds(embed); @@ -153,10 +157,10 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) logger.debug("Unable to mention user", member.getFailure()); return sendEmbedWithoutMention.apply(member); }) - .flatMap(any -> threadChannel.getManager().setArchived(true)) + .flatMap(any -> archiveThread.get()) .onErrorFlatMap(error -> { handleFailure.accept(error); - return threadChannel.getManager().setArchived(true); + return archiveThread.get(); }) .queue(); } From 77745c456eaf954706205659f82cd5396e741831 Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Wed, 13 Dec 2023 21:43:43 +0530 Subject: [PATCH 07/19] consumer only logs, so better name --- .../tjbot/features/help/HelpThreadAutoArchiver.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java index b55fac7442..ebc9d4f083 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java @@ -129,7 +129,7 @@ private static boolean shouldBeArchived(MessageChannel channel, Instant archiveA private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) { - Consumer handleFailure = error -> { + Consumer logFailure = error -> { if (error instanceof ErrorResponseException) { logger.warn( "Unknown error occurred during help thread auto archive routine, archiving thread", @@ -159,7 +159,7 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) }) .flatMap(any -> archiveThread.get()) .onErrorFlatMap(error -> { - handleFailure.accept(error); + logFailure.accept(error); return archiveThread.get(); }) .queue(); From 05ca0bab9b5e82af9d4f12941acf9c498355d972 Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Thu, 14 Dec 2023 00:07:57 +0530 Subject: [PATCH 08/19] refactor error handling in rest chain --- .../features/help/HelpThreadAutoArchiver.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java index ebc9d4f083..bafe71e27c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java @@ -23,7 +23,6 @@ import java.util.List; import java.util.Optional; import java.util.concurrent.TimeUnit; -import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; @@ -129,17 +128,18 @@ private static boolean shouldBeArchived(MessageChannel channel, Instant archiveA private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) { - Consumer logFailure = error -> { + Supplier> archiveThread = + () -> threadChannel.getManager().setArchived(true); + + Function> handleFailure = (error) -> { if (error instanceof ErrorResponseException) { logger.warn( "Unknown error occurred during help thread auto archive routine, archiving thread", error); } + return archiveThread.get(); }; - Supplier> archiveThread = - () -> threadChannel.getManager().setArchived(true); - Function, RestAction> sendEmbedWithMention = member -> threadChannel.sendMessage(member.get().getAsMention()).addEmbeds(embed); @@ -157,9 +157,11 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) logger.debug("Unable to mention user", member.getFailure()); return sendEmbedWithoutMention.apply(member); }) - .flatMap(any -> archiveThread.get()) - .onErrorFlatMap(error -> { - logFailure.accept(error); + .mapToResult() + .flatMap(sentEmbed -> { + if (sentEmbed.isFailure()) { + return handleFailure.apply(sentEmbed.getFailure()); + } return archiveThread.get(); }) .queue(); From d007a68380b50cd0072bd773d9e9553ba693c824 Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Thu, 14 Dec 2023 00:09:59 +0530 Subject: [PATCH 09/19] refactor if check for clarity --- .../tjbot/features/help/HelpThreadAutoArchiver.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java index bafe71e27c..2e7dad6206 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java @@ -159,10 +159,10 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) }) .mapToResult() .flatMap(sentEmbed -> { - if (sentEmbed.isFailure()) { - return handleFailure.apply(sentEmbed.getFailure()); + if (sentEmbed.isSuccess()) { + return archiveThread.get(); } - return archiveThread.get(); + return handleFailure.apply(sentEmbed.getFailure()); }) .queue(); } From 4ac70969c81bdacb557450a287a50f1fbd5c4f59 Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Thu, 14 Dec 2023 00:11:21 +0530 Subject: [PATCH 10/19] extra line for readabilty --- .../togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java | 1 + 1 file changed, 1 insertion(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java index 2e7dad6206..bd2371a429 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java @@ -162,6 +162,7 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) if (sentEmbed.isSuccess()) { return archiveThread.get(); } + return handleFailure.apply(sentEmbed.getFailure()); }) .queue(); From ddee8d5a6386ea463ac8270c97429ee78c9491bd Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Thu, 14 Dec 2023 00:15:53 +0530 Subject: [PATCH 11/19] sonar fix --- .../tjbot/features/help/HelpThreadAutoArchiver.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java index bd2371a429..55b7af2cc2 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java @@ -131,7 +131,7 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) Supplier> archiveThread = () -> threadChannel.getManager().setArchived(true); - Function> handleFailure = (error) -> { + Function> handleFailure = error -> { if (error instanceof ErrorResponseException) { logger.warn( "Unknown error occurred during help thread auto archive routine, archiving thread", From fcc670b2c411ae3a70dc6670a649471157bc09a3 Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Fri, 15 Dec 2023 20:15:17 +0530 Subject: [PATCH 12/19] handling error in pipeline --- .../features/help/HelpThreadAutoArchiver.java | 28 ++++++------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java index 55b7af2cc2..5859224aa8 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java @@ -24,7 +24,6 @@ import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.function.Function; -import java.util.function.Supplier; /** * Routine, which periodically checks all help threads and archives them if there has not been any @@ -32,8 +31,8 @@ */ public final class HelpThreadAutoArchiver implements Routine { private static final Logger logger = LoggerFactory.getLogger(HelpThreadAutoArchiver.class); - private static final int SCHEDULE_MINUTES = 60; - private static final Duration ARCHIVE_AFTER_INACTIVITY_OF = Duration.ofHours(12); + private static final int SCHEDULE_MINUTES = 6; + private static final Duration ARCHIVE_AFTER_INACTIVITY_OF = Duration.ofSeconds(60); private final HelpSystemHelper helper; @@ -48,7 +47,7 @@ public HelpThreadAutoArchiver(HelpSystemHelper helper) { @Override public Schedule createSchedule() { - return new Schedule(ScheduleMode.FIXED_RATE, 0, SCHEDULE_MINUTES, TimeUnit.MINUTES); + return new Schedule(ScheduleMode.FIXED_RATE, 0, SCHEDULE_MINUTES, TimeUnit.SECONDS); } @Override @@ -128,18 +127,6 @@ private static boolean shouldBeArchived(MessageChannel channel, Instant archiveA private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) { - Supplier> archiveThread = - () -> threadChannel.getManager().setArchived(true); - - Function> handleFailure = error -> { - if (error instanceof ErrorResponseException) { - logger.warn( - "Unknown error occurred during help thread auto archive routine, archiving thread", - error); - } - return archiveThread.get(); - }; - Function, RestAction> sendEmbedWithMention = member -> threadChannel.sendMessage(member.get().getAsMention()).addEmbeds(embed); @@ -159,11 +146,12 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) }) .mapToResult() .flatMap(sentEmbed -> { - if (sentEmbed.isSuccess()) { - return archiveThread.get(); + if (sentEmbed.getFailure() instanceof ErrorResponseException) { + logger.warn( + "Unknown error occurred during help thread auto archive routine, archiving thread", + sentEmbed.getFailure()); } - - return handleFailure.apply(sentEmbed.getFailure()); + return threadChannel.getManager().setArchived(true); }) .queue(); } From 763845cf441be9f051506c6b1d361ba291c1ed95 Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Fri, 15 Dec 2023 20:18:03 +0530 Subject: [PATCH 13/19] cleaning up debugging leftovers --- .../tjbot/features/help/HelpThreadAutoArchiver.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java index 5859224aa8..f84cc7e3ec 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java @@ -31,8 +31,8 @@ */ public final class HelpThreadAutoArchiver implements Routine { private static final Logger logger = LoggerFactory.getLogger(HelpThreadAutoArchiver.class); - private static final int SCHEDULE_MINUTES = 6; - private static final Duration ARCHIVE_AFTER_INACTIVITY_OF = Duration.ofSeconds(60); + private static final int SCHEDULE_MINUTES = 60; + private static final Duration ARCHIVE_AFTER_INACTIVITY_OF = Duration.ofHours(12); private final HelpSystemHelper helper; @@ -47,7 +47,7 @@ public HelpThreadAutoArchiver(HelpSystemHelper helper) { @Override public Schedule createSchedule() { - return new Schedule(ScheduleMode.FIXED_RATE, 0, SCHEDULE_MINUTES, TimeUnit.SECONDS); + return new Schedule(ScheduleMode.FIXED_RATE, 0, SCHEDULE_MINUTES, TimeUnit.MINUTES); } @Override From 1c81e5c18fa9173204fed3e1ed758c54b1680370 Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Fri, 15 Dec 2023 23:22:52 +0530 Subject: [PATCH 14/19] renaming from logger -> LOGGER --- .../features/help/HelpThreadAutoArchiver.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java index f84cc7e3ec..bbaec0bf7b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java @@ -30,7 +30,7 @@ * recent activity. */ public final class HelpThreadAutoArchiver implements Routine { - private static final Logger logger = LoggerFactory.getLogger(HelpThreadAutoArchiver.class); + private static final Logger LOGGER = LoggerFactory.getLogger(HelpThreadAutoArchiver.class); private static final int SCHEDULE_MINUTES = 60; private static final Duration ARCHIVE_AFTER_INACTIVITY_OF = Duration.ofHours(12); @@ -57,7 +57,7 @@ public void runRoutine(JDA jda) { private void autoArchiveForGuild(Guild guild) { Optional maybeHelpForum = helper - .handleRequireHelpForum(guild, channelPattern -> logger.warn( + .handleRequireHelpForum(guild, channelPattern -> LOGGER.warn( "Unable to auto archive help threads, did not find a help forum matching the configured pattern '{}' for guild '{}'", channelPattern, guild.getName())); @@ -65,10 +65,10 @@ private void autoArchiveForGuild(Guild guild) { return; } - logger.debug("Auto archiving of help threads"); + LOGGER.debug("Auto archiving of help threads"); List activeThreads = helper.getActiveThreadsIn(maybeHelpForum.orElseThrow()); - logger.debug("Found {} active questions", activeThreads.size()); + LOGGER.debug("Found {} active questions", activeThreads.size()); Instant archiveAfterMoment = computeArchiveAfterMoment(); activeThreads @@ -81,7 +81,7 @@ private Instant computeArchiveAfterMoment() { private void autoArchiveForThread(ThreadChannel threadChannel, Instant archiveAfterMoment) { if (shouldBeArchived(threadChannel, archiveAfterMoment)) { - logger.debug("Auto archiving help thread {}", threadChannel.getId()); + LOGGER.debug("Auto archiving help thread {}", threadChannel.getId()); String linkHowToAsk = "https://stackoverflow.com/help/how-to-ask"; @@ -141,13 +141,13 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) return sendEmbedWithMention.apply(member); } - logger.debug("Unable to mention user", member.getFailure()); + LOGGER.debug("Unable to mention user", member.getFailure()); return sendEmbedWithoutMention.apply(member); }) .mapToResult() .flatMap(sentEmbed -> { if (sentEmbed.getFailure() instanceof ErrorResponseException) { - logger.warn( + LOGGER.warn( "Unknown error occurred during help thread auto archive routine, archiving thread", sentEmbed.getFailure()); } From fcff4eb5a15e137f5d11b41caa73453105fe3580 Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Fri, 15 Dec 2023 23:37:28 +0530 Subject: [PATCH 15/19] log level changed to info with better message --- .../tjbot/features/help/HelpThreadAutoArchiver.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java index bbaec0bf7b..1d3e4d4075 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java @@ -140,8 +140,10 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) if (member.isSuccess()) { return sendEmbedWithMention.apply(member); } + LOGGER.info( + "Owner of thread with id: {} left the server, sending embed without mention", + threadChannel.getId(), member.getFailure()); - LOGGER.debug("Unable to mention user", member.getFailure()); return sendEmbedWithoutMention.apply(member); }) .mapToResult() From ec44733cca2dbd2a9569891253ef8318d3d962e2 Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Fri, 15 Dec 2023 23:42:25 +0530 Subject: [PATCH 16/19] logging all the errors, instead of just ErrorResponseException type --- .../tjbot/features/help/HelpThreadAutoArchiver.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java index 1d3e4d4075..0a84c7b0d4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java @@ -9,7 +9,6 @@ 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.requests.RestAction; import net.dv8tion.jda.api.utils.Result; import net.dv8tion.jda.api.utils.TimeUtil; @@ -148,7 +147,7 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) }) .mapToResult() .flatMap(sentEmbed -> { - if (sentEmbed.getFailure() instanceof ErrorResponseException) { + if (sentEmbed.isFailure()) { LOGGER.warn( "Unknown error occurred during help thread auto archive routine, archiving thread", sentEmbed.getFailure()); From c91e82ee1840ff5419496d193de2569205196e37 Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Fri, 15 Dec 2023 23:47:31 +0530 Subject: [PATCH 17/19] refactoring * replacing Function type with Supplier for sendEmbedWithoutMention * changing variable for more clarity on fetched members --- .../features/help/HelpThreadAutoArchiver.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java index 0a84c7b0d4..f3132edf4a 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java @@ -23,6 +23,7 @@ import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.function.Function; +import java.util.function.Supplier; /** * Routine, which periodically checks all help threads and archives them if there has not been any @@ -129,21 +130,21 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) Function, RestAction> sendEmbedWithMention = member -> threadChannel.sendMessage(member.get().getAsMention()).addEmbeds(embed); - Function, RestAction> sendEmbedWithoutMention = - member -> threadChannel.sendMessageEmbeds(embed); + Supplier> sendEmbedWithoutMention = + () -> threadChannel.sendMessageEmbeds(embed); threadChannel.getGuild() .retrieveMemberById(threadChannel.getOwnerIdLong()) .mapToResult() - .flatMap(member -> { - if (member.isSuccess()) { - return sendEmbedWithMention.apply(member); + .flatMap(foundMember -> { + if (foundMember.isSuccess()) { + return sendEmbedWithMention.apply(foundMember); } LOGGER.info( "Owner of thread with id: {} left the server, sending embed without mention", - threadChannel.getId(), member.getFailure()); + threadChannel.getId(), foundMember.getFailure()); - return sendEmbedWithoutMention.apply(member); + return sendEmbedWithoutMention.get(); }) .mapToResult() .flatMap(sentEmbed -> { From bd3cb18c751f283940e68b7eac41be33698e602a Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Sat, 16 Dec 2023 20:53:09 +0530 Subject: [PATCH 18/19] reverting back LOGGER->logger based on project style --- .../features/help/HelpThreadAutoArchiver.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java index f3132edf4a..c2b52d7d3c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java @@ -30,7 +30,7 @@ * recent activity. */ public final class HelpThreadAutoArchiver implements Routine { - private static final Logger LOGGER = LoggerFactory.getLogger(HelpThreadAutoArchiver.class); + private static final Logger logger = LoggerFactory.getLogger(HelpThreadAutoArchiver.class); private static final int SCHEDULE_MINUTES = 60; private static final Duration ARCHIVE_AFTER_INACTIVITY_OF = Duration.ofHours(12); @@ -57,7 +57,7 @@ public void runRoutine(JDA jda) { private void autoArchiveForGuild(Guild guild) { Optional maybeHelpForum = helper - .handleRequireHelpForum(guild, channelPattern -> LOGGER.warn( + .handleRequireHelpForum(guild, channelPattern -> logger.warn( "Unable to auto archive help threads, did not find a help forum matching the configured pattern '{}' for guild '{}'", channelPattern, guild.getName())); @@ -65,10 +65,10 @@ private void autoArchiveForGuild(Guild guild) { return; } - LOGGER.debug("Auto archiving of help threads"); + logger.debug("Auto archiving of help threads"); List activeThreads = helper.getActiveThreadsIn(maybeHelpForum.orElseThrow()); - LOGGER.debug("Found {} active questions", activeThreads.size()); + logger.debug("Found {} active questions", activeThreads.size()); Instant archiveAfterMoment = computeArchiveAfterMoment(); activeThreads @@ -81,7 +81,7 @@ private Instant computeArchiveAfterMoment() { private void autoArchiveForThread(ThreadChannel threadChannel, Instant archiveAfterMoment) { if (shouldBeArchived(threadChannel, archiveAfterMoment)) { - LOGGER.debug("Auto archiving help thread {}", threadChannel.getId()); + logger.debug("Auto archiving help thread {}", threadChannel.getId()); String linkHowToAsk = "https://stackoverflow.com/help/how-to-ask"; @@ -140,7 +140,7 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) if (foundMember.isSuccess()) { return sendEmbedWithMention.apply(foundMember); } - LOGGER.info( + logger.info( "Owner of thread with id: {} left the server, sending embed without mention", threadChannel.getId(), foundMember.getFailure()); @@ -149,7 +149,7 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) .mapToResult() .flatMap(sentEmbed -> { if (sentEmbed.isFailure()) { - LOGGER.warn( + logger.warn( "Unknown error occurred during help thread auto archive routine, archiving thread", sentEmbed.getFailure()); } From cdcc7a7d191e010a41ce78a01a58ec8de79f2f2c Mon Sep 17 00:00:00 2001 From: alphaBEE <61616007+ankitsmt211@users.noreply.github.com> Date: Sun, 17 Dec 2023 11:58:27 +0530 Subject: [PATCH 19/19] removing unnecessary error handling --- .../tjbot/features/help/HelpThreadAutoArchiver.java | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java index c2b52d7d3c..e44b5e9ae4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java @@ -146,15 +146,7 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) return sendEmbedWithoutMention.get(); }) - .mapToResult() - .flatMap(sentEmbed -> { - if (sentEmbed.isFailure()) { - logger.warn( - "Unknown error occurred during help thread auto archive routine, archiving thread", - sentEmbed.getFailure()); - } - return threadChannel.getManager().setArchived(true); - }) + .flatMap(any -> threadChannel.getManager().setArchived(true)) .queue(); } }