Added checks because receiving an empty file would make the bot crash (outoufbounds)

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-11-05 21:29:52 +01:00
parent 02fc4b90f5
commit af1c4e4cc3
3 changed files with 26 additions and 9 deletions

View File

@ -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) {

View File

@ -85,9 +85,27 @@ public class Download extends Command {
else {
ListResponse<SubtitleFile> subs = OpenSubtitles.downloadSubtitle(
subtitleInfo.get().getSubtitleFileId());
if (subs.getStatus().equals(ResponseStatus.OK)) {
List<SubtitleBlock> 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<SubtitleFile> 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<SubtitleBlock> 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);
}
}
}

View File

@ -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;
}