Skip to content

Commit ece11d0

Browse files
authored
Auto posting advice on uncategorized help threads (#554)
* after 5 mins
1 parent 8f9102a commit ece11d0

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
6464
ModerationActionsStore actionsStore = new ModerationActionsStore(database);
6565
ModAuditLogWriter modAuditLogWriter = new ModAuditLogWriter(config);
6666
ScamHistoryStore scamHistoryStore = new ScamHistoryStore(database);
67-
HelpSystemHelper helpSystemHelper = new HelpSystemHelper(config, database);
67+
HelpSystemHelper helpSystemHelper = new HelpSystemHelper(jda, config, database);
6868

6969
// NOTE The system can add special system relevant commands also by itself,
7070
// hence this list may not necessarily represent the full list of all commands actually

application/src/main/java/org/togetherjava/tjbot/commands/help/HelpSystemHelper.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.togetherjava.tjbot.commands.help;
22

33
import net.dv8tion.jda.api.EmbedBuilder;
4+
import net.dv8tion.jda.api.JDA;
5+
import net.dv8tion.jda.api.MessageBuilder;
46
import net.dv8tion.jda.api.entities.*;
57
import net.dv8tion.jda.api.requests.RestAction;
68
import net.dv8tion.jda.api.requests.restaction.MessageAction;
@@ -19,6 +21,9 @@
1921
import java.util.List;
2022
import java.util.Locale;
2123
import java.util.Optional;
24+
import java.util.concurrent.Executors;
25+
import java.util.concurrent.ScheduledExecutorService;
26+
import java.util.concurrent.TimeUnit;
2227
import java.util.function.Consumer;
2328
import java.util.function.Predicate;
2429
import java.util.regex.Matcher;
@@ -46,20 +51,26 @@ public final class HelpSystemHelper {
4651
static final int TITLE_COMPACT_LENGTH_MIN = 2;
4752
static final int TITLE_COMPACT_LENGTH_MAX = 70;
4853

54+
private static final ScheduledExecutorService SERVICE = Executors.newScheduledThreadPool(3);
55+
private static final int SEND_UNCATEGORIZED_ADVICE_AFTER_MINUTES = 5;
56+
4957
private final Predicate<String> isOverviewChannelName;
5058
private final String overviewChannelPattern;
5159
private final Predicate<String> isStagingChannelName;
5260
private final String stagingChannelPattern;
5361
private final String categoryRoleSuffix;
5462
private final Database database;
63+
private final JDA jda;
5564

5665
/**
5766
* Creates a new instance.
5867
*
68+
* @param jda the JDA instance to use
5969
* @param config the config to use
6070
* @param database the database to store help thread metadata in
6171
*/
62-
public HelpSystemHelper(Config config, Database database) {
72+
public HelpSystemHelper(JDA jda, Config config, Database database) {
73+
this.jda = jda;
6374
HelpSystemConfig helpConfig = config.getHelpSystem();
6475
this.database = database;
6576

@@ -239,6 +250,50 @@ List<ThreadChannel> getActiveThreadsIn(TextChannel channel) {
239250
.toList();
240251
}
241252

253+
void scheduleUncategorizedAdviceCheck(long threadChannelId, long authorId) {
254+
SERVICE.schedule(() -> {
255+
try {
256+
executeUncategorizedAdviceCheck(threadChannelId, authorId);
257+
} catch (Exception e) {
258+
logger.warn(
259+
"Unknown error during an uncategorized advice check on thread {} by author {}.",
260+
threadChannelId, authorId, e);
261+
}
262+
}, SEND_UNCATEGORIZED_ADVICE_AFTER_MINUTES, TimeUnit.MINUTES);
263+
}
264+
265+
private void executeUncategorizedAdviceCheck(long threadChannelId, long authorId) {
266+
logger.debug("Executing uncategorized advice check for thread {} by author {}.",
267+
threadChannelId, authorId);
268+
jda.retrieveUserById(authorId).flatMap(author -> {
269+
ThreadChannel threadChannel = jda.getThreadChannelById(threadChannelId);
270+
if (threadChannel == null) {
271+
logger.debug(
272+
"Channel for uncategorized advice check seems to be deleted (thread {} by author {}).",
273+
threadChannelId, authorId);
274+
return new CompletedRestAction<>(jda, null);
275+
}
276+
277+
Optional<String> category = getCategoryOfChannel(threadChannel);
278+
if (category.isPresent()) {
279+
logger.debug(
280+
"Channel for uncategorized advice check seems to have a category now (thread {} by author {}).",
281+
threadChannelId, authorId);
282+
return new CompletedRestAction<>(jda, null);
283+
}
284+
285+
// Still no category, send advice
286+
MessageEmbed embed = HelpSystemHelper.embedWith(
287+
"""
288+
Hey there 👋 You have to select a category for your help thread, otherwise nobody can see your question.
289+
Please use the `/change-help-category` slash-command and pick what fits best, thanks 🙂
290+
""");
291+
Message message = new MessageBuilder(author.getAsMention()).setEmbeds(embed).build();
292+
293+
return threadChannel.sendMessage(message);
294+
}).queue();
295+
}
296+
242297
record HelpThreadName(@Nullable ThreadActivity activity, @Nullable String category,
243298
String title) {
244299
static HelpThreadName ofChannelName(CharSequence channelName) {

application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,12 @@ private RestAction<?> handleEvent(ThreadChannel threadChannel, Message message,
162162
return sendInitialMessage(threadChannel, message, title)
163163
.flatMap(any -> notifyUser(threadChannel, message))
164164
.flatMap(any -> message.delete())
165-
.flatMap(any -> helper.sendExplanationMessage(threadChannel));
165+
.flatMap(any -> helper.sendExplanationMessage(threadChannel))
166+
.<Void>map(any -> {
167+
helper.scheduleUncategorizedAdviceCheck(threadChannel.getIdLong(),
168+
author.getIdLong());
169+
return null;
170+
});
166171
}
167172

168173
private static MessageAction sendInitialMessage(ThreadChannel threadChannel,

0 commit comments

Comments
 (0)