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:
parent
07d094add9
commit
ebd37fa1b5
@ -34,6 +34,7 @@ dependencies {
|
||||
implementation 'com.discord4j:discord4j-core:3.1.1'
|
||||
implementation 'com.github.wtekiela:opensub4j:0.3.0'
|
||||
implementation 'org.apache.commons:commons-lang3:3.11'
|
||||
implementation 'commons-cli:commons-cli:1.4'
|
||||
implementation 'org.apache.commons:commons-csv:1.8'
|
||||
implementation 'ch.qos.logback:logback-classic:1.2.3'
|
||||
implementation 'org.postgresql:postgresql:42.2.18.jre7'
|
||||
|
@ -5,6 +5,7 @@ import discord4j.core.event.domain.message.MessageCreateEvent;
|
||||
import discord4j.core.object.reaction.ReactionEmoji;
|
||||
import discord4j.core.spec.EmbedCreateSpec;
|
||||
import discord4j.rest.util.Color;
|
||||
import org.apache.commons.cli.Options;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import reactor.core.publisher.Mono;
|
||||
@ -29,6 +30,7 @@ public abstract class Command {
|
||||
protected final String usage;
|
||||
protected final int minArgs;
|
||||
protected final int maxArgs;
|
||||
protected final Options options;
|
||||
|
||||
protected Command(String name, String description, String usage, int minArgs, int maxArgs) {
|
||||
this.name = name;
|
||||
@ -36,6 +38,7 @@ public abstract class Command {
|
||||
this.usage = usage;
|
||||
this.minArgs = minArgs;
|
||||
this.maxArgs = maxArgs;
|
||||
this.options = new Options();
|
||||
}
|
||||
|
||||
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
|
||||
@ -85,6 +88,13 @@ public abstract class Command {
|
||||
.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) {
|
||||
try {
|
||||
Movie movie = OMDBClient.getMovie(arg, isId);
|
||||
|
@ -2,6 +2,12 @@ package xyz.vallat.louis.commands;
|
||||
|
||||
import discord4j.core.event.domain.message.MessageCreateEvent;
|
||||
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 xyz.vallat.louis.database.LanguageManager;
|
||||
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 java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
public class Quote extends Command {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(Quote.class.getCanonicalName());
|
||||
|
||||
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
|
||||
public Mono<Void> execute(MessageCreateEvent event) {
|
||||
List<String> args = getArguments(event);
|
||||
Lang language = LanguageManager.getLangFromAny(args.isEmpty() ? "english" : args.get(0));
|
||||
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);
|
||||
if (quote == null)
|
||||
embed.setDescription("We don't have any quote in that language right now! Sorry!").setColor(Color.ORANGE);
|
||||
try {
|
||||
CommandLine cmd = new DefaultParser().parse(options,
|
||||
event.getMessage().getContent().substring(name.length()).split(" "));
|
||||
Lang language = LanguageManager.getLangFromAny(cmd.hasOption("l") ? cmd.getOptionValue("l") : "english");
|
||||
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 {
|
||||
embed.setDescription(quote.getSubtitleBlock().getDialogue()
|
||||
.replaceAll("<i>|</i>", "*")
|
||||
.replaceAll("<b>|</b>", "**")
|
||||
);
|
||||
embed.setFooter(quote.getMovie().toString(), null);
|
||||
embed.setColor(Color.MEDIUM_SEA_GREEN);
|
||||
FilmQuote quote = SubtitleLineManager.getRandomLine(language);
|
||||
if (quote == null)
|
||||
embed.setDescription("We don't have any quote in that language right now! Sorry!").setColor(Color.ORANGE);
|
||||
else {
|
||||
embed.setDescription(quote.getSubtitleBlock().getDialogue()
|
||||
.replaceAll("<i>|</i>", "*")
|
||||
.replaceAll("<b>|</b>", "**")
|
||||
);
|
||||
embed.setFooter(quote.getMovie().toString(), null);
|
||||
embed.setColor(Color.MEDIUM_SEA_GREEN);
|
||||
}
|
||||
}
|
||||
}
|
||||
embed.setTimestamp(Instant.now());
|
||||
})).then();
|
||||
embed.setTimestamp(Instant.now());
|
||||
})).then();
|
||||
} catch (ParseException e) {
|
||||
logger.error("Parsing error: {}", e.getMessage());
|
||||
return parsingError(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import org.apache.commons.csv.CSVParser;
|
||||
import org.apache.commons.csv.CSVRecord;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import reactor.util.annotation.NonNull;
|
||||
import xyz.vallat.louis.codes.ExitCodes;
|
||||
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;
|
||||
name = name.toLowerCase();
|
||||
try (Connection connection = DBManager.getConnection()) {
|
||||
|
Loading…
Reference in New Issue
Block a user