Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
b5cc451
Added bookmark command
Nxllpointer Oct 16, 2022
6bf58ae
Made bookmarks feature sonarlint compliant
Nxllpointer Oct 16, 2022
681b687
Removed BookmarksCommand section comments
Nxllpointer Oct 17, 2022
2c99b97
Sort bookmarks by latest activity
Nxllpointer Oct 17, 2022
6345d6b
Made final embed creations in BookmarksCommand more readable
Nxllpointer Oct 17, 2022
b910a8f
Revert all changes for rewrite
Nxllpointer Oct 18, 2022
4a165e6
Full rewrite of the bookmarks feature
Nxllpointer Oct 25, 2022
9b813fa
Added length limit for note option
Nxllpointer Oct 25, 2022
04d6c3b
Added bookmarks limit to prevent spam
Nxllpointer Oct 25, 2022
365fad7
Created replyEmbedEphemeral method because all messages are ephemeral…
Nxllpointer Oct 25, 2022
3796929
Changed remove-select-menu to display thread name if the thread was f…
Nxllpointer Oct 25, 2022
ce00b8a
Added guild-leave bookmark-removal-scheduling
Nxllpointer Oct 25, 2022
6d39fa0
Fixed requested changes for version 1 that still applied to the rewrite
Nxllpointer Oct 25, 2022
5ce5aa4
Added missing javadoc dots
Nxllpointer Oct 25, 2022
7095af9
Fixed issue with deleting all bookmarks
Nxllpointer Oct 26, 2022
d950598
Converted unnecessary multiline Strings to normal Strings
Nxllpointer Oct 29, 2022
907a248
Split createTinyEmbed into separate methods for every color
Nxllpointer Oct 30, 2022
a7f5402
Rename BookmarkPaginatorInteractor to BookmarksPaginatorInteractor
Nxllpointer Oct 30, 2022
47b5866
Add javadoc for BookmarksPaginatorInteractor
Nxllpointer Oct 30, 2022
9b09c31
Don't overwrite args in onSelectMenuSelection
Nxllpointer Oct 30, 2022
ae12506
Removed generatePageEmbed overloading for readability
Nxllpointer Oct 30, 2022
7d10c7d
Renamed highestPageIndex to lastPageIndex in generatePageEmbed
Nxllpointer Oct 30, 2022
d7bb301
Removed newline to improve readability in generatePageEmbed
Nxllpointer Oct 30, 2022
710e841
Removed unnecessary comments in generatePageEmbed
Nxllpointer Oct 30, 2022
94ae092
Renamed uninformative variable from b to bookmark when creating PageE…
Nxllpointer Oct 30, 2022
7204881
Added emptiness check to getLastPageIndex to avoid issues in the future
Nxllpointer Oct 30, 2022
a02fc5d
Fixed ComponentArguments and RemoveComponentArguments having fromStri…
Nxllpointer Oct 30, 2022
01ecc5a
Improved the list subcommand description
Nxllpointer Oct 30, 2022
6a1009e
Renamed LEAVE_REMOVAL_DELAY to LEAVE_BOOKMARKS_REMOVAL_DELAY
Nxllpointer Oct 30, 2022
6ce813b
Changed requestListPagination and requestRemovePagination to use Obje…
Nxllpointer Oct 30, 2022
d4f029b
Renamed SCHEDULED_REMOVAL_AT to DELETE_AT
Nxllpointer Nov 2, 2022
4ad21b0
Improve naming about deleting old bookmarks of users that left the guild
Nxllpointer Nov 2, 2022
d043b26
Improve naming of MAX_BOOKMARK_COUNT_USER to MAX_BOOKMARK_COUNT_PER_USER
Nxllpointer Nov 2, 2022
5242d9f
Improve naming of LEAVE_BOOKMARKS_REMOVAL_DELAY to REMOVE_BOOKMARKS_A…
Nxllpointer Nov 2, 2022
042b0b8
Changed constants to be package private instead of public
Nxllpointer Nov 2, 2022
29752cb
Improve speed by using a Set for bookmarksToRemoveChannelIDs
Nxllpointer Nov 2, 2022
45d224a
Improved fetching the result of selectCount
Nxllpointer Nov 2, 2022
8690fa2
Improved speed of checking if a user already bookmarked a channel
Nxllpointer Nov 2, 2022
bd48f13
Created helper method for createXxxEmbed methods
Nxllpointer Nov 2, 2022
6423368
Improved javadoc @param descriptions
Nxllpointer Nov 2, 2022
4822d23
Renamed replyEmbedEphemeral to sendResponse for improved readability
Nxllpointer Nov 2, 2022
75e0a17
Delayed fetching of the users bookmark count to avoid unnecessary dat…
Nxllpointer Nov 2, 2022
40ef298
Removed subtracting 1 from WARN/MAX_BOOKMARK_COUNT_TOTAL to improve r…
Nxllpointer Nov 2, 2022
eb641ca
Fix naming convention violation of delete_at to deleteAt
Nxllpointer Nov 2, 2022
83560c6
Improved bookmark limit warning and error log messages
Nxllpointer Nov 2, 2022
45b45ab
Made error messages more friendly
Nxllpointer Nov 2, 2022
d85294c
Improved command descriptions
Nxllpointer Nov 2, 2022
607c7c4
Changed to correct way of formatting log arguments
Nxllpointer Nov 2, 2022
5557e6e
Revert "Improved fetching the result of selectCount"
Nxllpointer Nov 2, 2022
3f7b3b1
Actually improved fetching the result of selectCount
Nxllpointer Nov 2, 2022
ebacfd2
Improved BookmarksSystem constructor javadoc
Nxllpointer Nov 2, 2022
f396f20
Improved the remove subcommand description
Nxllpointer Nov 2, 2022
f744054
Added the users bookmarks limit to the BOOKMARK_LIMIT_USER_EMBED fail…
Nxllpointer Nov 4, 2022
42313e2
Renamed ADD_OPTION_NOTE to ADD_BOOKMARK_NOTE_OPTION
Nxllpointer Nov 4, 2022
dbc6990
Fixed onSelectMenuSelection not ensuring the page index
Nxllpointer Nov 4, 2022
3017010
Improved BookmarksPaginatorInteractor javadoc
Nxllpointer Nov 4, 2022
1576d1a
Added BookmarksCommand javadoc
Nxllpointer Nov 4, 2022
32d1802
Moved into proper package
Zabuzard Nov 10, 2022
a6e621a
overall flow adjustments
Zabuzard Nov 10, 2022
e5ea39d
More improvements
Zabuzard Nov 10, 2022
f294c76
CR Nxllpointer
Zabuzard Nov 11, 2022
767e097
Renamings from CR
Zabuzard Nov 16, 2022
dd85945
Merge Zabuzards rework
Nxllpointer Nov 17, 2022
492eb08
Fixed bookmark limit reached soon log message
Nxllpointer Nov 18, 2022
8931049
Added command usage javadoc for BookmarksCommand
Nxllpointer Nov 18, 2022
7523123
Improved BookmarksSystem javadoc
Nxllpointer Nov 26, 2022
843df93
Merge branch 'develop' into feature/bookmarks
Nxllpointer Nov 26, 2022
4f413a4
Spotless after resolving conflicts
Nxllpointer Nov 26, 2022
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 @@ -6,6 +6,7 @@
import org.togetherjava.tjbot.commands.basic.RoleSelectCommand;
import org.togetherjava.tjbot.commands.basic.SuggestionsUpDownVoter;
import org.togetherjava.tjbot.commands.basic.VcActivityCommand;
import org.togetherjava.tjbot.commands.bookmarks.*;
import org.togetherjava.tjbot.commands.code.CodeMessageAutoDetection;
import org.togetherjava.tjbot.commands.code.CodeMessageHandler;
import org.togetherjava.tjbot.commands.code.CodeMessageManualDetection;
Expand Down Expand Up @@ -67,6 +68,7 @@ private Features() {
*/
public static Collection<Feature> createFeatures(JDA jda, Database database, Config config) {
TagSystem tagSystem = new TagSystem(database);
BookmarksSystem bookmarksSystem = new BookmarksSystem(config, database);
ModerationActionsStore actionsStore = new ModerationActionsStore(database);
ModAuditLogWriter modAuditLogWriter = new ModAuditLogWriter(config);
ScamHistoryStore scamHistoryStore = new ScamHistoryStore(database);
Expand All @@ -90,6 +92,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
features
.add(new AutoPruneHelperRoutine(config, helpSystemHelper, modAuditLogWriter, database));
features.add(new HelpThreadAutoArchiver(helpSystemHelper));
features.add(new LeftoverBookmarksCleanupRoutine(bookmarksSystem));

// Message receivers
features.add(new TopHelpersMessageListener(database, config));
Expand All @@ -107,6 +110,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
features.add(new RejoinModerationRoleListener(actionsStore, config));
features.add(new OnGuildLeaveCloseThreadListener(database));
features.add(new UserBannedDeleteRecentThreadsListener(database));
features.add(new LeftoverBookmarksListener(bookmarksSystem));

// Message context commands

Expand Down Expand Up @@ -139,6 +143,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
features.add(new ModMailCommand(jda, config));
features.add(new HelpThreadCommand(config, helpSystemHelper));
features.add(new ReportCommand(config));
features.add(new BookmarksCommand(bookmarksSystem));

// Mixtures
features.add(new HelpThreadOverviewUpdater(config, helpSystemHelper));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package org.togetherjava.tjbot.commands.bookmarks;

import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.togetherjava.tjbot.commands.CommandVisibility;
import org.togetherjava.tjbot.commands.SlashCommandAdapter;

import java.util.List;
import java.util.Objects;

/**
* The bookmarks command is used for managing and viewing bookmarks. A bookmark is a link to a help
* thread that can have a note so you can easily remember why you bookmarked a help thread. Writing
* to the database and showing the list/remove messages is not done by this class, that is handled
* by the {@link BookmarksSystem}. This class only checks if you are able to add a bookmark in the
* current channel and tells the {@link BookmarksSystem} to do the rest.
* <p>
* Usage:
*
* <pre>
* /bookmarks add [note]
* /bookmarks list
* /bookmarks remove
* </pre>
*/
public final class BookmarksCommand extends SlashCommandAdapter {

private static final Logger logger = LoggerFactory.getLogger(BookmarksCommand.class);

public static final String COMMAND_NAME = "bookmarks";
public static final String SUBCOMMAND_ADD = "add";
public static final String SUBCOMMAND_LIST = "list";
public static final String SUBCOMMAND_REMOVE = "remove";
public static final String ADD_BOOKMARK_NOTE_OPTION = "note";

private static final MessageEmbed NOT_A_HELP_THREAD_EMBED =
BookmarksSystem.createFailureEmbed("You can only bookmark help threads.");

private static final MessageEmbed ALREADY_BOOKMARKED_EMBED =
BookmarksSystem.createFailureEmbed("You have already bookmarked this channel.");

private static final MessageEmbed BOOKMARK_ADDED_EMBED =
BookmarksSystem.createSuccessEmbed("Your bookmark was added.");

private static final MessageEmbed BOOKMARK_LIMIT_USER_EMBED = BookmarksSystem
.createFailureEmbed(
"You have exceeded your bookmarks limit of `%d`. Please delete some of your other bookmarks."
.formatted(BookmarksSystem.MAX_BOOKMARK_COUNT_PER_USER));

private static final MessageEmbed BOOKMARK_LIMIT_TOTAL_EMBED = BookmarksSystem
.createWarningEmbed(
"""
You cannot add a bookmark right now because the total amount of bookmarks has exceeded its limit.
Please wait a bit until some of them have been deleted or contact a moderator.
Sorry for the inconvenience.
""");

private final BookmarksSystem bookmarksSystem;
private final BookmarksListRemoveHandler listRemoveHandler;

/**
* Creates a new instance and registers every sub command.
*
* @param bookmarksSystem The {@link BookmarksSystem} to request pagination and manage bookmarks
*/
public BookmarksCommand(BookmarksSystem bookmarksSystem) {
super(COMMAND_NAME, "Bookmark help threads so that you can easily look them up again",
CommandVisibility.GLOBAL);
this.bookmarksSystem = bookmarksSystem;
listRemoveHandler =
new BookmarksListRemoveHandler(bookmarksSystem, this::generateComponentId);

OptionData addNoteOption = new OptionData(OptionType.STRING, ADD_BOOKMARK_NOTE_OPTION,
"Your personal comment on this bookmark")
.setMaxLength(BookmarksSystem.MAX_NOTE_LENGTH)
.setRequired(false);

SubcommandData addSubCommand = new SubcommandData(SUBCOMMAND_ADD,
"Bookmark this help thread, so that you can easily look it up again")
.addOptions(addNoteOption);

SubcommandData listSubCommand =
new SubcommandData(SUBCOMMAND_LIST, "List all of your bookmarks");

SubcommandData removeSubCommand =
new SubcommandData(SUBCOMMAND_REMOVE, "Remove some of your bookmarks");

getData().addSubcommands(addSubCommand, listSubCommand, removeSubCommand);
}

@Override
public void onSlashCommand(SlashCommandInteractionEvent event) {
String subCommandName = Objects.requireNonNull(event.getSubcommandName());

switch (subCommandName) {
case SUBCOMMAND_ADD -> addBookmark(event);
case SUBCOMMAND_LIST -> listRemoveHandler.handleListRequest(event);
case SUBCOMMAND_REMOVE -> listRemoveHandler.handleRemoveRequest(event);
default -> throw new IllegalArgumentException("Unknown subcommand");
}
}

@Override
public void onButtonClick(ButtonInteractionEvent event, List<String> args) {
listRemoveHandler.onButtonClick(event, args);
}

@Override
public void onSelectMenuSelection(SelectMenuInteractionEvent event, List<String> args) {
listRemoveHandler.onSelectMenuSelection(event, args);
}

private void addBookmark(SlashCommandInteractionEvent event) {
long userID = event.getUser().getIdLong();
long channelID = event.getChannel().getIdLong();
String note = event.getOption(ADD_BOOKMARK_NOTE_OPTION, OptionMapping::getAsString);

if (!handleCanAddBookmark(event)) {
return;
}

bookmarksSystem.addBookmark(userID, channelID, note);

sendResponse(event, BOOKMARK_ADDED_EMBED);
}

private boolean handleCanAddBookmark(SlashCommandInteractionEvent event) {
MessageChannelUnion channel = event.getChannel();
long channelID = channel.getIdLong();
long userID = event.getUser().getIdLong();

if (!bookmarksSystem.isHelpThread(channel)) {
sendResponse(event, NOT_A_HELP_THREAD_EMBED);
return false;
}

if (bookmarksSystem.didUserBookmarkChannel(userID, channelID)) {
sendResponse(event, ALREADY_BOOKMARKED_EMBED);
return false;
}

long bookmarkCountTotal = bookmarksSystem.getTotalBookmarkCount();
if (bookmarkCountTotal == BookmarksSystem.WARN_BOOKMARK_COUNT_TOTAL) {
logger.warn("""
The bookmark limit will be reached soon (`{}/{}` bookmarks)!
If the limit is reached no new bookmarks can be added!
Please delete some bookmarks!
""", BookmarksSystem.WARN_BOOKMARK_COUNT_TOTAL,
BookmarksSystem.MAX_BOOKMARK_COUNT_TOTAL);
}
if (bookmarkCountTotal == BookmarksSystem.MAX_BOOKMARK_COUNT_TOTAL) {
logger.error("""
The bookmark limit of `{}` has been reached!
No new bookmarks can be added anymore!
Please delete some bookmarks!
""", BookmarksSystem.MAX_BOOKMARK_COUNT_TOTAL);
}
if (bookmarkCountTotal > BookmarksSystem.MAX_BOOKMARK_COUNT_TOTAL) {
sendResponse(event, BOOKMARK_LIMIT_TOTAL_EMBED);
return false;
}

long bookmarkCountUser = bookmarksSystem.getUserBookmarkCount(userID);
if (bookmarkCountUser >= BookmarksSystem.MAX_BOOKMARK_COUNT_PER_USER) {
sendResponse(event, BOOKMARK_LIMIT_USER_EMBED);
return false;
}

return true;
}

private void sendResponse(SlashCommandInteractionEvent event, MessageEmbed embed) {
event.replyEmbeds(embed).setEphemeral(true).queue();
}
}
Loading