Implemented the new CLI args system to the download command
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
d1f847bc7a
commit
4214bcc8a4
@ -133,4 +133,8 @@ public abstract class Command {
|
||||
String[] arguments = event.getMessage().getContent().split(" ");
|
||||
return List.of(arguments).subList(1, arguments.length);
|
||||
}
|
||||
|
||||
protected String[] getArgArray(MessageCreateEvent event) {
|
||||
return event.getMessage().getContent().substring(name.length()).split(" ");
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import com.github.wtekiela.opensub4j.response.SubtitleInfo;
|
||||
import discord4j.core.event.domain.message.MessageCreateEvent;
|
||||
import discord4j.core.spec.EmbedCreateSpec;
|
||||
import discord4j.rest.util.Color;
|
||||
import org.apache.commons.cli.*;
|
||||
import org.apache.xmlrpc.XmlRpcException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -32,41 +33,51 @@ public class Download extends Command {
|
||||
private static final Logger logger = LoggerFactory.getLogger(Download.class.getCanonicalName());
|
||||
|
||||
public Download(String name) {
|
||||
super(name, "Download a subtitle.", name + " lang imdb|title value", 3, 3);
|
||||
super(name, "Download a subtitle.", name + " [-l|--lang <lang>] -i|--imdb <imdb> | -t|--title <title>", 3, 3);
|
||||
OptionGroup iOrT = new OptionGroup()
|
||||
.addOption(Option.builder("i").longOpt("imdb").hasArg().desc("imdb identifier for the film").build())
|
||||
.addOption(Option.builder("t").longOpt("title").hasArgs().numberOfArgs(Option.UNLIMITED_VALUES).desc("movie title for the film").build());
|
||||
iOrT.setRequired(true);
|
||||
options.addOption(Option.builder("l").longOpt("lang").hasArg().desc("specify a language (by default, english)").build());
|
||||
options.addOptionGroup(iOrT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> execute(MessageCreateEvent event) {
|
||||
List<String> args = getArguments(event);
|
||||
if (args.size() < minArgs) return notEnoughArguments(event);
|
||||
String value = String.join(" ", args.subList(2, args.size()));
|
||||
if (!args.get(1).equalsIgnoreCase("imdb") && !args.get(1).equalsIgnoreCase("title"))
|
||||
return badArgument(event, args.get(1));
|
||||
Lang language = LanguageManager.getLangFromAny(args.get(0));
|
||||
if (language == null) return unknownLanguage(event, args.get(0));
|
||||
return event.getMessage().getChannel().flatMap(channel -> event.getMessage().addReaction(WAITING).then(
|
||||
channel.createEmbed(embed -> {
|
||||
embed.setTitle("Importation").setColor(Color.RED);
|
||||
try {
|
||||
Movie movie = OMDBClient.getMovie(value, args.get(1).equalsIgnoreCase("imdb"));
|
||||
if (movie == null || movie.getId() <= 0)
|
||||
embed.setDescription("We couldn't find any movie with these information. Sorry!");
|
||||
else if (SubtitleManager.getSubtitlesId(movie, language.getId()) > 0)
|
||||
embed.setDescription("This movie already has already this language imported.")
|
||||
.setColor(Color.ORANGE);
|
||||
else
|
||||
computeImportation(event, args, language, embed, movie);
|
||||
} catch (XmlRpcException | IOException e) {
|
||||
logger.error("An error occurred while fetching the data from opensubtitles: {}", e.getMessage());
|
||||
embed.setDescription("An error occurred, please contact my administrator.");
|
||||
try {
|
||||
CommandLine cmd = new DefaultParser().parse(options, getArgArray(event));
|
||||
String langArg = cmd.hasOption("l") ? cmd.getOptionValue("l") : "english";
|
||||
Lang language = LanguageManager.getLangFromAny(langArg);
|
||||
if (language == null) return unknownLanguage(event, langArg);
|
||||
return event.getMessage().getChannel().flatMap(channel -> event.getMessage().addReaction(WAITING)
|
||||
.then(channel.createEmbed(embed -> {
|
||||
embed.setTitle("Importation").setColor(Color.RED);
|
||||
try {
|
||||
Movie movie = OMDBClient.getMovie(cmd.hasOption("i") ?
|
||||
cmd.getOptionValue("i") : String.join(" ", cmd.getOptionValues("t")),
|
||||
cmd.hasOption("i"));
|
||||
if (movie == null || movie.getId() <= 0)
|
||||
embed.setDescription("We couldn't find any movie with these information. Sorry!");
|
||||
else if (SubtitleManager.getSubtitlesId(movie, language.getId()) > 0)
|
||||
embed.setDescription("This movie already has already this language imported.")
|
||||
.setColor(Color.ORANGE);
|
||||
else
|
||||
computeImportation(event, language, embed, movie);
|
||||
} catch (XmlRpcException | IOException e) {
|
||||
logger.error("An error occurred while fetching the data from opensubtitles: {}", e.getMessage());
|
||||
embed.setDescription("An error occurred, please contact my administrator.");
|
||||
}
|
||||
embed.setTimestamp(Instant.now());
|
||||
}
|
||||
embed.setTimestamp(Instant.now());
|
||||
}
|
||||
))).then(event.getMessage().removeSelfReaction(WAITING)).then();
|
||||
))).then(event.getMessage().removeSelfReaction(WAITING)).then();
|
||||
} catch (ParseException e) {
|
||||
logger.debug("Parsing error: {}", e.getMessage());
|
||||
return parsingError(event);
|
||||
}
|
||||
}
|
||||
|
||||
private void computeImportation(MessageCreateEvent event, List<String> args,
|
||||
Lang language, EmbedCreateSpec embed, Movie movie) throws XmlRpcException {
|
||||
private void computeImportation(MessageCreateEvent event, Lang language, EmbedCreateSpec embed, Movie movie)
|
||||
throws XmlRpcException {
|
||||
Stream<SubtitleInfo> subtitleInfoStream = OpenSubtitles.getSubtitleStreamFromMovie(movie, language.getAlpha3b());
|
||||
Optional<SubtitleInfo> subtitleInfo = subtitleInfoStream == null ? Optional.empty() : subtitleInfoStream.findFirst();
|
||||
if (subtitleInfo.isEmpty())
|
||||
@ -86,7 +97,7 @@ public class Download extends Command {
|
||||
+ "for your contribution!");
|
||||
embed.addField("You imported", blocks.size() + " lines into my database", false);
|
||||
} else {
|
||||
logger.error("Could not download subtitle '{}': {}", args.get(0), subs.getStatus());
|
||||
logger.error("Could not download subtitle: {}", subs.getStatus());
|
||||
embed.setDescription("An error occurred. Please contact my administrator.");
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,7 @@ 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.apache.commons.cli.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import reactor.core.publisher.Mono;
|
||||
@ -23,10 +20,12 @@ 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);
|
||||
options.addOption(Option.builder("l").longOpt("lang").desc("specify a language").hasArg().build());
|
||||
options.addOption(Option.builder("i").longOpt("imdb").desc("specify an IMDB identifier").hasArg().build());
|
||||
options.addOption(Option.builder("t").longOpt("title").desc("specify a title").hasArgs()
|
||||
.numberOfArgs(Option.UNLIMITED_VALUES).build());
|
||||
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.addOptionGroup(iOrT);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -57,7 +56,7 @@ public class Quote extends Command {
|
||||
embed.setTimestamp(Instant.now());
|
||||
})).then();
|
||||
} catch (ParseException e) {
|
||||
logger.error("Parsing error: {}", e.getMessage());
|
||||
logger.debug("Parsing error: {}", e.getMessage());
|
||||
return parsingError(event);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user