use of com.sx4.bot.formatter.function.FormatterFunction in project Sx4 by sx4-discord-bot.
the class FormatterCommand method onCommand.
public void onCommand(Sx4CommandEvent event, @Argument(value = "arguments", nullDefault = true) String content) {
FormatterManager manager = FormatterManager.getDefaultManager();
String[] arguments = content == null ? new String[0] : content.split("\\.");
Set<Class<?>> functionClasses = manager.getFunctions().keySet();
Set<Class<?>> variableClasses = manager.getVariables().keySet();
Set<Class<?>> classes = new HashSet<>();
classes.addAll(functionClasses);
classes.addAll(variableClasses);
List<Class<?>> filteredClasses = classes.stream().filter(clazz -> arguments.length == 0 || arguments[0].equalsIgnoreCase(clazz.getSimpleName())).collect(Collectors.toList());
if (filteredClasses.isEmpty()) {
event.replyFailure("I could not find that formatter type").queue();
return;
}
PagedResult<Class<?>> paged = new PagedResult<>(event.getBot(), filteredClasses).setPerPage(15).setAutoSelect(true).setDisplayFunction(Class::getSimpleName);
paged.onSelect(select -> {
Class<?> clazz = select.getSelected();
for (int i = 1; i < arguments.length; i++) {
String name = arguments[i];
FormatterFunction<?> function = manager.getFunctions(clazz).stream().filter(f -> f.getName().equalsIgnoreCase(name)).findFirst().orElse(null);
FormatterVariable<?> variable = manager.getVariables(clazz).stream().filter(v -> v.getName().equalsIgnoreCase(name)).findFirst().orElse(null);
boolean last = i == arguments.length - 1;
MessageEmbed embed;
if (function != null) {
Method method = function.getMethod();
if (!last) {
clazz = method.getReturnType();
continue;
}
embed = new EmbedBuilder().setDescription(function.getDescription()).setTitle(this.getFunctionString(function)).addField("Return Type", method.getReturnType().getSimpleName(), true).build();
} else if (variable != null) {
Class<?> returnType = variable.getReturnType();
if (!last) {
clazz = returnType;
continue;
}
embed = new EmbedBuilder().setDescription(variable.getDescription()).setTitle(variable.getName()).addField("Return Type", returnType.getSimpleName(), true).build();
} else {
event.replyFailure("I could not find a variable or function named `" + name + "` on the type `" + clazz.getSimpleName() + "`").queue();
return;
}
event.reply(embed).queue();
return;
}
String variables = manager.getVariables(clazz).stream().map(FormatterVariable::getName).collect(Collectors.joining("\n"));
String functions = manager.getFunctions(clazz).stream().map(this::getFunctionString).collect(Collectors.joining("\n"));
EmbedBuilder embed = new EmbedBuilder().setTitle(clazz.getSimpleName()).addField("Functions", functions.isEmpty() ? "None" : functions, true).addField("Variables", variables.isEmpty() ? "None" : variables, true);
event.reply(embed.build()).queue();
});
paged.execute(event);
}
Aggregations