From a028dab28182062bb99e944ba8fc6e6c4a043e80 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Tue, 10 Nov 2020 12:44:08 +0100 Subject: [PATCH] Added KERNEL PANIC message when commands crash unexpectedly, so I can take a look at it later, and it doesn't break the command Signed-off-by: Louis Vallat --- .../java/xyz/vallat/louis/MoviesQuoteBot.java | 2 +- .../xyz/vallat/louis/commands/Command.java | 18 +++++++++++++++++- .../xyz/vallat/louis/commands/Download.java | 3 ++- .../java/xyz/vallat/louis/commands/Help.java | 2 +- .../xyz/vallat/louis/commands/ListLang.java | 3 ++- .../java/xyz/vallat/louis/commands/Ping.java | 2 +- .../java/xyz/vallat/louis/commands/Quote.java | 2 +- .../xyz/vallat/louis/commands/Version.java | 2 +- .../java/xyz/vallat/louis/omdb/OMDBClient.java | 3 --- 9 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/main/java/xyz/vallat/louis/MoviesQuoteBot.java b/src/main/java/xyz/vallat/louis/MoviesQuoteBot.java index dd949be..25134d5 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.4-SNAPSHOT"; + public static final String VERSION = "0.3.5-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/Command.java b/src/main/java/xyz/vallat/louis/commands/Command.java index 503f48b..5b16353 100644 --- a/src/main/java/xyz/vallat/louis/commands/Command.java +++ b/src/main/java/xyz/vallat/louis/commands/Command.java @@ -6,6 +6,7 @@ import discord4j.core.object.reaction.ReactionEmoji; import discord4j.core.spec.EmbedCreateSpec; import discord4j.rest.util.Color; import org.apache.commons.cli.Options; +import org.apache.commons.lang3.RandomStringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import reactor.core.publisher.Mono; @@ -14,6 +15,7 @@ import xyz.vallat.louis.omdb.objects.Movie; import xyz.vallat.louis.subtitles.OpenSubtitles; import java.io.IOException; +import java.time.Instant; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; @@ -22,8 +24,9 @@ import java.util.stream.Collectors; import java.util.stream.Stream; public abstract class Command { + protected static final ReactionEmoji WAITING = ReactionEmoji.unicode("⌛"); - private static final Logger logger = LoggerFactory.getLogger(ListLang.class.getCanonicalName()); + private static final Logger logger = LoggerFactory.getLogger(Command.class.getCanonicalName()); protected final String name; protected final String description; protected final String usage; @@ -81,6 +84,19 @@ public abstract class Command { .then(); } + protected Mono fatalError(MessageCreateEvent event, Throwable throwable) { + return event.getMessage().getChannel().flatMap( + channel -> channel.createEmbed(embed -> { + event.getMessage().removeSelfReaction(WAITING); + String errorCode = RandomStringUtils.random(8, true, true); + logger.error("KERNEL PANIC: {}", errorCode, throwable); + embed.setTitle("KERNEL PANIC").setColor(Color.RED) + .setDescription("A fatal and unhandled error has been encountered. " + + "Please transfer this message to my administrator.") + .addField("IDENTIFIER", errorCode, false).setTimestamp(Instant.now()); + })).then(); + } + protected void createEmbedListLang(String arg, EmbedCreateSpec embed, boolean isId) { try { Movie movie = OMDBClient.getMovie(arg, isId); diff --git a/src/main/java/xyz/vallat/louis/commands/Download.java b/src/main/java/xyz/vallat/louis/commands/Download.java index 739a0e6..e6aaecf 100644 --- a/src/main/java/xyz/vallat/louis/commands/Download.java +++ b/src/main/java/xyz/vallat/louis/commands/Download.java @@ -69,7 +69,8 @@ public class Download extends Command { } embed.setTimestamp(Instant.now()); } - ))).then(event.getMessage().removeSelfReaction(WAITING)).then(); + ))).then(event.getMessage().removeSelfReaction(WAITING)).then() + .onErrorResume(throwable -> fatalError(event, throwable)); } catch (ParseException e) { logger.debug("Parsing error: {}", e.getMessage()); return parsingError(event); diff --git a/src/main/java/xyz/vallat/louis/commands/Help.java b/src/main/java/xyz/vallat/louis/commands/Help.java index 93c403f..3a2cb59 100644 --- a/src/main/java/xyz/vallat/louis/commands/Help.java +++ b/src/main/java/xyz/vallat/louis/commands/Help.java @@ -22,7 +22,7 @@ public class Help extends Command { embed.addField(command.getName(), command.getDescription() + " Usage: " + command.getUsage() + ".", false); } - )).then(); + )).then().onErrorResume(throwable -> fatalError(event, throwable)); } } diff --git a/src/main/java/xyz/vallat/louis/commands/ListLang.java b/src/main/java/xyz/vallat/louis/commands/ListLang.java index e2d66f2..6e931de 100644 --- a/src/main/java/xyz/vallat/louis/commands/ListLang.java +++ b/src/main/java/xyz/vallat/louis/commands/ListLang.java @@ -46,7 +46,8 @@ public class ListLang extends Command { embed.setDescription("I cannot search for subtitle languages right now. Sorry."); } embed.setTimestamp(Instant.now()); - })))).then(event.getMessage().removeSelfReaction(WAITING)).then(); + })))).then(event.getMessage().removeSelfReaction(WAITING)).then() + .onErrorResume(throwable -> fatalError(event, throwable)); } catch (ParseException e) { logger.debug("Parsing error: {}", e.getMessage()); return parsingError(event); diff --git a/src/main/java/xyz/vallat/louis/commands/Ping.java b/src/main/java/xyz/vallat/louis/commands/Ping.java index f827f3e..5ac453b 100644 --- a/src/main/java/xyz/vallat/louis/commands/Ping.java +++ b/src/main/java/xyz/vallat/louis/commands/Ping.java @@ -13,6 +13,6 @@ public class Ping extends Command { public Mono execute(MessageCreateEvent event) { return event.getMessage().getChannel() .flatMap(channel -> channel.createMessage("Pong!")) - .then(); + .then().onErrorResume(throwable -> fatalError(event, throwable)); } } diff --git a/src/main/java/xyz/vallat/louis/commands/Quote.java b/src/main/java/xyz/vallat/louis/commands/Quote.java index 1ebc2fa..9c3630f 100644 --- a/src/main/java/xyz/vallat/louis/commands/Quote.java +++ b/src/main/java/xyz/vallat/louis/commands/Quote.java @@ -61,7 +61,7 @@ public class Quote extends Command { } } embed.setTimestamp(Instant.now()); - })).then(); + })).then().onErrorResume(throwable -> fatalError(event, throwable)); } catch (ParseException e) { logger.debug("Parsing error: {}", e.getMessage()); return parsingError(event); diff --git a/src/main/java/xyz/vallat/louis/commands/Version.java b/src/main/java/xyz/vallat/louis/commands/Version.java index 5a2a062..292402e 100644 --- a/src/main/java/xyz/vallat/louis/commands/Version.java +++ b/src/main/java/xyz/vallat/louis/commands/Version.java @@ -45,7 +45,7 @@ public class Version extends Command { } ) ) - .then(); + .then().onErrorResume(throwable -> fatalError(event, throwable)); } } diff --git a/src/main/java/xyz/vallat/louis/omdb/OMDBClient.java b/src/main/java/xyz/vallat/louis/omdb/OMDBClient.java index 03f17b3..585723c 100644 --- a/src/main/java/xyz/vallat/louis/omdb/OMDBClient.java +++ b/src/main/java/xyz/vallat/louis/omdb/OMDBClient.java @@ -1,6 +1,5 @@ package xyz.vallat.louis.omdb; -import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.slf4j.Logger; @@ -17,8 +16,6 @@ import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.List; public final class OMDBClient {