Search in sources :

Example 1 with ArgumentController

use of net.robinfriedli.aiode.command.argument.ArgumentController in project aiode by robinfriedli.

the class HelpCommand method showCommandHelp.

private void showCommandHelp() {
    getManager().getCommand(getContext(), getCommandInput()).ifPresentOrElse(command -> {
        String prefix;
        GuildSpecification specification = getContext().getGuildContext().getSpecification();
        String setPrefix = specification.getPrefix();
        String botName = specification.getBotName();
        if (!Strings.isNullOrEmpty(setPrefix)) {
            prefix = setPrefix;
        } else if (!Strings.isNullOrEmpty(botName)) {
            prefix = botName + " ";
        } else {
            prefix = PrefixProperty.DEFAULT_FALLBACK + " ";
        }
        char argumentPrefix = ArgumentPrefixProperty.getForCurrentContext().getArgumentPrefix();
        EmbedBuilder embedBuilder = new EmbedBuilder();
        embedBuilder.setTitle("Command " + command.getIdentifier() + ":");
        String descriptionFormat = command.getDescription();
        String descriptionText = String.format(descriptionFormat, prefix, argumentPrefix);
        embedBuilder.setDescription(descriptionText);
        Guild guild = getContext().getGuild();
        Optional<AccessConfiguration> accessConfiguration = Aiode.get().getSecurityManager().getAccessConfiguration(command.getPermissionTarget(), guild);
        if (accessConfiguration.isPresent()) {
            String title = "Available to roles: ";
            String text;
            List<Role> roles = accessConfiguration.get().getRoles(guild);
            if (!roles.isEmpty()) {
                text = StringList.create(roles, Role::getName).toSeparatedString(", ");
            } else {
                text = "Guild owner and administrator roles only";
            }
            embedBuilder.addField(title, text, false);
        }
        ArgumentController argumentController = command.getArgumentController();
        if (argumentController.hasArguments()) {
            embedBuilder.addField("__Arguments__", "Keywords that alter the command behavior or define a search scope.", false);
            argumentController.getArguments().values().stream().sorted(Comparator.comparing(CommandArgument::getIdentifier)).forEach(argument -> embedBuilder.addField(argumentPrefix + argument.getIdentifier(), String.format(argument.getDescription(), prefix, argumentPrefix), false));
        }
        List<XmlElement> examples = command.getCommandContribution().query(tagName("example")).collect();
        if (!examples.isEmpty()) {
            embedBuilder.addField("__Examples__", "Practical usage examples for this command.", false);
            for (XmlElement example : examples) {
                String exampleText = String.format(example.getTextContent(), prefix, argumentPrefix);
                String titleText = String.format(example.getAttribute("title").getValue(), prefix, argumentPrefix);
                embedBuilder.addField(titleText, exampleText, false);
            }
        }
        sendMessage(embedBuilder);
    }, () -> {
        throw new InvalidCommandException(String.format("No command found for '%s'", getCommandInput()));
    });
}
Also used : CommandArgument(net.robinfriedli.aiode.command.argument.CommandArgument) Guild(net.dv8tion.jda.api.entities.Guild) Role(net.dv8tion.jda.api.entities.Role) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ArgumentController(net.robinfriedli.aiode.command.argument.ArgumentController) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) XmlElement(net.robinfriedli.jxp.api.XmlElement) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration)

Example 2 with ArgumentController

use of net.robinfriedli.aiode.command.argument.ArgumentController in project aiode by robinfriedli.

the class ArgumentBuildingMode method terminate.

@Override
public void terminate() {
    try {
        if (argumentBuilder.length() == 0) {
            throw new InvalidArgumentException("Missing argument identifier");
        }
        ArgumentController argumentController = command.getArgumentController();
        String argument = argumentBuilder.toString().trim();
        String argumentValue = argumentValueBuilder.toString().trim();
        argumentController.setArgument(argument, argumentValue);
        commandParser.fireOnArgumentParsed(argument, argumentValue);
    } catch (UserException e) {
        throw new CommandParseException(e.getMessage(), command.getCommandBody(), e, conceptionIndex);
    }
}
Also used : CommandParseException(net.robinfriedli.aiode.exceptions.CommandParseException) InvalidArgumentException(net.robinfriedli.aiode.exceptions.InvalidArgumentException) ArgumentController(net.robinfriedli.aiode.command.argument.ArgumentController) UserException(net.robinfriedli.aiode.exceptions.UserException)

Aggregations

ArgumentController (net.robinfriedli.aiode.command.argument.ArgumentController)2 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 Guild (net.dv8tion.jda.api.entities.Guild)1 Role (net.dv8tion.jda.api.entities.Role)1 CommandArgument (net.robinfriedli.aiode.command.argument.CommandArgument)1 AccessConfiguration (net.robinfriedli.aiode.entities.AccessConfiguration)1 GuildSpecification (net.robinfriedli.aiode.entities.GuildSpecification)1 CommandParseException (net.robinfriedli.aiode.exceptions.CommandParseException)1 InvalidArgumentException (net.robinfriedli.aiode.exceptions.InvalidArgumentException)1 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)1 UserException (net.robinfriedli.aiode.exceptions.UserException)1 XmlElement (net.robinfriedli.jxp.api.XmlElement)1