From b27fad85d13f763d1ae9b26135fbc92d5e76fc8a Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Mon, 26 Oct 2020 17:11:24 +0100 Subject: [PATCH] Refactored Search and fixed an issue on a check in the Search command Signed-off-by: Louis Vallat --- .../xyz/vallat/louis/commands/Search.java | 59 +++++++++++-------- .../vallat/louis/subtitles/OpenSubtitles.java | 3 +- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/main/java/xyz/vallat/louis/commands/Search.java b/src/main/java/xyz/vallat/louis/commands/Search.java index 8d86c4b..797dcb5 100644 --- a/src/main/java/xyz/vallat/louis/commands/Search.java +++ b/src/main/java/xyz/vallat/louis/commands/Search.java @@ -4,7 +4,10 @@ import com.github.wtekiela.opensub4j.response.ListResponse; import com.github.wtekiela.opensub4j.response.MovieInfo; import com.github.wtekiela.opensub4j.response.ResponseStatus; import discord4j.core.event.domain.message.MessageCreateEvent; +import discord4j.core.object.entity.Message; +import discord4j.core.object.entity.channel.MessageChannel; import discord4j.core.object.reaction.ReactionEmoji; +import discord4j.rest.util.Color; import org.apache.xmlrpc.XmlRpcException; import reactor.core.publisher.Mono; import xyz.vallat.louis.subtitles.OpenSubtitles; @@ -12,7 +15,7 @@ import xyz.vallat.louis.subtitles.OpenSubtitles; public class Search extends Command { private static final ReactionEmoji WAITING = ReactionEmoji.unicode("⌛"); - String format = "%1$-7s - %2$s\n"; + private static final String FORMAT = "%1$-7s - %2$s\n"; public Search(String name) { super(name, "A command to search some subtitles.", name + " ", 1, 1); @@ -22,37 +25,45 @@ public class Search extends Command { public Mono execute(MessageCreateEvent event) { String[] args = event.getMessage().getContent().split(" "); if (args.length < minArgs) - return event.getMessage().getChannel() - .flatMap(channel -> channel.createMessage("Error.\n" + getUsage())) - .then(); + return notEnoughArguments(event); return event.getMessage().getChannel() .flatMap(channel -> { if (OpenSubtitles.isLoggedIn()) { return event.getMessage().addReaction(WAITING) - .then(event.getMessage().getChannel().flatMap(messageChannel -> { - try { - ListResponse response = OpenSubtitles.searchImdb(args[1]); - if (response.getStatus() == ResponseStatus.OK) { - return messageChannel.createEmbed( - embed -> { - embed.setTitle("Results") - .setDescription("Found " + response.getData().size() + " results."); - StringBuilder s = new StringBuilder("```txt\n"); - for (MovieInfo info : response.getData().subList(0, 10)) - s.append(String.format(format, info.getId(), info.getTitle())); - s.append("```"); - embed.addField("First 10 results:", s.toString(), false); - }); - } else - return messageChannel.createMessage("An error happened while fetching the results. Try again later, or contact my administrator."); - } catch (XmlRpcException e) { - return channel.createMessage("It seems like OpenSubtitles had a stroke."); - } - })) + .then(event.getMessage().getChannel().flatMap(messageChannel -> + searchAndShowFilms(args[1], channel, messageChannel))) .then(event.getMessage().removeSelfReaction(WAITING)) .then(); } else return channel.createMessage("I cannot search for subtitles right now. Sorry."); }) .then(); } + + private Mono searchAndShowFilms(String arg, MessageChannel channel, MessageChannel messageChannel) { + try { + ListResponse response = OpenSubtitles.searchImdb(arg); + if (response.getStatus().getCode() == ResponseStatus.OK.getCode()) { + return messageChannel.createEmbed( + embed -> { + embed.setTitle("Results") + .setDescription("Found " + response.getData().size() + " results.") + .setColor(Color.BISMARK); + StringBuilder s = new StringBuilder("```txt\n"); + for (MovieInfo info : response.getData().subList(0, 10)) + s.append(String.format(FORMAT, info.getId(), info.getTitle())); + s.append("```"); + embed.addField("First 10 results:", s.toString(), false); + }); + } else + return messageChannel.createMessage("An error happened while fetching the results. Try again later, or contact my administrator."); + } catch (XmlRpcException e) { + return channel.createMessage("It seems like OpenSubtitles had a stroke."); + } + } + + private Mono notEnoughArguments(MessageCreateEvent event) { + return event.getMessage().getChannel() + .flatMap(channel -> channel.createMessage("Error.\n" + getUsage())) + .then(); + } } diff --git a/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java b/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java index 3cb7c66..2a617b0 100644 --- a/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java +++ b/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java @@ -11,7 +11,6 @@ import org.slf4j.LoggerFactory; import java.net.MalformedURLException; import java.net.URL; -import java.util.Map; public final class OpenSubtitles { @@ -49,7 +48,7 @@ public final class OpenSubtitles { } } - public static ListResponse searchImdb (String name) throws XmlRpcException { + public static ListResponse searchImdb(String name) throws XmlRpcException { return client.searchMoviesOnImdb(name); }