Skip to content

Commit 601d43f

Browse files
committed
fix(CoolMessages): prevent duplicate message add
Fixes a bug where any user could star react to a message and the bot would unconditionally add that message to the cool messages board, considering the fact that it would surpass the minimum reaction count requirement. This commit gets the bot to react with the same cool messages emoji the first time and use that reaction as proof that it has quoted the message and that it should not deal with it in case somebody reacts to it again.
1 parent 34fe09f commit 601d43f

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

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

+20-4
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,18 @@ public void onMessageReactionAdd(MessageReactionAddEvent event) {
5454
return;
5555
}
5656

57+
// If the bot has already reacted to this message, then this means that
58+
// the message has been quoted to the cool messages board, so skip it.
59+
if (hasBotReacted(event.getJDA(), messageReaction)) {
60+
return;
61+
}
62+
5763
if (isCoolEmoji && originalReactionsCount + 1 >= config.minimumReactions()) {
58-
event.retrieveMessage()
59-
.queue(message -> insertCoolMessage(boardChannel.orElseThrow(), message),
60-
e -> logger.warn("Tried to retrieve cool message but got: {}",
61-
e.getMessage()));
64+
event.retrieveMessage().queue(message -> {
65+
message.addReaction(REACT_EMOJI).queue();
66+
67+
insertCoolMessage(boardChannel.orElseThrow(), message);
68+
}, e -> logger.warn("Tried to retrieve cool message but got: {}", e.getMessage()));
6269
}
6370
}
6471

@@ -110,4 +117,13 @@ private static MessageEmbed createQuoteEmbed(Message message) {
110117
.setTimestamp(message.getTimeCreated())
111118
.build();
112119
}
120+
121+
/**
122+
* Checks a {@link MessageReaction} to see if the bot has reacted to it.
123+
*/
124+
private static boolean hasBotReacted(JDA jda, MessageReaction messageReaction) {
125+
return messageReaction.retrieveUsers()
126+
.parallelStream()
127+
.anyMatch(user -> jda.getSelfUser().getIdLong() == user.getIdLong());
128+
}
113129
}

0 commit comments

Comments
 (0)