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 PREFIX = "!";
public static final String NAME = "Movies Quote Bot"; 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 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()); private static final Logger logger = LoggerFactory.getLogger(MoviesQuoteBot.class.getCanonicalName());
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -85,9 +85,27 @@ public class Download extends Command {
else { else {
ListResponse<SubtitleFile> subs = OpenSubtitles.downloadSubtitle( ListResponse<SubtitleFile> subs = OpenSubtitles.downloadSubtitle(
subtitleInfo.get().getSubtitleFileId()); subtitleInfo.get().getSubtitleFileId());
if (subs.getStatus().equals(ResponseStatus.OK)) { if (subs.getStatus().equals(ResponseStatus.OK) && !subs.getData().isEmpty()) {
List<SubtitleBlock> blocks = new SubtitleParser().parseSRT( importSubtitles(event, language, embed, movie, subtitleInfo.get(), subs.getData());
subs.getData().get(0).getContentAsString(subtitleInfo.get().getEncoding())); } 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, SubtitleLineManager.importSubtitleLines(blocks, language, movie,
event.getMember().isPresent() ? event.getMember().get().getId() : null, event.getMember().isPresent() ? event.getMember().get().getId() : null,
event.getGuildId().isPresent() ? event.getGuildId().get() : null); event.getGuildId().isPresent() ? event.getGuildId().get() : null);
@ -95,10 +113,9 @@ public class Download extends Command {
"Congratulations and thank you" + "Congratulations and thank you" +
(event.getMember().isPresent() ? " <@!" + event.getMember().get().getId().asString() + "> " : " ") (event.getMember().isPresent() ? " <@!" + event.getMember().get().getId().asString() + "> " : " ")
+ "for your contribution!"); + "for your contribution!");
embed.addField("You imported", blocks.size() + " lines into my database", false); embed.addField("You imported", blocks.size() + " lines into my database", true);
} else { embed.addField("From", movie.toString(), true);
logger.error("Could not download subtitle: {}", subs.getStatus()); embed.addField("In", language.getEnglish(), true);
embed.setDescription("An error occurred. Please contact my administrator.");
} }
} }
} }

View File

@ -21,7 +21,7 @@ public class SubtitleParser {
splitBlock.get(1), String.join("\n", splitBlock.subList(2, splitBlock.size())))); splitBlock.get(1), String.join("\n", splitBlock.subList(2, splitBlock.size()))));
} }
subtitleBlocks.remove(0); subtitleBlocks.remove(0);
subtitleBlocks.remove(subtitleBlocks.size() - 1); subtitleBlocks.remove(Math.min(0, subtitleBlocks.size() - 1));
return subtitleBlocks; return subtitleBlocks;
} }