Search in sources :

Example 6 with WebhookClient

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()));
}
Also used : WebhookMessage(club.minnced.discord.webhook.send.WebhookMessage) WebhookClient(com.sx4.bot.entities.webhook.WebhookClient) WebhookMessageBuilder(club.minnced.discord.webhook.send.WebhookMessageBuilder) ReadonlyMessage(com.sx4.bot.entities.webhook.ReadonlyMessage)

Example 7 with WebhookClient

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);
    });
}
Also used : Document(org.bson.Document) java.util(java.util) TwitchStreamStartEvent(com.sx4.bot.events.twitch.TwitchStreamStartEvent) Permission(net.dv8tion.jda.api.Permission) Projections(com.mongodb.client.model.Projections) CompletableFuture(java.util.concurrent.CompletableFuture) RequestBody(okhttp3.RequestBody) Filters(com.mongodb.client.model.Filters) WebhookClient(com.sx4.bot.entities.webhook.WebhookClient) Bson(org.bson.conversions.Bson) FindOneAndDeleteOptions(com.mongodb.client.model.FindOneAndDeleteOptions) TwitchListener(com.sx4.bot.hooks.TwitchListener) Sx4(com.sx4.bot.core.Sx4) TwitchSubscriptionType(com.sx4.bot.entities.twitch.TwitchSubscriptionType) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ReadonlyMessage(com.sx4.bot.entities.webhook.ReadonlyMessage) MediaType(okhttp3.MediaType) BotPermissionException(com.sx4.bot.exceptions.mod.BotPermissionException) Request(okhttp3.Request) BaseGuildMessageChannel(net.dv8tion.jda.api.entities.BaseGuildMessageChannel) HttpCallback(com.sx4.bot.http.HttpCallback) Updates(com.mongodb.client.model.Updates) CompletionException(java.util.concurrent.CompletionException) Executors(java.util.concurrent.Executors) HttpException(club.minnced.discord.webhook.exception.HttpException) TwitchEvent(com.sx4.bot.events.twitch.TwitchEvent) OkHttpClient(okhttp3.OkHttpClient) WebhookMessage(club.minnced.discord.webhook.send.WebhookMessage) WebhookClient(com.sx4.bot.entities.webhook.WebhookClient) CompletionException(java.util.concurrent.CompletionException) HttpException(club.minnced.discord.webhook.exception.HttpException) ReadonlyMessage(com.sx4.bot.entities.webhook.ReadonlyMessage)

Example 8 with WebhookClient

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());
    });
}
Also used : WebhookMessage(club.minnced.discord.webhook.send.WebhookMessage) Document(org.bson.Document) java.util(java.util) JsonFormatter(com.sx4.bot.formatter.JsonFormatter) Permission(net.dv8tion.jda.api.Permission) MongoDatabase(com.sx4.bot.database.mongo.MongoDatabase) Channel(net.dv8tion.jda.api.entities.Channel) WebhookClient(com.sx4.bot.entities.webhook.WebhookClient) Bson(org.bson.conversions.Bson) Formatter(com.sx4.bot.formatter.Formatter) UpdateResult(com.mongodb.client.result.UpdateResult) Sx4(com.sx4.bot.core.Sx4) WebhookEmbed(club.minnced.discord.webhook.send.WebhookEmbed) Element(org.jsoup.nodes.Element) Duration(java.time.Duration) FreeGameUtility(com.sx4.bot.utility.FreeGameUtility) com.mongodb.client.model(com.mongodb.client.model) FutureUtility(com.sx4.bot.utility.FutureUtility) ZoneOffset(java.time.ZoneOffset) ReadonlyMessage(com.sx4.bot.entities.webhook.ReadonlyMessage) WebhookMessageBuilder(club.minnced.discord.webhook.send.WebhookMessageBuilder) Operators(com.sx4.bot.database.mongo.model.Operators) Request(okhttp3.Request) BaseGuildMessageChannel(net.dv8tion.jda.api.entities.BaseGuildMessageChannel) HttpCallback(com.sx4.bot.http.HttpCallback) java.util.concurrent(java.util.concurrent) Predicate(java.util.function.Predicate) MessageUtility(com.sx4.bot.utility.MessageUtility) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) HttpException(club.minnced.discord.webhook.exception.HttpException) OkHttpClient(okhttp3.OkHttpClient) OffsetDateTime(java.time.OffsetDateTime) WebhookMessage(club.minnced.discord.webhook.send.WebhookMessage) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) Jsoup(org.jsoup.Jsoup) Elements(org.jsoup.select.Elements) com.sx4.bot.entities.info.game(com.sx4.bot.entities.info.game) WebhookClient(com.sx4.bot.entities.webhook.WebhookClient) HttpException(club.minnced.discord.webhook.exception.HttpException) ReadonlyMessage(com.sx4.bot.entities.webhook.ReadonlyMessage)

