diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadLifecycleListener.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadLifecycleListener.java index 3c2e778ba1..3669ed2517 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadLifecycleListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadLifecycleListener.java @@ -1,5 +1,6 @@ package org.togetherjava.tjbot.features.help; +import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; import net.dv8tion.jda.api.entities.channel.forums.ForumTag; import net.dv8tion.jda.api.events.channel.update.ChannelUpdateAppliedTagsEvent; @@ -75,14 +76,25 @@ private void handleThreadStatus(ThreadChannel threadChannel) { boolean isArchived = threadChannel.isArchived(); if (isArchived) { - handleArchiveStatus(closedAt, threadChannel); + handleArchiveStatus(closedAt, threadId, threadChannel.getJDA()); return; } updateThreadStatusToActive(threadId); } - void handleArchiveStatus(Instant closedAt, ThreadChannel threadChannel) { + void handleArchiveStatus(Instant closedAt, long id, JDA jda) { + ThreadChannel threadChannel = jda.getThreadChannelById(id); + if (threadChannel == null) { + logger.info("thread with id: {} no longer exists, marking archived in records", id); + database.write(context -> context.update(HELP_THREADS) + .set(HELP_THREADS.CLOSED_AT, closedAt) + .set(HELP_THREADS.TICKET_STATUS, HelpSystemHelper.TicketStatus.ARCHIVED.val) + .where(HELP_THREADS.CHANNEL_ID.eq(id)) + .execute()); + return; + } + long threadId = threadChannel.getIdLong(); int messageCount = threadChannel.getMessageCount(); int participantsExceptAuthor = threadChannel.getMemberCount() - 1; @@ -96,6 +108,7 @@ void handleArchiveStatus(Instant closedAt, ThreadChannel threadChannel) { .execute()); logger.info("Thread with id: {}, updated to archived status in database", threadId); + } private void updateThreadStatusToActive(long threadId) { diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java b/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java index ec96d77c72..65cca49c6b 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/MarkHelpThreadCloseInDBRoutine.java @@ -1,7 +1,6 @@ package org.togetherjava.tjbot.features.help; import net.dv8tion.jda.api.JDA; -import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,13 +58,12 @@ private void updateTicketStatus(JDA jda) { .map(HelpThreadsRecord::getChannelId) .toList()); - threadIdsToClose.forEach(id -> { try { - ThreadChannel threadChannel = jda.getThreadChannelById(id); - helpThreadLifecycleListener.handleArchiveStatus(now, threadChannel); + helpThreadLifecycleListener.handleArchiveStatus(now, id, jda); } catch (Exception exception) { - logger.warn("unable to mark thread as close with id :{}", id, exception); + logger.warn("Failed to update status of thread with id: {} to archived", id, + exception); } }); }