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,34 +17,39 @@ 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)); return event.getMessage().getChannel()
boolean isId = arguments.get(0).equalsIgnoreCase("imdb"); .flatMap(channel -> event.getMessage().addReaction(WAITING).then(event.getMessage().getChannel()
String arg = String.join(" ", arguments.subList(1, arguments.size())); .flatMap(messageChannel -> messageChannel.createEmbed(embed -> {
if (arg.isBlank()) return notEnoughArguments(event); embed.setTitle("Subtitle languages").setColor(Color.RED);
logger.debug("Executing command with argument '{}'.", arg); try {
return event.getMessage().getChannel() if (OpenSubtitles.isLoggedIn())
.flatMap(channel -> event.getMessage().addReaction(WAITING).then(event.getMessage().getChannel() createEmbedListLang(imdbOrTitle, embed, cmd.hasOption("i"));
.flatMap(messageChannel -> messageChannel.createEmbed(embed -> { else throw new UnauthorizedException("isLoggedIn returned false.");
embed.setTitle("Subtitle languages").setColor(Color.RED); } catch (UnauthorizedException e) {
try { logger.error("Not logged in on OpenSubtitles! {}", e.getMessage());
if (OpenSubtitles.isLoggedIn()) createEmbedListLang(arg, embed, isId); logger.warn("It may be due to a website being down. If it's not " +
else throw new UnauthorizedException("isLoggedIn returned false."); "the case, please fix this issue quickly.");
} catch (UnauthorizedException e) { embed.setDescription("I cannot search for subtitle languages right now. Sorry.");
logger.error("Not logged in on OpenSubtitles! {}", e.getMessage()); }
logger.warn("It may be due to a website being down. If it's not " + embed.setTimestamp(Instant.now());
"the case, please fix this issue quickly."); })))).then(event.getMessage().removeSelfReaction(WAITING)).then();
embed.setDescription("I cannot search for subtitle languages right now. Sorry."); } catch (ParseException e) {
} logger.debug("Parsing error: {}", e.getMessage());
embed.setTimestamp(Instant.now()); return parsingError(event);
})))).then(event.getMessage().removeSelfReaction(WAITING)).then(); }
} }
} }

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"));