Search in sources :

Example 16 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(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());
    });
}
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) EpicFreeGame(com.sx4.bot.entities.info.EpicFreeGame) TextChannel(net.dv8tion.jda.api.entities.TextChannel) FreeGame(com.sx4.bot.entities.info.FreeGame) WebhookClient(com.sx4.bot.entities.webhook.WebhookClient) Bson(org.bson.conversions.Bson) Formatter(com.sx4.bot.formatter.Formatter) Sx4(com.sx4.bot.core.Sx4) SteamFreeGame(com.sx4.bot.entities.info.SteamFreeGame) 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) FreeGameType(com.sx4.bot.entities.info.FreeGameType) ReadonlyMessage(com.sx4.bot.entities.webhook.ReadonlyMessage) Operators(com.sx4.bot.database.mongo.model.Operators) Request(okhttp3.Request) HttpCallback(com.sx4.bot.http.HttpCallback) java.util.concurrent(java.util.concurrent) Predicate(java.util.function.Predicate) MessageUtility(com.sx4.bot.utility.MessageUtility) 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) ExceptionUtility(com.sx4.bot.utility.ExceptionUtility) Jsoup(org.jsoup.Jsoup) WebhookClient(com.sx4.bot.entities.webhook.WebhookClient) HttpException(club.minnced.discord.webhook.exception.HttpException) ReadonlyMessage(com.sx4.bot.entities.webhook.ReadonlyMessage)

Example 17 with WebhookClient

use of com.sx4.bot.entities.webhook.WebhookClient 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);
    });
}
Also used : Document(org.bson.Document) BotPermissionException(com.sx4.bot.exceptions.mod.BotPermissionException) Request(okhttp3.Request) java.util(java.util) HttpCallback(com.sx4.bot.http.HttpCallback) java.util.concurrent(java.util.concurrent) Permission(net.dv8tion.jda.api.Permission) MongoDatabase(com.sx4.bot.database.mongo.MongoDatabase) TextChannel(net.dv8tion.jda.api.entities.TextChannel) RequestBody(okhttp3.RequestBody) HttpException(club.minnced.discord.webhook.exception.HttpException) WebhookClient(com.sx4.bot.entities.webhook.WebhookClient) Bson(org.bson.conversions.Bson) OkHttpClient(okhttp3.OkHttpClient) MultipartBody(okhttp3.MultipartBody) Sx4(com.sx4.bot.core.Sx4) YouTubeListener(com.sx4.bot.hooks.YouTubeListener) com.sx4.bot.events.youtube(com.sx4.bot.events.youtube) WebhookMessage(club.minnced.discord.webhook.send.WebhookMessage) Clock(java.time.Clock) com.mongodb.client.model(com.mongodb.client.model) ReadonlyMessage(com.sx4.bot.entities.webhook.ReadonlyMessage) WebhookClient(com.sx4.bot.entities.webhook.WebhookClient) 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