From ec248ea22ee17ce20dbe40051c22a0c1f94a44df Mon Sep 17 00:00:00 2001 From: SebClem Date: Wed, 2 Dec 2020 00:23:18 +0100 Subject: [PATCH] :ambulance: Fix crash if message not found in cmd 'ChannelsReview' --- .../net/Broken/Commands/ChannelsReview.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/Broken/Commands/ChannelsReview.java b/src/main/java/net/Broken/Commands/ChannelsReview.java index 00d172d..6307a35 100644 --- a/src/main/java/net/Broken/Commands/ChannelsReview.java +++ b/src/main/java/net/Broken/Commands/ChannelsReview.java @@ -2,19 +2,35 @@ package net.Broken.Commands; import net.Broken.Commande; import net.Broken.Tools.EmbedMessageUtils; +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.MessageHistory; import net.dv8tion.jda.api.entities.TextChannel; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.time.format.DateTimeFormatter; public class ChannelsReview implements Commande { + Logger logger = LogManager.getLogger(); @Override public void action(String[] args, MessageReceivedEvent event) { StringBuilder messageToSend= new StringBuilder(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd LLLL yyyy"); for( TextChannel textChannel: event.getGuild().getTextChannels()){ if(textChannel.hasLatestMessage()){ - messageToSend.append("\nChannel : ").append(textChannel.getName()).append(" Date :").append((textChannel.retrieveMessageById(textChannel.getLatestMessageId()).complete().getTimeCreated()).toLocalDate().format(formatter)); + String lastMessageId = textChannel.getLatestMessageId(); + logger.debug("Last message in chanel " + textChannel.toString() + " is " + lastMessageId ); + try { + Message lastMessage = textChannel.retrieveMessageById(lastMessageId).complete(); + String date = lastMessage.getTimeCreated().toLocalDate().format(formatter); + messageToSend.append("\nChannel : ").append(textChannel.getName()).append(" Date :").append(date); + }catch (RuntimeException e){ + logger.warn("Can't find message with id: " + lastMessageId); + messageToSend.append("\nChannel : ERROR"); + } + + } else{ messageToSend.append("\nChannel : ").append(textChannel.getName()).append(" was never used.");