From af1c4e4cc307de6cfbaf91dc7db9b2b883f5ba4e Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Thu, 5 Nov 2020 21:29:52 +0100 Subject: [PATCH] Added checks because receiving an empty file would make the bot crash (outoufbounds) Signed-off-by: Louis Vallat --- .../java/xyz/vallat/louis/MoviesQuoteBot.java | 2 +- .../xyz/vallat/louis/commands/Download.java | 31 ++++++++++++++----- .../subtitles/parser/SubtitleParser.java | 2 +- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/main/java/xyz/vallat/louis/MoviesQuoteBot.java b/src/main/java/xyz/vallat/louis/MoviesQuoteBot.java index daea710..74431f1 100644 --- a/src/main/java/xyz/vallat/louis/MoviesQuoteBot.java +++ b/src/main/java/xyz/vallat/louis/MoviesQuoteBot.java @@ -11,7 +11,7 @@ public class MoviesQuoteBot { public static final String PREFIX = "!"; public static final String NAME = "Movies Quote Bot"; public static final String DESCRIPTION = "I may know some quotes from some movies."; - public static final String VERSION = "0.3-SNAPSHOT"; + public static final String VERSION = "0.3.2-SNAPSHOT"; private static final Logger logger = LoggerFactory.getLogger(MoviesQuoteBot.class.getCanonicalName()); public static void main(String[] args) { diff --git a/src/main/java/xyz/vallat/louis/commands/Download.java b/src/main/java/xyz/vallat/louis/commands/Download.java index e80ac7b..739a0e6 100644 --- a/src/main/java/xyz/vallat/louis/commands/Download.java +++ b/src/main/java/xyz/vallat/louis/commands/Download.java @@ -85,9 +85,27 @@ public class Download extends Command { else { ListResponse subs = OpenSubtitles.downloadSubtitle( subtitleInfo.get().getSubtitleFileId()); - if (subs.getStatus().equals(ResponseStatus.OK)) { - List blocks = new SubtitleParser().parseSRT( - subs.getData().get(0).getContentAsString(subtitleInfo.get().getEncoding())); + if (subs.getStatus().equals(ResponseStatus.OK) && !subs.getData().isEmpty()) { + importSubtitles(event, language, embed, movie, subtitleInfo.get(), subs.getData()); + } else { + logger.error("Could not download subtitle: {}", subs.getStatus()); + embed.setDescription("An error occurred. Please contact my administrator."); + } + } + } + + private void importSubtitles(MessageCreateEvent event, Lang language, EmbedCreateSpec embed, + Movie movie, SubtitleInfo subtitleInfo, List subs) { + if (subs.isEmpty()) + embed.setDescription("An error occurred, try again please, and if this error keeps appearing, then please" + + " contact my administrator."); + else { + List blocks = new SubtitleParser().parseSRT( + subs.get(0).getContentAsString(subtitleInfo.getEncoding())); + if (blocks.isEmpty()) + embed.setDescription("The file we downloaded was empty... \uD83E\uDD28 It should be a temporary error! " + + "Try again please! And if you still have this issue, try contacting my administrator, please."); + else { SubtitleLineManager.importSubtitleLines(blocks, language, movie, event.getMember().isPresent() ? event.getMember().get().getId() : null, event.getGuildId().isPresent() ? event.getGuildId().get() : null); @@ -95,10 +113,9 @@ public class Download extends Command { "Congratulations and thank you" + (event.getMember().isPresent() ? " <@!" + event.getMember().get().getId().asString() + "> " : " ") + "for your contribution!"); - embed.addField("You imported", blocks.size() + " lines into my database", false); - } else { - logger.error("Could not download subtitle: {}", subs.getStatus()); - embed.setDescription("An error occurred. Please contact my administrator."); + embed.addField("You imported", blocks.size() + " lines into my database", true); + embed.addField("From", movie.toString(), true); + embed.addField("In", language.getEnglish(), true); } } } diff --git a/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleParser.java b/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleParser.java index 0f89862..6398760 100644 --- a/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleParser.java +++ b/src/main/java/xyz/vallat/louis/subtitles/parser/SubtitleParser.java @@ -21,7 +21,7 @@ public class SubtitleParser { splitBlock.get(1), String.join("\n", splitBlock.subList(2, splitBlock.size())))); } subtitleBlocks.remove(0); - subtitleBlocks.remove(subtitleBlocks.size() - 1); + subtitleBlocks.remove(Math.min(0, subtitleBlocks.size() - 1)); return subtitleBlocks; }