[SIS-24] adding ability to only search quotes of a given parameter for a text

changing quote search to return a random matching quote
adding local function to tilt to update packages
This commit is contained in:
Sheldan
2023-09-03 00:48:15 +02:00
parent 7a37f7e040
commit a26503650e
6 changed files with 51 additions and 10 deletions

View File

@@ -21,6 +21,7 @@ import dev.sheldan.sissi.module.quotes.config.QuotesModuleDefinition;
import dev.sheldan.sissi.module.quotes.exception.QuoteNotFoundException;
import dev.sheldan.sissi.module.quotes.model.database.Quote;
import dev.sheldan.sissi.module.quotes.service.QuoteServiceBean;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.InteractionHook;
@@ -28,7 +29,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collections;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
@@ -38,6 +39,7 @@ public class QuoteSearch extends AbstractConditionableCommand {
private static final String QUOTE_SEARCH_COMMAND = "quoteSearch";
private static final String QUERY_PARAMETER = "query";
private static final String MEMBER_PARAMETER = "member";
@Autowired
private QuoteServiceBean quoteServiceBean;
@@ -80,7 +82,14 @@ public class QuoteSearch extends AbstractConditionableCommand {
String query = slashCommandParameterService.getCommandOption(QUERY_PARAMETER, event, String.class);
AServer server = serverManagementService.loadServer(event.getGuild().getIdLong());
Optional<Quote> possibleQuote = quoteServiceBean.searchQuote(query, server);
Optional<Quote> possibleQuote;
if(slashCommandParameterService.hasCommandOption(MEMBER_PARAMETER, event)) {
Member targetMember = slashCommandParameterService.getCommandOption(MEMBER_PARAMETER, event, Member.class);
possibleQuote = quoteServiceBean.searchQuote(query, server, targetMember);
} else {
possibleQuote = quoteServiceBean.searchQuote(query, server);
}
Quote quoteToDisplay = possibleQuote.orElseThrow(QuoteNotFoundException::new);
return quoteServiceBean.renderQuoteToMessageToSend(quoteToDisplay)
.thenCompose(messageToSend -> self.replyMessage(event, messageToSend))
@@ -100,7 +109,17 @@ public class QuoteSearch extends AbstractConditionableCommand {
.name(QUERY_PARAMETER)
.type(String.class)
.build();
List<Parameter> parameters = Collections.singletonList(searchParameter);
Parameter memberParameter = Parameter
.builder()
.templated(true)
.name(MEMBER_PARAMETER)
.slashCommandOnly(true)
.optional(true)
.type(Member.class)
.build();
List<Parameter> parameters = Arrays.asList(searchParameter, memberParameter);
HelpInfo helpInfo = HelpInfo
.builder()
.templated(true)

View File

@@ -12,6 +12,7 @@ import java.util.List;
@Repository
public interface QuoteRepository extends JpaRepository<Quote, ServerSpecificId> {
List<Quote> findByTextContainingAndServer(String text, AServer server);
List<Quote> findByTextContainingAndServerAndAuthor(String text, AServer server, AUserInAServer author);
List<Quote> findByServer(AServer server);
List<Quote> findByAuthor(AUserInAServer author);
Long countByAuthor(AUserInAServer author);

View File

@@ -214,9 +214,27 @@ public class QuoteServiceBean {
return Optional.empty();
}
if(foundQuotes.size() > 1) {
log.info("Found multiple quotes in server {}, returning first one.", server.getId());
log.info("Found multiple quotes in server {}, returning random one.", server.getId());
int randomIndex = secureRandom.nextInt(foundQuotes.size());
return Optional.of(foundQuotes.get(randomIndex));
} else {
return Optional.of(foundQuotes.get(0));
}
}
public Optional<Quote> searchQuote(String query, AServer server, Member targetMember) {
AUserInAServer author = userInServerManagementService.loadOrCreateUser(targetMember);
List<Quote> foundQuotes = quoteRepository.findByTextContainingAndServerAndAuthor(query, server, author);
if(foundQuotes.isEmpty()) {
return Optional.empty();
}
if(foundQuotes.size() > 1) {
log.info("Found multiple quotes in server {}, returning random one.", server.getId());
int randomIndex = secureRandom.nextInt(foundQuotes.size());
return Optional.of(foundQuotes.get(randomIndex));
} else {
return Optional.of(foundQuotes.get(0));
}
return Optional.of(foundQuotes.get(0));
}
public QuoteStatsModel getQuoteStats(Member member) {