Search in sources :

Example 1 with BaseCommandEvent

use of com.freya02.botcommands.api.prefixed.BaseCommandEvent in project BotCommands by freya022.

the class HelpCommand method getCommandHelpEmbed.

@NotNull
private EmbedBuilder getCommandHelpEmbed(BaseCommandEvent event, TextCommandCandidates candidates) {
    final CommandPath path = candidates.first().getPath();
    final Member member = event.getMember();
    final long memberId = member.getIdLong();
    final Map<CommandPath, EmbedBuilder> map = memberEmbedMap.computeIfAbsent(memberId, x -> new HashMap<>());
    final EmbedBuilder builder;
    final EmbedBuilder existingEmbed = map.get(path);
    if (existingEmbed != null) {
        builder = new EmbedBuilder(existingEmbed);
    } else {
        builder = Utils.generateCommandHelp(candidates, event);
        map.put(path, builder);
    }
    builder.setTimestamp(Instant.now());
    builder.setColor(member.getColorRaw());
    return builder;
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Member(net.dv8tion.jda.api.entities.Member) CommandPath(com.freya02.botcommands.api.application.CommandPath) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with BaseCommandEvent

use of com.freya02.botcommands.api.prefixed.BaseCommandEvent in project BotCommands by freya022.

the class HelpCommand method sendCommandHelp.

public void sendCommandHelp(BaseCommandEvent event, CommandPath cmdPath) {
    TextCommandCandidates cmds = event.getContext().findCommands(cmdPath);
    if (cmds == null) {
        event.respond("Command '" + getSpacedPath(cmdPath) + "' does not exist").queue(null, event.failureReporter("Failed to send help"));
        return;
    }
    final Member member = event.getMember();
    final GuildMessageChannel channel = event.getGuildChannel();
    final Usability usability = Usability.of(context, cmds.first(), member, channel, !context.isOwner(member.getIdLong()));
    if (usability.isNotShowable()) {
        event.respond("Command '" + getSpacedPath(cmdPath) + "' does not exist").queue(null, event.failureReporter("Failed to send help"));
        return;
    }
    final EmbedBuilder embed = getCommandHelpEmbed(event, cmds);
    event.respond(embed.build()).queue();
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Member(net.dv8tion.jda.api.entities.Member) Usability(com.freya02.botcommands.internal.Usability) GuildMessageChannel(net.dv8tion.jda.api.entities.GuildMessageChannel)

Example 3 with BaseCommandEvent

use of com.freya02.botcommands.api.prefixed.BaseCommandEvent in project BotCommands by freya022.

the class HelpCommand method getAllHelp.

private synchronized void getAllHelp(BaseCommandEvent event) {
    final EmbedBuilder builder = event.getContext().isOwner(event.getAuthor().getIdLong()) ? ownerHelpBuilder : getMemberGlobalHelpContent(event.getMember(), event.getGuildChannel());
    builder.setTimestamp(Instant.now());
    final Member member = event.getMember();
    builder.setColor(member.getColorRaw());
    builder.setFooter("NSFW commands might not be shown\nRun help in an NSFW channel to see them\n");
    final MessageEmbed embed = builder.build();
    event.getAuthor().openPrivateChannel().queue(privateChannel -> event.sendWithEmbedFooterIcon(privateChannel, embed, event.failureReporter("Unable to send help message")).queue(m -> event.reactSuccess().queue(), t -> event.reactError().queue()), t -> event.getChannel().sendMessage(context.getDefaultMessages(event.getGuild()).getClosedDMErrorMsg()).queue());
}
Also used : BaseCommandEvent(com.freya02.botcommands.api.prefixed.BaseCommandEvent) java.util(java.util) BContextImpl(com.freya02.botcommands.internal.BContextImpl) Category(com.freya02.botcommands.api.prefixed.annotations.Category) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) GuildMessageChannel(net.dv8tion.jda.api.entities.GuildMessageChannel) Member(net.dv8tion.jda.api.entities.Member) TextOption(com.freya02.botcommands.api.prefixed.annotations.TextOption) Instant(java.time.Instant) Function(java.util.function.Function) CommandEvent(com.freya02.botcommands.api.prefixed.CommandEvent) Usability(com.freya02.botcommands.internal.Usability) Consumer(java.util.function.Consumer) JDATextCommand(com.freya02.botcommands.api.prefixed.annotations.JDATextCommand) CommandPath(com.freya02.botcommands.api.application.CommandPath) TextCommand(com.freya02.botcommands.api.prefixed.TextCommand) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) BContext(com.freya02.botcommands.api.BContext) Description(com.freya02.botcommands.api.prefixed.annotations.Description) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) Member(net.dv8tion.jda.api.entities.Member)

Example 4 with BaseCommandEvent

use of com.freya02.botcommands.api.prefixed.BaseCommandEvent in project BotCommands by freya022.

the class Utils method generateCommandHelp.

