Skip to content

Commit dba15c4

Browse files
committed
Auto-free help channels after 2h of inactivity
1 parent 41829f8 commit dba15c4

File tree

11 files changed

+296
-199
lines changed

11 files changed

+296
-199
lines changed

application/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ dependencies {
5353
implementation 'org.scilab.forge:jlatexmath-font-cyrillic:1.0.7'
5454

5555
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-csv:2.13.0'
56+
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.0'
5657
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.0'
5758

5859
implementation 'com.github.freva:ascii-table:1.2.0'

application/config.json.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"tagManageRolePattern": "Moderator|Staff Assistant|Top Helpers .+",
1111
"freeCommand": [
1212
{
13+
"inactiveChannelDuration": "PT2H",
14+
"messageRetrieveLimit": 10,
1315
"statusChannel": <put_a_channel_id_here>,
1416
"monitoredChannels": [
1517
<put_a_channel_id_here>

application/src/main/java/org/togetherjava/tjbot/commands/Features.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.togetherjava.tjbot.commands.basic.RoleSelectCommand;
77
import org.togetherjava.tjbot.commands.basic.SuggestionsUpDownVoter;
88
import org.togetherjava.tjbot.commands.basic.VcActivityCommand;
9+
import org.togetherjava.tjbot.commands.free.AutoFreeRoutine;
10+
import org.togetherjava.tjbot.commands.free.FreeChannelMonitor;
911
import org.togetherjava.tjbot.commands.free.FreeCommand;
1012
import org.togetherjava.tjbot.commands.mathcommands.TeXCommand;
1113
import org.togetherjava.tjbot.commands.moderation.*;
@@ -59,6 +61,7 @@ public enum Features {
5961
ModerationActionsStore actionsStore = new ModerationActionsStore(database);
6062
ModAuditLogWriter modAuditLogWriter = new ModAuditLogWriter(config);
6163
ScamHistoryStore scamHistoryStore = new ScamHistoryStore(database);
64+
FreeChannelMonitor freeChannelMonitor = new FreeChannelMonitor(config);
6265

6366
// NOTE The system can add special system relevant commands also by itself,
6467
// hence this list may not necessarily represent the full list of all commands actually
@@ -71,6 +74,7 @@ public enum Features {
7174
features.add(new TopHelpersPurgeMessagesRoutine(database));
7275
features.add(new RemindRoutine(database));
7376
features.add(new ScamHistoryPurgeRoutine(scamHistoryStore));
77+
features.add(new AutoFreeRoutine(freeChannelMonitor));
7478

7579
// Message receivers
7680
features.add(new TopHelpersMessageListener(database, config));
@@ -102,7 +106,7 @@ public enum Features {
102106
features.add(new UnquarantineCommand(actionsStore, config));
103107

104108
// Mixtures
105-
features.add(new FreeCommand(config));
109+
features.add(new FreeCommand(config, freeChannelMonitor));
106110

107111
return features;
108112
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.togetherjava.tjbot.commands.free;
2+
3+
import net.dv8tion.jda.api.JDA;
4+
import net.dv8tion.jda.api.entities.Guild;
5+
import net.dv8tion.jda.api.entities.TextChannel;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.togetherjava.tjbot.commands.Routine;
8+
9+
import java.util.Collection;
10+
import java.util.Objects;
11+
import java.util.concurrent.TimeUnit;
12+
13+
/**
14+
* Routine that automatically marks busy help channels free after a certain time without any
15+
* activity.
16+
*/
17+
public final class AutoFreeRoutine implements Routine {
18+
private final FreeChannelMonitor channelMonitor;
19+
20+
/**
21+
* Creates a new instance.
22+
*
23+
* @param channelMonitor used to monitor and control the free-status of channels
24+
*/
25+
public AutoFreeRoutine(@NotNull FreeChannelMonitor channelMonitor) {
26+
this.channelMonitor = channelMonitor;
27+
}
28+
29+
@Override
30+
public void runRoutine(@NotNull JDA jda) {
31+
channelMonitor.guildIds()
32+
.map(jda::getGuildById)
33+
.filter(Objects::nonNull)
34+
.forEach(this::processGuild);
35+
}
36+
37+
private void processGuild(@NotNull Guild guild) {
38+
// Mark inactive channels free
39+
Collection<TextChannel> inactiveChannels = channelMonitor.freeInactiveChannels(guild);
40+
41+
// Then update the status
42+
channelMonitor.displayStatus(guild);
43+
44+
// Finally, send the messages (the order is important to ensure sane behavior in case of
45+
// crashes)
46+
inactiveChannels.forEach(inactiveChannel -> inactiveChannel
47+
.sendMessage(UserStrings.AUTO_MARK_AS_FREE.message())
48+
.queue());
49+
}
50+
51+
@Override
52+
public @NotNull Schedule createSchedule() {
53+
return new Schedule(ScheduleMode.FIXED_RATE, 1, 5, TimeUnit.MINUTES);
54+
}
55+
}

application/src/main/java/org/togetherjava/tjbot/commands/free/ChannelStatus.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ private void setName(@NotNull final String name) {
101101
*
102102
* @param guild the {@link Guild} that the channel belongs to, to retrieve its name from.
103103
* @throws IllegalArgumentException if the guild has not been added, see
104-
* {@link ChannelMonitor#addChannelForStatus(TextChannel)}
104+
* {@link FreeChannelMonitor#addChannelForStatus(TextChannel)}
105105
* @throws IllegalStateException if a channel was added, see
106-
* {@link ChannelMonitor#addChannelToMonitor(long)}, that is not a {@link TextChannel}.
107-
* Since addChannelToMonitor does not access the {@link JDA} the entry can only be
108-
* validated before use instead of on addition.
106+
* {@link FreeChannelMonitor#addChannelToMonitor(long)}, that is not a
107+
* {@link TextChannel}. Since addChannelToMonitor does not access the {@link JDA} the
108+
* entry can only be validated before use instead of on addition.
109109
*/
110110
public void updateChannelName(@NotNull final Guild guild) {
111111
GuildChannel channel = guild.getGuildChannelById(channelId);

0 commit comments

Comments
 (0)