✏️ Reformat result of command 'channelsReview'

This commit is contained in:
SebClem 2020-12-02 00:54:35 +01:00
parent e906b70670
commit 7c363f2fed
2 changed files with 18 additions and 9 deletions

View File

@ -8,35 +8,38 @@ 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 org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Locale;
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");
HashMap<String, String> result = new HashMap<>();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMMM yyyy").withLocale(Locale.ENGLISH);
for( TextChannel textChannel: event.getGuild().getTextChannels()){
if(textChannel.hasLatestMessage()){
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);
String date = lastMessage.getTimeCreated().format(formatter);
result.put(textChannel.getName(), date);
}catch (RuntimeException e){
logger.warn("Can't find message with id: " + lastMessageId);
messageToSend.append("\nChannel : ERROR");
result.put(textChannel.getName(), "ERROR");
}
}
else{
messageToSend.append("\nChannel : ").append(textChannel.getName()).append(" was never used.");
result.put(textChannel.getName(), "No message or access denied.");
}
}
event.getTextChannel().sendMessage(EmbedMessageUtils.getLastMessageFromTextChannel(messageToSend.toString())).queue();
event.getTextChannel().sendMessage(EmbedMessageUtils.getLastMessageFromTextChannel(result)).queue();
}
@Override

View File

@ -13,6 +13,8 @@ import java.io.FileNotFoundException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
/**
* Pre build Message Embed
@ -108,8 +110,12 @@ public class EmbedMessageUtils {
return new EmbedBuilder().setTitle("Bot Command ("+role+")").setDescription(message).setFooter("Use '//help <command>' for more info",MainBot.jda.getSelfUser().getAvatarUrl()).setTimestamp(Instant.now()).setColor(Color.green).setThumbnail(MainBot.jda.getSelfUser().getAvatarUrl()).build();
}
public static MessageEmbed getLastMessageFromTextChannel(String message) {
EmbedBuilder temp = new EmbedBuilder().setTitle("Channel uses checker").setDescription(message).setColor(Color.green);
public static MessageEmbed getLastMessageFromTextChannel(HashMap<String, String> message) {
EmbedBuilder temp = new EmbedBuilder().setTitle("Channel uses checker").setDescription("Last message date for channels:").setColor(Color.green);
for(Map.Entry<String, String> entry : message.entrySet()) {
temp.addField(entry.getKey(),entry.getValue(), false);
}
return buildStandar(temp);
}