Implemented the new CLI args system to the ListLang command

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-11-04 11:22:14 +01:00
parent 9a389f5dd3
commit a366662290
2 changed files with 32 additions and 27 deletions

View File

@ -2,6 +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.*;
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;
@ -9,7 +10,6 @@ import xyz.vallat.louis.subtitles.OpenSubtitles;
import xyz.vallat.louis.subtitles.exceptions.UnauthorizedException; import xyz.vallat.louis.subtitles.exceptions.UnauthorizedException;
import java.time.Instant; import java.time.Instant;
import java.util.List;
public class ListLang extends Command { public class ListLang extends Command {
@ -17,26 +17,27 @@ public class ListLang extends Command {
public ListLang(String name) { public ListLang(String name) {
super(name, "List all languages attached to a movie title or an IMDB Identifier.", super(name, "List all languages attached to a movie title or an IMDB Identifier.",
name + " imdb|title value", 2, 2); name + " -i|--imdb <imdb identifier> | -t|--title <title>", 2, 2);
OptionGroup iOrT = new OptionGroup()
.addOption(Option.builder("i").longOpt("imdb").desc("specify the imdb identifier").hasArg().build())
.addOption(Option.builder("t").longOpt("title").desc("specify the movie's title").hasArgs().numberOfArgs(Option.UNLIMITED_VALUES).build());
iOrT.setRequired(true);
options.addOptionGroup(iOrT);
} }
@Override @Override
public Mono<Void> execute(MessageCreateEvent event) { public Mono<Void> execute(MessageCreateEvent event) {
List<String> arguments = getArguments(event); try {
if (arguments.size() < minArgs) return notEnoughArguments(event); CommandLine cmd = new DefaultParser().parse(options,
else if (!arguments.get(0).equalsIgnoreCase("imdb") && event.getMessage().getContent().substring(name.length()).split(" "));
!arguments.get(0).equalsIgnoreCase("title")) String imdbOrTitle = cmd.hasOption("i") ? cmd.getOptionValue("i") : String.join(" ", cmd.getOptionValues("t"));
return badArgument(event, arguments.get(0));
boolean isId = arguments.get(0).equalsIgnoreCase("imdb");
String arg = String.join(" ", arguments.subList(1, arguments.size()));
if (arg.isBlank()) return notEnoughArguments(event);
logger.debug("Executing command with argument '{}'.", arg);
return event.getMessage().getChannel() return event.getMessage().getChannel()
.flatMap(channel -> event.getMessage().addReaction(WAITING).then(event.getMessage().getChannel() .flatMap(channel -> event.getMessage().addReaction(WAITING).then(event.getMessage().getChannel()
.flatMap(messageChannel -> messageChannel.createEmbed(embed -> { .flatMap(messageChannel -> messageChannel.createEmbed(embed -> {
embed.setTitle("Subtitle languages").setColor(Color.RED); embed.setTitle("Subtitle languages").setColor(Color.RED);
try { try {
if (OpenSubtitles.isLoggedIn()) createEmbedListLang(arg, embed, isId); if (OpenSubtitles.isLoggedIn())
createEmbedListLang(imdbOrTitle, embed, cmd.hasOption("i"));
else throw new UnauthorizedException("isLoggedIn returned false."); else throw new UnauthorizedException("isLoggedIn returned false.");
} catch (UnauthorizedException e) { } catch (UnauthorizedException e) {
logger.error("Not logged in on OpenSubtitles! {}", e.getMessage()); logger.error("Not logged in on OpenSubtitles! {}", e.getMessage());
@ -46,5 +47,9 @@ public class ListLang 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);
}
} }
} }

View File

@ -24,7 +24,7 @@ public final class DiscordManager {
private static GatewayDiscordClient discordClient; private static GatewayDiscordClient discordClient;
static { static {
commands.add(new ListLang(PREFIX + "listLangMovie")); commands.add(new ListLang(PREFIX + "listLang"));
commands.add(new Download(PREFIX + "download")); commands.add(new Download(PREFIX + "download"));
commands.add(new Quote(PREFIX + "quote")); commands.add(new Quote(PREFIX + "quote"));
commands.add(new Ping(PREFIX + "ping")); commands.add(new Ping(PREFIX + "ping"));