Advertisement
Vaerys_Dawn

new Commands thinger

Mar 17th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.33 KB | None | 0 0
  1. //------------------- old code ------------------------
  2. @Override
  3. public String execute(String args, CommandObject command) {
  4.     XEmbedBuilder helpEmbed = new XEmbedBuilder(command);
  5.     List<SAILType> types = new ArrayList<>();
  6.     List<SAILType> filteredTypes = new ArrayList<>();
  7.  
  8.     //get commands
  9.     List<Command> commands = new ArrayList<>(command.guild.getAllCommands(command));
  10.  
  11.     //if creator get creator commands
  12.     if (command.user.checkIsCreator()) {
  13.         commands.addAll(Globals.getALLCreatorCommands());
  14.     }
  15.  
  16.     //add dm commands.
  17.     types.add(SAILType.DM);
  18.     commands.addAll(Globals.getCommands(true));
  19.  
  20.     //get types
  21.     commands.forEach(command1 -> {
  22.         if (!types.contains(command1.type)) types.add(command1.type);
  23.     });
  24.  
  25.     //remove types that the user does not have permission to see.
  26.     Collections.sort(types);
  27.     filteredTypes.addAll(types.stream()
  28.             .filter(t -> Utility.getCommandsByType(commands, command, t, true).size() != 0)
  29.             .collect(Collectors.toList()));
  30.  
  31.  
  32.     //print out list of types.
  33.     if (args == null || args.isEmpty()) {
  34.         StringBuilder builder = new StringBuilder();
  35.         builder.append(codeBlock + "\n");
  36.         builder.append(Utility.listEnumFormatter(filteredTypes, false));
  37.         builder.append(codeBlock + "\n");
  38.         helpEmbed.withTitle("Here are the Command Types I have available for use:");
  39.         builder.append(Utility.getCommandInfo(this, command) + "\n");
  40.         helpEmbed.withDescription(builder.toString());
  41.         RequestHandler.sendEmbedMessage("", helpEmbed, command.channel.get());
  42.         return null;
  43.     }
  44.  
  45.     //make sure that the type exits
  46.     SAILType type = SAILType.get(args);
  47.     if (type == null || !types.contains(type)) {
  48.         return "> There are no commands with the type: **" + args + "**.\n\n" + Utility.getCommandInfo(this, command);
  49.     }
  50.  
  51.     //make sure that the user is allowed to view the type.
  52.     if (!filteredTypes.contains(type)) {
  53.         return "> You do not have permission to see the **" + type.toString() + "** page.";
  54.     }
  55.  
  56.  
  57.     StringHandler builder = new StringHandler();
  58.     String suffix = Utility.getCommandInfo(new Help(), command);
  59.     helpEmbed.withTitle("> Here are all of the " + type.toString() + " Commands I have available.");
  60.  
  61.     //special footers
  62.     switch (type) {
  63.         case DM:
  64.             suffix = "**These commands can only be performed in DMs.**\n" +
  65.                     "> If you send a non command message to my DMs it will send it to my creator.\n\n" + suffix;
  66.             break;
  67.         case CREATOR:
  68.             suffix = "**Only the creator of this bot can run these commands.**\n\n" + suffix;
  69.             break;
  70.     }
  71.  
  72.     //build command list
  73.     TreeMap<String, Boolean> commandNames = new TreeMap<>();
  74.     for (Command c : commands) {
  75.         //filter out subCommands that aren't allowed to be seen due to missing perms or inactive module.
  76.         List<SubCommandObject> subCommands = c.subCommands.stream()
  77.                 .filter(s -> types.contains(s.getType()))
  78.                 .filter(s -> GuildHandler.testForPerms(command, s.getPermissions()))
  79.                 .collect(Collectors.toList());
  80.         //add command if command type == type specified.
  81.         if (c.type == type || (c.type != type && c.channel == ChannelSetting.FROM_DM)) {
  82.             commandNames.put(c.getCommand(command), subCommands.size() != 0);
  83.         }
  84.         //add any valid subCommands.
  85.         for (SubCommandObject s : subCommands) {
  86.             if (s.getType() == type) {
  87.                 if (c.type == type && !c.showIndividualSubs) break;
  88.                 String subName = s.getCommand(command);
  89.                 if (!commandNames.containsKey(subName)) {
  90.                     commandNames.put(s.getCommand(command), true);
  91.                 }
  92.                 if (!showIndividualSubs) break;
  93.             }
  94.         }
  95.     }
  96.     //formats list
  97.     List<String> list = new LinkedList<>();
  98.     if (commandNames.containsValue(true)) {
  99.         List<String> finalList = list;
  100.         commandNames.forEach((s, hasSub) -> {
  101.             if (hasSub) {
  102.                 finalList.add("* " + s);
  103.             } else {
  104.                 finalList.add("  " + s);
  105.             }
  106.         });
  107.     } else {
  108.         list = commandNames.keySet().stream().collect(Collectors.toList());
  109.     }
  110.     //build embed.
  111.     builder.append(codeBlock + "\n");
  112.     builder.append(Utility.listFormatter(list, false));
  113.     builder.append(codeBlock + "\n");
  114.     builder.append(suffix);
  115.     helpEmbed.withDescription(builder.toString());
  116.     RequestHandler.sendEmbedMessage("", helpEmbed, command.channel.get());
  117.     return null;
  118. }
  119.  
  120. //------------------- new code -------------------
  121. @Override
  122. public String execute(String args, CommandObject command) {
  123.     XEmbedBuilder builder = new XEmbedBuilder(command);
  124.     List<SAILType> types = new LinkedList<>();
  125.     Map<SAILType, String> pages = new TreeMap<>();
  126.  
  127.     //get dm commands
  128.     List<Command> dmCommands = Globals.getCommands(true);
  129.     //is creator
  130.     if (command.user.checkIsCreator()) {
  131.         dmCommands.addAll(Globals.getCreatorCommands(true));
  132.         List<Command> creatorCommands = Globals.getCreatorCommands(false);
  133.         //add creator type and page
  134.         pages.put(SAILType.CREATOR, buildPage(creatorCommands, command, SAILType.CREATOR));
  135.         types.add(SAILType.CREATOR);
  136.     }
  137.     //add dm type and page
  138.     pages.put(SAILType.DM, buildPage(dmCommands, command, SAILType.DM));
  139.     types.add(SAILType.DM);
  140.  
  141.     //check visible commands;
  142.     List<Command> visibleCommands = command.guild.commands.stream().filter(c -> GuildHandler.testForPerms(command, c.perms)).collect(Collectors.toList());
  143.  
  144.     //add all extra types
  145.     types.addAll(visibleCommands.stream().map(c -> c.type).collect(Collectors.toList()));
  146.     //remove duplicates
  147.     types = types.stream().distinct().collect(Collectors.toList());
  148.  
  149.     //build pages
  150.     for (SAILType s : types) {
  151.         if (s == SAILType.CREATOR || s == SAILType.DM) continue;
  152.         List<Command> typeCommands = visibleCommands.stream().filter(c -> c.type == s).collect(Collectors.toList());
  153.         for (Command c : visibleCommands) {
  154.             for (SubCommandObject sub : c.subCommands) {
  155.                 if (sub.getType() == s && GuildHandler.testForPerms(command, sub.getPermissions())) {
  156.                     typeCommands.add(c);
  157.                 }
  158.             }
  159.         }
  160.         pages.put(s, buildPage(typeCommands, command, s));
  161.     }
  162.  
  163.     //post type list
  164.     SAILType type = SAILType.get(args);
  165.     boolean typeNull = type == null || !types.contains(type);
  166.     boolean argsNull = args == null || args.isEmpty();
  167.     if (typeNull || argsNull) {
  168.         //get prefix
  169.         String prefix = typeNull && !argsNull ? "> There are no commands with the type: **" + args + "**." : "";
  170.         //title
  171.         builder.withTitle("Here are the Command Types I have available for use:");
  172.         //desc
  173.         builder.withDesc("```\n" +
  174.                 Utility.listFormatter(types.stream().map(t -> t.toString()).collect(Collectors.toList()), false) +
  175.                 "```\n" + missingArgs(command));
  176.         builder.send(prefix, command);
  177.         return null;
  178.     }
  179.  
  180.     //send page
  181.     builder.withTitle("> Here are all of the " + type.toString() + " Commands I have available.");
  182.     builder.withDesc(pages.get(type) + missingArgs(command));
  183.     builder.send(command);
  184.     return null;
  185. }
  186.  
  187. private String buildPage(List<Command> commands, CommandObject command, SAILType type) {
  188.     Map<String, Boolean> commandNames = new TreeMap<>();
  189.  
  190.     //build command name list
  191.     for (Command c : commands) {
  192.         //put command in the map
  193.         boolean isDm = (type == SAILType.DM && c.channel == ChannelSetting.FROM_DM);
  194.         if (c.type == type || isDm) {
  195.             commandNames.put(c.getCommand(command), c.subCommands.size() != 0);
  196.         }
  197.         //add any valid subCommands.
  198.         for (SubCommandObject s : c.subCommands) {
  199.             if (s.getType() == type) {
  200.                 if (c.type == type && !c.showIndividualSubs) break;
  201.                 String subName = s.getCommand(command);
  202.                 if (!commandNames.containsKey(subName)) {
  203.                     commandNames.put(s.getCommand(command), true);
  204.                 }
  205.                 if (!showIndividualSubs) break;
  206.             }
  207.         }
  208.     }
  209.     //format command names
  210.     List<String> list = new LinkedList<>();
  211.     if (commandNames.containsValue(true)) {
  212.         List<String> finalList = list;
  213.         commandNames.forEach((s, hasSub) -> {
  214.             //test prefix
  215.             String prefix = hasSub ? "* " : "  ";
  216.             finalList.add(prefix + s);
  217.         });
  218.     } else {
  219.         list = commandNames.keySet().stream().collect(Collectors.toList());
  220.     }
  221.     //add suffixes to special pages
  222.     String suffix = "";
  223.     switch (type) {
  224.         case DM:
  225.             suffix = "**These commands can only be performed in DMs.**\n" +
  226.                     "> If you send a non command message to my DMs it will send it to my creator.\n\n";
  227.             break;
  228.         case CREATOR:
  229.             suffix = "**Only the creator of this bot can run these commands.**\n\n";
  230.             break;
  231.     }
  232.  
  233.     //finalise page
  234.     return "```\n" + Utility.listFormatter(list, false) + "```\n" + suffix;
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement