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();
}
});
}
}
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();
});
}
Aggregations