use of com.sx4.bot.entities.webhook.WebhookClient in project Sx4 by sx4-discord-bot.
the class SuggestionManager method editSuggestion.
public CompletableFuture<ReadonlyMessage> editSuggestion(long messageId, long channelId, Document webhookData, WebhookEmbed embed) {
User selfUser = this.bot.getShardManager().getShardById(0).getSelfUser();
WebhookMessage message = new WebhookMessageBuilder().setAvatarUrl(webhookData.get("url", selfUser.getEffectiveAvatarUrl())).setUsername(webhookData.get("name", selfUser.getName())).addEmbeds(embed).build();
WebhookClient webhook;
if (this.webhooks.containsKey(channelId)) {
webhook = this.webhooks.get(channelId);
} else if (!webhookData.containsKey("id")) {
return CompletableFuture.completedFuture(null);
} else {
webhook = new WebhookClient(webhookData.getLong("id"), webhookData.getString("token"), this.executor, this.client);
this.webhooks.put(channelId, webhook);
}
return webhook.edit(messageId, message).thenApply(webhookMessage -> new ReadonlyMessage(webhookMessage, webhook.getId(), webhook.getToken()));
}
use of com.sx4.bot.entities.webhook.WebhookClient in project Sx4 by sx4-discord-bot.
the class TwitchManager method sendTwitchNotification.
public CompletableFuture<ReadonlyMessage> sendTwitchNotification(BaseGuildMessageChannel channel, Document webhookData, WebhookMessage message) {
long channelId = channel.getIdLong();
WebhookClient webhook;
if (this.webhooks.containsKey(channelId)) {
webhook = this.webhooks.get(channelId);
} else if (!webhookData.containsKey("id")) {
return this.createWebhook(channel, message);
} else {
webhook = new WebhookClient(webhookData.getLong("id"), webhookData.getString("token"), this.scheduledExecutor, this.client);
this.webhooks.put(channelId, webhook);
}
return webhook.send(message).thenApply(webhookMessage -> new ReadonlyMessage(webhookMessage, webhook.getId(), webhook.getToken())).exceptionallyCompose(exception -> {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof HttpException && ((HttpException) cause).getCode() == 404) {
this.webhooks.remove(channelId);
return this.createWebhook(channel, message);
}
return CompletableFuture.failedFuture(exception);
});
}
use of com.sx4.bot.entities.webhook.WebhookClient in project Sx4 by sx4-discord-bot.
the class FreeGameManager method sendFreeGameNotificationMessages.
public CompletableFuture<List<ReadonlyMessage>> sendFreeGameNotificationMessages(BaseGuildMessageChannel channel, Document webhookData, List<WebhookMessage> messages) {
WebhookClient webhook;
if (this.webhooks.containsKey(channel.getIdLong())) {
webhook = this.webhooks.get(channel.getIdLong());
} else if (!webhookData.containsKey("id")) {
return this.createWebhook(channel, messages);
} else {
webhook = new WebhookClient(webhookData.getLong("id"), webhookData.getString("token"), this.client);
this.webhooks.put(channel.getIdLong(), webhook);
}
List<ReadonlyMessage> completedMessages = new ArrayList<>();
CompletableFuture<Boolean> future = CompletableFuture.completedFuture(null);
for (WebhookMessage message : messages) {
future = future.thenCompose($ -> webhook.send(message)).thenApply(webhookMessage -> completedMessages.add(new ReadonlyMessage(webhookMessage, webhook.getId(), webhook.getToken())));
}
return future.thenApply($ -> completedMessages).exceptionallyCompose(exception -> {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof HttpException && ((HttpException) cause).getCode() == 404) {
this.webhooks.remove(channel.getIdLong());
return this.createWebhook(channel, messages);
}
return CompletableFuture.completedFuture(Collections.emptyList());
});
}
use of com.sx4.bot.entities.webhook.WebhookClient in project Sx4 by sx4-discord-bot.
the class LoggerManager method createWebhook.
private void createWebhook(BaseGuildMessageChannel channel, List<Request> requests) {
channel.createWebhook("Sx4 - Logger").submit().whenComplete((webhook, exception) -> {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof ErrorResponseException && ((ErrorResponseException) cause).getErrorResponse() == ErrorResponse.MAX_WEBHOOKS) {
this.disableLogger(channel.getIdLong());
return;
}
if (ExceptionUtility.sendErrorMessage(exception)) {
requests.forEach(failedRequest -> this.queue.addFirst(failedRequest.incrementAttempts()));
this.handleQueue();
return;
}
this.webhook = new WebhookClient(webhook.getIdLong(), webhook.getToken(), this.webhookExecutor, this.webhookClient);
Bson update = Updates.combine(Updates.set("webhook.id", webhook.getIdLong()), Updates.set("webhook.token", webhook.getToken()));
this.bot.getMongo().updateLogger(Filters.eq("channelId", channel.getIdLong()), update, new UpdateOptions()).whenComplete((result, databaseException) -> {
ExceptionUtility.sendErrorMessage(databaseException);
requests.forEach(failedRequest -> this.queue.addFirst(failedRequest.incrementAttempts()));
this.handleQueue();
});
});
}
use of com.sx4.bot.entities.webhook.WebhookClient in project Sx4 by sx4-discord-bot.
the class ModLogManager method sendModLog.
public CompletableFuture<ReadonlyMessage> sendModLog(BaseGuildMessageChannel channel, Document webhookData, WebhookEmbed embed, boolean premium) {
User selfUser = channel.getJDA().getSelfUser();
WebhookMessage message = new WebhookMessageBuilder().setAvatarUrl(premium ? webhookData.get("avatar", selfUser.getEffectiveAvatarUrl()) : selfUser.getEffectiveAvatarUrl()).setUsername(premium ? webhookData.get("name", "Sx4 - Mod Logs") : "Sx4 - Mod Logs").addEmbeds(embed).build();
WebhookClient webhook;
if (this.webhooks.containsKey(channel.getIdLong())) {
webhook = this.webhooks.get(channel.getIdLong());
} else if (!webhookData.containsKey("id")) {
return this.createWebhook(channel, message);
} else {
webhook = new WebhookClient(webhookData.getLong("id"), webhookData.getString("token"), this.executor, this.client);
this.webhooks.put(channel.getIdLong(), webhook);
}
return webhook.send(message).thenApply(webhookMessage -> new ReadonlyMessage(webhookMessage, webhook.getId(), webhook.getToken())).exceptionallyCompose(exception -> {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof HttpException && ((HttpException) cause).getCode() == 404) {
this.webhooks.remove(channel.getIdLong());
return this.createWebhook(channel, message);
}
return CompletableFuture.failedFuture(exception);
});
}
Aggregations