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(" ");
|
String[] arguments = event.getMessage().getContent().split(" ");
|
||||||
return List.of(arguments).subList(1, arguments.length);
|
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.event.domain.message.MessageCreateEvent;
|
||||||
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.*;
|
||||||
import org.apache.xmlrpc.XmlRpcException;
|
import org.apache.xmlrpc.XmlRpcException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -32,30 +33,36 @@ public class Download extends Command {
|
|||||||
private static final Logger logger = LoggerFactory.getLogger(Download.class.getCanonicalName());
|
private static final Logger logger = LoggerFactory.getLogger(Download.class.getCanonicalName());
|
||||||
|
|
||||||
public Download(String name) {
|
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
|
@Override
|
||||||
public Mono<Void> execute(MessageCreateEvent event) {
|
public Mono<Void> execute(MessageCreateEvent event) {
|
||||||
List<String> args = getArguments(event);
|
try {
|
||||||
if (args.size() < minArgs) return notEnoughArguments(event);
|
CommandLine cmd = new DefaultParser().parse(options, getArgArray(event));
|
||||||
String value = String.join(" ", args.subList(2, args.size()));
|
String langArg = cmd.hasOption("l") ? cmd.getOptionValue("l") : "english";
|
||||||
if (!args.get(1).equalsIgnoreCase("imdb") && !args.get(1).equalsIgnoreCase("title"))
|
Lang language = LanguageManager.getLangFromAny(langArg);
|
||||||
return badArgument(event, args.get(1));
|
if (language == null) return unknownLanguage(event, langArg);
|
||||||
Lang language = LanguageManager.getLangFromAny(args.get(0));
|
return event.getMessage().getChannel().flatMap(channel -> event.getMessage().addReaction(WAITING)
|
||||||
if (language == null) return unknownLanguage(event, args.get(0));
|
.then(channel.createEmbed(embed -> {
|
||||||
return event.getMessage().getChannel().flatMap(channel -> event.getMessage().addReaction(WAITING).then(
|
|
||||||
channel.createEmbed(embed -> {
|
|
||||||
embed.setTitle("Importation").setColor(Color.RED);
|
embed.setTitle("Importation").setColor(Color.RED);
|
||||||
try {
|
try {
|
||||||
Movie movie = OMDBClient.getMovie(value, args.get(1).equalsIgnoreCase("imdb"));
|
Movie movie = OMDBClient.getMovie(cmd.hasOption("i") ?
|
||||||
|
cmd.getOptionValue("i") : String.join(" ", cmd.getOptionValues("t")),
|
||||||
|
cmd.hasOption("i"));
|
||||||
if (movie == null || movie.getId() <= 0)
|
if (movie == null || movie.getId() <= 0)
|
||||||
embed.setDescription("We couldn't find any movie with these information. Sorry!");
|
embed.setDescription("We couldn't find any movie with these information. Sorry!");
|
||||||
else if (SubtitleManager.getSubtitlesId(movie, language.getId()) > 0)
|
else if (SubtitleManager.getSubtitlesId(movie, language.getId()) > 0)
|
||||||
embed.setDescription("This movie already has already this language imported.")
|
embed.setDescription("This movie already has already this language imported.")
|
||||||
.setColor(Color.ORANGE);
|
.setColor(Color.ORANGE);
|
||||||
else
|
else
|
||||||
computeImportation(event, args, language, embed, movie);
|
computeImportation(event, language, embed, movie);
|
||||||
} catch (XmlRpcException | IOException e) {
|
} catch (XmlRpcException | IOException e) {
|
||||||
logger.error("An error occurred while fetching the data from opensubtitles: {}", e.getMessage());
|
logger.error("An error occurred while fetching the data from opensubtitles: {}", e.getMessage());
|
||||||
embed.setDescription("An error occurred, please contact my administrator.");
|
embed.setDescription("An error occurred, please contact my administrator.");
|
||||||
@ -63,10 +70,14 @@ public class Download extends Command {
|
|||||||
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,
|
private void computeImportation(MessageCreateEvent event, Lang language, EmbedCreateSpec embed, Movie movie)
|
||||||
Lang language, EmbedCreateSpec embed, Movie movie) throws XmlRpcException {
|
throws XmlRpcException {
|
||||||
Stream<SubtitleInfo> subtitleInfoStream = OpenSubtitles.getSubtitleStreamFromMovie(movie, language.getAlpha3b());
|
Stream<SubtitleInfo> subtitleInfoStream = OpenSubtitles.getSubtitleStreamFromMovie(movie, language.getAlpha3b());
|
||||||
Optional<SubtitleInfo> subtitleInfo = subtitleInfoStream == null ? Optional.empty() : subtitleInfoStream.findFirst();
|
Optional<SubtitleInfo> subtitleInfo = subtitleInfoStream == null ? Optional.empty() : subtitleInfoStream.findFirst();
|
||||||
if (subtitleInfo.isEmpty())
|
if (subtitleInfo.isEmpty())
|
||||||
@ -86,7 +97,7 @@ public class Download extends Command {
|
|||||||
+ "for your contribution!");
|
+ "for your contribution!");
|
||||||
embed.addField("You imported", blocks.size() + " lines into my database", false);
|
embed.addField("You imported", blocks.size() + " lines into my database", false);
|
||||||
} else {
|
} 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.");
|
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.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.*;
|
||||||
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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
@ -23,10 +20,12 @@ public class Quote extends Command {
|
|||||||
public Quote(String name) {
|
public Quote(String name) {
|
||||||
super(name, "Get a random quote from any movie.", name + " [-l|--lang <lang>] " +
|
super(name, "Get a random quote from any movie.", name + " [-l|--lang <lang>] " +
|
||||||
"[-i|--imdb imdb_identifier]|[-t|--title title]", 0, 1);
|
"[-i|--imdb imdb_identifier]|[-t|--title title]", 0, 1);
|
||||||
options.addOption(Option.builder("l").longOpt("lang").desc("specify a language").hasArg().build());
|
OptionGroup iOrT = new OptionGroup()
|
||||||
options.addOption(Option.builder("i").longOpt("imdb").desc("specify an IMDB identifier").hasArg().build());
|
.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()
|
.addOption(Option.builder("t").longOpt("title").desc("specify a title").hasArgs()
|
||||||
.numberOfArgs(Option.UNLIMITED_VALUES).build());
|
.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
|
@Override
|
||||||
@ -57,7 +56,7 @@ public class Quote extends Command {
|
|||||||
embed.setTimestamp(Instant.now());
|
embed.setTimestamp(Instant.now());
|
||||||
})).then();
|
})).then();
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
logger.error("Parsing error: {}", e.getMessage());
|
logger.debug("Parsing error: {}", e.getMessage());
|
||||||
return parsingError(event);
|
return parsingError(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user