use of com.freya02.botcommands.internal.application.LocalizedCommandData.LocalizedOption in project BotCommands by freya022.
the class LocalizationData method getData.
public static LocalizationData getData(BContext context, @Nullable Guild guild, @NotNull ApplicationCommandInfo info) {
final CommandPath localizedPath;
final String localizedDescription;
final List<LocalizedOption> localizedOptions;
final List<List<Command.Choice>> localizedChoices;
final Locale locale = context.getEffectiveLocale(guild);
final BResourceBundle bundle = BResourceBundle.getBundle("BotCommands", locale);
if (bundle == null) {
return null;
}
final String prefix;
if (info instanceof SlashCommandInfo) {
prefix = "slash";
} else if (info instanceof UserCommandInfo) {
prefix = "user";
} else if (info instanceof MessageCommandInfo) {
prefix = "message";
} else {
throw new IllegalArgumentException("Unknown localization prefix for class: " + info.getClass().getSimpleName());
}
final String qualifier = info.getMethod().getName();
final StringJoiner pathJoiner = new StringJoiner("/");
pathJoiner.add(tryLocalize(bundle, info.getPath().getName(), prefix, qualifier, "name"));
if (info instanceof SlashCommandInfo) {
final String notLocalizedGroup = info.getPath().getGroup();
final String notLocalizedSubname = info.getPath().getSubname();
if (notLocalizedGroup != null)
pathJoiner.add(tryLocalize(bundle, notLocalizedGroup, prefix, qualifier, "group"));
if (notLocalizedSubname != null)
pathJoiner.add(tryLocalize(bundle, notLocalizedSubname, prefix, qualifier, "subname"));
}
localizedPath = CommandPath.of(pathJoiner.toString());
if (info instanceof SlashCommandInfo) {
localizedDescription = tryLocalize(bundle, ((SlashCommandInfo) info).getDescription(), prefix, qualifier, "description");
} else
localizedDescription = null;
if (info instanceof SlashCommandInfo) {
localizedOptions = new ArrayList<>();
localizedChoices = new ArrayList<>();
final List<List<Command.Choice>> notLocalizedChoices = SlashUtils.getNotLocalizedChoices(context, guild, info);
final List<? extends ApplicationCommandParameter<?>> parameters = info.getOptionParameters();
for (int optionIndex = 0, parametersSize = parameters.size(); optionIndex < parametersSize; optionIndex++) {
ApplicationCommandParameter<?> parameter = parameters.get(optionIndex);
final ApplicationOptionData optionData = parameter.getApplicationOptionData();
final String optionName = tryLocalize(bundle, optionData.getEffectiveName(), prefix, qualifier, "options", optionIndex, "name");
final String optionDescription = tryLocalize(bundle, optionData.getEffectiveDescription(), prefix, qualifier, "options", optionIndex, "description");
localizedOptions.add(new LocalizedOption(optionName, optionDescription));
final List<Command.Choice> choices = getLocalizedChoices(bundle, prefix, qualifier, notLocalizedChoices, optionIndex, parameter);
localizedChoices.add(choices);
}
} else {
localizedOptions = null;
localizedChoices = null;
}
return new LocalizationData(localizedPath, localizedDescription, localizedOptions, localizedChoices);
}
use of com.freya02.botcommands.internal.application.LocalizedCommandData.LocalizedOption 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);
}
Aggregations