Search in sources :

Example 1 with SubCommandObject

use of com.github.vaerys.objects.SubCommandObject in project DiscordSailv2 by Vaerys-Dawn.

the class NewCC method handleNameFilter.

private boolean handleNameFilter(CommandObject command, String nameCC) {
    // ccs cannot have names that match existing commands:
    List<Command> toTest = new ArrayList<>(command.guild.commands);
    toTest.removeAll(exceptions);
    for (Command c : command.guild.commands) {
        // get all commands names.
        List<String> names = new ArrayList<>(Arrays.asList(c.names));
        for (SubCommandObject sc : c.subCommands) {
            names.addAll(Arrays.asList(sc.getNames()));
        }
        // convert them to lowercase.
        ListIterator<String> li = names.listIterator();
        while (li.hasNext()) {
            li.set(li.next().toLowerCase());
        }
        // do check
        if (names.contains(nameCC.toLowerCase())) {
            return true;
        }
    }
    return false;
}
Also used : Command(com.github.vaerys.templates.Command) ArrayList(java.util.ArrayList) SubCommandObject(com.github.vaerys.objects.SubCommandObject)

Example 2 with SubCommandObject

use of com.github.vaerys.objects.SubCommandObject in project DiscordSailv2 by Vaerys-Dawn.

the class Command method getCommandInfo.

/**
 * Creates a message used to fetch the command's documentations
 *
 * @param command
 * @return
 */
public XEmbedBuilder getCommandInfo(CommandObject command) {
    XEmbedBuilder infoEmbed = new XEmbedBuilder(command);
    // command info
    StringBuilder builder = new StringBuilder();
    builder.append(description(command) + "\n");
    builder.append("**Type: **" + type.toString() + ".");
    // display permissions
    if (perms != null && perms.length != 0) {
        builder.append("\n**Perms: **");
        ArrayList<String> permList = new ArrayList<>(perms.length);
        for (Permissions p : perms) {
            permList.add(Utility.enumToString(p));
        }
        builder.append(Utility.listFormatter(permList, true));
    }
    if (names.length > 1) {
        List<String> aliases = Arrays.asList(names).stream().map(s -> command.guild.config.getPrefixCommand() + s).collect(Collectors.toList());
        aliases.remove(0);
        builder.append("\n**Aliases:** " + Utility.listFormatter(aliases, true));
    }
    List<SubCommandObject> objectList = subCommands.stream().filter(subCommandObject -> GuildHandler.testForPerms(command, subCommandObject.getPermissions())).collect(Collectors.toList());
    if (objectList.size() != 0)
        builder.append("\n" + Command.spacer);
    infoEmbed.withTitle("> Help - " + names()[0]);
    infoEmbed.appendField("**" + getUsage(command) + "**    " + Command.spacer, builder.toString(), true);
    for (SubCommandObject s : objectList) {
        infoEmbed.appendField(s.getCommandUsage(command) + "    " + Command.spacer, s.getHelpDesc(command), true);
    }
    // Handle channels
    List<IChannel> channels = command.guild.getChannelsByType(channel);
    List<String> channelMentions = Utility.getChannelMentions(channels);
    // channel
    if (channelMentions.size() > 0) {
        if (channelMentions.size() == 1) {
            infoEmbed.appendField("Channel ", Utility.listFormatter(channelMentions, true), false);
        } else {
            infoEmbed.appendField("Channels ", Utility.listFormatter(channelMentions, true), false);
        }
    }
    return infoEmbed;
}
Also used : XEmbedBuilder(com.github.vaerys.objects.XEmbedBuilder) Arrays(java.util.Arrays) CommandObject(com.github.vaerys.commands.CommandObject) Globals(com.github.vaerys.main.Globals) Logger(org.slf4j.Logger) ChannelSetting(com.github.vaerys.enums.ChannelSetting) LoggerFactory(org.slf4j.LoggerFactory) SAILType(com.github.vaerys.enums.SAILType) GuildHandler(com.github.vaerys.handlers.GuildHandler) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) SubCommandObject(com.github.vaerys.objects.SubCommandObject) List(java.util.List) IChannel(sx.blah.discord.handle.obj.IChannel) SplitFirstObject(com.github.vaerys.objects.SplitFirstObject) Permissions(sx.blah.discord.handle.obj.Permissions) Pattern(java.util.regex.Pattern) LinkedList(java.util.LinkedList) Utility(com.github.vaerys.main.Utility) XEmbedBuilder(com.github.vaerys.objects.XEmbedBuilder) IChannel(sx.blah.discord.handle.obj.IChannel) ArrayList(java.util.ArrayList) Permissions(sx.blah.discord.handle.obj.Permissions) SubCommandObject(com.github.vaerys.objects.SubCommandObject)

