Skip to content

Commit bc6574f

Browse files
Auto post advice (#646)
* forwarded method calls, gotta make the actual method * Added method * Removed double schdeuling and fixed mention * removed todos * Fixed bug and extracted check into method * Spotless apply * Changed message * Changes based on CR * Small change * Figured something out * spotless apply * SonarLint are you happy now? * Changes based on review
1 parent db5ca07 commit bc6574f

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,9 @@ private RestAction<Message> handleEvent(InteractionHook eventHook, ThreadChannel
214214
return sendInitialMessage(guild, threadChannel, author, title, category)
215215
.flatMap(Message::pin)
216216
.flatMap(any -> notifyUser(eventHook, threadChannel))
217-
.flatMap(any -> helper.sendExplanationMessage(threadChannel));
217+
.flatMap(any -> helper.sendExplanationMessage(threadChannel))
218+
.onSuccess(any -> helper.scheduleNoActivityAdviceCheck(threadChannel.getIdLong(),
219+
author.getIdLong()));
218220
}
219221

220222
private RestAction<Message> sendInitialMessage(Guild guild, ThreadChannel threadChannel,

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

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public final class HelpSystemHelper {
6464

6565
private static final ScheduledExecutorService SERVICE = Executors.newScheduledThreadPool(3);
6666
private static final int SEND_UNCATEGORIZED_ADVICE_AFTER_MINUTES = 5;
67-
67+
private static final int SEND_NO_ACTIVITY_ADVICE_AFTER_MINUTES = 5;
68+
public static final int MAX_MESSAGES_TO_LOOK_BACK = 10;
6869
private final Predicate<String> isOverviewChannelName;
6970
private final String overviewChannelPattern;
7071
private final Predicate<String> isStagingChannelName;
@@ -333,6 +334,61 @@ private void executeUncategorizedAdviceCheck(long threadChannelId, long authorId
333334
}).queue();
334335
}
335336

337+
void scheduleNoActivityAdviceCheck(long threadChannelId, long authorId) {
338+
SERVICE.schedule(() -> {
339+
try {
340+
executeNoActivityAdviceCheck(threadChannelId, authorId);
341+
} catch (Exception e) {
342+
logger.warn(
343+
"Unknown error during a no activity advice check on thread {} by author {}.",
344+
threadChannelId, authorId, e);
345+
}
346+
}, SEND_NO_ACTIVITY_ADVICE_AFTER_MINUTES, TimeUnit.MINUTES);
347+
}
348+
349+
private void executeNoActivityAdviceCheck(long threadChannelId, long authorId) {
350+
logger.debug("Executing no activity advice check for thread {} by author {}.",
351+
threadChannelId, authorId);
352+
353+
ThreadChannel threadChannel = jda.getThreadChannelById(threadChannelId);
354+
if (threadChannel == null) {
355+
logger.debug(
356+
"Channel for no activity advice check seems to be deleted (thread {} by author {}).",
357+
threadChannelId, authorId);
358+
return;
359+
}
360+
361+
if (threadChannel.isArchived()) {
362+
logger.debug(
363+
"Channel for no activity advice check is archived already (thread {} by author {}).",
364+
threadChannelId, authorId);
365+
return;
366+
}
367+
if (hasNoAuthorActivity(authorId, threadChannel)) {
368+
MessageEmbed embed = HelpSystemHelper.embedWith(
369+
"""
370+
Hey there %s👋 It has been a bit after you created this thread and you still did not share any details of your question.
371+
Helpers have seen your question already and are just waiting for you to elaborate on your problem and provide detailed information on it 👌
372+
"""
373+
.formatted(User.fromId(authorId).getAsMention()));
374+
threadChannel.sendMessageEmbeds(embed).queue();
375+
}
376+
}
377+
378+
private static boolean hasNoAuthorActivity(long authorId, ThreadChannel threadChannel) {
379+
int messagesFromOthers = 0;
380+
for (Message message : threadChannel.getIterableHistory()) {
381+
if (messagesFromOthers == MAX_MESSAGES_TO_LOOK_BACK) {
382+
return false;
383+
}
384+
if (message.getAuthor().getIdLong() == authorId) {
385+
return false;
386+
}
387+
messagesFromOthers++;
388+
}
389+
return true;
390+
}
391+
336392
record HelpThreadName(@Nullable ThreadActivity activity, @Nullable String category,
337393
String title) {
338394
static HelpThreadName ofChannelName(CharSequence channelName) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ private RestAction<?> handleEvent(ThreadChannel threadChannel, Message message,
170170
.flatMap(any -> message.delete())
171171
.flatMap(any -> helper.sendExplanationMessage(threadChannel))
172172
.onSuccess(any -> helper.scheduleUncategorizedAdviceCheck(threadChannel.getIdLong(),
173+
author.getIdLong()))
174+
.onSuccess(any -> helper.scheduleNoActivityAdviceCheck(threadChannel.getIdLong(),
173175
author.getIdLong()));
174176
}
175177

0 commit comments

Comments
 (0)