From aede68e521fba43230bf24f486999f1f6c5ef237 Mon Sep 17 00:00:00 2001 From: BrokenFire Date: Tue, 27 Feb 2018 12:12:37 +0100 Subject: [PATCH] Show private usable command in help #10 --- src/main/java/net/Broken/Commands/Help.java | 30 ++++- .../net/Broken/Tools/EmbedMessageUtils.java | 6 +- .../java/net/Broken/Tools/TableRenderer.java | 108 ++++++++++++++++++ src/main/resources/Help/main.md | 5 + 4 files changed, 141 insertions(+), 8 deletions(-) create mode 100644 src/main/java/net/Broken/Tools/TableRenderer.java create mode 100644 src/main/resources/Help/main.md diff --git a/src/main/java/net/Broken/Commands/Help.java b/src/main/java/net/Broken/Commands/Help.java index a732e19..4871874 100644 --- a/src/main/java/net/Broken/Commands/Help.java +++ b/src/main/java/net/Broken/Commands/Help.java @@ -6,6 +6,7 @@ import net.Broken.RestApi.CommandInterface; import net.Broken.Tools.EmbedMessageUtils; import net.Broken.Tools.MessageTimeOut; import net.Broken.Tools.PrivateMessage; +import net.Broken.Tools.TableRenderer; import net.dv8tion.jda.core.EmbedBuilder; import net.dv8tion.jda.core.Permission; import net.dv8tion.jda.core.entities.ChannelType; @@ -26,6 +27,7 @@ import java.util.Map; */ public class Help implements Commande { Logger logger = LogManager.getLogger(); + private int cellLenght = 25; @Override public boolean called(String[] args, MessageReceivedEvent event) { return true; @@ -114,7 +116,9 @@ public class Help implements Commande { } else { - StringBuilder txt= new StringBuilder(); + TableRenderer table = new TableRenderer(); + List noPu = new ArrayList<>(); + table.setHeader("Command","PU"); boolean isAdmin; if(event.isFromType(ChannelType.PRIVATE)) @@ -124,10 +128,21 @@ public class Help implements Commande { for (Map.Entry e : MainBot.commandes.entrySet()) { - if(!e.getValue().isAdminCmd() || isAdmin) - txt.append("\n- ").append(e.getKey()); + if(!e.getValue().isAdminCmd() || isAdmin){ + if(e.getValue().isPrivateUsable()) + table.addRow(e.getKey(), "XX"); + else + noPu.add(e.getKey()); + } + + } + for(String key : noPu) + table.addRow(key, ""); + + String txt = table.build(); + if(!event.isFromType(ChannelType.PRIVATE)){ Message rest = event.getTextChannel().sendMessage(new EmbedBuilder().setTitle("Command envoyées par message privé").setColor(Color.green).build()).complete(); List messages = new ArrayList(){{ @@ -144,10 +159,13 @@ public class Help implements Commande { else role = "Non Admin"; - PrivateMessage.send(event.getAuthor(),EmbedMessageUtils.getHelpList(role, txt.toString()),logger); - - + try { + PrivateMessage.send(event.getAuthor(),EmbedMessageUtils.getHelpList(role, txt),logger); + } catch (FileNotFoundException e) { + logger.catching(e); + PrivateMessage.send(event.getAuthor(),EmbedMessageUtils.getInternalError(), logger); + } } diff --git a/src/main/java/net/Broken/Tools/EmbedMessageUtils.java b/src/main/java/net/Broken/Tools/EmbedMessageUtils.java index 05c793d..ecfeaef 100644 --- a/src/main/java/net/Broken/Tools/EmbedMessageUtils.java +++ b/src/main/java/net/Broken/Tools/EmbedMessageUtils.java @@ -98,8 +98,10 @@ public class EmbedMessageUtils { return buildStandar(getError("Vous n'avez pas l'autorisation de faire ça!")); } - public static MessageEmbed getHelpList(String role, String list){ - return new EmbedBuilder().setTitle("Command du bot ("+role+")").setDescription(list).setFooter("Utilise '//help ' pour plus de détails.",null).setColor(Color.green).setThumbnail(MainBot.jda.getSelfUser().getAvatarUrl()).build(); + public static MessageEmbed getHelpList(String role, String list) throws FileNotFoundException { + String message = new ResourceLoader().getFile("Help/main.md"); + message = message.replace("@list", list); + return new EmbedBuilder().setTitle("Command du bot ("+role+")").setDescription(message).setFooter("Utilise '//help ' pour plus de détails.",null).setColor(Color.green).setThumbnail(MainBot.jda.getSelfUser().getAvatarUrl()).build(); } diff --git a/src/main/java/net/Broken/Tools/TableRenderer.java b/src/main/java/net/Broken/Tools/TableRenderer.java new file mode 100644 index 0000000..37aa2b3 --- /dev/null +++ b/src/main/java/net/Broken/Tools/TableRenderer.java @@ -0,0 +1,108 @@ +package net.Broken.Tools; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import org.apache.commons.lang3.StringUtils; + +public class TableRenderer { + + private int width; + private List header; + private List> table = new ArrayList<>(); + + private String empty = ""; + + public TableRenderer() { + + } + + public void setHeader(Object... header) { + this.header = Arrays.asList(header); + if (header.length > this.width) + this.width = header.length; + } + + public void addRow(Object... header) { + List objects = Arrays.asList(header); + table.add(objects); + if (header.length > this.width) + this.width = header.length; + } + + public void setEmptyString(String str) { + this.empty = str; + } + + private String[][] normalizeTable() { + int height = header == null ? table.size() : (table.size() + 1); + String[][] normalized = new String[height][width]; + + int vIndex = 0; + if (header != null) { + for (int hIndex = 0; hIndex < width; hIndex++) { + if (header.size() > hIndex) + normalized[vIndex][hIndex] = header.get(hIndex).toString(); + else + normalized[vIndex][hIndex] = this.empty; + } + vIndex++; + } + + for (List obj : table) { + for (int hIndex = 0; hIndex < width; hIndex++) { + if (obj.size() > hIndex) + normalized[vIndex][hIndex] = obj.get(hIndex).toString(); + else + normalized[vIndex][hIndex] = this.empty+"s"; + } + vIndex++; + } + + return normalized; + } + + private int[] getCollumnWidths(String[][] table, int padding) { + int collums[] = new int[width]; + for (int vIndex = 0; vIndex < table.length; vIndex++) + for (int hIndex = 0; hIndex < width; hIndex++) + if (table[vIndex][hIndex].length() + padding > collums[hIndex]) + collums[hIndex] = table[vIndex][hIndex].length() + padding; + collums[collums.length-1] -= padding; + return collums; + } + + + private String buildElement(String element, int width, String emptyChar) { + String result = element; + if (result.length() < width) + result += StringUtils.repeat(emptyChar, width - result.length()); + return result; + } + + private String buildLine(String[] strings, int[] widths, boolean header) { + String line = IntStream.range(0, strings.length) + .mapToObj((i) -> buildElement(strings[i], widths[i], " ")) + .collect(Collectors.joining("│ ")); + line = "│ "+ line + " │"; + if (header) { + String seperator = IntStream.range(0, strings.length) + .mapToObj((i) -> buildElement("", widths[i], "═")) + .collect(Collectors.joining("╪")); + line += "\n" + "╪═" + seperator + "══╪"; + } + + return line; + } + + public String build() { + String[][] table = normalizeTable(); + int[] widths = getCollumnWidths(table, 1); + return IntStream.range(0, table.length) + .mapToObj(i -> buildLine(table[i], widths, header != null && i==0)) + .collect(Collectors.joining("\n")); + } +} diff --git a/src/main/resources/Help/main.md b/src/main/resources/Help/main.md new file mode 100644 index 0000000..7492f4d --- /dev/null +++ b/src/main/resources/Help/main.md @@ -0,0 +1,5 @@ +``` +@list +``` + +_PU = Private Usable (Utilisable en Message Privée)_ \ No newline at end of file