From a3666622907cc1ccf9adf6e336b3d9a73fad4e93 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Wed, 4 Nov 2020 11:22:14 +0100 Subject: [PATCH] Implemented the new CLI args system to the ListLang command Signed-off-by: Louis Vallat --- .../xyz/vallat/louis/commands/ListLang.java | 57 ++++++++++--------- .../vallat/louis/discord/DiscordManager.java | 2 +- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/main/java/xyz/vallat/louis/commands/ListLang.java b/src/main/java/xyz/vallat/louis/commands/ListLang.java index e97ed21..e2d66f2 100644 --- a/src/main/java/xyz/vallat/louis/commands/ListLang.java +++ b/src/main/java/xyz/vallat/louis/commands/ListLang.java @@ -2,6 +2,7 @@ package xyz.vallat.louis.commands; import discord4j.core.event.domain.message.MessageCreateEvent; import discord4j.rest.util.Color; +import org.apache.commons.cli.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import reactor.core.publisher.Mono; @@ -9,7 +10,6 @@ import xyz.vallat.louis.subtitles.OpenSubtitles; import xyz.vallat.louis.subtitles.exceptions.UnauthorizedException; import java.time.Instant; -import java.util.List; public class ListLang extends Command { @@ -17,34 +17,39 @@ public class ListLang extends Command { public ListLang(String name) { super(name, "List all languages attached to a movie title or an IMDB Identifier.", - name + " imdb|title value", 2, 2); + name + " -i|--imdb | -t|--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 public Mono<Void> execute(MessageCreateEvent event) { - List<String> arguments = getArguments(event); - if (arguments.size() < minArgs) return notEnoughArguments(event); - else if (!arguments.get(0).equalsIgnoreCase("imdb") && - !arguments.get(0).equalsIgnoreCase("title")) - 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() - .flatMap(channel -> event.getMessage().addReaction(WAITING).then(event.getMessage().getChannel() - .flatMap(messageChannel -> messageChannel.createEmbed(embed -> { - embed.setTitle("Subtitle languages").setColor(Color.RED); - try { - if (OpenSubtitles.isLoggedIn()) createEmbedListLang(arg, embed, isId); - else throw new UnauthorizedException("isLoggedIn returned false."); - } catch (UnauthorizedException e) { - logger.error("Not logged in on OpenSubtitles! {}", e.getMessage()); - logger.warn("It may be due to a website being down. If it's not " + - "the case, please fix this issue quickly."); - embed.setDescription("I cannot search for subtitle languages right now. Sorry."); - } - embed.setTimestamp(Instant.now()); - })))).then(event.getMessage().removeSelfReaction(WAITING)).then(); + try { + CommandLine cmd = new DefaultParser().parse(options, + event.getMessage().getContent().substring(name.length()).split(" ")); + String imdbOrTitle = cmd.hasOption("i") ? cmd.getOptionValue("i") : String.join(" ", cmd.getOptionValues("t")); + return event.getMessage().getChannel() + .flatMap(channel -> event.getMessage().addReaction(WAITING).then(event.getMessage().getChannel() + .flatMap(messageChannel -> messageChannel.createEmbed(embed -> { + embed.setTitle("Subtitle languages").setColor(Color.RED); + try { + if (OpenSubtitles.isLoggedIn()) + createEmbedListLang(imdbOrTitle, embed, cmd.hasOption("i")); + else throw new UnauthorizedException("isLoggedIn returned false."); + } catch (UnauthorizedException e) { + logger.error("Not logged in on OpenSubtitles! {}", e.getMessage()); + logger.warn("It may be due to a website being down. If it's not " + + "the case, please fix this issue quickly."); + embed.setDescription("I cannot search for subtitle languages right now. Sorry."); + } + embed.setTimestamp(Instant.now()); + })))).then(event.getMessage().removeSelfReaction(WAITING)).then(); + } catch (ParseException e) { + logger.debug("Parsing error: {}", e.getMessage()); + return parsingError(event); + } } } diff --git a/src/main/java/xyz/vallat/louis/discord/DiscordManager.java b/src/main/java/xyz/vallat/louis/discord/DiscordManager.java index 908bd71..41e1a34 100644 --- a/src/main/java/xyz/vallat/louis/discord/DiscordManager.java +++ b/src/main/java/xyz/vallat/louis/discord/DiscordManager.java @@ -24,7 +24,7 @@ public final class DiscordManager { private static GatewayDiscordClient discordClient; static { - commands.add(new ListLang(PREFIX + "listLangMovie")); + commands.add(new ListLang(PREFIX + "listLang")); commands.add(new Download(PREFIX + "download")); commands.add(new Quote(PREFIX + "quote")); commands.add(new Ping(PREFIX + "ping"));