Implemented the new CLI args system to the ListLang command
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
parent
9a389f5dd3
commit
a366662290
@ -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,26 +17,27 @@ 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 <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
|
||||
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);
|
||||
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(arg, embed, isId);
|
||||
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());
|
||||
@ -46,5 +47,9 @@ public class ListLang extends Command {
|
||||
}
|
||||
embed.setTimestamp(Instant.now());
|
||||
})))).then(event.getMessage().removeSelfReaction(WAITING)).then();
|
||||
} catch (ParseException e) {
|
||||
logger.debug("Parsing error: {}", e.getMessage());
|
||||
return parsingError(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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"));
|
||||
|
Loading…
Reference in New Issue
Block a user