From 26c016fea2db6112550898470c4f074ccb8fbc1c Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Mon, 9 Aug 2021 11:48:01 -0700 Subject: [PATCH] Add config option to allow webhooks/bots to send commands (#4442) ```yml # Console relay settings # The console relay sends every message shown in the console to a Discord channel. console: ... # Set to true if bots/webhooks should be able to send commands through the command relay. bot-command-relay: false ``` --- .../java/net/essentialsx/discord/DiscordSettings.java | 4 ++++ .../discord/listeners/DiscordCommandDispatcher.java | 8 ++++++-- EssentialsDiscord/src/main/resources/config.yml | 2 ++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java index 3e18ea828af..29c862613f1 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java @@ -141,6 +141,10 @@ public boolean isConsoleCommandRelay() { return config.getBoolean("console.command-relay", false); } + public boolean isConsoleBotCommandRelay() { + return config.getBoolean("console.bot-command-relay", false); + } + public Level getConsoleLogLevel() { return consoleLogLevel; } diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/DiscordCommandDispatcher.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/DiscordCommandDispatcher.java index 40757572de9..f78f13bef09 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/DiscordCommandDispatcher.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/listeners/DiscordCommandDispatcher.java @@ -4,6 +4,7 @@ import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.essentialsx.discord.JDADiscordService; import net.essentialsx.discord.util.DiscordCommandSender; +import net.essentialsx.discord.util.DiscordUtil; import org.bukkit.Bukkit; import org.jetbrains.annotations.NotNull; @@ -17,8 +18,11 @@ public DiscordCommandDispatcher(JDADiscordService jda) { @Override public void onGuildMessageReceived(@NotNull GuildMessageReceivedEvent event) { - if (jda.getConsoleWebhook() != null && event.getChannel().getId().equals(channelId) - && !event.isWebhookMessage() && !event.getAuthor().isBot()) { + if (jda.getConsoleWebhook() != null && event.getChannel().getId().equals(channelId)) { + if ((event.isWebhookMessage() || event.getAuthor().isBot()) && (!jda.getSettings().isConsoleBotCommandRelay() || DiscordUtil.ACTIVE_WEBHOOKS.contains(event.getAuthor().getId()))) { + return; + } + Bukkit.getScheduler().runTask(jda.getPlugin(), () -> Bukkit.dispatchCommand(new DiscordCommandSender(jda, Bukkit.getConsoleSender(), message -> event.getMessage().reply(message).queue()).getSender(), event.getMessage().getContentRaw())); diff --git a/EssentialsDiscord/src/main/resources/config.yml b/EssentialsDiscord/src/main/resources/config.yml index aecf5b0351c..b1faa522735 100644 --- a/EssentialsDiscord/src/main/resources/config.yml +++ b/EssentialsDiscord/src/main/resources/config.yml @@ -86,6 +86,8 @@ console: # console. It's recommended you stick to the /execute command which has role permission checks (see command configuration below). # Note 2: This option requires a channel ID and is not supported if you specify a webhook URL above. You'll need to use /execute in Discord if you use a webhook URL. command-relay: false + # Set to true if bots/webhooks should be able to send commands through the command relay. + bot-command-relay: false # The maximum log level of messages to send to the console relay. # The following is a list of available log levels in order of lowest to highest. # Changing the log level will send all log levels above it to the console relay.