Search in sources :

Example 1 with Webhook

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

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 2 with Webhook

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

the class JDADiscordService method updateConsoleRelay.

public void updateConsoleRelay() {
    final String consoleDef = getSettings().getConsoleChannelDef();
    final Matcher matcher = WebhookClientBuilder.WEBHOOK_PATTERN.matcher(consoleDef);
    final long webhookId;
    final String webhookToken;
    if (matcher.matches()) {
        webhookId = Long.parseUnsignedLong(matcher.group(1));
        webhookToken = matcher.group(2);
        if (commandDispatcher != null) {
            jda.removeEventListener(commandDispatcher);
            commandDispatcher = null;
        }
    } else {
        final TextChannel channel = getChannel(consoleDef, false);
        if (channel != null) {
            if (getSettings().isConsoleCommandRelay()) {
                if (commandDispatcher == null) {
                    commandDispatcher = new DiscordCommandDispatcher(this);
                    jda.addEventListener(commandDispatcher);
                }
                commandDispatcher.setChannelId(channel.getId());
            } else if (commandDispatcher != null) {
                jda.removeEventListener(commandDispatcher);
                commandDispatcher = null;
            }
            if (channel.getId().equals(lastConsoleId)) {
                return;
            }
            final Webhook webhook = DiscordUtil.getOrCreateWebhook(channel, DiscordUtil.CONSOLE_RELAY_NAME).join();
            if (webhook == null) {
                logger.info(tl("discordErrorLoggerNoPerms"));
                return;
            }
            webhookId = webhook.getIdLong();
            webhookToken = webhook.getToken();
            lastConsoleId = channel.getId();
        } else if (!getSettings().getConsoleChannelDef().equals("none") && !getSettings().getConsoleChannelDef().startsWith("0")) {
            logger.info(tl("discordErrorLoggerInvalidChannel"));
            shutdownConsoleRelay(true);
            return;
        } else {
            // It's either not configured at all or knowingly disabled.
            shutdownConsoleRelay(true);
            return;
        }
    }
    shutdownConsoleRelay(false);
    consoleWebhook = DiscordUtil.getWebhookClient(webhookId, webhookToken, jda.getHttpClient());
    if (injector == null) {
        injector = new ConsoleInjector(this);
        injector.start();
    }
}
Also used : TextChannel(net.dv8tion.jda.api.entities.TextChannel) ConsoleInjector(net.essentialsx.discord.util.ConsoleInjector) Matcher(java.util.regex.Matcher) DiscordCommandDispatcher(net.essentialsx.discord.listeners.DiscordCommandDispatcher) Webhook(net.dv8tion.jda.api.entities.Webhook)

Example 3 with Webhook

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

the class BaseGuildMessageChannelMixin method retrieveWebhooks.

@Nonnull
@Override
default RestAction<List<Webhook>> retrieveWebhooks() {
    checkPermission(Permission.MANAGE_WEBHOOKS);
    Route.CompiledRoute route = Route.Channels.GET_WEBHOOKS.compile(getId());
    JDAImpl jda = (JDAImpl) getJDA();
    return new RestActionImpl<>(jda, route, (response, request) -> {
        DataArray array = response.getArray();
        List<Webhook> webhooks = new ArrayList<>(array.length());
        EntityBuilder builder = jda.getEntityBuilder();
        for (int i = 0; i < array.length(); i++) {
            try {
                webhooks.add(builder.createWebhook(array.getObject(i)));
            } catch (UncheckedIOException | NullPointerException e) {
                JDAImpl.LOG.error("Error while creating websocket from json", e);
            }
        }
        return Collections.unmodifiableList(webhooks);
    });
}
Also used : ArrayList(java.util.ArrayList) JDAImpl(net.dv8tion.jda.internal.JDAImpl) UncheckedIOException(java.io.UncheckedIOException) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) DataArray(net.dv8tion.jda.api.utils.data.DataArray) RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) AuditableRestActionImpl(net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl) Webhook(net.dv8tion.jda.api.entities.Webhook) Route(net.dv8tion.jda.internal.requests.Route) Nonnull(javax.annotation.Nonnull)

Example 4 with Webhook

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

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 5 with Webhook

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

the class JDADiscordService method updateConsoleRelay.

public void updateConsoleRelay() {
    final String consoleDef = getSettings().getConsoleChannelDef();
    final Matcher matcher = WebhookClientBuilder.WEBHOOK_PATTERN.matcher(consoleDef);
    final long webhookId;
    final String webhookToken;
    if (matcher.matches()) {
        webhookId = Long.parseUnsignedLong(matcher.group(1));
        webhookToken = matcher.group(2);
        if (commandDispatcher != null) {
            jda.removeEventListener(commandDispatcher);
            commandDispatcher = null;
        }
    } else {
        final TextChannel channel = getChannel(consoleDef, false);
        if (channel != null) {
            if (getSettings().isConsoleCommandRelay()) {
                if (commandDispatcher == null) {
                    commandDispatcher = new DiscordCommandDispatcher(this);
                    jda.addEventListener(commandDispatcher);
                }
                commandDispatcher.setChannelId(channel.getId());
            } else if (commandDispatcher != null) {
                jda.removeEventListener(commandDispatcher);
                commandDispatcher = null;
            }
            if (channel.getId().equals(lastConsoleId)) {
                return;
            }
            final Webhook webhook = DiscordUtil.getOrCreateWebhook(channel, DiscordUtil.CONSOLE_RELAY_NAME).join();
            if (webhook == null) {
                logger.info(tl("discordErrorLoggerNoPerms"));
                return;
            }
            webhookId = webhook.getIdLong();
            webhookToken = webhook.getToken();
            lastConsoleId = channel.getId();
        } else if (!getSettings().getConsoleChannelDef().equals("none") && !getSettings().getConsoleChannelDef().startsWith("0")) {
            logger.info(tl("discordErrorLoggerInvalidChannel"));
            shutdownConsoleRelay(true);
            return;
        } else {
            // It's either not configured at all or knowingly disabled.
            shutdownConsoleRelay(true);
            return;
        }
    }
    shutdownConsoleRelay(false);
    consoleWebhook = DiscordUtil.getWebhookClient(webhookId, webhookToken, jda.getHttpClient());
    if (injector == null) {
        injector = new ConsoleInjector(this);
        injector.start();
    }
}
Also used : TextChannel(net.dv8tion.jda.api.entities.TextChannel) ConsoleInjector(net.essentialsx.discord.util.ConsoleInjector) Matcher(java.util.regex.Matcher) DiscordCommandDispatcher(net.essentialsx.discord.listeners.DiscordCommandDispatcher) 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