3
3
import net .dv8tion .jda .api .EmbedBuilder ;
4
4
import net .dv8tion .jda .api .JDA ;
5
5
import net .dv8tion .jda .api .entities .Guild ;
6
- import net .dv8tion .jda .api .entities .Member ;
7
6
import net .dv8tion .jda .api .entities .MessageEmbed ;
8
7
import net .dv8tion .jda .api .entities .channel .concrete .ForumChannel ;
9
8
import net .dv8tion .jda .api .entities .channel .concrete .ThreadChannel ;
10
9
import net .dv8tion .jda .api .entities .channel .middleman .MessageChannel ;
10
+ import net .dv8tion .jda .api .exceptions .ErrorResponseException ;
11
11
import net .dv8tion .jda .api .utils .TimeUtil ;
12
12
import org .slf4j .Logger ;
13
13
import org .slf4j .LoggerFactory ;
19
19
import java .util .List ;
20
20
import java .util .Optional ;
21
21
import java .util .concurrent .TimeUnit ;
22
+ import java .util .function .Consumer ;
22
23
23
24
/**
24
25
* 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) {
66
67
logger .debug ("Found {} active questions" , activeThreads .size ());
67
68
68
69
Instant archiveAfterMoment = computeArchiveAfterMoment ();
69
- activeThreads . forEach ( activeThread -> autoArchiveForThread ( activeThread , archiveAfterMoment ,
70
- activeThread . getOwner () ));
70
+ activeThreads
71
+ . forEach ( activeThread -> autoArchiveForThread ( activeThread , archiveAfterMoment ));
71
72
}
72
73
73
74
private Instant computeArchiveAfterMoment () {
74
75
return Instant .now ().minus (ARCHIVE_AFTER_INACTIVITY_OF );
75
76
}
76
77
77
- private void autoArchiveForThread (ThreadChannel threadChannel , Instant archiveAfterMoment ,
78
- Member author ) {
78
+ private void autoArchiveForThread (ThreadChannel threadChannel , Instant archiveAfterMoment ) {
79
79
if (shouldBeArchived (threadChannel , archiveAfterMoment )) {
80
80
logger .debug ("Auto archiving help thread {}" , threadChannel .getId ());
81
81
@@ -110,10 +110,7 @@ private void autoArchiveForThread(ThreadChannel threadChannel, Instant archiveAf
110
110
.setColor (HelpSystemHelper .AMBIENT_COLOR )
111
111
.build ();
112
112
113
- threadChannel .sendMessage (author .getAsMention ())
114
- .addEmbeds (embed )
115
- .flatMap (any -> threadChannel .getManager ().setArchived (true ))
116
- .queue ();
113
+ handleArchiveFlow (threadChannel , embed );
117
114
}
118
115
}
119
116
@@ -123,4 +120,20 @@ private static boolean shouldBeArchived(MessageChannel channel, Instant archiveA
123
120
124
121
return lastActivity .isBefore (archiveAfterMoment );
125
122
}
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
+ }
126
139
}
0 commit comments