55import net .dv8tion .jda .api .entities .*;
66import net .dv8tion .jda .api .events .message .MessageReceivedEvent ;
77import net .dv8tion .jda .api .requests .RestAction ;
8+ import net .dv8tion .jda .internal .requests .CompletedRestAction ;
89import org .jetbrains .annotations .NotNull ;
10+ import org .jetbrains .annotations .Nullable ;
911import org .slf4j .Logger ;
1012import org .slf4j .LoggerFactory ;
1113import org .togetherjava .tjbot .commands .MessageReceiverAdapter ;
1618import java .util .concurrent .Executors ;
1719import java .util .concurrent .ScheduledExecutorService ;
1820import java .util .concurrent .TimeUnit ;
21+ import java .util .concurrent .atomic .AtomicInteger ;
1922import java .util .function .Predicate ;
2023import java .util .regex .Pattern ;
2124import java .util .stream .Collectors ;
@@ -33,6 +36,9 @@ public final class HelpThreadOverviewUpdater extends MessageReceiverAdapter impl
3336
3437 private static final String STATUS_TITLE = "## __**Active questions**__ ##" ;
3538 private static final int OVERVIEW_QUESTION_LIMIT = 150 ;
39+ private static final AtomicInteger FIND_STATUS_MESSAGE_CONSECUTIVE_FAILURES =
40+ new AtomicInteger (0 );
41+ private static final int FIND_STATUS_MESSAGE_FAILURE_THRESHOLD = 3 ;
3642
3743 private final HelpSystemHelper helper ;
3844 private final List <String > allCategories ;
@@ -124,15 +130,10 @@ private void updateOverview(@NotNull TextChannel overviewChannel) {
124130 .setContent (STATUS_TITLE + "\n \n " + createDescription (activeThreads ))
125131 .build ();
126132
127- getStatusMessage (overviewChannel ).flatMap (maybeStatusMessage -> {
128- logger .debug ("Sending the updated question overview" );
129- if (maybeStatusMessage .isEmpty ()) {
130- return overviewChannel .sendMessage (message );
131- }
132-
133- String statusMessageId = maybeStatusMessage .orElseThrow ().getId ();
134- return overviewChannel .editMessageById (statusMessageId , message );
135- }).queue ();
133+ getStatusMessage (overviewChannel )
134+ .flatMap (maybeStatusMessage -> sendUpdatedOverview (maybeStatusMessage .orElse (null ),
135+ message , overviewChannel ))
136+ .queue ();
136137 }
137138
138139 private @ NotNull String createDescription (@ NotNull Collection <ThreadChannel > activeThreads ) {
@@ -178,6 +179,30 @@ private static boolean isStatusMessage(@NotNull Message message) {
178179 return content .startsWith (STATUS_TITLE );
179180 }
180181
182+ private @ NotNull RestAction <Message > sendUpdatedOverview (@ Nullable Message statusMessage ,
183+ @ NotNull Message updatedStatusMessage , @ NotNull MessageChannel overviewChannel ) {
184+ logger .debug ("Sending the updated question overview" );
185+ if (statusMessage == null ) {
186+ int currentFailures = FIND_STATUS_MESSAGE_CONSECUTIVE_FAILURES .incrementAndGet ();
187+ if (currentFailures >= FIND_STATUS_MESSAGE_FAILURE_THRESHOLD ) {
188+ logger .warn (
189+ "Failed to locate the question overview too often ({} times), sending a fresh message instead." ,
190+ currentFailures );
191+ FIND_STATUS_MESSAGE_CONSECUTIVE_FAILURES .set (0 );
192+ return overviewChannel .sendMessage (updatedStatusMessage );
193+ }
194+
195+ logger .info (
196+ "Failed to locate the question overview ({} times), trying again next time." ,
197+ currentFailures );
198+ return new CompletedRestAction <>(overviewChannel .getJDA (), null , null );
199+ }
200+
201+ FIND_STATUS_MESSAGE_CONSECUTIVE_FAILURES .set (0 );
202+ String statusMessageId = statusMessage .getId ();
203+ return overviewChannel .editMessageById (statusMessageId , updatedStatusMessage );
204+ }
205+
181206 private enum ChannelType {
182207 OVERVIEW ,
183208 STAGING
0 commit comments