use of com.freya02.botcommands.internal.application.CommandParameter 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;
}
use of com.freya02.botcommands.internal.application.CommandParameter in project BotCommands by freya022.
the class LocalizedCommandData method of.
public static LocalizedCommandData of(@NotNull BContext context, @Nullable Guild guild, @NotNull ApplicationCommandInfo info) {
final LocalizationData data = LocalizationData.getData(context, guild, info);
final CommandPath localizedPath;
if (data != null) {
localizedPath = data.getLocalizedPath();
} else {
localizedPath = info.getPath();
}
if (!(info instanceof SlashCommandInfo))
return new LocalizedCommandData(localizedPath, "No description", Collections.emptyList(), Collections.emptyList());
final String localizedDescription;
final List<LocalizedOption> localizedOptions;
final List<List<Command.Choice>> localizedChoices;
if (data != null && data.getLocalizedDescription() != null) {
localizedDescription = data.getLocalizedDescription();
} else {
localizedDescription = ((SlashCommandInfo) info).getDescription();
}
if (data != null && data.getLocalizedOptions() != null) {
localizedOptions = data.getLocalizedOptions();
} else {
localizedOptions = info.getOptionParameters().stream().filter(CommandParameter::isOption).map(ApplicationCommandParameter::getApplicationOptionData).map(a -> new LocalizedOption(a.getEffectiveName(), a.getEffectiveDescription())).collect(Collectors.toList());
}
if (data != null && data.getLocalizedChoices() != null) {
localizedChoices = data.getLocalizedChoices();
} else {
localizedChoices = SlashUtils.getNotLocalizedChoices(context, guild, info);
}
return new LocalizedCommandData(localizedPath, localizedDescription, localizedOptions, localizedChoices);
}
use of com.freya02.botcommands.internal.application.CommandParameter in project BotCommands by freya022.
the class PrefixedCommandsBuilder method checkMethodDuplicates.
private void checkMethodDuplicates() {
for (TextCommandCandidates command : context.getCommands()) {
for (TextCommandInfo info : command) {
for (TextCommandInfo commandInfo : command) {
if (info == commandInfo)
continue;
final Method commandMethod1 = info.getMethod();
final Method commandMethod2 = commandInfo.getMethod();
final List<? extends TextCommandParameter> parameters1 = info.getOptionParameters();
final List<? extends TextCommandParameter> parameters2 = commandInfo.getOptionParameters();
if (parameters1.stream().map(CommandParameter::getParameter).collect(Collectors.toList()).equals(parameters2.stream().map(CommandParameter::getParameter).collect(Collectors.toList()))) {
throw new IllegalStateException("Method " + Utils.formatMethodShort(commandMethod1) + " has the same parameters as " + Utils.formatMethodShort(commandMethod2));
}
}
}
}
}
Aggregations