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 <louis@louis-vallat.xyz>
This commit is contained in:
parent
c134e87d89
commit
a028dab281
@ -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) {
|
||||
|
@ -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<Void> 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);
|
||||
|
@ -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);
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -13,6 +13,6 @@ public class Ping extends Command {
|
||||
public Mono<Void> execute(MessageCreateEvent event) {
|
||||
return event.getMessage().getChannel()
|
||||
.flatMap(channel -> channel.createMessage("Pong!"))
|
||||
.then();
|
||||
.then().onErrorResume(throwable -> fatalError(event, throwable));
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -45,7 +45,7 @@ public class Version extends Command {
|
||||
}
|
||||
)
|
||||
)
|
||||
.then();
|
||||
.then().onErrorResume(throwable -> fatalError(event, throwable));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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 {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user