Skip to content

Commit

Permalink
fix(CoolMessages): prevent duplicate message add
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
christolis committed Feb 21, 2024
1 parent b910040 commit cf81565
Showing 1 changed file with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,18 @@ public void onMessageReactionAdd(MessageReactionAddEvent event) {
return;
}

// If the bot has already reacted to this message, then this means that
// the message has been quoted to the cool messages board, so skip it.
if (hasBotReacted(event.getJDA(), messageReaction)) {
return;
}

if (isCoolEmoji && originalReactionsCount + 1 >= config.minimumReactions()) {
event.retrieveMessage()
.queue(message -> insertCoolMessage(boardChannel.orElseThrow(), message),
e -> logger.warn("Tried to retrieve cool message but got: {}",
e.getMessage()));
event.retrieveMessage().queue(message -> {
message.addReaction(REACT_EMOJI).queue();

insertCoolMessage(boardChannel.orElseThrow(), message);
}, e -> logger.warn("Tried to retrieve cool message but got: {}", e.getMessage()));
}
}

Expand Down Expand Up @@ -110,4 +117,13 @@ private static MessageEmbed createQuoteEmbed(Message message) {
.setTimestamp(message.getTimeCreated())
.build();
}

/**
* Checks a {@link MessageReaction} to see if the bot has reacted to it.
*/
private static boolean hasBotReacted(JDA jda, MessageReaction messageReaction) {
return messageReaction.retrieveUsers()
.parallelStream()
.anyMatch(user -> jda.getSelfUser().getIdLong() == user.getIdLong());
}
}

0 comments on commit cf81565

Please sign in to comment.