Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
ModerationActionsStore actionsStore = new ModerationActionsStore(database);
ModAuditLogWriter modAuditLogWriter = new ModAuditLogWriter(config);
ScamHistoryStore scamHistoryStore = new ScamHistoryStore(database);
HelpSystemHelper helpSystemHelper = new HelpSystemHelper(config, database);
HelpSystemHelper helpSystemHelper = new HelpSystemHelper(jda, config, database);

// NOTE The system can add special system relevant commands also by itself,
// hence this list may not necessarily represent the full list of all commands actually
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.togetherjava.tjbot.commands.help;

import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.requests.restaction.MessageAction;
Expand All @@ -19,6 +21,9 @@
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -46,20 +51,26 @@ public final class HelpSystemHelper {
static final int TITLE_COMPACT_LENGTH_MIN = 2;
static final int TITLE_COMPACT_LENGTH_MAX = 70;

private static final ScheduledExecutorService SERVICE = Executors.newScheduledThreadPool(3);
private static final int SEND_UNCATEGORIZED_ADVICE_AFTER_MINUTES = 5;

private final Predicate<String> isOverviewChannelName;
private final String overviewChannelPattern;
private final Predicate<String> isStagingChannelName;
private final String stagingChannelPattern;
private final String categoryRoleSuffix;
private final Database database;
private final JDA jda;

/**
* Creates a new instance.
*
* @param jda the JDA instance to use
* @param config the config to use
* @param database the database to store help thread metadata in
*/
public HelpSystemHelper(Config config, Database database) {
public HelpSystemHelper(JDA jda, Config config, Database database) {
this.jda = jda;
HelpSystemConfig helpConfig = config.getHelpSystem();
this.database = database;

Expand Down Expand Up @@ -239,6 +250,50 @@ List<ThreadChannel> getActiveThreadsIn(TextChannel channel) {
.toList();
}

void scheduleUncategorizedAdviceCheck(long threadChannelId, long authorId) {
SERVICE.schedule(() -> {
try {
executeUncategorizedAdviceCheck(threadChannelId, authorId);
} catch (Exception e) {
logger.warn(
"Unknown error during an uncategorized advice check on thread {} by author {}.",
threadChannelId, authorId, e);
}
}, SEND_UNCATEGORIZED_ADVICE_AFTER_MINUTES, TimeUnit.MINUTES);
}

private void executeUncategorizedAdviceCheck(long threadChannelId, long authorId) {
logger.debug("Executing uncategorized advice check for thread {} by author {}.",
threadChannelId, authorId);
jda.retrieveUserById(authorId).flatMap(author -> {
ThreadChannel threadChannel = jda.getThreadChannelById(threadChannelId);
if (threadChannel == null) {
logger.debug(
"Channel for uncategorized advice check seems to be deleted (thread {} by author {}).",
threadChannelId, authorId);
return new CompletedRestAction<>(jda, null);
}

Optional<String> category = getCategoryOfChannel(threadChannel);
if (category.isPresent()) {
logger.debug(
"Channel for uncategorized advice check seems to have a category now (thread {} by author {}).",
threadChannelId, authorId);
return new CompletedRestAction<>(jda, null);
}

// Still no category, send advice
MessageEmbed embed = HelpSystemHelper.embedWith(
"""
Hey there 👋 You have to select a category for your help thread, otherwise nobody can see your question.
Please use the `/change-help-category` slash-command and pick what fits best, thanks 🙂
""");
Message message = new MessageBuilder(author.getAsMention()).setEmbeds(embed).build();

return threadChannel.sendMessage(message);
}).queue();
}

record HelpThreadName(@Nullable ThreadActivity activity, @Nullable String category,
String title) {
static HelpThreadName ofChannelName(CharSequence channelName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ private RestAction<?> handleEvent(ThreadChannel threadChannel, Message message,
return sendInitialMessage(threadChannel, message, title)
.flatMap(any -> notifyUser(threadChannel, message))
.flatMap(any -> message.delete())
.flatMap(any -> helper.sendExplanationMessage(threadChannel));
.flatMap(any -> helper.sendExplanationMessage(threadChannel))
.<Void>map(any -> {
helper.scheduleUncategorizedAdviceCheck(threadChannel.getIdLong(),
author.getIdLong());
return null;
});
}

private static MessageAction sendInitialMessage(ThreadChannel threadChannel,
Expand Down