🚑 Fix crash if message not found in cmd 'ChannelsReview'

This commit is contained in:
SebClem 2020-12-02 00:23:18 +01:00
parent ec17bd81e9
commit ec248ea22e

View File

@ -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.");