|
| 1 | +package org.togetherjava.tjbot.commands.bookmarks; |
| 2 | + |
| 3 | +import net.dv8tion.jda.api.entities.MessageEmbed; |
| 4 | +import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion; |
| 5 | +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; |
| 6 | +import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; |
| 7 | +import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent; |
| 8 | +import net.dv8tion.jda.api.interactions.commands.OptionMapping; |
| 9 | +import net.dv8tion.jda.api.interactions.commands.OptionType; |
| 10 | +import net.dv8tion.jda.api.interactions.commands.build.OptionData; |
| 11 | +import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; |
| 12 | +import org.slf4j.Logger; |
| 13 | +import org.slf4j.LoggerFactory; |
| 14 | + |
| 15 | +import org.togetherjava.tjbot.commands.CommandVisibility; |
| 16 | +import org.togetherjava.tjbot.commands.SlashCommandAdapter; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | +import java.util.Objects; |
| 20 | + |
| 21 | +/** |
| 22 | + * The bookmarks command is used for managing and viewing bookmarks. A bookmark is a link to a help |
| 23 | + * thread that can have a note so you can easily remember why you bookmarked a help thread. Writing |
| 24 | + * to the database and showing the list/remove messages is not done by this class, that is handled |
| 25 | + * by the {@link BookmarksSystem}. This class only checks if you are able to add a bookmark in the |
| 26 | + * current channel and tells the {@link BookmarksSystem} to do the rest. |
| 27 | + * <p> |
| 28 | + * Usage: |
| 29 | + * |
| 30 | + * <pre> |
| 31 | + * /bookmarks add [note] |
| 32 | + * /bookmarks list |
| 33 | + * /bookmarks remove |
| 34 | + * </pre> |
| 35 | + */ |
| 36 | +public final class BookmarksCommand extends SlashCommandAdapter { |
| 37 | + |
| 38 | + private static final Logger logger = LoggerFactory.getLogger(BookmarksCommand.class); |
| 39 | + |
| 40 | + public static final String COMMAND_NAME = "bookmarks"; |
| 41 | + public static final String SUBCOMMAND_ADD = "add"; |
| 42 | + public static final String SUBCOMMAND_LIST = "list"; |
| 43 | + public static final String SUBCOMMAND_REMOVE = "remove"; |
| 44 | + public static final String ADD_BOOKMARK_NOTE_OPTION = "note"; |
| 45 | + |
| 46 | + private static final MessageEmbed NOT_A_HELP_THREAD_EMBED = |
| 47 | + BookmarksSystem.createFailureEmbed("You can only bookmark help threads."); |
| 48 | + |
| 49 | + private static final MessageEmbed ALREADY_BOOKMARKED_EMBED = |
| 50 | + BookmarksSystem.createFailureEmbed("You have already bookmarked this channel."); |
| 51 | + |
| 52 | + private static final MessageEmbed BOOKMARK_ADDED_EMBED = |
| 53 | + BookmarksSystem.createSuccessEmbed("Your bookmark was added."); |
| 54 | + |
| 55 | + private static final MessageEmbed BOOKMARK_LIMIT_USER_EMBED = BookmarksSystem |
| 56 | + .createFailureEmbed( |
| 57 | + "You have exceeded your bookmarks limit of `%d`. Please delete some of your other bookmarks." |
| 58 | + .formatted(BookmarksSystem.MAX_BOOKMARK_COUNT_PER_USER)); |
| 59 | + |
| 60 | + private static final MessageEmbed BOOKMARK_LIMIT_TOTAL_EMBED = BookmarksSystem |
| 61 | + .createWarningEmbed( |
| 62 | + """ |
| 63 | + You cannot add a bookmark right now because the total amount of bookmarks has exceeded its limit. |
| 64 | + Please wait a bit until some of them have been deleted or contact a moderator. |
| 65 | + Sorry for the inconvenience. |
| 66 | + """); |
| 67 | + |
| 68 | + private final BookmarksSystem bookmarksSystem; |
| 69 | + private final BookmarksListRemoveHandler listRemoveHandler; |
| 70 | + |
| 71 | + /** |
| 72 | + * Creates a new instance and registers every sub command. |
| 73 | + * |
| 74 | + * @param bookmarksSystem The {@link BookmarksSystem} to request pagination and manage bookmarks |
| 75 | + */ |
| 76 | + public BookmarksCommand(BookmarksSystem bookmarksSystem) { |
| 77 | + super(COMMAND_NAME, "Bookmark help threads so that you can easily look them up again", |
| 78 | + CommandVisibility.GLOBAL); |
| 79 | + this.bookmarksSystem = bookmarksSystem; |
| 80 | + listRemoveHandler = |
| 81 | + new BookmarksListRemoveHandler(bookmarksSystem, this::generateComponentId); |
| 82 | + |
| 83 | + OptionData addNoteOption = new OptionData(OptionType.STRING, ADD_BOOKMARK_NOTE_OPTION, |
| 84 | + "Your personal comment on this bookmark") |
| 85 | + .setMaxLength(BookmarksSystem.MAX_NOTE_LENGTH) |
| 86 | + .setRequired(false); |
| 87 | + |
| 88 | + SubcommandData addSubCommand = new SubcommandData(SUBCOMMAND_ADD, |
| 89 | + "Bookmark this help thread, so that you can easily look it up again") |
| 90 | + .addOptions(addNoteOption); |
| 91 | + |
| 92 | + SubcommandData listSubCommand = |
| 93 | + new SubcommandData(SUBCOMMAND_LIST, "List all of your bookmarks"); |
| 94 | + |
| 95 | + SubcommandData removeSubCommand = |
| 96 | + new SubcommandData(SUBCOMMAND_REMOVE, "Remove some of your bookmarks"); |
| 97 | + |
| 98 | + getData().addSubcommands(addSubCommand, listSubCommand, removeSubCommand); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void onSlashCommand(SlashCommandInteractionEvent event) { |
| 103 | + String subCommandName = Objects.requireNonNull(event.getSubcommandName()); |
| 104 | + |
| 105 | + switch (subCommandName) { |
| 106 | + case SUBCOMMAND_ADD -> addBookmark(event); |
| 107 | + case SUBCOMMAND_LIST -> listRemoveHandler.handleListRequest(event); |
| 108 | + case SUBCOMMAND_REMOVE -> listRemoveHandler.handleRemoveRequest(event); |
| 109 | + default -> throw new IllegalArgumentException("Unknown subcommand"); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void onButtonClick(ButtonInteractionEvent event, List<String> args) { |
| 115 | + listRemoveHandler.onButtonClick(event, args); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void onSelectMenuSelection(SelectMenuInteractionEvent event, List<String> args) { |
| 120 | + listRemoveHandler.onSelectMenuSelection(event, args); |
| 121 | + } |
| 122 | + |
| 123 | + private void addBookmark(SlashCommandInteractionEvent event) { |
| 124 | + long userID = event.getUser().getIdLong(); |
| 125 | + long channelID = event.getChannel().getIdLong(); |
| 126 | + String note = event.getOption(ADD_BOOKMARK_NOTE_OPTION, OptionMapping::getAsString); |
| 127 | + |
| 128 | + if (!handleCanAddBookmark(event)) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + bookmarksSystem.addBookmark(userID, channelID, note); |
| 133 | + |
| 134 | + sendResponse(event, BOOKMARK_ADDED_EMBED); |
| 135 | + } |
| 136 | + |
| 137 | + private boolean handleCanAddBookmark(SlashCommandInteractionEvent event) { |
| 138 | + MessageChannelUnion channel = event.getChannel(); |
| 139 | + long channelID = channel.getIdLong(); |
| 140 | + long userID = event.getUser().getIdLong(); |
| 141 | + |
| 142 | + if (!bookmarksSystem.isHelpThread(channel)) { |
| 143 | + sendResponse(event, NOT_A_HELP_THREAD_EMBED); |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
| 147 | + if (bookmarksSystem.didUserBookmarkChannel(userID, channelID)) { |
| 148 | + sendResponse(event, ALREADY_BOOKMARKED_EMBED); |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + long bookmarkCountTotal = bookmarksSystem.getTotalBookmarkCount(); |
| 153 | + if (bookmarkCountTotal == BookmarksSystem.WARN_BOOKMARK_COUNT_TOTAL) { |
| 154 | + logger.warn(""" |
| 155 | + The bookmark limit will be reached soon (`{}/{}` bookmarks)! |
| 156 | + If the limit is reached no new bookmarks can be added! |
| 157 | + Please delete some bookmarks! |
| 158 | + """, BookmarksSystem.WARN_BOOKMARK_COUNT_TOTAL, |
| 159 | + BookmarksSystem.MAX_BOOKMARK_COUNT_TOTAL); |
| 160 | + } |
| 161 | + if (bookmarkCountTotal == BookmarksSystem.MAX_BOOKMARK_COUNT_TOTAL) { |
| 162 | + logger.error(""" |
| 163 | + The bookmark limit of `{}` has been reached! |
| 164 | + No new bookmarks can be added anymore! |
| 165 | + Please delete some bookmarks! |
| 166 | + """, BookmarksSystem.MAX_BOOKMARK_COUNT_TOTAL); |
| 167 | + } |
| 168 | + if (bookmarkCountTotal > BookmarksSystem.MAX_BOOKMARK_COUNT_TOTAL) { |
| 169 | + sendResponse(event, BOOKMARK_LIMIT_TOTAL_EMBED); |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + long bookmarkCountUser = bookmarksSystem.getUserBookmarkCount(userID); |
| 174 | + if (bookmarkCountUser >= BookmarksSystem.MAX_BOOKMARK_COUNT_PER_USER) { |
| 175 | + sendResponse(event, BOOKMARK_LIMIT_USER_EMBED); |
| 176 | + return false; |
| 177 | + } |
| 178 | + |
| 179 | + return true; |
| 180 | + } |
| 181 | + |
| 182 | + private void sendResponse(SlashCommandInteractionEvent event, MessageEmbed embed) { |
| 183 | + event.replyEmbeds(embed).setEphemeral(true).queue(); |
| 184 | + } |
| 185 | +} |
0 commit comments