33import net .dv8tion .jda .api .EmbedBuilder ;
44import net .dv8tion .jda .api .JDA ;
55import net .dv8tion .jda .api .entities .Guild ;
6- import net .dv8tion .jda .api .entities .Member ;
76import net .dv8tion .jda .api .entities .MessageEmbed ;
87import net .dv8tion .jda .api .entities .channel .concrete .ForumChannel ;
98import net .dv8tion .jda .api .entities .channel .concrete .ThreadChannel ;
109import net .dv8tion .jda .api .entities .channel .middleman .MessageChannel ;
10+ import net .dv8tion .jda .api .exceptions .ErrorResponseException ;
1111import net .dv8tion .jda .api .utils .TimeUtil ;
1212import org .slf4j .Logger ;
1313import org .slf4j .LoggerFactory ;
1919import java .util .List ;
2020import java .util .Optional ;
2121import java .util .concurrent .TimeUnit ;
22+ import java .util .function .Consumer ;
2223
2324/**
2425 * Routine, which periodically checks all help threads and archives them if there has not been any
@@ -66,16 +67,15 @@ private void autoArchiveForGuild(Guild guild) {
6667 logger .debug ("Found {} active questions" , activeThreads .size ());
6768
6869 Instant archiveAfterMoment = computeArchiveAfterMoment ();
69- activeThreads . forEach ( activeThread -> autoArchiveForThread ( activeThread , archiveAfterMoment ,
70- activeThread . getOwner () ));
70+ activeThreads
71+ . forEach ( activeThread -> autoArchiveForThread ( activeThread , archiveAfterMoment ));
7172 }
7273
7374 private Instant computeArchiveAfterMoment () {
7475 return Instant .now ().minus (ARCHIVE_AFTER_INACTIVITY_OF );
7576 }
7677
77- private void autoArchiveForThread (ThreadChannel threadChannel , Instant archiveAfterMoment ,
78- Member author ) {
78+ private void autoArchiveForThread (ThreadChannel threadChannel , Instant archiveAfterMoment ) {
7979 if (shouldBeArchived (threadChannel , archiveAfterMoment )) {
8080 logger .debug ("Auto archiving help thread {}" , threadChannel .getId ());
8181
@@ -110,10 +110,7 @@ private void autoArchiveForThread(ThreadChannel threadChannel, Instant archiveAf
110110 .setColor (HelpSystemHelper .AMBIENT_COLOR )
111111 .build ();
112112
113- threadChannel .sendMessage (author .getAsMention ())
114- .addEmbeds (embed )
115- .flatMap (any -> threadChannel .getManager ().setArchived (true ))
116- .queue ();
113+ handleArchiveFlow (threadChannel , embed );
117114 }
118115 }
119116
@@ -123,4 +120,20 @@ private static boolean shouldBeArchived(MessageChannel channel, Instant archiveA
123120
124121 return lastActivity .isBefore (archiveAfterMoment );
125122 }
123+
124+ private void handleArchiveFlow (ThreadChannel threadChannel , MessageEmbed embed ) {
125+ Consumer <Throwable > handleFailure = error -> {
126+ if (error instanceof ErrorResponseException ) {
127+ logger .warn ("Unknown error occurred during help thread auto archive routine" ,
128+ error );
129+ }
130+ };
131+
132+ threadChannel .getGuild ()
133+ .retrieveMemberById (threadChannel .getOwnerIdLong ())
134+ .flatMap (author -> threadChannel .sendMessage (author .getAsMention ()).addEmbeds (embed ))
135+ .flatMap (any -> threadChannel .getManager ().setArchived (true ))
136+ .queue (any -> {
137+ }, handleFailure );
138+ }
126139}
0 commit comments