public static EmbedBuilder generateCommandHelp(TextCommandCandidates candidates, BaseCommandEvent event) {
    final EmbedBuilder builder = event.getDefaultEmbed();
    final TextCommandInfo commandInfo = candidates.last();
    final String name = commandInfo.getPath().getFullPath().replace('/', ' ');
    final String description = Utils.getDescription(commandInfo);
    final String prefix = event.getContext().getPrefix();
    final MessageEmbed.AuthorInfo author = builder.isEmpty() ? null : event.getDefaultEmbed().build().getAuthor();
    if (author != null) {
        builder.setAuthor(author.getName() + " – '" + name + "' command", author.getUrl(), author.getIconUrl());
    } else {
        builder.setAuthor('\'' + name + "' command");
    }
    if (description != null) {
        builder.addField("Description", description, false);
    }
    final ArrayList<TextCommandInfo> reversedCandidates = new ArrayList<>(candidates);
    Collections.reverse(reversedCandidates);
    int i = 1;
    for (TextCommandInfo candidate : reversedCandidates) {
        final List<? extends TextCommandParameter> commandParameters = candidate.getOptionParameters();
        final StringBuilder syntax = new StringBuilder("**Syntax**: " + prefix + name + ' ');
        final StringBuilder example = new StringBuilder("**Example**: " + prefix + name + ' ');
        if (candidate.isRegexCommand()) {
            final boolean needsQuote = hasMultipleQuotable(commandParameters);
            for (TextCommandParameter commandParameter : commandParameters) {
                final Class<?> boxedType = commandParameter.getBoxedType();
                final String argName = getArgName(needsQuote, commandParameter, boxedType);
                final String argExample = getArgExample(needsQuote, commandParameter, boxedType);
                final boolean isOptional = commandParameter.isOptional();
                syntax.append(isOptional ? '[' : '`').append(argName).append(isOptional ? ']' : '`').append(' ');
                example.append(argExample).append(' ');
            }
        }
        final String effectiveCandidateDescription = !candidate.hasDescription() ? "" : ("**Description**: " + candidate.getDescription() + "\n");
        if (candidates.size() == 1) {
            builder.addField("Usage", effectiveCandidateDescription + syntax + "\n" + example, false);
        } else {
            builder.addField("Overload #" + i, effectiveCandidateDescription + syntax + "\n" + example, false);
        }
        i++;
    }
    final List<TextCommandCandidates> textSubcommands = event.getContext().findTextSubcommands(commandInfo.getPath());
    if (textSubcommands != null) {
        final String subcommandHelp = textSubcommands.stream().map(TreeSet::first).map(info -> "**" + info.getPath().getNameAt(info.getPath().getNameCount() - commandInfo.getPath().getNameCount()) + "** : " + Utils.getNonBlankDescription(info)).collect(Collectors.joining("\n - "));
        if (!subcommandHelp.isBlank()) {
            builder.addField("Subcommands", subcommandHelp, false);
        }
    }
    final Consumer<EmbedBuilder> descConsumer = commandInfo.getInstance().getDetailedDescription();
    if (descConsumer != null) {
        descConsumer.accept(builder);
    }
    return builder;
}
Also used : BaseCommandEvent(com.freya02.botcommands.api.prefixed.BaseCommandEvent) net.dv8tion.jda.api.entities(net.dv8tion.jda.api.entities) java.util(java.util) Logger(org.slf4j.Logger) Category(com.freya02.botcommands.api.prefixed.annotations.Category) Emoji(com.freya02.botcommands.api.entities.Emoji) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Logging(com.freya02.botcommands.api.Logging) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) QuotableRegexParameterResolver(com.freya02.botcommands.api.parameters.QuotableRegexParameterResolver) Consumer(java.util.function.Consumer) Nullable(org.jetbrains.annotations.Nullable) Parameter(java.lang.reflect.Parameter) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) NotNull(org.jetbrains.annotations.NotNull) EmojiOrEmote(com.freya02.botcommands.api.entities.EmojiOrEmote) Description(com.freya02.botcommands.api.prefixed.annotations.Description) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder)

Aggregations

EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)4 Member (net.dv8tion.jda.api.entities.Member)3 NotNull (org.jetbrains.annotations.NotNull)3 CommandPath (com.freya02.botcommands.api.application.CommandPath)2 BaseCommandEvent (com.freya02.botcommands.api.prefixed.BaseCommandEvent)2 Category (com.freya02.botcommands.api.prefixed.annotations.Category)2 Description (com.freya02.botcommands.api.prefixed.annotations.Description)2 Usability (com.freya02.botcommands.internal.Usability)2 java.util (java.util)2 Consumer (java.util.function.Consumer)2 GuildMessageChannel (net.dv8tion.jda.api.entities.GuildMessageChannel)2 BContext (com.freya02.botcommands.api.BContext)1 Logging (com.freya02.botcommands.api.Logging)1 Emoji (com.freya02.botcommands.api.entities.Emoji)1 EmojiOrEmote (com.freya02.botcommands.api.entities.EmojiOrEmote)1 QuotableRegexParameterResolver (com.freya02.botcommands.api.parameters.QuotableRegexParameterResolver)1 CommandEvent (com.freya02.botcommands.api.prefixed.CommandEvent)1 TextCommand (com.freya02.botcommands.api.prefixed.TextCommand)1 JDATextCommand (com.freya02.botcommands.api.prefixed.annotations.JDATextCommand)1 TextOption (com.freya02.botcommands.api.prefixed.annotations.TextOption)1