-
-
Notifications
You must be signed in to change notification settings - Fork 101
Bookmark Command #634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Zabuzard
merged 68 commits into
Together-Java:develop
from
Nxllpointer:feature/bookmarks
Nov 29, 2022
Merged
Bookmark Command #634
Changes from all commits
Commits
Show all changes
68 commits
Select commit
Hold shift + click to select a range
b5cc451
Added bookmark command
Nxllpointer 6bf58ae
Made bookmarks feature sonarlint compliant
Nxllpointer 681b687
Removed BookmarksCommand section comments
Nxllpointer 2c99b97
Sort bookmarks by latest activity
Nxllpointer 6345d6b
Made final embed creations in BookmarksCommand more readable
Nxllpointer b910a8f
Revert all changes for rewrite
Nxllpointer 4a165e6
Full rewrite of the bookmarks feature
Nxllpointer 9b813fa
Added length limit for note option
Nxllpointer 04d6c3b
Added bookmarks limit to prevent spam
Nxllpointer 365fad7
Created replyEmbedEphemeral method because all messages are ephemeral…
Nxllpointer 3796929
Changed remove-select-menu to display thread name if the thread was f…
Nxllpointer ce00b8a
Added guild-leave bookmark-removal-scheduling
Nxllpointer 6d39fa0
Fixed requested changes for version 1 that still applied to the rewrite
Nxllpointer 5ce5aa4
Added missing javadoc dots
Nxllpointer 7095af9
Fixed issue with deleting all bookmarks
Nxllpointer d950598
Converted unnecessary multiline Strings to normal Strings
Nxllpointer 907a248
Split createTinyEmbed into separate methods for every color
Nxllpointer a7f5402
Rename BookmarkPaginatorInteractor to BookmarksPaginatorInteractor
Nxllpointer 47b5866
Add javadoc for BookmarksPaginatorInteractor
Nxllpointer 9b09c31
Don't overwrite args in onSelectMenuSelection
Nxllpointer ae12506
Removed generatePageEmbed overloading for readability
Nxllpointer 7d10c7d
Renamed highestPageIndex to lastPageIndex in generatePageEmbed
Nxllpointer d7bb301
Removed newline to improve readability in generatePageEmbed
Nxllpointer 710e841
Removed unnecessary comments in generatePageEmbed
Nxllpointer 94ae092
Renamed uninformative variable from b to bookmark when creating PageE…
Nxllpointer 7204881
Added emptiness check to getLastPageIndex to avoid issues in the future
Nxllpointer a02fc5d
Fixed ComponentArguments and RemoveComponentArguments having fromStri…
Nxllpointer 01ecc5a
Improved the list subcommand description
Nxllpointer 6a1009e
Renamed LEAVE_REMOVAL_DELAY to LEAVE_BOOKMARKS_REMOVAL_DELAY
Nxllpointer 6ce813b
Changed requestListPagination and requestRemovePagination to use Obje…
Nxllpointer d4f029b
Renamed SCHEDULED_REMOVAL_AT to DELETE_AT
Nxllpointer 4ad21b0
Improve naming about deleting old bookmarks of users that left the guild
Nxllpointer d043b26
Improve naming of MAX_BOOKMARK_COUNT_USER to MAX_BOOKMARK_COUNT_PER_USER
Nxllpointer 5242d9f
Improve naming of LEAVE_BOOKMARKS_REMOVAL_DELAY to REMOVE_BOOKMARKS_A…
Nxllpointer 042b0b8
Changed constants to be package private instead of public
Nxllpointer 29752cb
Improve speed by using a Set for bookmarksToRemoveChannelIDs
Nxllpointer 45d224a
Improved fetching the result of selectCount
Nxllpointer 8690fa2
Improved speed of checking if a user already bookmarked a channel
Nxllpointer bd48f13
Created helper method for createXxxEmbed methods
Nxllpointer 6423368
Improved javadoc @param descriptions
Nxllpointer 4822d23
Renamed replyEmbedEphemeral to sendResponse for improved readability
Nxllpointer 75e0a17
Delayed fetching of the users bookmark count to avoid unnecessary dat…
Nxllpointer 40ef298
Removed subtracting 1 from WARN/MAX_BOOKMARK_COUNT_TOTAL to improve r…
Nxllpointer eb641ca
Fix naming convention violation of delete_at to deleteAt
Nxllpointer 83560c6
Improved bookmark limit warning and error log messages
Nxllpointer 45b45ab
Made error messages more friendly
Nxllpointer d85294c
Improved command descriptions
Nxllpointer 607c7c4
Changed to correct way of formatting log arguments
Nxllpointer 5557e6e
Revert "Improved fetching the result of selectCount"
Nxllpointer 3f7b3b1
Actually improved fetching the result of selectCount
Nxllpointer ebacfd2
Improved BookmarksSystem constructor javadoc
Nxllpointer f396f20
Improved the remove subcommand description
Nxllpointer f744054
Added the users bookmarks limit to the BOOKMARK_LIMIT_USER_EMBED fail…
Nxllpointer 42313e2
Renamed ADD_OPTION_NOTE to ADD_BOOKMARK_NOTE_OPTION
Nxllpointer dbc6990
Fixed onSelectMenuSelection not ensuring the page index
Nxllpointer 3017010
Improved BookmarksPaginatorInteractor javadoc
Nxllpointer 1576d1a
Added BookmarksCommand javadoc
Nxllpointer 32d1802
Moved into proper package
Zabuzard a6e621a
overall flow adjustments
Zabuzard e5ea39d
More improvements
Zabuzard f294c76
CR Nxllpointer
Zabuzard 767e097
Renamings from CR
Zabuzard dd85945
Merge Zabuzards rework
Nxllpointer 492eb08
Fixed bookmark limit reached soon log message
Nxllpointer 8931049
Added command usage javadoc for BookmarksCommand
Nxllpointer 7523123
Improved BookmarksSystem javadoc
Nxllpointer 843df93
Merge branch 'develop' into feature/bookmarks
Nxllpointer 4f413a4
Spotless after resolving conflicts
Nxllpointer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
application/src/main/java/org/togetherjava/tjbot/commands/bookmarks/BookmarksCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.