1414import org .togetherjava .tjbot .config .QuoteBoardConfig ;
1515import org .togetherjava .tjbot .features .MessageReceiverAdapter ;
1616
17+ import java .util .List ;
1718import java .util .Optional ;
1819import java .util .function .Predicate ;
1920import java .util .regex .Pattern ;
@@ -45,47 +46,59 @@ public final class QuoteBoardForwarder extends MessageReceiverAdapter {
4546 * including the reaction emoji and the pattern to match board channel names
4647 */
4748 public QuoteBoardForwarder (Config config ) {
48- this .config = config .getQuoteMessagesConfig ();
49+ this .config = config .getQuoteBoardConfig ();
4950 this .triggerReaction = Emoji .fromUnicode (this .config .reactionEmoji ());
5051
51- isQuoteBoardChannelName =
52- Pattern .compile (this .config .boardChannelPattern ()).asMatchPredicate ();
52+ this .isQuoteBoardChannelName = Pattern .compile (this .config .channel ()).asMatchPredicate ();
5353 }
5454
5555 @ Override
5656 public void onMessageReactionAdd (MessageReactionAddEvent event ) {
57+ logger .debug ("Received MessageReactionAddEvent: messageId={}, channelId={}, userId={}" ,
58+ event .getMessageId (), event .getChannel ().getId (), event .getUserId ());
59+
5760 final MessageReaction messageReaction = event .getReaction ();
58- boolean isTriggerEmoji = messageReaction .getEmoji ().equals (triggerReaction );
59- long guildId = event .getGuild ().getIdLong ();
6061
61- if (!isTriggerEmoji ) {
62+ if (!messageReaction .getEmoji ().equals (triggerReaction )) {
63+ logger .debug ("Reaction emoji '{}' does not match trigger emoji '{}'. Ignoring." ,
64+ messageReaction .getEmoji (), triggerReaction );
6265 return ;
6366 }
6467
6568 if (hasAlreadyForwardedMessage (event .getJDA (), messageReaction )) {
69+ logger .debug ("Message has already been forwarded by the bot. Skipping." );
6670 return ;
6771 }
6872
69- final int reactionsCount = (int ) messageReaction .retrieveUsers ().stream ().count ();
70-
71- if (reactionsCount < config .minimumReactions ()) {
73+ long reactionCount = messageReaction .retrieveUsers ().stream ().count ();
74+ if (reactionCount < config .minimumReactions ()) {
75+ logger .debug ("Reaction count {} is less than minimum required {}. Skipping." ,
76+ reactionCount , config .minimumReactions ());
7277 return ;
7378 }
7479
80+ final long guildId = event .getGuild ().getIdLong ();
81+
7582 Optional <TextChannel > boardChannel = findQuoteBoardChannel (event .getJDA (), guildId );
7683
7784 if (boardChannel .isEmpty ()) {
7885 logger .warn (
7986 "Could not find board channel with pattern '{}' in server with ID '{}'. Skipping reaction handling..." ,
80- this .config .boardChannelPattern (), guildId );
87+ this .config .channel (), guildId );
8188 return ;
8289 }
8390
91+ logger .debug ("Forwarding message to quote board channel: {}" , boardChannel .get ().getName ());
92+
8493 event .retrieveMessage ()
85- .queue (message -> markAsProcessed (message ).flatMap (v -> message
86- .forwardTo (boardChannel .orElseThrow ())).queue (), e -> logger .warn (
87- "Unknown error while attempting to retrieve and forward message for quote-board, message is ignored." ,
88- e ));
94+ .queue (message -> markAsProcessed (message )
95+ .flatMap (v -> message .forwardTo (boardChannel .orElseThrow ()))
96+ .queue (_ -> logger .debug ("Message forwarded to quote board channel: {}" ,
97+ boardChannel .get ().getName ())),
98+
99+ e -> logger .warn (
100+ "Unknown error while attempting to retrieve and forward message for quote-board, message is ignored." ,
101+ e ));
89102
90103 }
91104
@@ -101,18 +114,26 @@ private RestAction<Void> markAsProcessed(Message message) {
101114 * @return the board text channel
102115 */
103116 private Optional <TextChannel > findQuoteBoardChannel (JDA jda , long guildId ) {
104- return jda .getGuildById (guildId )
105- .getTextChannelCache ()
117+ var guild = jda .getGuildById (guildId );
118+
119+ if (guild == null ) {
120+ throw new IllegalStateException (
121+ String .format ("Guild with ID '%d' not found." , guildId ));
122+ }
123+
124+ List <TextChannel > matchingChannels = guild .getTextChannelCache ()
106125 .stream ()
107126 .filter (channel -> isQuoteBoardChannelName .test (channel .getName ()))
108- .findAny ();
109- }
127+ .toList ();
110128
111- /**
112- * Inserts a message to the specified text channel
113- *
114- * @return a {@link MessageCreateAction} of the call to make
115- */
129+ if (matchingChannels .size () > 1 ) {
130+ logger .warn (
131+ "Multiple quote board channels found matching pattern '{}' in guild with ID '{}'. Selecting the first one anyway." ,
132+ this .config .channel (), guildId );
133+ }
134+
135+ return matchingChannels .stream ().findFirst ();
136+ }
116137
117138 /**
118139 * Checks a {@link MessageReaction} to see if the bot has reacted to it.
0 commit comments