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.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'
|
||||||
|
@ -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);
|
||||||
|
@ -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,19 +15,22 @@ 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,
|
||||||
|
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 -> {
|
return event.getMessage().getChannel().flatMap(messageChannel -> messageChannel.createEmbed(embed -> {
|
||||||
embed.setTitle("Quote").setColor(Color.RED);
|
embed.setTitle("Quote").setColor(Color.RED);
|
||||||
if (language == null)
|
if (language == null)
|
||||||
@ -41,5 +50,9 @@ public class Quote extends Command {
|
|||||||
}
|
}
|
||||||
embed.setTimestamp(Instant.now());
|
embed.setTimestamp(Instant.now());
|
||||||
})).then();
|
})).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.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()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user