2121import net .dv8tion .jda .api .entities .emoji .Emoji ;
2222import net .dv8tion .jda .api .entities .emoji .UnicodeEmoji ;
2323import net .dv8tion .jda .api .events .message .MessageDeleteEvent ;
24+ import net .dv8tion .jda .api .events .message .react .GenericMessageReactionEvent ;
2425import net .dv8tion .jda .api .events .message .react .MessageReactionAddEvent ;
2526import net .dv8tion .jda .api .events .message .react .MessageReactionRemoveEvent ;
2627import net .dv8tion .jda .api .hooks .ListenerAdapter ;
3031import org .jetbrains .annotations .NotNull ;
3132import org .springframework .dao .DataAccessException ;
3233
34+ import java .util .HashSet ;
3335import java .util .List ;
3436import java .util .Optional ;
37+ import java .util .Set ;
3538import java .util .concurrent .ExecutionException ;
3639import java .util .concurrent .ExecutorService ;
3740
@@ -47,34 +50,55 @@ public class StarboardManager extends ListenerAdapter {
4750
4851 @ Override
4952 public void onMessageReactionAdd (@ NotNull MessageReactionAddEvent event ) {
50- if (isInvalidUser (event .getUser ())) return ;
51- if (isInvalidChannel (event .getChannel ())) return ;
53+ if (shouldSkip (event )) {
54+ return ;
55+ }
56+ if (isInvalidUser (event .getUser ())) {
57+ return ;
58+ }
59+ if (event .getMessageAuthorIdLong () == event .getUserIdLong ()) {
60+ return ;
61+ }
5262 handleReactionEvent (event .getGuild (), event .getEmoji (), event .getChannel (), event .getMessageIdLong ());
5363 }
5464
5565 @ Override
5666 public void onMessageReactionRemove (@ NotNull MessageReactionRemoveEvent event ) {
57- if (isInvalidUser (event .getUser ())) return ;
58- if (isInvalidChannel (event .getGuildChannel ())) return ;
67+ if (shouldSkip (event )) {
68+ return ;
69+ }
5970 handleReactionEvent (event .getGuild (), event .getEmoji (), event .getChannel (), event .getMessageIdLong ());
6071 }
61-
72+
73+ private boolean shouldSkip (GenericMessageReactionEvent event ) {
74+ if (isInvalidChannel (event .getChannel ())) {
75+ return true ;
76+ }
77+ StarboardConfig config = botConfig .get (event .getGuild ()).getStarboardConfig ();
78+ if (config .getDisallowedChannelIds ().contains (event .getChannel ().getIdLong ())) {
79+ return true ;
80+ }
81+ if (config .getStarboardChannelId () == event .getChannel ().getIdLong ()) {
82+ return true ;
83+ }
84+ return false ;
85+ }
86+
6287 private void handleReactionEvent (Guild guild , Emoji emoji , MessageChannel channel , long messageId ) {
6388 asyncPool .submit (() -> {
6489 StarboardConfig config = botConfig .get (guild ).getStarboardConfig ();
65- if (config .getStarboardChannel ().equals (channel )) return ;
66- Emoji starEmote = config .getEmojis ().get (0 );
67- if (!emoji .equals (starEmote )) return ;
90+ Set <Emoji > starEmojis = new HashSet <>(config .getEmojis ());
91+ if (!starEmojis .contains (emoji )) return ;
6892 channel .retrieveMessageById (messageId ).queue (
6993 message -> {
70- int stars = getReactionCountForEmote (starEmote , message );
7194 asyncPool .execute (()->{
95+ long stars = getReactionCountForEmotes (starEmojis , message );
7296 try {
7397 starboardRepository
7498 .getEntryByMessageId (message .getIdLong ())
75- .ifPresentOrElse (entry ->{
76- updateStarboardMessage (message , stars , config );
77- } , ()->{
99+ .ifPresentOrElse (_ ->
100+ updateStarboardMessage (message , stars , config )
101+ , ()->{
78102 if (stars >= config .getReactionThreshold ()) {
79103 addMessageToStarboard (message , stars , config );
80104 } else if (stars < 1 && !removeMessageFromStarboard (message .getIdLong (), channel , config )) {
@@ -122,21 +146,24 @@ private boolean isInvalidUser(User user) {
122146 }
123147
124148 /**
125- * Attemps to get the amount of reactions for the given emote .
149+ * Attemps to get the amount of reactions for the given emotes .
126150 *
127151 * @param emoji The emote.
128152 * @param message The message.
129153 * @return The amount of reactions.
130154 */
131- private int getReactionCountForEmote ( Emoji emoji , @ NotNull Message message ) {
155+ private long getReactionCountForEmotes ( Set < Emoji > emojis , @ NotNull Message message ) {
132156 return message .getReactions ().stream ()
133- .filter (r -> r .getEmoji ().equals (emoji ))
134- .findFirst ()
135- .map (MessageReaction ::getCount )
136- .orElse (0 );
157+ .filter (r -> emojis .contains (r .getEmoji ()))
158+ .flatMap (reaction -> reaction .retrieveUsers ().complete ().stream ())
159+ .filter (user -> !isInvalidUser (user ))
160+ .mapToLong (User ::getIdLong )
161+ .filter (userId -> userId != message .getAuthor ().getIdLong ())
162+ .distinct ()
163+ .count ();
137164 }
138165
139- private void addMessageToStarboard (Message message , int stars , @ NotNull StarboardConfig config ) throws DataAccessException {
166+ private void addMessageToStarboard (Message message , long stars , @ NotNull StarboardConfig config ) throws DataAccessException {
140167 if (stars < config .getReactionThreshold ()) return ;
141168 MessageEmbed embed = buildStarboardEmbed (message );
142169 MessageCreateAction action = config .getStarboardChannel ()
@@ -177,7 +204,7 @@ private void addMessageToStarboard(Message message, int stars, @NotNull Starboar
177204 );
178205 }
179206
180- private void updateStarboardMessage (@ NotNull Message message , int stars , @ NotNull StarboardConfig config ) throws DataAccessException {
207+ private void updateStarboardMessage (@ NotNull Message message , long stars , @ NotNull StarboardConfig config ) throws DataAccessException {
181208 Optional <StarboardEntry > starboardEntry = starboardRepository .getEntryByMessageId (message .getIdLong ());
182209 starboardEntry .ifPresentOrElse (entry ->{
183210 long starboardId = entry .getStarboardMessageId ();
0 commit comments