Search in sources :

Example 1 with FormatterFunction

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);
}
Also used : Arrays(java.util.Arrays) FormatterVariable(com.sx4.bot.formatter.function.FormatterVariable) Sx4Command(com.sx4.bot.core.Sx4Command) Set(java.util.Set) FormatterManager(com.sx4.bot.formatter.FormatterManager) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) PagedResult(com.sx4.bot.paged.PagedResult) Collectors(java.util.stream.Collectors) FormatterFunction(com.sx4.bot.formatter.function.FormatterFunction) HashSet(java.util.HashSet) ModuleCategory(com.sx4.bot.category.ModuleCategory) List(java.util.List) Parameter(java.lang.reflect.Parameter) Sx4CommandEvent(com.sx4.bot.core.Sx4CommandEvent) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) Method(java.lang.reflect.Method) Argument(com.jockie.bot.core.argument.Argument) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) Method(java.lang.reflect.Method) FormatterManager(com.sx4.bot.formatter.FormatterManager) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) HashSet(java.util.HashSet)

Aggregations

Argument (com.jockie.bot.core.argument.Argument)1 ModuleCategory (com.sx4.bot.category.ModuleCategory)1 Sx4Command (com.sx4.bot.core.Sx4Command)1 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)1 FormatterManager (com.sx4.bot.formatter.FormatterManager)1 FormatterFunction (com.sx4.bot.formatter.function.FormatterFunction)1 FormatterVariable (com.sx4.bot.formatter.function.FormatterVariable)1 PagedResult (com.sx4.bot.paged.PagedResult)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 MessageEmbed (net.dv8tion.jda.api.entities.MessageEmbed)1