Search in sources :

Example 1 with ConsoleInjector

use of net.essentialsx.discord.util.ConsoleInjector 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 2 with ConsoleInjector

use of net.essentialsx.discord.util.ConsoleInjector 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

Matcher (java.util.regex.Matcher)2 TextChannel (net.dv8tion.jda.api.entities.TextChannel)2 Webhook (net.dv8tion.jda.api.entities.Webhook)2 DiscordCommandDispatcher (net.essentialsx.discord.listeners.DiscordCommandDispatcher)2 ConsoleInjector (net.essentialsx.discord.util.ConsoleInjector)2