From d15b5bf7aab8a99e9238b764c95d28ad85c5aaed Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Mon, 14 Mar 2022 16:32:29 +0200 Subject: [PATCH 1/6] warped the line that reacts to the message in a `try catch` block --- .../basic/SuggestionsUpDownVoter.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java b/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java index 48b05e1bb2..c83a75fba0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java @@ -4,6 +4,8 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; +import net.dv8tion.jda.api.exceptions.ErrorResponseException; +import net.dv8tion.jda.api.requests.ErrorResponse; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -51,12 +53,18 @@ public void onMessageReceived(@NotNull GuildMessageReceivedEvent event) { private static void reactWith(@NotNull String emoteName, @NotNull String fallbackUnicodeEmote, @NotNull Guild guild, @NotNull Message message) { - getEmoteByName(emoteName, guild).map(message::addReaction).orElseGet(() -> { - logger.warn( - "Unable to vote on a suggestion with the configured emote ('{}'), using fallback instead.", - emoteName); - return message.addReaction(fallbackUnicodeEmote); - }).queue(); + try { + getEmoteByName(emoteName, guild).map(message::addReaction).orElseGet(() -> { + logger.warn( + "Unable to vote on a suggestion with the configured emote ('{}'), using fallback instead.", + emoteName); + return message.addReaction(fallbackUnicodeEmote); + }).complete(); + } catch (ErrorResponseException exception) { + if (exception.getErrorResponse() != ErrorResponse.REACTION_BLOCKED) { + throw exception; + } + } } private static @NotNull Optional getEmoteByName(@NotNull String name, From ba3a3a0bb53e0b04c45b41e11faa443c1cebf721 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Mon, 14 Mar 2022 19:28:37 +0200 Subject: [PATCH 2/6] reverted and added an handler to `queue` --- .../basic/SuggestionsUpDownVoter.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java b/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java index c83a75fba0..bba976fe50 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java @@ -53,18 +53,20 @@ public void onMessageReceived(@NotNull GuildMessageReceivedEvent event) { private static void reactWith(@NotNull String emoteName, @NotNull String fallbackUnicodeEmote, @NotNull Guild guild, @NotNull Message message) { - try { - getEmoteByName(emoteName, guild).map(message::addReaction).orElseGet(() -> { - logger.warn( - "Unable to vote on a suggestion with the configured emote ('{}'), using fallback instead.", - emoteName); - return message.addReaction(fallbackUnicodeEmote); - }).complete(); - } catch (ErrorResponseException exception) { - if (exception.getErrorResponse() != ErrorResponse.REACTION_BLOCKED) { - throw exception; + getEmoteByName(emoteName, guild).map(message::addReaction).orElseGet(() -> { + logger.warn( + "Unable to vote on a suggestion with the configured emote ('{}'), using fallback instead.", + emoteName); + return message.addReaction(fallbackUnicodeEmote); + }).queue(ignored -> { + }, exception -> { + if (exception instanceof ErrorResponseException && ((ErrorResponseException) exception) + .getErrorResponse() == ErrorResponse.REACTION_BLOCKED) { + return; } - } + + logger.error(exception.getMessage(), exception); + }); } private static @NotNull Optional getEmoteByName(@NotNull String name, From 8d554a3ede924c5db838bb5219f86cc92b5008f4 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Tue, 15 Mar 2022 09:49:51 +0200 Subject: [PATCH 3/6] Changed log message --- .../tjbot/commands/basic/SuggestionsUpDownVoter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java b/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java index bba976fe50..f2c9652f58 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java @@ -65,7 +65,7 @@ private static void reactWith(@NotNull String emoteName, @NotNull String fallbac return; } - logger.error(exception.getMessage(), exception); + logger.error("Attempted to react to a suggestion, but failed", exception); }); } From a1668ab5592372ee6ed02f5143f2a68c6d3a6c48 Mon Sep 17 00:00:00 2001 From: Mai Porat <63251610+MaiTheLord@users.noreply.github.com> Date: Tue, 15 Mar 2022 10:07:48 +0200 Subject: [PATCH 4/6] changed `instanceof cast` style. Co-authored-by: Tais993 <49957334+Tais993@users.noreply.github.com> --- .../tjbot/commands/basic/SuggestionsUpDownVoter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java b/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java index f2c9652f58..c22f157569 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java @@ -60,8 +60,8 @@ private static void reactWith(@NotNull String emoteName, @NotNull String fallbac return message.addReaction(fallbackUnicodeEmote); }).queue(ignored -> { }, exception -> { - if (exception instanceof ErrorResponseException && ((ErrorResponseException) exception) - .getErrorResponse() == ErrorResponse.REACTION_BLOCKED) { + if (exception instanceof ErrorResponseException responseException && + responseException.getErrorResponse() == ErrorResponse.REACTION_BLOCKED) { return; } From 2bf68a056d9e4b6163cbd3a823576edd9fd2c2b5 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Tue, 15 Mar 2022 10:15:42 +0200 Subject: [PATCH 5/6] spotless fix --- .../tjbot/commands/basic/SuggestionsUpDownVoter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java b/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java index c22f157569..7095fc83c2 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java @@ -60,8 +60,8 @@ private static void reactWith(@NotNull String emoteName, @NotNull String fallbac return message.addReaction(fallbackUnicodeEmote); }).queue(ignored -> { }, exception -> { - if (exception instanceof ErrorResponseException responseException && - responseException.getErrorResponse() == ErrorResponse.REACTION_BLOCKED) { + if (exception instanceof ErrorResponseException responseException + && responseException.getErrorResponse() == ErrorResponse.REACTION_BLOCKED) { return; } From 8c2aa24c42c68791218adcf3b0183491af6d9554 Mon Sep 17 00:00:00 2001 From: MaiTheLord <63251610+MaiTheLord@users.noreply.github.com> Date: Tue, 15 Mar 2022 11:00:35 +0200 Subject: [PATCH 6/6] added descriptive comment. --- .../tjbot/commands/basic/SuggestionsUpDownVoter.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java b/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java index 7095fc83c2..8966932dcd 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/basic/SuggestionsUpDownVoter.java @@ -62,6 +62,8 @@ private static void reactWith(@NotNull String emoteName, @NotNull String fallbac }, exception -> { if (exception instanceof ErrorResponseException responseException && responseException.getErrorResponse() == ErrorResponse.REACTION_BLOCKED) { + // User blocked the bot, hence the bot can not add reactions to their messages. + // Nothing we can do here. return; }