diff --git a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java index cd1c884..48d8a86 100644 --- a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java +++ b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java @@ -34,7 +34,6 @@ public class MovieQuoteBot { static { commands.put("ping", new Ping(PREFIX + "ping")); commands.put("version", new Version(PREFIX + "version")); - commands.put("searchTitle", new SearchTitle(PREFIX + "searchTitle")); commands.put("listLang", new ListLang(PREFIX + "listLang")); } @@ -69,6 +68,7 @@ public class MovieQuoteBot { Runtime.getRuntime().addShutdownHook(new Thread(() -> { logger.info("Received shut down signal. Bye!"); + OpenSubtitles.logout(); discordClient.logout().block(); })); diff --git a/src/main/java/xyz/vallat/louis/commands/ListLang.java b/src/main/java/xyz/vallat/louis/commands/ListLang.java index 4b3b64d..6768257 100644 --- a/src/main/java/xyz/vallat/louis/commands/ListLang.java +++ b/src/main/java/xyz/vallat/louis/commands/ListLang.java @@ -1,7 +1,6 @@ package xyz.vallat.louis.commands; import com.github.wtekiela.opensub4j.response.ListResponse; -import com.github.wtekiela.opensub4j.response.MovieInfo; import com.github.wtekiela.opensub4j.response.ResponseStatus; import com.github.wtekiela.opensub4j.response.SubtitleInfo; import discord4j.core.event.domain.message.MessageCreateEvent; @@ -23,15 +22,15 @@ public class ListLang extends Command { @Override public Mono execute(MessageCreateEvent event) { - String[] args = event.getMessage().getContent().split(" "); - if (args.length < minArgs) + String arg = event.getMessage().getContent().substring(name.length() + 1); + if (arg.replace(" ", "").length() == 0) return notEnoughArguments(event); return event.getMessage().getChannel() .flatMap(channel -> { if (OpenSubtitles.isLoggedIn()) { return event.getMessage().addReaction(WAITING) .then(event.getMessage().getChannel().flatMap(messageChannel -> - searchAndShowFilms(args[1], channel, messageChannel))) + searchAndShowFilms(arg, channel, messageChannel))) .then(event.getMessage().removeSelfReaction(WAITING)) .then(); } else return channel.createMessage("I cannot search for subtitle languages right now. Sorry."); @@ -45,28 +44,27 @@ public class ListLang extends Command { .then(); } + // TODO: ADD MOVIE TITLE private Mono searchAndShowFilms(String arg, MessageChannel channel, MessageChannel messageChannel) { try { ListResponse subtitles = OpenSubtitles.searchSubtitles("", arg); - ListResponse movies = OpenSubtitles.searchImdb(arg); if (subtitles.getStatus().getCode() == ResponseStatus.OK.getCode()) { return messageChannel.createEmbed( embed -> { embed.setTitle("Results") .setDescription("Found " + subtitles.getData().size() + " results.") .setColor(Color.BISMARK); - if (movies.getStatus().getCode() == ResponseStatus.OK.getCode() && !movies.getData().isEmpty()) - embed.addField("Title", movies.getData().get(0).getTitle(), false); StringBuilder s = new StringBuilder(); for (SubtitleInfo info : subtitles.getData().subList(0, Math.min(10, subtitles.getData().size()))) s.append(info.getId()).append(" - ").append(info.getLanguage()).append("\n"); embed.addField("Languages found", s.toString(), false); }); - } else if (subtitles.getStatus().getCode() == ResponseStatus.INVALID_IMDB_ID.getCode() || - subtitles.getStatus().getCode() == ResponseStatus.INVALID_PARAMETERS.getCode()) - return messageChannel.createMessage("It looks like this IMDB id is invalid. Try again with a valid one."); - else - return messageChannel.createMessage("An error happened while fetching the results. Try again later, or contact my administrator." + subtitles.getStatus()); + } else if (subtitles.getStatus().getCode() == ResponseStatus.INVALID_PARAMETERS.getCode()) { + return messageChannel.createMessage("It looks like it is an invalid IMDB identifier."); + } else { + return messageChannel.createMessage("An error happened while fetching the results. " + + "Try again later, or contact my administrator." + subtitles.getStatus()); + } } catch (XmlRpcException e) { return channel.createMessage("It seems like OpenSubtitles had a stroke."); } diff --git a/src/main/java/xyz/vallat/louis/commands/SearchTitle.java b/src/main/java/xyz/vallat/louis/commands/SearchTitle.java index fc778f4..9a9d9c7 100644 --- a/src/main/java/xyz/vallat/louis/commands/SearchTitle.java +++ b/src/main/java/xyz/vallat/louis/commands/SearchTitle.java @@ -23,15 +23,15 @@ public class SearchTitle extends Command { @Override public Mono execute(MessageCreateEvent event) { - String[] args = event.getMessage().getContent().split(" "); - if (args.length < minArgs) + String arg = event.getMessage().getContent().substring(name.length() + 1); + if (arg.replace(" ", "").length() == 0) return notEnoughArguments(event); return event.getMessage().getChannel() .flatMap(channel -> { if (OpenSubtitles.isLoggedIn()) { return event.getMessage().addReaction(WAITING) .then(event.getMessage().getChannel().flatMap(messageChannel -> - searchAndShowFilms(args[1], channel, messageChannel))) + searchAndShowFilms(arg, channel, messageChannel))) .then(event.getMessage().removeSelfReaction(WAITING)) .then(); } else return channel.createMessage("I cannot search for subtitles right now. Sorry."); diff --git a/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java b/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java index 54a7960..a2d54de 100644 --- a/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java +++ b/src/main/java/xyz/vallat/louis/subtitles/OpenSubtitles.java @@ -49,6 +49,15 @@ public final class OpenSubtitles { } } + public static void logout() { + try { + client.logout(); + } catch (XmlRpcException e) { + logger.error("Could not log out. Skipping."); + } + } + + // TODO: NUKE THAT FUNCTION AND REPLACE IT WITH OMDB IMPLEMENTATION public static ListResponse searchImdb(String name) throws XmlRpcException { return client.searchMoviesOnImdb(name); }