Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
dc1fd1a
add HelpThreadHistoryCache class;
CodePawfect Dec 30, 2022
7072821
Implement feature #567: Add command to reset activity for a help thread
CodePawfect Jan 1, 2023
3f2a7c2
change access modifier back to private and class back to final;
CodePawfect Jan 1, 2023
07dfec7
changed ResetActivityCommand from own command to Subcommand;
CodePawfect Jan 1, 2023
9ae5835
add removeChannel method to cache; adjust methods;
CodePawfect Jan 2, 2023
3aefdbe
replaced HelpThreadManuallyResetHistoryCache.java with Caffeine Object.
CodePawfect Jan 2, 2023
e2464bf
adjust cache setting and variable name;
CodePawfect Jan 3, 2023
49d15a4
changes command response message;
CodePawfect Jan 3, 2023
0ef8cb3
changed access modifier to package-private; adjust resetActivity;
CodePawfect Jan 3, 2023
8e61a4f
changed suggestions from code findings;
CodePawfect Jan 3, 2023
ca4f636
removed not needed sorting;
CodePawfect Jan 5, 2023
ba631e6
adjust line formatting;
CodePawfect Jan 5, 2023
3625a00
added if case that only thread author can reset activity;
CodePawfect Jan 5, 2023
485e277
added if case that only thread author can reset activity;
CodePawfect Jan 5, 2023
d5aaff7
changed blocking to async method;
CodePawfect Jan 5, 2023
d03a114
add return;
CodePawfect Jan 5, 2023
b2d6ae7
removed lines from lambda;
CodePawfect Jan 6, 2023
142e20d
changed cache type
CodePawfect Jan 6, 2023
d96edac
changed getId to getIdLong and removed line;
CodePawfect Jan 6, 2023
e2d7d3a
resolved code findings;
CodePawfect Jan 9, 2023
dd339a8
resolved code findings #2;
CodePawfect Jan 10, 2023
ae2d562
resolved code findings #3;
CodePawfect Jan 13, 2023
895d487
resolved code findings #4;
CodePawfect Jan 18, 2023
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
@@ -1,8 +1,11 @@
package org.togetherjava.tjbot.commands.help;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageHistory;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.channel.concrete.ForumChannel;
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
Expand All @@ -13,6 +16,7 @@

import org.togetherjava.tjbot.commands.Routine;

