Skip to content

Commit fec4420

Browse files
committed
Archive threads of forums with no activity in 3 days
1 parent 5fbc030 commit fec4420

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/watcher/java/com/mcmoddev/mmdbot/watcher/TheWatcher.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.mcmoddev.mmdbot.core.util.config.ConfigurateUtils;
3737
import com.mcmoddev.mmdbot.core.util.config.SnowflakeValue;
3838
import com.mcmoddev.mmdbot.core.util.event.DismissListener;
39+
import com.mcmoddev.mmdbot.core.util.event.OneTimeEventListener;
3940
import com.mcmoddev.mmdbot.core.util.event.ThreadedEventListener;
4041
import com.mcmoddev.mmdbot.watcher.commands.information.InviteCommand;
4142
import com.mcmoddev.mmdbot.watcher.commands.moderation.BanCommand;
@@ -145,6 +146,8 @@ public Logger getLogger() {
145146
Executors.newFixedThreadPool(2, r -> com.mcmoddev.mmdbot.core.util.Utils.setThreadDaemon(new Thread(TheWatcher.THREAD_GROUP, r, "PunishableActions"), true))
146147
);
147148

149+
public static final OneTimeEventListener<TaskScheduler.CollectTasksEvent> ARCHIVE_FORUM_THREADS = new OneTimeEventListener<>(ForumListener::onCollectTasks);
150+
148151
private static final Set<GatewayIntent> INTENTS = Set.of(
149152
GatewayIntent.DIRECT_MESSAGES,
150153
GatewayIntent.GUILD_BANS,
@@ -284,6 +287,8 @@ public void start() {
284287
MISC_LISTENER.addListener(UpdateRulesCommand::onEvent);
285288
MISC_LISTENER.addListeners(new EventReactionAdded(), new PersistedRolesEvents(), new ForumListener());
286289

290+
ARCHIVE_FORUM_THREADS.register(Events.MISC_BUS);
291+
287292
try {
288293
final var builder = JDABuilder
289294
.create(getToken(), INTENTS)

src/watcher/java/com/mcmoddev/mmdbot/watcher/event/ForumListener.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
*/
2121
package com.mcmoddev.mmdbot.watcher.event;
2222

23+
import com.mcmoddev.mmdbot.core.util.TaskScheduler;
24+
import com.mcmoddev.mmdbot.watcher.TheWatcher;
2325
import net.dv8tion.jda.api.JDA;
26+
import net.dv8tion.jda.api.entities.Message;
2427
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
2528
import net.dv8tion.jda.api.entities.channel.forums.ForumTag;
2629
import net.dv8tion.jda.api.entities.emoji.CustomEmoji;
@@ -30,8 +33,12 @@
3033
import net.dv8tion.jda.api.hooks.ListenerAdapter;
3134
import org.jetbrains.annotations.NotNull;
3235

36+
import java.time.Instant;
37+
import java.time.temporal.ChronoUnit;
3338
import java.util.List;
3439
import java.util.Objects;
40+
import java.util.concurrent.TimeUnit;
41+
import java.util.function.Consumer;
3542

3643
public 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

Comments
 (0)