Added search capability in the quote command
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
07f3d76502
commit
c830abcb4d
@ -19,12 +19,15 @@ public class Quote extends Command {
|
||||
|
||||
public Quote(String name) {
|
||||
super(name, "Get a random quote from any movie.", name + " [-l|--lang <lang>] " +
|
||||
"[-i|--imdb imdb_identifier]|[-t|--title title]", 0, 1);
|
||||
"[-s|--search <quote extract>] [-i|--imdb imdb_identifier]|[-t|--title title]", 0, 1);
|
||||
OptionGroup iOrT = new OptionGroup()
|
||||
.addOption(Option.builder("i").longOpt("imdb").desc("specify an IMDB identifier").hasArg().build())
|
||||
.addOption(Option.builder("t").longOpt("title").desc("specify a title").hasArgs()
|
||||
.numberOfArgs(Option.UNLIMITED_VALUES).build());
|
||||
options.addOption(Option.builder("l").longOpt("lang").desc("specify a language (by default, english)").hasArg().build());
|
||||
options.addOption(Option.builder("l").longOpt("lang").desc("specify a language (by default, english)")
|
||||
.hasArg().build());
|
||||
options.addOption(Option.builder("s").longOpt("search").desc("specify a part of the quote").hasArgs()
|
||||
.numberOfArgs(Option.UNLIMITED_VALUES).build());
|
||||
options.addOptionGroup(iOrT);
|
||||
}
|
||||
|
||||
@ -36,12 +39,13 @@ public class Quote extends Command {
|
||||
Lang language = LanguageManager.getLangFromAny(cmd.hasOption("l") ? cmd.getOptionValue("l") : "english");
|
||||
String imdb = cmd.hasOption("i") ? cmd.getOptionValue("i") : null;
|
||||
String title = cmd.hasOption("t") ? String.join(" ", cmd.getOptionValues("t")) : null;
|
||||
String search = cmd.hasOption("s") ? String.join(" ", cmd.getOptionValues("s")) : null;
|
||||
return event.getMessage().getChannel().flatMap(messageChannel -> messageChannel.createEmbed(embed -> {
|
||||
embed.setTitle("Quote").setColor(Color.RED);
|
||||
if (language == null)
|
||||
embed.setDescription("This language is unknown. Try again with another one. Or don't try at all.");
|
||||
else {
|
||||
FilmQuote quote = SubtitleLineManager.getRandomLine(language, imdb, title);
|
||||
FilmQuote quote = SubtitleLineManager.getRandomLine(language, imdb, title, search);
|
||||
if (quote == null)
|
||||
embed.setDescription("We don't have any quote in that language right now! Sorry!").setColor(Color.ORANGE);
|
||||
else {
|
||||
|
@ -115,21 +115,24 @@ public final class SubtitleLineManager {
|
||||
}
|
||||
}
|
||||
|
||||
public static FilmQuote getRandomLine(Lang language, String imdb, String title) {
|
||||
public static FilmQuote getRandomLine(Lang language, String imdb, String title, String search) {
|
||||
FilmQuote filmQuote = null;
|
||||
try (Connection connection = DBManager.getConnection()) {
|
||||
String query = "SELECT imdb_id, dialog_line, time_code FROM subtitle_lines " +
|
||||
"INNER JOIN subtitles ON subtitles.id = subtitle_lines.subtitle_id " +
|
||||
"INNER JOIN films ON subtitles.film_id = films.id " +
|
||||
"WHERE subtitles.language_id = ? " +
|
||||
(search != null ? "AND regexp_replace(dialog_line, E'[\\\\n\\\\r]+', ' ', 'g' ) LIKE ? " : "") +
|
||||
(imdb != null ? "AND films.imdb_id = ? " : "") +
|
||||
(title != null ? "AND UPPER(films.title) = UPPER(?) " : "") +
|
||||
"ORDER BY RANDOM()" +
|
||||
"LIMIT 1;";
|
||||
try (PreparedStatement stmt = connection.prepareStatement(query)) {
|
||||
stmt.setInt(1, language.getId());
|
||||
if (imdb != null) stmt.setString(2, imdb);
|
||||
if (title != null) stmt.setString((imdb != null ? 3 : 2), title);
|
||||
int position = 0;
|
||||
stmt.setInt(++position, language.getId());
|
||||
if (search != null) stmt.setString(++position, "%" + search + "%");
|
||||
if (imdb != null) stmt.setString(++position, imdb);
|
||||
if (title != null) stmt.setString(++position, title);
|
||||
stmt.execute();
|
||||
if (stmt.getResultSet().next()) {
|
||||
Movie movie = FilmManager.getMovieFromTitleOrImdbId(
|
||||
|
Loading…
Reference in New Issue
Block a user