import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -30,8 +34,14 @@ public final class HelpThreadActivityUpdater implements Routine {
private static final Logger logger = LoggerFactory.getLogger(HelpThreadActivityUpdater.class);
private static final int SCHEDULE_MINUTES = 30;
private static final int ACTIVITY_DETERMINE_MESSAGE_LIMIT = 11;

private static final int PERSIST_DURATION_VALUE = 12;
private static final ChronoUnit PERSIST_DURATION_UNIT = ChronoUnit.HOURS;
private final HelpSystemHelper helper;
public static final Cache<MessageChannel, String> manuallyResetChannelActivityCache =
Caffeine.newBuilder()
.maximumSize(1_000)
.expireAfterWrite(PERSIST_DURATION_VALUE, TimeUnit.of(PERSIST_DURATION_UNIT))
.build();

/**
* Creates a new instance.
Expand Down Expand Up @@ -70,15 +80,27 @@ private void updateActivityForGuild(Guild guild) {
activeThreads.forEach(this::updateActivityForThread);
}

private void updateActivityForThread(ThreadChannel threadChannel) {
void updateActivityForThread(ThreadChannel threadChannel) {
determineActivity(threadChannel)
.flatMap(threadActivity -> helper.changeChannelActivity(threadChannel, threadActivity))
.queue();
}

private static RestAction<HelpSystemHelper.ThreadActivity> determineActivity(
MessageChannel channel) {
return channel.getHistory().retrievePast(ACTIVITY_DETERMINE_MESSAGE_LIMIT).map(messages -> {
String mostRecentMessageId = manuallyResetChannelActivityCache.getIfPresent(channel);
RestAction<List<Message>> restActionMessages;

if (mostRecentMessageId != null) {
MessageHistory.MessageRetrieveAction historyAfter =
channel.getHistoryAfter(mostRecentMessageId, ACTIVITY_DETERMINE_MESSAGE_LIMIT);
restActionMessages = historyAfter.map(MessageHistory::getRetrievedHistory);
} else {
restActionMessages =
channel.getHistory().retrievePast(ACTIVITY_DETERMINE_MESSAGE_LIMIT);
}

return restActionMessages.map(messages -> {
if (messages.size() >= ACTIVITY_DETERMINE_MESSAGE_LIMIT) {
// There are likely even more messages, but we hit the limit
return HelpSystemHelper.ThreadActivity.HIGH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.InteractionHook;
Expand All @@ -31,6 +28,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.togetherjava.tjbot.commands.help.HelpThreadActivityUpdater.manuallyResetChannelActivityCache;

/**
* Implements the {@code /help-thread} command, used to maintain certain aspects of help threads,
* such as renaming or closing them.
Expand All @@ -42,6 +41,8 @@ public final class HelpThreadCommand extends SlashCommandAdapter {
private static final String CHANGE_CATEGORY_OPTION = "category";
public static final String CHANGE_TITLE_SUBCOMMAND = "title";
private static final String CHANGE_TITLE_OPTION = "title";
private static final String CLOSE_SUBCOMMAND = "close";
private static final String RESET_ACTIVITY_SUBCOMMAND = "reset-activity";
public static final String CHANGE_SUBCOMMAND_GROUP = "change";
public static final String COMMAND_NAME = "help-thread";

Expand Down Expand Up @@ -80,6 +81,7 @@ public HelpThreadCommand(Config config, HelpSystemHelper helper) {
getData().addSubcommandGroups(changeCommands);

getData().addSubcommands(Subcommand.CLOSE.toSubcommandData());
getData().addSubcommands(Subcommand.RESET_ACTIVITY.toSubcommandData());

this.helper = helper;

Expand All @@ -92,9 +94,9 @@ public HelpThreadCommand(Config config, HelpSystemHelper helper) {
subcommandToCooldownCache = new EnumMap<>(streamSubcommands()
.filter(Subcommand::hasCooldown)
.collect(Collectors.toMap(Function.identity(), any -> createCooldownCache.get())));
subcommandToEventHandler = new EnumMap<>(
Map.of(Subcommand.CHANGE_CATEGORY, this::changeCategory, Subcommand.CHANGE_TITLE,
this::changeTitle, Subcommand.CLOSE, this::closeThread));
subcommandToEventHandler = new EnumMap<>(Map.of(Subcommand.CHANGE_CATEGORY,
this::changeCategory, Subcommand.CHANGE_TITLE, this::changeTitle, Subcommand.CLOSE,
this::closeThread, Subcommand.RESET_ACTIVITY, this::resetActivity));
}

@Override
Expand Down Expand Up @@ -195,9 +197,26 @@ private void closeThread(SlashCommandInteractionEvent event, ThreadChannel helpT
.setColor(HelpSystemHelper.AMBIENT_COLOR)
.build();

manuallyResetChannelActivityCache.invalidate(helpThread);
event.replyEmbeds(embed).flatMap(any -> helpThread.getManager().setArchived(true)).queue();
}

private void resetActivity(SlashCommandInteractionEvent event, ThreadChannel helpThread) {
refreshCooldownFor(Subcommand.RESET_ACTIVITY, helpThread);

List<Message> messages = helpThread.getIterableHistory()
.stream()
.sorted(Comparator.comparing(ISnowflake::getTimeCreated).reversed())
.toList();

manuallyResetChannelActivityCache.put(helpThread, messages.get(0).getId());

HelpThreadActivityUpdater helpThreadActivityUpdater = new HelpThreadActivityUpdater(helper);
helpThreadActivityUpdater.updateActivityForThread(helpThread);

event.reply("Activities have been reset.").queue();
}

private static Stream<Subcommand> streamSubcommands() {
return Arrays.stream(Subcommand.values());
}
Expand All @@ -206,7 +225,9 @@ enum Subcommand {
CHANGE_CATEGORY(CHANGE_CATEGORY_SUBCOMMAND, "Change the category of this help thread",
Cooldown.YES),
CHANGE_TITLE(CHANGE_TITLE_SUBCOMMAND, "Change the title of this help thread", Cooldown.YES),
CLOSE("close", "Close this help thread", Cooldown.YES);
CLOSE(CLOSE_SUBCOMMAND, "Close this help thread", Cooldown.YES),
RESET_ACTIVITY(RESET_ACTIVITY_SUBCOMMAND, "resets the activity in a help thread",
Cooldown.YES);

private final String commandName;
private final String description;
Expand Down