Search in sources :

Example 1 with DummyCommand

use of com.jockie.bot.core.command.impl.DummyCommand in project Sx4 by sx4-discord-bot.

the class Sx4CommandEventListener method onCommandExecuted.

public void onCommandExecuted(ICommand command, CommandEvent event) {
    Sx4Command effectiveCommand = command instanceof DummyCommand ? (Sx4Command) ((DummyCommand) command).getActualCommand() : (Sx4Command) command;
    Map<String, Object> options = event.getOptions();
    Map<String, String> rawOptions = new HashMap<>();
    for (String key : options.keySet()) {
        rawOptions.put(key, String.valueOf(options.get(key)));
    }
    Document commandData = new Document("messageId", event.getMessage().getIdLong()).append("content", event.getMessage().getContentRaw()).append("command", new Document("name", command.getCommandTrigger()).append("id", effectiveCommand.getId())).append("module", command.getCategory() == null ? null : command.getCategory().getName()).append("aliasUsed", event.getCommandTrigger()).append("authorId", event.getAuthor().getIdLong()).append("channelId", event.getChannel().getIdLong()).append("arguments", Arrays.asList(event.getRawArguments())).append("options", rawOptions).append("prefix", event.getPrefix()).append("executionDuration", event.getTimeSinceStarted());
    if (event.isFromGuild()) {
        commandData.append("guildId", event.getGuild().getIdLong());
    }
    this.bot.getMongo().insertCommand(commandData).whenComplete(MongoDatabase.exceptionally());
}
Also used : HashMap(java.util.HashMap) DummyCommand(com.jockie.bot.core.command.impl.DummyCommand) Document(org.bson.Document)

Example 2 with DummyCommand

use of com.jockie.bot.core.command.impl.DummyCommand 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

DummyCommand (com.jockie.bot.core.command.impl.DummyCommand)2 ICommand (com.jockie.bot.core.command.ICommand)1 Sx4Command (com.sx4.bot.core.Sx4Command)1 Formatter (java.util.Formatter)1 HashMap (java.util.HashMap)1 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 MessageBuilder (net.dv8tion.jda.api.MessageBuilder)1 Permission (net.dv8tion.jda.api.Permission)1 Message (net.dv8tion.jda.api.entities.Message)1 Document (org.bson.Document)1