|
| 1 | +package net.discordjug.javabot.systems.staff_commands.tags.commands; |
| 2 | + |
| 3 | +import java.sql.SQLException; |
| 4 | +import java.util.List; |
| 5 | +import java.util.concurrent.ExecutorService; |
| 6 | +import java.util.stream.Collectors; |
| 7 | + |
| 8 | +import net.discordjug.javabot.data.config.BotConfig; |
| 9 | +import net.discordjug.javabot.systems.staff_commands.tags.dao.CustomTagRepository; |
| 10 | +import net.discordjug.javabot.systems.staff_commands.tags.model.CustomTag; |
| 11 | +import net.discordjug.javabot.util.ExceptionLogger; |
| 12 | +import net.discordjug.javabot.util.Responses; |
| 13 | +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; |
| 14 | +import net.dv8tion.jda.api.interactions.commands.OptionMapping; |
| 15 | +import net.dv8tion.jda.api.interactions.commands.OptionType; |
| 16 | +import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; |
| 17 | +import net.dv8tion.jda.api.requests.restaction.interactions.InteractionCallbackAction; |
| 18 | +import net.dv8tion.jda.api.utils.MarkdownUtil; |
| 19 | +import org.jetbrains.annotations.NotNull; |
| 20 | +import org.springframework.dao.DataAccessException; |
| 21 | + |
| 22 | +/** |
| 23 | + * The /tag search command which lists all tags matching a query. |
| 24 | + */ |
| 25 | +public class TagSearchSubcommand extends TagsSubcommand { |
| 26 | + |
| 27 | + private final ExecutorService asyncPool; |
| 28 | + private final CustomTagRepository customTagRepository; |
| 29 | + |
| 30 | + /** |
| 31 | + * The constructor of this class, which sets the corresponding {@link net.dv8tion.jda.api.interactions.commands.build.SlashCommandData}. |
| 32 | + * @param botConfig The main configuration of the bot. |
| 33 | + * @param asyncPool Thread pool for asynchronous operations. |
| 34 | + * @param customTagRepository The repository for accessing tags in the database. |
| 35 | + */ |
| 36 | + public TagSearchSubcommand(BotConfig botConfig, ExecutorService asyncPool, CustomTagRepository customTagRepository) { |
| 37 | + super(botConfig); |
| 38 | + this.asyncPool = asyncPool; |
| 39 | + this.customTagRepository = customTagRepository; |
| 40 | + setCommandData( |
| 41 | + new SubcommandData("search", "Searches for tags using a query") |
| 42 | + .addOption(OptionType.STRING, "query", "The search query", true)); |
| 43 | + setRequiredStaff(false); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + protected InteractionCallbackAction<?> handleCustomTagsSubcommand(@NotNull SlashCommandInteractionEvent event) |
| 48 | + throws SQLException { |
| 49 | + |
| 50 | + String query = event.getOption("query", "", OptionMapping::getAsString); |
| 51 | + |
| 52 | + asyncPool.execute(()->{ |
| 53 | + try { |
| 54 | + List<CustomTag> tags = customTagRepository.search(event.getGuild().getIdLong(), query); |
| 55 | + String tagList = tags |
| 56 | + .stream() |
| 57 | + .map(CustomTag::getName) |
| 58 | + .map(MarkdownUtil::monospace) |
| 59 | + .collect(Collectors.joining(", ")); |
| 60 | + Responses.info(event.getHook(), "Custom tags containing \"" + query + "\"", |
| 61 | + String.format(tagList.length() > 0 ? tagList : "No Custom Tags have been found.")).queue(); |
| 62 | + } catch (DataAccessException e) { |
| 63 | + ExceptionLogger.capture(e, TagListSubcommand.class.getSimpleName()); |
| 64 | + Responses.error(event.getHook(), "An error occured trying to search for tags").queue(); |
| 65 | + } |
| 66 | + }); |
| 67 | + return event.deferReply(false); |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments