Advertisement
Vaerys_Dawn

Commands command

Feb 26th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.27 KB | None | 0 0
  1.     @Override
  2.     public String execute(String args, CommandObject command) {
  3.         XEmbedBuilder helpEmbed = new XEmbedBuilder(command);
  4.         List<SAILType> types = new ArrayList<>();
  5.  
  6.         //get commands
  7.         List<Command> commands = new ArrayList<>(command.guild.getAllCommands(command));
  8.  
  9.         //if creator get creator commands
  10.         if (command.user.longID == command.client.creator.longID) {
  11.             commands.addAll(Globals.getCreatorCommands(false));
  12.         }
  13.  
  14.         //get types
  15.         commands.forEach(command1 -> {
  16.             if (!types.contains(command1.type))
  17.                 types.add(command1.type);
  18.         });
  19.  
  20.         //remove types that the user does not have permission to see.
  21.         ListIterator iterator = types.listIterator();
  22.         while (iterator.hasNext()) {
  23.             SAILType t = (SAILType) iterator.next();
  24.             if (Utility.getCommandsByType(commands, command, t, true).size() == 0) {
  25.                 iterator.remove();
  26.             }
  27.         }
  28.  
  29.         //sort types
  30.         Collections.sort(types);
  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(types, 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) {
  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 (!types.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.         switch (type) {
  62.             case DM:
  63.                 suffix = "**These commands can only be performed in DMs.**\n" +
  64.                         "> If you send a non command message to my DMs it will send it to my creator.\n\n" + suffix;
  65.                 break;
  66.             case CREATOR:
  67.                 suffix = "**Only the creator of this bot can run these commands.**\n\n" + suffix;
  68.                 break;
  69.         }
  70.        
  71.         //build command list
  72.         List<Command> typeCommands = Utility.getCommandsByType(commands, command, type, true);
  73.         typeCommands.sort(Comparator.comparing(o -> o.getCommand(command)));
  74.         final boolean[] subCommandFound = {false};
  75.         List<String> commandNames = typeCommands.stream().map(c -> {
  76.             StringHandler commandName = new StringHandler("  " + c.getCommand(command));
  77.             for (SubCommandObject sub : c.subCommands) {
  78.                 if (GuildHandler.testForPerms(command, sub.getPermissions())) {
  79.                     commandName.replace(0, 1, "*");
  80.                     subCommandFound[0] = true;
  81.                     break;
  82.                 }
  83.             }
  84.             return commandName.toString();
  85.         }).collect(Collectors.toList());
  86.        
  87.         //remove indent if no subCommands are found
  88.         if (!subCommandFound[0]) {
  89.             commandNames = commandNames.stream()
  90.                     .map(s1 -> new StringHandler(s1).replace(0, 2, "")
  91.                             .toString()).collect(Collectors.toList());
  92.         }
  93.  
  94.         //build embed.
  95.         builder.append(codeBlock + "\n");
  96.         builder.append(Utility.listFormatter(commandNames, false));
  97.         builder.append(codeBlock + "\n");
  98.         builder.append(suffix);
  99.         helpEmbed.withDescription(builder.toString());
  100.         RequestHandler.sendEmbedMessage("", helpEmbed, command.channel.get());
  101.         return null;
  102.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement