From d2abeb35fce4d5f79e30a6acefe78448040ff77f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Sun, 15 Jan 2023 04:51:30 +0100 Subject: [PATCH] Implement various null checks --- .../hidekobot/commands/base/Trivia.java | 6 +++++- .../commands/base/UserPunishment.java | 9 +++++++++ .../commands/message/TriviaCommand.java | 19 ++++++++++++++----- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/base/Trivia.java b/src/main/java/wtf/beatrice/hidekobot/commands/base/Trivia.java index e012366..b4bf9c6 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/base/Trivia.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/base/Trivia.java @@ -61,9 +61,13 @@ public class Trivia public static MessageResponse generateMainScreen() { - // todo null checks JSONObject categoriesJson = Trivia.fetchJson(Trivia.getCategoriesLink()); + if(categoriesJson == null) + return new MessageResponse("Error fetching trivia!", null); // todo nicer with emojis List categories = Trivia.parseCategories(categoriesJson); + if(categories.isEmpty()) + return new MessageResponse("Error parsing trivia categories!", null); // todo nicer with emojis + categories.sort(new TriviaCategoryComparator()); EmbedBuilder embedBuilder = new EmbedBuilder(); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/base/UserPunishment.java b/src/main/java/wtf/beatrice/hidekobot/commands/base/UserPunishment.java index c46068a..1b14373 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/base/UserPunishment.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/base/UserPunishment.java @@ -166,6 +166,7 @@ public class UserPunishment Duration duration = null; AuditableRestAction punishmentAction = null; + boolean impossible = false; try { switch (punishmentType) { @@ -197,6 +198,14 @@ public class UserPunishment } } } catch (RuntimeException ignored) { + impossible = true; + } + + if(punishmentAction == null) + impossible = true; + + if(impossible) + { // todo nicer looking with emojis return new MessageResponse("Sorry, I couldn't " + punishmentTypeName + " " + mentioned.getAsMention() + "!", null); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/message/TriviaCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/message/TriviaCommand.java index 38f2b83..56c6b41 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/message/TriviaCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/message/TriviaCommand.java @@ -5,6 +5,7 @@ import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.requests.restaction.MessageCreateAction; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import wtf.beatrice.hidekobot.Cache; @@ -77,11 +78,19 @@ public class TriviaCommand implements MessageCommand MessageResponse response = Trivia.generateMainScreen(); - event.getMessage().replyEmbeds(response.embed()).addActionRow(response.components()).queue(message -> - { - Cache.getDatabaseSource().trackRanCommandReply(message, event.getAuthor()); - Cache.getDatabaseSource().queueDisabling(message); - }); + Message recvMessage = event.getMessage(); + MessageCreateAction responseAction = null; + if(response.content() != null) responseAction = recvMessage.reply(response.content()); + else if(response.embed() != null) responseAction = recvMessage.replyEmbeds(response.embed()); + + if(responseAction != null) { + if(response.components() != null) responseAction = responseAction.addActionRow(response.components()); + + responseAction.queue(message -> { + Cache.getDatabaseSource().trackRanCommandReply(message, event.getAuthor()); + Cache.getDatabaseSource().queueDisabling(message); + }); + } }