Search in sources :

Example 11 with Sx4Command

use of com.sx4.bot.core.Sx4Command in project Sx4 by sx4-discord-bot.

the class HelpUtility method getHelpMessage.

public static Message getHelpMessage(ICommand initialCommand, boolean embed) {
    MessageBuilder builder = new MessageBuilder();
    Sx4Command command = initialCommand instanceof DummyCommand ? (Sx4Command) ((DummyCommand) initialCommand).getActualCommand() : (Sx4Command) initialCommand;
    String usage = command.getSubCommands().isEmpty() ? command.getUsage() : command.getUsage().trim().equals(command.getCommandTrigger()) ? command.getUsage() + " <sub command>" : command.getUsage() + " | <sub command>";
    StringBuilder options = new StringBuilder();
    for (int i = 0; i < command.getOptions().size(); i++) {
        IOption<?> option = command.getOptions().get(i);
        options.append("`").append(option.getName()).append(option.getType() != boolean.class ? "=<value>" : "").append("` - ").append(option.getDescription()).append(i == command.getOptions().size() - 1 ? "" : "\n");
    }
    if (embed) {
        EmbedBuilder embedBuilder = new EmbedBuilder();
        embedBuilder.setTitle(command.getCommandTrigger());
        embedBuilder.addField("Description", command.getDescription(), false);
        embedBuilder.addField("Usage", usage, false);
        if (!command.getOptions().isEmpty()) {
            embedBuilder.addField("Options", options.toString(), false);
        }
        if (command.getExamples().length != 0) {
            embedBuilder.addField("Examples", "`" + String.join("`\n`", command.getExamples()) + "`", false);
        }
        if (!command.getAuthorDiscordPermissions().isEmpty()) {
            embedBuilder.addField("Required Permissions", command.getAuthorDiscordPermissions().stream().map(Permission::getName).collect(Collectors.joining(", ")), false);
        }
        if (!command.getAliases().isEmpty()) {
            embedBuilder.addField("Aliases", String.join(", ", command.getAliases()), false);
        }
        if (command.getRedirects().length != 0) {
            embedBuilder.addField("Redirects", String.join(", ", command.getRedirects()), false);
        }
        if (!command.getSubCommands().isEmpty()) {
            embedBuilder.addField("Sub Commands", command.getSubCommands().stream().map(ICommand::getCommand).collect(Collectors.joining(", ")), false);
        }
        if (command.isPremiumCommand()) {
            embedBuilder.setFooter("Premium Command ⭐");
        }
        return builder.setEmbeds(embedBuilder.build()).build();
    } else {
        String placeHolder = "%s:\n%s\n\n";
        Formatter formatter = new Formatter();
        formatter.format(">>> **" + command.getCommandTrigger() + "**\n\n");
        formatter.format(placeHolder, "Description", command.getDescription());
        formatter.format(placeHolder, "Usage", usage);
        if (!command.getOptions().isEmpty()) {
            formatter.format(placeHolder, "Options", options);
        }
        if (command.getExamples().length != 0) {
            formatter.format(placeHolder, "Examples", "`" + String.join("`\n`", command.getExamples()) + "`");
        }
        if (!command.getAuthorDiscordPermissions().isEmpty()) {
            formatter.format(placeHolder, "Required Permissions", command.getAuthorDiscordPermissions().stream().map(Permission::getName).collect(Collectors.joining(", ")));
        }
        if (!command.getAliases().isEmpty()) {
            formatter.format(placeHolder, "Aliases", String.join(", ", command.getAliases()));
        }
        if (command.getRedirects().length != 0) {
            formatter.format(placeHolder, "Redirects", String.join(", ", command.getRedirects()));
        }
        if (!command.getSubCommands().isEmpty()) {
            formatter.format(placeHolder, "Required Permissions", command.getSubCommands().stream().map(ICommand::getCommand).collect(Collectors.joining(", ")));
        }
        Message message = builder.setContent(formatter.toString()).build();
        formatter.close();
        return message;
    }
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Sx4Command(com.sx4.bot.core.Sx4Command) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) Message(net.dv8tion.jda.api.entities.Message) ICommand(com.jockie.bot.core.command.ICommand) Formatter(java.util.Formatter) Permission(net.dv8tion.jda.api.Permission) DummyCommand(com.jockie.bot.core.command.impl.DummyCommand)

Aggregations

Sx4Command (com.sx4.bot.core.Sx4Command)10 Argument (com.jockie.bot.core.argument.Argument)8 ModuleCategory (com.sx4.bot.category.ModuleCategory)8 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)8 PagedResult (com.sx4.bot.paged.PagedResult)8 Permission (net.dv8tion.jda.api.Permission)8 Collectors (java.util.stream.Collectors)7 User (net.dv8tion.jda.api.entities.User)7 Document (org.bson.Document)7 Bson (org.bson.conversions.Bson)7 Command (com.jockie.bot.core.command.Command)6 com.mongodb.client.model (com.mongodb.client.model)6 AlternativeOptions (com.sx4.bot.annotations.argument.AlternativeOptions)6 AuthorPermissions (com.sx4.bot.annotations.command.AuthorPermissions)6 CommandId (com.sx4.bot.annotations.command.CommandId)6 Examples (com.sx4.bot.annotations.command.Examples)6 Operators (com.sx4.bot.database.mongo.model.Operators)6 Alternative (com.sx4.bot.entities.argument.Alternative)6 HolderType (com.sx4.bot.entities.settings.HolderType)6 ExceptionUtility (com.sx4.bot.utility.ExceptionUtility)6