|
1 | 1 | package org.togetherjava.tjbot.commands.help;
|
2 | 2 |
|
3 | 3 | import net.dv8tion.jda.api.EmbedBuilder;
|
| 4 | +import net.dv8tion.jda.api.JDA; |
| 5 | +import net.dv8tion.jda.api.MessageBuilder; |
4 | 6 | import net.dv8tion.jda.api.entities.*;
|
5 | 7 | import net.dv8tion.jda.api.requests.RestAction;
|
6 | 8 | import net.dv8tion.jda.api.requests.restaction.MessageAction;
|
|
19 | 21 | import java.util.List;
|
20 | 22 | import java.util.Locale;
|
21 | 23 | import java.util.Optional;
|
| 24 | +import java.util.concurrent.Executors; |
| 25 | +import java.util.concurrent.ScheduledExecutorService; |
| 26 | +import java.util.concurrent.TimeUnit; |
22 | 27 | import java.util.function.Consumer;
|
23 | 28 | import java.util.function.Predicate;
|
24 | 29 | import java.util.regex.Matcher;
|
@@ -46,20 +51,26 @@ public final class HelpSystemHelper {
|
46 | 51 | static final int TITLE_COMPACT_LENGTH_MIN = 2;
|
47 | 52 | static final int TITLE_COMPACT_LENGTH_MAX = 70;
|
48 | 53 |
|
| 54 | + private static final ScheduledExecutorService SERVICE = Executors.newScheduledThreadPool(3); |
| 55 | + private static final int SEND_UNCATEGORIZED_ADVICE_AFTER_MINUTES = 5; |
| 56 | + |
49 | 57 | private final Predicate<String> isOverviewChannelName;
|
50 | 58 | private final String overviewChannelPattern;
|
51 | 59 | private final Predicate<String> isStagingChannelName;
|
52 | 60 | private final String stagingChannelPattern;
|
53 | 61 | private final String categoryRoleSuffix;
|
54 | 62 | private final Database database;
|
| 63 | + private final JDA jda; |
55 | 64 |
|
56 | 65 | /**
|
57 | 66 | * Creates a new instance.
|
58 | 67 | *
|
| 68 | + * @param jda the JDA instance to use |
59 | 69 | * @param config the config to use
|
60 | 70 | * @param database the database to store help thread metadata in
|
61 | 71 | */
|
62 |
| - public HelpSystemHelper(Config config, Database database) { |
| 72 | + public HelpSystemHelper(JDA jda, Config config, Database database) { |
| 73 | + this.jda = jda; |
63 | 74 | HelpSystemConfig helpConfig = config.getHelpSystem();
|
64 | 75 | this.database = database;
|
65 | 76 |
|
@@ -239,6 +250,50 @@ List<ThreadChannel> getActiveThreadsIn(TextChannel channel) {
|
239 | 250 | .toList();
|
240 | 251 | }
|
241 | 252 |
|
| 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 | + |
242 | 297 | record HelpThreadName(@Nullable ThreadActivity activity, @Nullable String category,
|
243 | 298 | String title) {
|
244 | 299 | static HelpThreadName ofChannelName(CharSequence channelName) {
|
|
0 commit comments