2020 */
2121package com .mcmoddev .mmdbot .watcher .event ;
2222
23+ import com .mcmoddev .mmdbot .core .util .TaskScheduler ;
24+ import com .mcmoddev .mmdbot .watcher .TheWatcher ;
2325import net .dv8tion .jda .api .JDA ;
26+ import net .dv8tion .jda .api .entities .Message ;
2427import net .dv8tion .jda .api .entities .channel .concrete .ThreadChannel ;
2528import net .dv8tion .jda .api .entities .channel .forums .ForumTag ;
2629import net .dv8tion .jda .api .entities .emoji .CustomEmoji ;
3033import net .dv8tion .jda .api .hooks .ListenerAdapter ;
3134import org .jetbrains .annotations .NotNull ;
3235
36+ import java .time .Instant ;
37+ import java .time .temporal .ChronoUnit ;
3338import java .util .List ;
3439import java .util .Objects ;
40+ import java .util .concurrent .TimeUnit ;
41+ import java .util .function .Consumer ;
3542
3643public class ForumListener extends ListenerAdapter {
3744 @ Override
@@ -55,6 +62,31 @@ public void onChannelUpdateAppliedTags(@NotNull final ChannelUpdateAppliedTagsEv
5562 thread .sendMessage (message ).queue ();
5663 }
5764
65+ public static void onCollectTasks (TaskScheduler .CollectTasksEvent event ) {
66+ event .addTask (() -> {
67+ if (TheWatcher .getInstance () == null ) return ;
68+ final Instant _3DaysAgo = Instant .now ().minus (3 , ChronoUnit .DAYS );
69+ TheWatcher .getInstance ().getJda ().getForumChannelCache ()
70+ .stream ().flatMap (it -> it .getThreadChannels ().stream ()).forEach (channel -> {
71+ final Consumer <Message > lastMessage = (Message last ) -> {
72+ if (last .getTimeCreated ().toInstant ().isBefore (_3DaysAgo )) {
73+ channel .getManager ().setArchived (true ).reason ("3 days without activity" ).queue ();
74+ }
75+ };
76+ final var his = channel .getHistory ().getRetrievedHistory ();
77+ if (his .isEmpty ()) {
78+ channel .getHistory ().retrievePast (1 ).queue (it -> {
79+ if (!it .isEmpty ()) {
80+ lastMessage .accept (it .get (0 ));
81+ }
82+ });
83+ } else {
84+ lastMessage .accept (his .get (his .size () - 1 ));
85+ }
86+ });
87+ }, 1 , 24 , TimeUnit .HOURS );
88+ }
89+
5890 private String formatTags (List <ForumTag > tags , JDA jda ) {
5991 return String .join (", " , tags .stream ()
6092 .map (it -> (it .getEmoji () == null ? "" : format (it .getEmoji (), jda ) + " " ) + "`" + it .getName () + "`" )
0 commit comments