Show private usable command in help #10

This commit is contained in:
BrokenFire 2018-02-27 12:12:37 +01:00
parent 1a1c508cf4
commit aede68e521
4 changed files with 141 additions and 8 deletions

View File

@ -6,6 +6,7 @@ import net.Broken.RestApi.CommandInterface;
import net.Broken.Tools.EmbedMessageUtils; import net.Broken.Tools.EmbedMessageUtils;
import net.Broken.Tools.MessageTimeOut; import net.Broken.Tools.MessageTimeOut;
import net.Broken.Tools.PrivateMessage; import net.Broken.Tools.PrivateMessage;
import net.Broken.Tools.TableRenderer;
import net.dv8tion.jda.core.EmbedBuilder; import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.Permission; import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.ChannelType;
@ -26,6 +27,7 @@ import java.util.Map;
*/ */
public class Help implements Commande { public class Help implements Commande {
Logger logger = LogManager.getLogger(); Logger logger = LogManager.getLogger();
private int cellLenght = 25;
@Override @Override
public boolean called(String[] args, MessageReceivedEvent event) { public boolean called(String[] args, MessageReceivedEvent event) {
return true; return true;
@ -114,7 +116,9 @@ public class Help implements Commande {
} }
else else
{ {
StringBuilder txt= new StringBuilder(); TableRenderer table = new TableRenderer();
List<String> noPu = new ArrayList<>();
table.setHeader("Command","PU");
boolean isAdmin; boolean isAdmin;
if(event.isFromType(ChannelType.PRIVATE)) if(event.isFromType(ChannelType.PRIVATE))
@ -124,10 +128,21 @@ public class Help implements Commande {
for (Map.Entry<String, Commande> e : MainBot.commandes.entrySet()) { for (Map.Entry<String, Commande> e : MainBot.commandes.entrySet()) {
if(!e.getValue().isAdminCmd() || isAdmin) if(!e.getValue().isAdminCmd() || isAdmin){
txt.append("\n- ").append(e.getKey()); 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)){ if(!event.isFromType(ChannelType.PRIVATE)){
Message rest = event.getTextChannel().sendMessage(new EmbedBuilder().setTitle("Command envoyées par message privé").setColor(Color.green).build()).complete(); Message rest = event.getTextChannel().sendMessage(new EmbedBuilder().setTitle("Command envoyées par message privé").setColor(Color.green).build()).complete();
List<Message> messages = new ArrayList<Message>(){{ List<Message> messages = new ArrayList<Message>(){{
@ -144,10 +159,13 @@ public class Help implements Commande {
else else
role = "Non Admin"; 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);
}
} }

View File

@ -98,8 +98,10 @@ public class EmbedMessageUtils {
return buildStandar(getError("Vous n'avez pas l'autorisation de faire ça!")); return buildStandar(getError("Vous n'avez pas l'autorisation de faire ça!"));
} }
public static MessageEmbed getHelpList(String role, String list){ public static MessageEmbed getHelpList(String role, String list) throws FileNotFoundException {
return new EmbedBuilder().setTitle("Command du bot ("+role+")").setDescription(list).setFooter("Utilise '//help <commande>' pour plus de détails.",null).setColor(Color.green).setThumbnail(MainBot.jda.getSelfUser().getAvatarUrl()).build(); 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 <commande>' pour plus de détails.",null).setColor(Color.green).setThumbnail(MainBot.jda.getSelfUser().getAvatarUrl()).build();
} }

View File

@ -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<Object> header;
private List<List<Object>> 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<Object> 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<Object> 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"));
}
}

View File

@ -0,0 +1,5 @@
```
@list
```
_PU = Private Usable (Utilisable en Message Privée)_