Search in sources :

Example 6 with Webhook

use of net.dv8tion.jda.api.entities.Webhook in project Essentials by drtshock.

the class DiscordUtil method getOrCreateWebhook.

/**
 * Gets or creates a webhook with the given name in the given channel.
 *
 * @param channel     The channel to search for/create webhooks in.
 * @param webhookName The name of the webhook to search for/create.
 *
 * @return A future which completes with the webhook by the given name in the given channel, or null
 * if the bot lacks the proper permissions.
 */
public static CompletableFuture<Webhook> getOrCreateWebhook(final TextChannel channel, final String webhookName) {
    if (!channel.getGuild().getSelfMember().hasPermission(channel, Permission.MANAGE_WEBHOOKS)) {
        return CompletableFuture.completedFuture(null);
    }
    final CompletableFuture<Webhook> future = new CompletableFuture<>();
    channel.retrieveWebhooks().queue(webhooks -> {
        for (final Webhook webhook : webhooks) {
            if (webhook.getName().equals(webhookName) && webhook.getToken() != null) {
                ACTIVE_WEBHOOKS.addIfAbsent(webhook.getId());
                future.complete(webhook);
                return;
            }
        }
        createWebhook(channel, webhookName).thenAccept(future::complete);
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Webhook(net.dv8tion.jda.api.entities.Webhook)

Example 7 with Webhook

use of net.dv8tion.jda.api.entities.Webhook in project Essentials by drtshock.

the class JDADiscordService method updateTypesRelay.

public void updateTypesRelay() {
    if (!getSettings().isShowAvatar() && !getSettings().isShowName() && !getSettings().isShowDisplayName()) {
        for (WebhookClient webhook : channelIdToWebhook.values()) {
            webhook.close();
        }
        typeToChannelId.clear();
        channelIdToWebhook.clear();
        return;
    }
    for (MessageType type : MessageType.DefaultTypes.values()) {
        if (!type.isPlayer()) {
            continue;
        }
        final TextChannel channel = getChannel(type.getKey(), true);
        if (channel.getId().equals(typeToChannelId.get(type))) {
            continue;
        }
        final Webhook webhook = DiscordUtil.getOrCreateWebhook(channel, DiscordUtil.ADVANCED_RELAY_NAME).join();
        if (webhook == null) {
            final WebhookClient current = channelIdToWebhook.get(channel.getId());
            if (current != null) {
                current.close();
            }
            channelIdToWebhook.remove(channel.getId());
            continue;
        }
        typeToChannelId.put(type, channel.getId());
        channelIdToWebhook.put(channel.getId(), DiscordUtil.getWebhookClient(webhook.getIdLong(), webhook.getToken(), jda.getHttpClient()));
    }
}
Also used : TextChannel(net.dv8tion.jda.api.entities.TextChannel) WebhookClient(club.minnced.discord.webhook.WebhookClient) Webhook(net.dv8tion.jda.api.entities.Webhook) MessageType(net.essentialsx.api.v2.services.discord.MessageType)

Example 8 with Webhook

use of net.dv8tion.jda.api.entities.Webhook in project JDA by DV8FromTheWorld.

the class WebhookActionImpl method handleSuccess.

@Override
protected void handleSuccess(Response response, Request<Webhook> request) {
    DataObject json = response.getObject();
    Webhook webhook = api.getEntityBuilder().createWebhook(json);
    request.onSuccess(webhook);
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) Webhook(net.dv8tion.jda.api.entities.Webhook)

Example 9 with Webhook

use of net.dv8tion.jda.api.entities.Webhook in project c10ver by Gartham.

the class GarmonUtils method queueWithFeasibleWebhook.

public static void queueWithFeasibleWebhook(TextChannel channel, Consumer<Webhook> consumer) {
    channel.retrieveWebhooks().queue(t -> {
        for (Webhook wb : t) if (wb.getOwner().getId().equals(channel.getJDA().getSelfUser().getId())) {
            consumer.accept(wb);
            return;
        }
        byte[] b = new byte[5];
        // 1/2^40 collision chance.
        new Random().nextBytes(b);
        channel.createWebhook(StringTools.toHexString(b)).queue(t1 -> consumer.accept(t1));
    });
}
Also used : Random(java.util.Random) Webhook(net.dv8tion.jda.api.entities.Webhook)

Example 10 with Webhook

use of net.dv8tion.jda.api.entities.Webhook in project c10ver by Gartham.

the class GarmonUtils method getFeasibleWebhook.

/**
 * Tries to provide a feasible webhook for use. This method iterates over all
 * the webhooks it retrieves from the specified channel. If any one of them is
 * created by the bot user (more specifically, if its owner is the bot user), it
 * is returned. Otherwise, an attempt is made to create a new webhook. This
 * function will throw exceptions if it does not have the appropriate
 * permissions or if an error occurs during the retrieval or creation of a
 * webhook.
 *
 * @param channel The channel that the webhook will belong to.
 * @return The {@link Webhook} that was found or newly created.
 */
public static Webhook getFeasibleWebhook(TextChannel channel) {
    for (Webhook wb : channel.retrieveWebhooks().complete()) if (wb.getOwner().getId().equals(channel.getJDA().getSelfUser().getId()))
        return wb;
    byte[] b = new byte[5];
    // 1/2^40 collision chance.
    new Random().nextBytes(b);
    return channel.createWebhook(StringTools.toHexString(b)).complete();
}
Also used : Random(java.util.Random) Webhook(net.dv8tion.jda.api.entities.Webhook)

Aggregations

Webhook (net.dv8tion.jda.api.entities.Webhook)10 TextChannel (net.dv8tion.jda.api.entities.TextChannel)4 WebhookClient (club.minnced.discord.webhook.WebhookClient)2 Random (java.util.Random)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Matcher (java.util.regex.Matcher)2 MessageType (net.essentialsx.api.v2.services.discord.MessageType)2 DiscordCommandDispatcher (net.essentialsx.discord.listeners.DiscordCommandDispatcher)2 ConsoleInjector (net.essentialsx.discord.util.ConsoleInjector)2 UncheckedIOException (java.io.UncheckedIOException)1 ArrayList (java.util.ArrayList)1 Nonnull (javax.annotation.Nonnull)1 DataArray (net.dv8tion.jda.api.utils.data.DataArray)1 DataObject (net.dv8tion.jda.api.utils.data.DataObject)1 JDAImpl (net.dv8tion.jda.internal.JDAImpl)1 EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)1 RestActionImpl (net.dv8tion.jda.internal.requests.RestActionImpl)1 Route (net.dv8tion.jda.internal.requests.Route)1 AuditableRestActionImpl (net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl)1