diff --git a/application/src/main/java/org/togetherjava/tjbot/features/basic/SuggestionsUpDownVoter.java b/application/src/main/java/org/togetherjava/tjbot/features/basic/SuggestionsUpDownVoter.java index 5dbbdbf8b2..e07682725f 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/basic/SuggestionsUpDownVoter.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/basic/SuggestionsUpDownVoter.java @@ -50,13 +50,22 @@ public void onMessageReceived(MessageReceivedEvent event) { Message message = event.getMessage(); createThread(message); + reactWith(config.getUpVoteEmoteName(), FALLBACK_UP_VOTE, guild, message); reactWith(config.getDownVoteEmoteName(), FALLBACK_DOWN_VOTE, guild, message); } private static void createThread(Message message) { String threadTitle = generateThreadTitle(message); - message.createThreadChannel(threadTitle).queue(); + message.createThreadChannel(threadTitle).queue(_ -> { + }, exception -> { + if (exception instanceof ErrorResponseException responseException + && responseException.getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) { + logger.info("Failed to start suggestion thread: source message deleted"); + return; + + } + }); } /** @@ -91,19 +100,25 @@ private static void reactWith(String emojiName, Emoji fallbackEmoji, Guild guild "Unable to vote on a suggestion with the configured emoji ('{}'), using fallback instead.", emojiName); return message.addReaction(fallbackEmoji); - }).queue(ignored -> { - }, exception -> { - if (exception instanceof ErrorResponseException responseException - && responseException.getErrorResponse() == ErrorResponse.REACTION_BLOCKED) { + }).queue(_ -> { + }, SuggestionsUpDownVoter::handleReactionFailure); + } + + private static void handleReactionFailure(Throwable exception) { + if (exception instanceof ErrorResponseException responseException) { + if (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; } - - logger.error("Attempted to react to a suggestion, but failed", exception); - }); + if (responseException.getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) { + logger.info("Failed to react to suggestion: source message deleted"); + return; + } + } + logger.error("Attempted to react to a suggestion, but failed", exception); } + private static Optional getEmojiByName(String name, Guild guild) { return guild.getEmojisByName(name, false).stream().findAny(); }