Skip to content

Commit 8a9a469

Browse files
Implement automatic inactivity post closing
1 parent dbae107 commit 8a9a469

File tree

4 files changed

+19
-27
lines changed

4 files changed

+19
-27
lines changed

src/main/java/net/javadiscord/javabot/data/config/guild/HelpConfig.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ public class HelpConfig extends GuildConfigItem {
2121

2222
private long helpForumChannelId = 0;
2323

24-
/**
25-
* The id of the channel category that contains all open channels.
26-
*/
27-
private long openCategoryId;
28-
2924
/**
3025
* The id of the helper role.
3126
*/
@@ -40,13 +35,13 @@ public class HelpConfig extends GuildConfigItem {
4035
* The message that's sent as soon as a user asks a question in an open help
4136
* channel. This is only sent if it's not null.
4237
*/
43-
private String reservedChannelMessageTemplate = "`⌛` **This channel has been reserved for your question.**\n> Please use `/close` when you're finished.";
38+
private String reservedChannelMessageTemplate = "`⌛` **This post has been reserved for your question.**\n> Hey %s! Please use `/close` or the 'Close Post' button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after %s minutes of inactivity.\n\n**TIP:** Narrow down your issue to __simple__ and __precise__ questions to maximize the chance that others will reply in here.";
4439

4540
/**
4641
* The message that's sent in a post to tell users that it
4742
* is now marked as dormant and no more messages can be sent.
4843
*/
49-
private String dormantChannelMessageTemplate = "`\uD83D\uDCA4` **Channel marked as dormant**\n> This post has been inactive for over %s minutes. It is no longer possible to send messages in this channel.\n> If your question was not answered yet, feel free to create a new post.";
44+
private String dormantChannelMessageTemplate = "`\uD83D\uDCA4` **Post marked as dormant**\n> This post has been inactive for over %s minutes. It is no longer possible to send messages in this channel.\n> If your question was not answered yet, feel free to create a new post.";
5045

5146
/**
5247
* The message that's sent when a user unreserved a channel where other users

src/main/java/net/javadiscord/javabot/systems/help/HelpForumUpdater.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import lombok.extern.slf4j.Slf4j;
55
import net.dv8tion.jda.api.JDA;
66
import net.dv8tion.jda.api.entities.Guild;
7+
import net.dv8tion.jda.api.entities.UserSnowflake;
78
import net.dv8tion.jda.api.entities.channel.concrete.ForumChannel;
89
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
910
import net.javadiscord.javabot.data.config.BotConfig;
@@ -12,7 +13,7 @@
1213
import org.springframework.scheduling.annotation.Scheduled;
1314
import org.springframework.stereotype.Service;
1415

15-
import java.time.OffsetDateTime;
16+
import java.time.Instant;
1617

1718
/**
1819
* Loops through all guilds and updates the corresponding help forum channel, closing inactive ones.
@@ -27,27 +28,33 @@ public class HelpForumUpdater {
2728
/**
2829
* Updates all help channels.
2930
*/
30-
@Scheduled(cron = "*/10 * * * *") // Run every 10 minutes
31+
@Scheduled(cron = "0 */10 * * * *") // Run every 10 minutes
3132
public void execute() {
3233
for (Guild guild : jda.getGuilds()) {
33-
log.info("Updating help channels in {}", guild.getName());
34+
log.info("Checking for inactive forum posts in {}", guild.getName());
3435
HelpConfig config = botConfig.get(guild).getHelpConfig();
3536
ForumChannel forum = config.getHelpForumChannel();
3637
if (forum != null) {
3738
for (ThreadChannel post : forum.getThreadChannels()) {
3839
if (post.isArchived() || post.isLocked()) continue;
3940
checkForumPost(post, config);
4041
}
42+
} else {
43+
log.warn("Could not find forum channel for guild {}", guild.getName());
4144
}
4245
}
4346
}
4447

4548
private void checkForumPost(@NotNull ThreadChannel post, HelpConfig config) {
4649
post.retrieveMessageById(post.getLatestMessageId()).queue(latest -> {
47-
if (latest.getTimeCreated().plusMinutes(config.getInactivityTimeoutMinutes()).isBefore(OffsetDateTime.now())) {
48-
post.sendMessage(config.getDormantChannelMessageTemplate().formatted(config.getInactivityTimeoutMinutes())).queue(s ->
49-
post.getManager().setLocked(true).setArchived(true).queue());
50+
long minutesAgo = (Instant.now().getEpochSecond() - latest.getTimeCreated().toEpochSecond()) / 60;
51+
if (minutesAgo > config.getInactivityTimeoutMinutes()) {
52+
post.sendMessage(config.getDormantChannelMessageTemplate().formatted(config.getInactivityTimeoutMinutes())).queue(s -> {
53+
post.getManager().setLocked(true).setArchived(true).queue();
54+
log.info("Archived & locked forum thread '{}' (by {}) for inactivity (last message sent {} minutes ago)",
55+
post.getName(), post.getOwnerId(), minutesAgo);
56+
});
5057
}
51-
});
58+
}, e -> log.error("Could not find message with id {}", post.getLatestMessageId()));
5259
}
5360
}

src/main/java/net/javadiscord/javabot/systems/help/HelpListener.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {
7979

8080
@Override
8181
public void onChannelCreate(@NotNull ChannelCreateEvent event) {
82-
if (event.getGuild() == null || isInvalidForumPost(event.getChannel())) {
82+
if (isInvalidForumPost(event.getChannel())) {
8383
return;
8484
}
8585
HelpConfig config = botConfig.get(event.getGuild()).getHelpConfig();
@@ -91,13 +91,7 @@ public void onChannelCreate(@NotNull ChannelCreateEvent event) {
9191
post.sendMessageComponents(ActionRow.of(
9292
Button.primary(ComponentIdBuilder.build(HelpManager.HELP_CLOSE_IDENTIFIER, post.getIdLong()), "Close Post"),
9393
Button.secondary(ComponentIdBuilder.build(HelpManager.HELP_GUIDELINES_IDENTIFIER), "View Help Guidelines")
94-
)).queue(success -> {
95-
// send /close reminder (if enabled)
96-
UserPreference preference = userPreferenceService.getOrCreate(post.getOwnerIdLong(), Preference.FORUM_CLOSE_REMINDER);
97-
if (Boolean.parseBoolean(preference.getState())) {
98-
post.sendMessageFormat(config.getDormantChannelMessageTemplate(), UserSnowflake.fromId(post.getOwnerIdLong()).getAsMention()).queue();
99-
}
100-
});
94+
)).queue(success -> post.sendMessageFormat(config.getReservedChannelMessageTemplate(), UserSnowflake.fromId(post.getOwnerId()).getAsMention(), config.getInactivityTimeoutMinutes()).queue());
10195
}
10296

10397
@Override

src/main/java/net/javadiscord/javabot/systems/user_preferences/model/Preference.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ public enum Preference {
77
/**
88
* Enables/Disables QOTW reminders.
99
*/
10-
QOTW_REMINDER("Question of the Week Reminder", "false", new BooleanPreference()),
11-
/**
12-
* A boolean preference which enables/disables the close reminder in forum help posts.
13-
*/
14-
FORUM_CLOSE_REMINDER("Help Forum Close Reminder", "true", new BooleanPreference());
10+
QOTW_REMINDER("Question of the Week Reminder", "false", new BooleanPreference());
1511

1612
private final String name;
1713
private final String defaultState;

0 commit comments

Comments
 (0)