From 62aa9575499819d58d1160e349da3157efd073f9 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Fri, 8 Oct 2021 10:13:36 +0200 Subject: [PATCH] Error reporting if bot added with wrong scope --- .../tjbot/commands/system/CommandSystem.java | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java index 4a5a0db0d7..de122d64a6 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java @@ -1,19 +1,24 @@ package org.togetherjava.tjbot.commands.system; import com.fasterxml.jackson.core.JsonProcessingException; +import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.TextChannel; import net.dv8tion.jda.api.events.ReadyEvent; import net.dv8tion.jda.api.events.interaction.ButtonClickEvent; import net.dv8tion.jda.api.events.interaction.SelectionMenuEvent; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.exceptions.ErrorHandler; import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.interactions.commands.Command; import net.dv8tion.jda.api.interactions.components.ComponentInteraction; +import net.dv8tion.jda.api.requests.ErrorResponse; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.Commands; import org.togetherjava.tjbot.commands.SlashCommand; +import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.db.Database; import java.util.*; @@ -116,7 +121,7 @@ private void registerReloadCommand(@NotNull Guild guild) { guild.upsertCommand(reloadCommand.getData()) .queue(command -> logger.debug("Registered '{}' for guild '{}'", RELOAD_COMMAND, guild.getName())); - }); + }, ex -> handleRegisterErrors(ex, guild)); } /** @@ -168,6 +173,36 @@ private void forwardComponentCommand(@NotNull T return Objects.requireNonNull(nameToSlashCommands.get(name)); } + private static void handleRegisterErrors(Throwable ex, Guild guild) { + new ErrorHandler().handle(ErrorResponse.MISSING_ACCESS, errorResponse -> { + // Find a channel that we have permissions to write to + // NOTE Unfortunately, there is no better accurate way to find a proper channel + // where we can report the setup problems other than simply iterating all of them. + Optional channelToReportTo = guild.getTextChannelCache() + .stream() + .filter(channel -> guild.getPublicRole() + .hasPermission(channel, Permission.MESSAGE_WRITE)) + .findAny(); + + // Report the problem to the guild + Config config = Config.getInstance(); + channelToReportTo.ifPresent(textChannel -> textChannel + .sendMessage("I need the commands scope, please invite me correctly." + + " You can join '%s' or visit '%s' for more info, I will leave your guild now." + .formatted(config.getDiscordGuildInvite(), config.getProjectWebsite())) + .queue()); + + guild.leave().queue(); + + String unableToReportText = channelToReportTo.isPresent() ? "" + : " Did not find any public text channel to report the issue to, unable to inform the guild."; + logger.warn( + "Guild '{}' does not have the required command scope, unable to register, leaving it.{}", + guild.getName(), unableToReportText, ex); + }).accept(ex); + + } + /** * Extension of {@link java.util.function.BiConsumer} but for 3 elements. *