Search in sources :

Example 6 with Formatter

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

the class WelcomerUtility method getWelcomerMessage.

public static void getWelcomerMessage(OkHttpClient httpClient, Document messageData, String bannerId, Member member, boolean canary, boolean image, boolean gif, BiConsumer<WebhookMessageBuilder, Throwable> consumer) {
    Guild guild = member.getGuild();
    User user = member.getUser();
    OffsetDateTime now = OffsetDateTime.now();
    Formatter<Document> formatter = new JsonFormatter(messageData).member(member).user(user).guild(guild).addVariable(User.class, "age", TimeUtility.LONG_TIME_FORMATTER.parse(Duration.between(user.getTimeCreated(), now).toSeconds())).addVariable("now", now);
    if (!image) {
        WebhookMessageBuilder builder;
        if (messageData != null) {
            try {
                builder = MessageUtility.fromJson(formatter.parse());
            } catch (IllegalArgumentException e) {
                consumer.accept(null, e);
                return;
            }
        } else {
            builder = new WebhookMessageBuilder();
        }
        consumer.accept(builder, null);
    } else {
        ImageRequest request = new ImageRequest(Config.get().getImageWebserverUrl("welcomer")).addQuery("avatar", user.getEffectiveAvatarUrl()).addQuery("name", user.getAsTag()).addQuery("gif", gif).addQuery("directory", canary ? "sx4-canary" : "sx4-main");
        if (bannerId != null) {
            request.addQuery("banner_id", bannerId);
        }
        httpClient.newCall(request.build(Config.get().getImageWebserver())).enqueue((HttpCallback) response -> {
            if (response.isSuccessful()) {
                String fileName = "welcomer." + response.header("Content-Type").split("/")[1];
                formatter.addVariable("file.name", fileName).addVariable("file.url", "attachment://" + fileName);
                WebhookMessageBuilder builder;
                if (messageData == null) {
                    builder = new WebhookMessageBuilder();
                } else {
                    try {
                        builder = MessageUtility.fromJson(formatter.parse());
                    } catch (IllegalArgumentException e) {
                        consumer.accept(null, e);
                        return;
                    }
                }
                builder.addFile(fileName, response.body().bytes());
                consumer.accept(builder, null);
            } else {
                response.close();
            }
        });
    }
}
Also used : JsonFormatter(com.sx4.bot.formatter.JsonFormatter) WebhookMessageBuilder(club.minnced.discord.webhook.send.WebhookMessageBuilder) Document(org.bson.Document) Config(com.sx4.bot.config.Config) ImageRequest(com.sx4.bot.entities.image.ImageRequest) HttpCallback(com.sx4.bot.http.HttpCallback) JsonFormatter(com.sx4.bot.formatter.JsonFormatter) Member(net.dv8tion.jda.api.entities.Member) User(net.dv8tion.jda.api.entities.User) Guild(net.dv8tion.jda.api.entities.Guild) OkHttpClient(okhttp3.OkHttpClient) OffsetDateTime(java.time.OffsetDateTime) Formatter(com.sx4.bot.formatter.Formatter) Duration(java.time.Duration) BiConsumer(java.util.function.BiConsumer) User(net.dv8tion.jda.api.entities.User) OffsetDateTime(java.time.OffsetDateTime) WebhookMessageBuilder(club.minnced.discord.webhook.send.WebhookMessageBuilder) ImageRequest(com.sx4.bot.entities.image.ImageRequest) Guild(net.dv8tion.jda.api.entities.Guild) Document(org.bson.Document)

Example 7 with Formatter

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

the class FreeGamesCommand method preview.

@Command(value = "preview", description = "Preview your free game notification message")
@CommandId(481)
@Examples({ "free games preview" })
public void preview(Sx4CommandEvent event) {
    Document data = event.getMongo().getFreeGameChannel(Filters.eq("guildId", event.getGuild().getIdLong()), Projections.include("message"));
    if (data == null) {
        event.replyFailure("You don't have a free game channel setup").queue();
        return;
    }
    FreeGameUtility.retrieveFreeGames(event.getHttpClient(), freeGames -> {
        Formatter<Document> formatter = new JsonFormatter(data.get("message", FreeGameManager.DEFAULT_MESSAGE)).addVariable("game", freeGames.get(0));
        MessageUtility.fromWebhookMessage(event.getChannel(), MessageUtility.fromJson(formatter.parse()).build()).queue();
    });
}
Also used : JsonFormatter(com.sx4.bot.formatter.JsonFormatter) Document(org.bson.Document) Command(com.jockie.bot.core.command.Command) Sx4Command(com.sx4.bot.core.Sx4Command)

Aggregations

JsonFormatter (com.sx4.bot.formatter.JsonFormatter)5 Document (org.bson.Document)5 Sx4Command (com.sx4.bot.core.Sx4Command)4 WebhookMessageBuilder (club.minnced.discord.webhook.send.WebhookMessageBuilder)2 Command (com.jockie.bot.core.command.Command)2 OffsetDateTime (java.time.OffsetDateTime)2 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)2 BaseGuildMessageChannel (net.dv8tion.jda.api.entities.BaseGuildMessageChannel)2 Guild (net.dv8tion.jda.api.entities.Guild)2 User (net.dv8tion.jda.api.entities.User)2 WebhookEmbed (club.minnced.discord.webhook.send.WebhookEmbed)1 WebhookMessage (club.minnced.discord.webhook.send.WebhookMessage)1 Argument (com.jockie.bot.core.argument.Argument)1 ICommand (com.jockie.bot.core.command.ICommand)1 DummyCommand (com.jockie.bot.core.command.impl.DummyCommand)1 ModuleCategory (com.sx4.bot.category.ModuleCategory)1 Config (com.sx4.bot.config.Config)1 Sx4CommandEvent (com.sx4.bot.core.Sx4CommandEvent)1 ImageRequest (com.sx4.bot.entities.image.ImageRequest)1 ReadonlyMessage (com.sx4.bot.entities.webhook.ReadonlyMessage)1