Search in sources :

Example 6 with ReadonlyMessage

use of com.sx4.bot.entities.webhook.ReadonlyMessage 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

WebhookMessage (club.minnced.discord.webhook.send.WebhookMessage)6 ReadonlyMessage (com.sx4.bot.entities.webhook.ReadonlyMessage)6 WebhookClient (com.sx4.bot.entities.webhook.WebhookClient)5 TextChannel (net.dv8tion.jda.api.entities.TextChannel)5 Document (org.bson.Document)5 Bson (org.bson.conversions.Bson)5 HttpException (club.minnced.discord.webhook.exception.HttpException)4 Sx4 (com.sx4.bot.core.Sx4)4 MongoDatabase (com.sx4.bot.database.mongo.MongoDatabase)4 Permission (net.dv8tion.jda.api.Permission)4 OkHttpClient (okhttp3.OkHttpClient)4 WebhookMessageBuilder (club.minnced.discord.webhook.send.WebhookMessageBuilder)3 BotPermissionException (com.sx4.bot.exceptions.mod.BotPermissionException)3 WebhookEmbed (club.minnced.discord.webhook.send.WebhookEmbed)2 com.mongodb.client.model (com.mongodb.client.model)2 Updates (com.mongodb.client.model.Updates)2 JsonFormatter (com.sx4.bot.formatter.JsonFormatter)2 HttpCallback (com.sx4.bot.http.HttpCallback)2 java.util (java.util)2 HashMap (java.util.HashMap)2