Implemented a better way to parse options for the Quote command

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-11-03 23:49:13 +01:00
parent 07d094add9
commit ebd37fa1b5
4 changed files with 47 additions and 22 deletions

View File

@ -34,6 +34,7 @@ dependencies {
implementation 'com.discord4j:discord4j-core:3.1.1' implementation 'com.discord4j:discord4j-core:3.1.1'
implementation 'com.github.wtekiela:opensub4j:0.3.0' implementation 'com.github.wtekiela:opensub4j:0.3.0'
implementation 'org.apache.commons:commons-lang3:3.11' implementation 'org.apache.commons:commons-lang3:3.11'
implementation 'commons-cli:commons-cli:1.4'
implementation 'org.apache.commons:commons-csv:1.8' implementation 'org.apache.commons:commons-csv:1.8'
implementation 'ch.qos.logback:logback-classic:1.2.3' implementation 'ch.qos.logback:logback-classic:1.2.3'
implementation 'org.postgresql:postgresql:42.2.18.jre7' implementation 'org.postgresql:postgresql:42.2.18.jre7'

View File

@ -5,6 +5,7 @@ import discord4j.core.event.domain.message.MessageCreateEvent;
import discord4j.core.object.reaction.ReactionEmoji; import discord4j.core.object.reaction.ReactionEmoji;
import discord4j.core.spec.EmbedCreateSpec; import discord4j.core.spec.EmbedCreateSpec;
import discord4j.rest.util.Color; import discord4j.rest.util.Color;
import org.apache.commons.cli.Options;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@ -29,6 +30,7 @@ public abstract class Command {
protected final String usage; protected final String usage;
protected final int minArgs; protected final int minArgs;
protected final int maxArgs; protected final int maxArgs;
protected final Options options;
protected Command(String name, String description, String usage, int minArgs, int maxArgs) { protected Command(String name, String description, String usage, int minArgs, int maxArgs) {
this.name = name; this.name = name;
@ -36,6 +38,7 @@ public abstract class Command {
this.usage = usage; this.usage = usage;
this.minArgs = minArgs; this.minArgs = minArgs;
this.maxArgs = maxArgs; this.maxArgs = maxArgs;
this.options = new Options();
} }
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) { public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
@ -85,6 +88,13 @@ public abstract class Command {
.then(); .then();
} }
protected Mono<Void> parsingError(MessageCreateEvent event) {
return event.getMessage().getChannel()
.flatMap(channel ->
channel.createMessage("An error occurred while parsing your command. Usage: ``" + getUsage() + "``."))
.then();
}
protected void createEmbedListLang(String arg, EmbedCreateSpec embed, boolean isId) { protected void createEmbedListLang(String arg, EmbedCreateSpec embed, boolean isId) {
try { try {
Movie movie = OMDBClient.getMovie(arg, isId); Movie movie = OMDBClient.getMovie(arg, isId);

View File

@ -2,6 +2,12 @@ package xyz.vallat.louis.commands;
import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.core.event.domain.message.MessageCreateEvent;
import discord4j.rest.util.Color; import discord4j.rest.util.Color;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import xyz.vallat.louis.database.LanguageManager; import xyz.vallat.louis.database.LanguageManager;
import xyz.vallat.louis.database.SubtitleLineManager; import xyz.vallat.louis.database.SubtitleLineManager;
@ -9,37 +15,44 @@ import xyz.vallat.louis.subtitles.dao.FilmQuote;
import xyz.vallat.louis.subtitles.dao.Lang; import xyz.vallat.louis.subtitles.dao.Lang;
import java.time.Instant; import java.time.Instant;
import java.util.List;
public class Quote extends Command { public class Quote extends Command {
private static final Logger logger = LoggerFactory.getLogger(Quote.class.getCanonicalName());
public Quote(String name) { public Quote(String name) {
super(name, "Get a random quote from any movie.", name + " [lang]", 0, 1); super(name, "Get a random quote from any movie.", name + " [-l|--lang <lang>]", 0, 1);
options.addOption(Option.builder("l").longOpt("lang").desc("specify a language").hasArg().build());
} }
@Override @Override
public Mono<Void> execute(MessageCreateEvent event) { public Mono<Void> execute(MessageCreateEvent event) {
List<String> args = getArguments(event); try {
Lang language = LanguageManager.getLangFromAny(args.isEmpty() ? "english" : args.get(0)); CommandLine cmd = new DefaultParser().parse(options,
return event.getMessage().getChannel().flatMap(messageChannel -> messageChannel.createEmbed(embed -> { event.getMessage().getContent().substring(name.length()).split(" "));
embed.setTitle("Quote").setColor(Color.RED); Lang language = LanguageManager.getLangFromAny(cmd.hasOption("l") ? cmd.getOptionValue("l") : "english");
if (language == null) return event.getMessage().getChannel().flatMap(messageChannel -> messageChannel.createEmbed(embed -> {
embed.setDescription("This language is unknown. Try again with another one. Or don't try at all."); embed.setTitle("Quote").setColor(Color.RED);
else { if (language == null)
FilmQuote quote = SubtitleLineManager.getRandomLine(language); embed.setDescription("This language is unknown. Try again with another one. Or don't try at all.");
if (quote == null)
embed.setDescription("We don't have any quote in that language right now! Sorry!").setColor(Color.ORANGE);
else { else {
embed.setDescription(quote.getSubtitleBlock().getDialogue() FilmQuote quote = SubtitleLineManager.getRandomLine(language);
.replaceAll("<i>|</i>", "*") if (quote == null)
.replaceAll("<b>|</b>", "**") embed.setDescription("We don't have any quote in that language right now! Sorry!").setColor(Color.ORANGE);
); else {
embed.setFooter(quote.getMovie().toString(), null); embed.setDescription(quote.getSubtitleBlock().getDialogue()
embed.setColor(Color.MEDIUM_SEA_GREEN); .replaceAll("<i>|</i>", "*")
.replaceAll("<b>|</b>", "**")
);
embed.setFooter(quote.getMovie().toString(), null);
embed.setColor(Color.MEDIUM_SEA_GREEN);
}
} }
} embed.setTimestamp(Instant.now());
embed.setTimestamp(Instant.now()); })).then();
})).then(); } catch (ParseException e) {
logger.error("Parsing error: {}", e.getMessage());
return parsingError(event);
}
} }
} }

View File

@ -5,6 +5,7 @@ import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord; import org.apache.commons.csv.CSVRecord;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import reactor.util.annotation.NonNull;
import xyz.vallat.louis.codes.ExitCodes; import xyz.vallat.louis.codes.ExitCodes;
import xyz.vallat.louis.subtitles.dao.Lang; import xyz.vallat.louis.subtitles.dao.Lang;
@ -103,7 +104,7 @@ public final class LanguageManager {
} }
} }
public static Lang getLangFromAny(String name) { public static Lang getLangFromAny(@NonNull String name) {
Lang lang = null; Lang lang = null;
name = name.toLowerCase(); name = name.toLowerCase();
try (Connection connection = DBManager.getConnection()) { try (Connection connection = DBManager.getConnection()) {