Search in sources :

Example 1 with FormatterVariable

use of com.sx4.bot.formatter.function.FormatterVariable in project Sx4 by sx4-discord-bot.

the class WelcomerCommand method formatters.

@Command(value = "formatters", aliases = { "format", "formatting" }, description = "Get all the formatters for welcomer you can use")
@CommandId(441)
@Examples({ "welcomer formatters" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void formatters(Sx4CommandEvent event) {
    EmbedBuilder embed = new EmbedBuilder().setAuthor("Welcomer Formatters", null, event.getSelfUser().getEffectiveAvatarUrl());
    FormatterManager manager = FormatterManager.getDefaultManager();
    StringJoiner content = new StringJoiner("\n");
    for (FormatterVariable<?> variable : manager.getVariables(User.class)) {
        content.add("`{user." + variable.getName() + "}` - " + variable.getDescription());
    }
    for (FormatterVariable<?> variable : manager.getVariables(Member.class)) {
        content.add("`{member." + variable.getName() + "}` - " + variable.getDescription());
    }
    for (FormatterVariable<?> variable : manager.getVariables(Guild.class)) {
        content.add("`{server." + variable.getName() + "}` - " + variable.getDescription());
    }
    for (FormatterVariable<?> variable : manager.getVariables(OffsetDateTime.class)) {
        content.add("`{now." + variable.getName() + "}` - " + variable.getDescription());
    }
    content.add("`{user.age}` - Gets the age of a user as a string");
    embed.setDescription(content.toString());
    event.reply(embed.build()).queue();
}
Also used : FormatterManager(com.sx4.bot.formatter.FormatterManager) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) StringJoiner(java.util.StringJoiner) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 2 with FormatterVariable

use of com.sx4.bot.formatter.function.FormatterVariable 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)

Example 3 with FormatterVariable

use of com.sx4.bot.formatter.function.FormatterVariable in project Sx4 by sx4-discord-bot.

the class FreeGamesCommand method formatters.

@Command(value = "formatters", aliases = { "formatting", "format" }, description = "View the formatters you can use for a custom message")
@CommandId(476)
@Examples({ "free games formatters" })
public void formatters(Sx4CommandEvent event) {
    EmbedBuilder embed = new EmbedBuilder().setAuthor("Free Game Formatters", null, event.getSelfUser().getEffectiveAvatarUrl());
    FormatterManager manager = FormatterManager.getDefaultManager();
    StringJoiner content = new StringJoiner("\n");
    for (FormatterVariable<?> variable : manager.getVariables(EpicFreeGame.class)) {
        content.add("`{game." + variable.getName() + "}` - " + variable.getDescription());
    }
    embed.setDescription(content.toString());
    event.reply(embed.build()).queue();
}
Also used : FormatterManager(com.sx4.bot.formatter.FormatterManager) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) StringJoiner(java.util.StringJoiner) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Example 4 with FormatterVariable

use of com.sx4.bot.formatter.function.FormatterVariable in project Sx4 by sx4-discord-bot.

the class YouTubeNotificationCommand method formatters.

@Command(value = "formatters", aliases = { "format", "formatting" }, description = "Get all the formatters for YouTube notifications you can use")
@CommandId(445)
@Examples({ "youtube notification formatters" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void formatters(Sx4CommandEvent event) {
    EmbedBuilder embed = new EmbedBuilder().setAuthor("YouTube Notification Formatters", null, event.getSelfUser().getEffectiveAvatarUrl());
    FormatterManager manager = FormatterManager.getDefaultManager();
    StringJoiner content = new StringJoiner("\n");
    for (FormatterVariable<?> variable : manager.getVariables(YouTubeVideo.class)) {
        content.add("`{video." + variable.getName() + "}` - " + variable.getDescription());
    }
    for (FormatterVariable<?> variable : manager.getVariables(YouTubeChannel.class)) {
        content.add("`{channel." + variable.getName() + "}` - " + variable.getDescription());
    }
    embed.setDescription(content.toString());
    event.reply(embed.build()).queue();
}
Also used : FormatterManager(com.sx4.bot.formatter.FormatterManager) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) BotPermissions(com.sx4.bot.annotations.command.BotPermissions) Sx4Command(com.sx4.bot.core.Sx4Command) Command(com.jockie.bot.core.command.Command) CommandId(com.sx4.bot.annotations.command.CommandId) Examples(com.sx4.bot.annotations.command.Examples)

Example 5 with FormatterVariable

use of com.sx4.bot.formatter.function.FormatterVariable in project Sx4 by sx4-discord-bot.

the class LeaverCommand method formatters.

@Command(value = "formatters", aliases = { "format", "formatting" }, description = "Get all the formatters for leaver you can use")
@CommandId(442)
@Examples({ "leaver formatters" })
@BotPermissions(permissions = { Permission.MESSAGE_EMBED_LINKS })
public void formatters(Sx4CommandEvent event) {
    EmbedBuilder embed = new EmbedBuilder().setAuthor("Leaver Formatters", null, event.getSelfUser().getEffectiveAvatarUrl());
    FormatterManager manager = FormatterManager.getDefaultManager();
    StringJoiner content = new StringJoiner("\n");
    for (FormatterVariable<?> variable : manager.getVariables(User.class)) {
        content.add("`{user." + variable.getName() + "}` - " + variable.getDescription());
    }
    for (FormatterVariable<?> variable : manager.getVariables(Member.class)) {
        content.add("`{member." + variable.getName() + "}` - " + variable.getDescription());
    }
    for (FormatterVariable<?> variable : manager.getVariables(Guild.class)) {
        content.add("`{server." + variable.getName() + "}` - " + variable.getDescription());
    }
    for (FormatterVariable<?> variable : manager.getVariables(OffsetDateTime.class)) {
        content.add("`{now." + variable.getName() + "}` - " + variable.getDescription());
    }
    content.add("`{user.age}` - Gets the age of a user as a string");
    content.add("`{member.age}` - Gets the age of a member as a string");
    embed.setDescription(content.toString());
    event.reply(embed.build()).queue();
}
Also used : FormatterManager(com.sx4.bot.formatter.FormatterManager) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) StringJoiner(java.util.StringJoiner) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Aggregations

Sx4Command (com.sx4.bot.core.Sx4Command)7 FormatterManager (com.sx4.bot.formatter.FormatterManager)7 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)7 Command (com.jockie.bot.core.command.Command)6 StringJoiner (java.util.StringJoiner)5 BotPermissions (com.sx4.bot.annotations.command.BotPermissions)2 CommandId (com.sx4.bot.annotations.command.CommandId)2 Examples (com.sx4.bot.annotations.command.Examples)2 Argument (com.jockie.bot.core.argument.Argument)1 ModuleCategory (com.sx4.bot.category.ModuleCategory)1 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)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