Skip to content

Commit e6a2657

Browse files
authored
fix: Handle UNKNOWN_MESSAGE exception in SuggestionsUpDownVoter (#1415)
* SuggestionsUpDownVoter should have UNKNOWN_MESSAGE exception gracefully with an INFO log instead #1414 * SuggestionsUpDownVoter should have UNKNOWN_MESSAGE exception gracefully with an INFO log instead #1414 Refactored and idomaticized * SuggestionsUpDownVoter should have UNKNOWN_MESSAGE exception gracefully with an INFO log instead #1414 Refactored and idomaticized -- oops updated to make sure its similar via both react and create thread * updated info log strings
1 parent 7f63240 commit e6a2657

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

application/src/main/java/org/togetherjava/tjbot/features/basic/SuggestionsUpDownVoter.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,22 @@ public void onMessageReceived(MessageReceivedEvent event) {
5050
Message message = event.getMessage();
5151

5252
createThread(message);
53+
5354
reactWith(config.getUpVoteEmoteName(), FALLBACK_UP_VOTE, guild, message);
5455
reactWith(config.getDownVoteEmoteName(), FALLBACK_DOWN_VOTE, guild, message);
5556
}
5657

5758
private static void createThread(Message message) {
5859
String threadTitle = generateThreadTitle(message);
59-
message.createThreadChannel(threadTitle).queue();
60+
message.createThreadChannel(threadTitle).queue(_ -> {
61+
}, exception -> {
62+
if (exception instanceof ErrorResponseException responseException
63+
&& responseException.getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) {
64+
logger.info("Failed to start suggestion thread: source message deleted");
65+
return;
66+
67+
}
68+
});
6069
}
6170

6271
/**
@@ -91,19 +100,25 @@ private static void reactWith(String emojiName, Emoji fallbackEmoji, Guild guild
91100
"Unable to vote on a suggestion with the configured emoji ('{}'), using fallback instead.",
92101
emojiName);
93102
return message.addReaction(fallbackEmoji);
94-
}).queue(ignored -> {
95-
}, exception -> {
96-
if (exception instanceof ErrorResponseException responseException
97-
&& responseException.getErrorResponse() == ErrorResponse.REACTION_BLOCKED) {
103+
}).queue(_ -> {
104+
}, SuggestionsUpDownVoter::handleReactionFailure);
105+
}
106+
107+
private static void handleReactionFailure(Throwable exception) {
108+
if (exception instanceof ErrorResponseException responseException) {
109+
if (responseException.getErrorResponse() == ErrorResponse.REACTION_BLOCKED) {
98110
// User blocked the bot, hence the bot can not add reactions to their messages.
99-
// Nothing we can do here.
100111
return;
101112
}
102-
103-
logger.error("Attempted to react to a suggestion, but failed", exception);
104-
});
113+
if (responseException.getErrorResponse() == ErrorResponse.UNKNOWN_MESSAGE) {
114+
logger.info("Failed to react to suggestion: source message deleted");
115+
return;
116+
}
117+
}
118+
logger.error("Attempted to react to a suggestion, but failed", exception);
105119
}
106120

121+
107122
private static Optional<RichCustomEmoji> getEmojiByName(String name, Guild guild) {
108123
return guild.getEmojisByName(name, false).stream().findAny();
109124
}

0 commit comments

Comments
 (0)