use of club.minnced.discord.webhook.exception.HttpException in project Sx4 by sx4-discord-bot.
the class ModLogManager method sendModLog.
public CompletableFuture<ReadonlyMessage> sendModLog(TextChannel 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);
});
}
use of club.minnced.discord.webhook.exception.HttpException in project Sx4 by sx4-discord-bot.
the class SuggestionManager method sendSuggestion.
public CompletableFuture<ReadonlyMessage> sendSuggestion(TextChannel channel, Document webhookData, boolean premium, WebhookEmbed embed) {
User selfUser = channel.getJDA().getSelfUser();
WebhookMessage message = new WebhookMessageBuilder().setAvatarUrl(webhookData.get("avatar", selfUser.getEffectiveAvatarUrl())).setUsername(webhookData.get("name", "Sx4 - Suggestions")).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);
});
}
use of club.minnced.discord.webhook.exception.HttpException in project Sx4 by sx4-discord-bot.
the class FreeGameManager method sendFreeGameNotificationMessages.
public CompletableFuture<List<ReadonlyMessage>> sendFreeGameNotificationMessages(TextChannel 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.webhookExecutor, 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 club.minnced.discord.webhook.exception.HttpException in project Sx4 by sx4-discord-bot.
the class LoggerManager method handleQueue.
private void handleQueue() {
this.executor.submit(() -> {
try {
Request request = this.queue.poll();
if (request == null) {
return;
}
if (request.getAttempts() == LoggerManager.MAX_RETRIES) {
this.handleQueue();
return;
}
Guild guild = request.getGuild();
if (guild == null) {
this.handleQueue();
return;
}
long channelId = request.getChannelId();
TextChannel channel = request.getChannel(guild);
if (channel == null) {
this.bot.getMongo().deleteLogger(Filters.eq("channelId", channelId)).whenComplete((result, exception) -> {
ExceptionUtility.sendErrorMessage(exception);
this.queue.clear();
});
return;
}
List<WebhookEmbed> embeds = new ArrayList<>(request.getEmbeds());
int length = MessageUtility.getWebhookEmbedLength(embeds);
List<Request> skippedRequests = new ArrayList<>(), requests = new ArrayList<>();
requests.add(request);
Request nextRequest;
while ((nextRequest = this.queue.poll()) != null) {
List<WebhookEmbed> nextEmbeds = nextRequest.getEmbeds();
int nextLength = MessageUtility.getWebhookEmbedLength(nextEmbeds);
if (embeds.size() + nextEmbeds.size() > WebhookMessage.MAX_EMBEDS || length + nextLength > MessageEmbed.EMBED_MAX_LENGTH_BOT) {
skippedRequests.add(nextRequest);
break;
}
embeds.addAll(nextEmbeds);
requests.add(nextRequest);
length += nextLength;
}
// Keep order of logs
skippedRequests.forEach(this.queue::addFirst);
Document logger = request.getLogger();
Document webhookData = logger.get("webhook", MongoDatabase.EMPTY_DOCUMENT);
boolean premium = logger.getBoolean("premium");
WebhookMessage message = new WebhookMessageBuilder().addEmbeds(embeds).setUsername(premium ? webhookData.get("name", "Sx4 - Logger") : "Sx4 - Logger").setAvatarUrl(premium ? webhookData.get("avatar", request.getJDA().getSelfUser().getEffectiveAvatarUrl()) : request.getJDA().getSelfUser().getEffectiveAvatarUrl()).build();
if (this.webhook == null) {
if (!webhookData.containsKey("id")) {
if (guild.getSelfMember().hasPermission(channel, Permission.MANAGE_WEBHOOKS)) {
this.createWebhook(channel, requests);
return;
}
this.handleQueue();
return;
} else {
this.webhook = new WebhookClient(webhookData.getLong("id"), webhookData.getString("token"), this.webhookExecutor, this.webhookClient);
}
}
this.webhook.send(message).whenComplete((result, exception) -> {
Throwable cause = exception instanceof CompletionException ? exception.getCause() : exception;
if (cause instanceof HttpException && ((HttpException) cause).getCode() == 404) {
if (guild.getSelfMember().hasPermission(channel, Permission.MANAGE_WEBHOOKS)) {
this.createWebhook(channel, requests);
return;
}
this.disableLogger(channel.getIdLong());
return;
}
if (ExceptionUtility.sendErrorMessage(exception)) {
requests.forEach(failedRequest -> this.queue.addFirst(failedRequest.incrementAttempts()));
}
this.handleQueue();
});
} catch (Throwable exception) {
// Continue queue even if an exception occurs to avoid the queue getting stuck
ExceptionUtility.sendErrorMessage(exception);
this.handleQueue();
}
});
}
use of club.minnced.discord.webhook.exception.HttpException in project Sx4 by sx4-discord-bot.
the class YouTubeManager method sendYouTubeNotification.
public CompletableFuture<ReadonlyMessage> sendYouTubeNotification(TextChannel 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);
});
}
Aggregations