use of com.sx4.bot.http.HttpCallback in project Sx4 by sx4-discord-bot.
the class EmoteCommand method getBytes.
private void getBytes(OkHttpClient httpClient, String url, TriConsumer<byte[], Boolean, Integer> bytes) {
Request request = new Request.Builder().url(RequestUtility.getWorkerUrl(url)).build();
httpClient.newCall(request).enqueue((HttpCallback) response -> {
if (response.code() == 200) {
String contentType = response.header("Content-Type"), extension = null;
if (contentType != null && contentType.contains("/")) {
extension = contentType.split("/")[1].toLowerCase();
}
bytes.accept(response.body().bytes(), extension == null ? null : extension.equals("gif"), 200);
return;
} else if (response.code() == 415) {
int periodIndex = url.lastIndexOf('.');
if (periodIndex != -1) {
String extension = url.substring(periodIndex + 1);
if (extension.equalsIgnoreCase("gif")) {
this.getBytes(httpClient, url.substring(0, periodIndex + 1) + "png", bytes);
return;
}
}
}
bytes.accept(null, null, response.code());
});
}
use of com.sx4.bot.http.HttpCallback 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.http.HttpCallback in project Sx4 by sx4-discord-bot.
the class TwitchTokenManager method retrieveToken.
public void retrieveToken() {
Request request = new Request.Builder().post(RequestBody.create(null, new byte[0])).url("https://id.twitch.tv/oauth2/token?client_id=" + this.bot.getConfig().getTwitchClientId() + "&client_secret=" + this.bot.getConfig().getTwitchClientSecret() + "&grant_type=client_credentials").build();
this.bot.getHttpClient().newCall(request).enqueue((HttpCallback) response -> {
Document json = Document.parse(response.body().string());
this.bot.getConfig().set("token.twitch.token", json.getString("access_token")).set("token.twitch.expiresAt", Clock.systemUTC().instant().getEpochSecond() + json.getInteger("expires_in")).update();
this.schedule(json.getInteger("expires_in"));
});
}
Aggregations