Implement various null checks
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bea 2023-01-15 04:51:30 +01:00
parent 546eb49144
commit d2abeb35fc
3 changed files with 28 additions and 6 deletions

View File

@ -61,9 +61,13 @@ public class Trivia
public static MessageResponse generateMainScreen() public static MessageResponse generateMainScreen()
{ {
// todo null checks
JSONObject categoriesJson = Trivia.fetchJson(Trivia.getCategoriesLink()); JSONObject categoriesJson = Trivia.fetchJson(Trivia.getCategoriesLink());
if(categoriesJson == null)
return new MessageResponse("Error fetching trivia!", null); // todo nicer with emojis
List<TriviaCategory> categories = Trivia.parseCategories(categoriesJson); List<TriviaCategory> categories = Trivia.parseCategories(categoriesJson);
if(categories.isEmpty())
return new MessageResponse("Error parsing trivia categories!", null); // todo nicer with emojis
categories.sort(new TriviaCategoryComparator()); categories.sort(new TriviaCategoryComparator());
EmbedBuilder embedBuilder = new EmbedBuilder(); EmbedBuilder embedBuilder = new EmbedBuilder();

View File

@ -166,6 +166,7 @@ public class UserPunishment
Duration duration = null; Duration duration = null;
AuditableRestAction<Void> punishmentAction = null; AuditableRestAction<Void> punishmentAction = null;
boolean impossible = false;
try { try {
switch (punishmentType) { switch (punishmentType) {
@ -197,6 +198,14 @@ public class UserPunishment
} }
} }
} catch (RuntimeException ignored) { } catch (RuntimeException ignored) {
impossible = true;
}
if(punishmentAction == null)
impossible = true;
if(impossible)
{
// todo nicer looking with emojis // todo nicer looking with emojis
return new MessageResponse("Sorry, I couldn't " + punishmentTypeName + " " + mentioned.getAsMention() + "!", return new MessageResponse("Sorry, I couldn't " + punishmentTypeName + " " + mentioned.getAsMention() + "!",
null); null);

View File

@ -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.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; 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.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import wtf.beatrice.hidekobot.Cache; import wtf.beatrice.hidekobot.Cache;
@ -77,11 +78,19 @@ public class TriviaCommand implements MessageCommand
MessageResponse response = Trivia.generateMainScreen(); MessageResponse response = Trivia.generateMainScreen();
event.getMessage().replyEmbeds(response.embed()).addActionRow(response.components()).queue(message -> Message recvMessage = event.getMessage();
{ MessageCreateAction responseAction = null;
Cache.getDatabaseSource().trackRanCommandReply(message, event.getAuthor()); if(response.content() != null) responseAction = recvMessage.reply(response.content());
Cache.getDatabaseSource().queueDisabling(message); 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);
});
}
} }