Example 9 with WebhookClient

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();
        });
    });
}
Also used : WebhookClient(com.sx4.bot.entities.webhook.WebhookClient) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) UpdateOptions(com.mongodb.client.model.UpdateOptions) Bson(org.bson.conversions.Bson)

Example 10 with WebhookClient

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);
    });
}
Also used : WebhookMessage(club.minnced.discord.webhook.send.WebhookMessage) WebhookMessageBuilder(club.minnced.discord.webhook.send.WebhookMessageBuilder) Document(org.bson.Document) BotPermissionException(com.sx4.bot.exceptions.mod.BotPermissionException) BaseGuildMessageChannel(net.dv8tion.jda.api.entities.BaseGuildMessageChannel) Permission(net.dv8tion.jda.api.Permission) MongoDatabase(com.sx4.bot.database.mongo.MongoDatabase) Updates(com.mongodb.client.model.Updates) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) Executors(java.util.concurrent.Executors) User(net.dv8tion.jda.api.entities.User) HttpException(club.minnced.discord.webhook.exception.HttpException) WebhookClient(com.sx4.bot.entities.webhook.WebhookClient) Bson(org.bson.conversions.Bson) Guild(net.dv8tion.jda.api.entities.Guild) OkHttpClient(okhttp3.OkHttpClient) Sx4(com.sx4.bot.core.Sx4) WebhookEmbed(club.minnced.discord.webhook.send.WebhookEmbed) Map(java.util.Map) WebhookMessage(club.minnced.discord.webhook.send.WebhookMessage) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ReadonlyMessage(com.sx4.bot.entities.webhook.ReadonlyMessage) User(net.dv8tion.jda.api.entities.User) WebhookClient(com.sx4.bot.entities.webhook.WebhookClient) WebhookMessageBuilder(club.minnced.discord.webhook.send.WebhookMessageBuilder) CompletionException(java.util.concurrent.CompletionException) HttpException(club.minnced.discord.webhook.exception.HttpException) ReadonlyMessage(com.sx4.bot.entities.webhook.ReadonlyMessage)

Aggregations

WebhookClient (com.sx4.bot.entities.webhook.WebhookClient)14 Bson (org.bson.conversions.Bson)13 WebhookMessage (club.minnced.discord.webhook.send.WebhookMessage)12 Document (org.bson.Document)11 HttpException (club.minnced.discord.webhook.exception.HttpException)10 ReadonlyMessage (com.sx4.bot.entities.webhook.ReadonlyMessage)10 Permission (net.dv8tion.jda.api.Permission)10 Sx4 (com.sx4.bot.core.Sx4)9 OkHttpClient (okhttp3.OkHttpClient)9 WebhookMessageBuilder (club.minnced.discord.webhook.send.WebhookMessageBuilder)8 MongoDatabase (com.sx4.bot.database.mongo.MongoDatabase)8 BotPermissionException (com.sx4.bot.exceptions.mod.BotPermissionException)7 WebhookEmbed (club.minnced.discord.webhook.send.WebhookEmbed)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 com.mongodb.client.model (com.mongodb.client.model)5 Updates (com.mongodb.client.model.Updates)5 HttpCallback (com.sx4.bot.http.HttpCallback)5 java.util (java.util)5 BaseGuildMessageChannel (net.dv8tion.jda.api.entities.BaseGuildMessageChannel)5 User (net.dv8tion.jda.api.entities.User)5