Example 3 with SubCommandObject

use of com.github.vaerys.objects.SubCommandObject in project DiscordSailv2 by Vaerys-Dawn.

the class Commands method buildPage.

private String buildPage(List<Command> commands, CommandObject command, SAILType type, List<SAILType> types) {
    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.testSubCommands(command, types));
        }
        // add any valid subCommands.
        for (SubCommandObject s : c.subCommands) {
            if (s.getType() == type) {
                if (c.type == type && !c.showIndividualSubs)
                    break;
                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) -> finalList.add((hasSub ? "* " : "  ") + s));
    } else {
        list = commandNames.keySet().stream().collect(Collectors.toList());
    }
    // add suffixes to special pages
    String suffix = "";
    if (type == SAILType.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";
    } else if (type == SAILType.CREATOR) {
        suffix = "**Only the creator of this bot can run these commands.**\n\n";
    }
    // finalise page
    return "```\n" + Utility.listFormatter(list, false) + "```\n" + suffix;
}
Also used : Command(com.github.vaerys.templates.Command) SubCommandObject(com.github.vaerys.objects.SubCommandObject)

Example 4 with SubCommandObject

use of com.github.vaerys.objects.SubCommandObject in project DiscordSailv2 by Vaerys-Dawn.

the class Commands method execute.

@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));
        // add creator type and page
        List<Command> creatorCommands = Globals.getCreatorCommands(false);
        pages.put(SAILType.CREATOR, buildPage(creatorCommands, command, SAILType.CREATOR, types));
        types.add(SAILType.CREATOR);
    }
    // add dm type and page
    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 dm page
    pages.put(SAILType.DM, buildPage(dmCommands, command, SAILType.DM, types));
    // 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, types));
    }
    // 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;
}
Also used : SubCommandObject(com.github.vaerys.objects.SubCommandObject) XEmbedBuilder(com.github.vaerys.objects.XEmbedBuilder) java.util(java.util) CommandObject(com.github.vaerys.commands.CommandObject) Globals(com.github.vaerys.main.Globals) ChannelSetting(com.github.vaerys.enums.ChannelSetting) SAILType(com.github.vaerys.enums.SAILType) Permissions(sx.blah.discord.handle.obj.Permissions) Command(com.github.vaerys.templates.Command) GuildHandler(com.github.vaerys.handlers.GuildHandler) Utility(com.github.vaerys.main.Utility) Collectors(java.util.stream.Collectors) XEmbedBuilder(com.github.vaerys.objects.XEmbedBuilder) SAILType(com.github.vaerys.enums.SAILType) Command(com.github.vaerys.templates.Command) SubCommandObject(com.github.vaerys.objects.SubCommandObject)

Example 5 with SubCommandObject

use of com.github.vaerys.objects.SubCommandObject in project DiscordSailv2 by Vaerys-Dawn.

the class Command method isVisibleInType.

public boolean isVisibleInType(CommandObject commandObject, SAILType type) {
    if (this.type == type) {
        return GuildHandler.testForPerms(commandObject, perms);
    } else {
        boolean hasPerms = false;
        for (SubCommandObject s : subCommands) {
            List<Permissions> allPerms = new ArrayList<>(Arrays.asList(perms));
            allPerms.addAll(Arrays.asList(s.getPermissions()));
            if (GuildHandler.testForPerms(commandObject, allPerms)) {
                hasPerms = true;
            }
        }
        return hasPerms;
    }
}
Also used : Permissions(sx.blah.discord.handle.obj.Permissions) ArrayList(java.util.ArrayList) SubCommandObject(com.github.vaerys.objects.SubCommandObject)

Aggregations

SubCommandObject (com.github.vaerys.objects.SubCommandObject)6 ArrayList (java.util.ArrayList)4 Command (com.github.vaerys.templates.Command)3 Permissions (sx.blah.discord.handle.obj.Permissions)3 CommandObject (com.github.vaerys.commands.CommandObject)2 ChannelSetting (com.github.vaerys.enums.ChannelSetting)2 SAILType (com.github.vaerys.enums.SAILType)2 GuildHandler (com.github.vaerys.handlers.GuildHandler)2 Globals (com.github.vaerys.main.Globals)2 Utility (com.github.vaerys.main.Utility)2 XEmbedBuilder (com.github.vaerys.objects.XEmbedBuilder)2 Collectors (java.util.stream.Collectors)2 SplitFirstObject (com.github.vaerys.objects.SplitFirstObject)1 java.util (java.util)1 Arrays (java.util.Arrays)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Pattern (java.util.regex.Pattern)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Logger (org.slf4j.Logger)1