Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //------------------- old code ------------------------
- @Override
- public String execute(String args, CommandObject command) {
- XEmbedBuilder helpEmbed = new XEmbedBuilder(command);
- List<SAILType> types = new ArrayList<>();
- List<SAILType> filteredTypes = new ArrayList<>();
- //get commands
- List<Command> commands = new ArrayList<>(command.guild.getAllCommands(command));
- //if creator get creator commands
- if (command.user.checkIsCreator()) {
- commands.addAll(Globals.getALLCreatorCommands());
- }
- //add dm commands.
- types.add(SAILType.DM);
- commands.addAll(Globals.getCommands(true));
- //get types
- commands.forEach(command1 -> {
- if (!types.contains(command1.type)) types.add(command1.type);
- });
- //remove types that the user does not have permission to see.
- Collections.sort(types);
- filteredTypes.addAll(types.stream()
- .filter(t -> Utility.getCommandsByType(commands, command, t, true).size() != 0)
- .collect(Collectors.toList()));
- //print out list of types.
- if (args == null || args.isEmpty()) {
- StringBuilder builder = new StringBuilder();
- builder.append(codeBlock + "\n");
- builder.append(Utility.listEnumFormatter(filteredTypes, false));
- builder.append(codeBlock + "\n");
- helpEmbed.withTitle("Here are the Command Types I have available for use:");
- builder.append(Utility.getCommandInfo(this, command) + "\n");
- helpEmbed.withDescription(builder.toString());
- RequestHandler.sendEmbedMessage("", helpEmbed, command.channel.get());
- return null;
- }
- //make sure that the type exits
- SAILType type = SAILType.get(args);
- if (type == null || !types.contains(type)) {
- return "> There are no commands with the type: **" + args + "**.\n\n" + Utility.getCommandInfo(this, command);
- }
- //make sure that the user is allowed to view the type.
- if (!filteredTypes.contains(type)) {
- return "> You do not have permission to see the **" + type.toString() + "** page.";
- }
- StringHandler builder = new StringHandler();
- String suffix = Utility.getCommandInfo(new Help(), command);
- helpEmbed.withTitle("> Here are all of the " + type.toString() + " Commands I have available.");
- //special footers
- switch (type) {
- case DM:
- suffix = "**These commands can only be performed in DMs.**\n" +
- "> If you send a non command message to my DMs it will send it to my creator.\n\n" + suffix;
- break;
- case CREATOR:
- suffix = "**Only the creator of this bot can run these commands.**\n\n" + suffix;
- break;
- }
- //build command list
- TreeMap<String, Boolean> commandNames = new TreeMap<>();
- for (Command c : commands) {
- //filter out subCommands that aren't allowed to be seen due to missing perms or inactive module.
- List<SubCommandObject> subCommands = c.subCommands.stream()
- .filter(s -> types.contains(s.getType()))
- .filter(s -> GuildHandler.testForPerms(command, s.getPermissions()))
- .collect(Collectors.toList());
- //add command if command type == type specified.
- if (c.type == type || (c.type != type && c.channel == ChannelSetting.FROM_DM)) {
- commandNames.put(c.getCommand(command), subCommands.size() != 0);
- }
- //add any valid subCommands.
- for (SubCommandObject s : subCommands) {
- if (s.getType() == type) {
- if (c.type == type && !c.showIndividualSubs) break;
- String subName = s.getCommand(command);
- if (!commandNames.containsKey(subName)) {
- commandNames.put(s.getCommand(command), true);
- }
- if (!showIndividualSubs) break;
- }
- }
- }
- //formats list
- List<String> list = new LinkedList<>();
- if (commandNames.containsValue(true)) {
- List<String> finalList = list;
- commandNames.forEach((s, hasSub) -> {
- if (hasSub) {
- finalList.add("* " + s);
- } else {
- finalList.add(" " + s);
- }
- });
- } else {
- list = commandNames.keySet().stream().collect(Collectors.toList());
- }
- //build embed.
- builder.append(codeBlock + "\n");
- builder.append(Utility.listFormatter(list, false));
- builder.append(codeBlock + "\n");
- builder.append(suffix);
- helpEmbed.withDescription(builder.toString());
- RequestHandler.sendEmbedMessage("", helpEmbed, command.channel.get());
- return null;
- }
- //------------------- new code -------------------
- @Override
- public String execute(String args, CommandObject command) {
- XEmbedBuilder builder = new XEmbedBuilder(command);
- List<SAILType> types = new LinkedList<>();
- Map<SAILType, String> pages = new TreeMap<>();
- //get dm commands
- List<Command> dmCommands = Globals.getCommands(true);
- //is creator
- if (command.user.checkIsCreator()) {
- dmCommands.addAll(Globals.getCreatorCommands(true));
- List<Command> creatorCommands = Globals.getCreatorCommands(false);
- //add creator type and page
- pages.put(SAILType.CREATOR, buildPage(creatorCommands, command, SAILType.CREATOR));
- types.add(SAILType.CREATOR);
- }
- //add dm type and page
- pages.put(SAILType.DM, buildPage(dmCommands, command, SAILType.DM));
- types.add(SAILType.DM);
- //check visible commands;
- List<Command> visibleCommands = command.guild.commands.stream().filter(c -> GuildHandler.testForPerms(command, c.perms)).collect(Collectors.toList());
- //add all extra types
- types.addAll(visibleCommands.stream().map(c -> c.type).collect(Collectors.toList()));
- //remove duplicates
- types = types.stream().distinct().collect(Collectors.toList());
- //build pages
- for (SAILType s : types) {
- if (s == SAILType.CREATOR || s == SAILType.DM) continue;
- List<Command> typeCommands = visibleCommands.stream().filter(c -> c.type == s).collect(Collectors.toList());
- for (Command c : visibleCommands) {
- for (SubCommandObject sub : c.subCommands) {
- if (sub.getType() == s && GuildHandler.testForPerms(command, sub.getPermissions())) {
- typeCommands.add(c);
- }
- }
- }
- pages.put(s, buildPage(typeCommands, command, s));
- }
- //post type list
- SAILType type = SAILType.get(args);
- boolean typeNull = type == null || !types.contains(type);
- boolean argsNull = args == null || args.isEmpty();
- if (typeNull || argsNull) {
- //get prefix
- String prefix = typeNull && !argsNull ? "> There are no commands with the type: **" + args + "**." : "";
- //title
- builder.withTitle("Here are the Command Types I have available for use:");
- //desc
- builder.withDesc("```\n" +
- Utility.listFormatter(types.stream().map(t -> t.toString()).collect(Collectors.toList()), false) +
- "```\n" + missingArgs(command));
- builder.send(prefix, command);
- return null;
- }
- //send page
- builder.withTitle("> Here are all of the " + type.toString() + " Commands I have available.");
- builder.withDesc(pages.get(type) + missingArgs(command));
- builder.send(command);
- return null;
- }
- private String buildPage(List<Command> commands, CommandObject command, SAILType type) {
- Map<String, Boolean> commandNames = new TreeMap<>();
- //build command name list
- for (Command c : commands) {
- //put command in the map
- boolean isDm = (type == SAILType.DM && c.channel == ChannelSetting.FROM_DM);
- if (c.type == type || isDm) {
- commandNames.put(c.getCommand(command), c.subCommands.size() != 0);
- }
- //add any valid subCommands.
- for (SubCommandObject s : c.subCommands) {
- if (s.getType() == type) {
- if (c.type == type && !c.showIndividualSubs) break;
- String subName = s.getCommand(command);
- if (!commandNames.containsKey(subName)) {
- commandNames.put(s.getCommand(command), true);
- }
- if (!showIndividualSubs) break;
- }
- }
- }
- //format command names
- List<String> list = new LinkedList<>();
- if (commandNames.containsValue(true)) {
- List<String> finalList = list;
- commandNames.forEach((s, hasSub) -> {
- //test prefix
- String prefix = hasSub ? "* " : " ";
- finalList.add(prefix + s);
- });
- } else {
- list = commandNames.keySet().stream().collect(Collectors.toList());
- }
- //add suffixes to special pages
- String suffix = "";
- switch (type) {
- case DM:
- suffix = "**These commands can only be performed in DMs.**\n" +
- "> If you send a non command message to my DMs it will send it to my creator.\n\n";
- break;
- case CREATOR:
- suffix = "**Only the creator of this bot can run these commands.**\n\n";
- break;
- }
- //finalise page
- return "```\n" + Utility.listFormatter(list, false) + "```\n" + suffix;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement