66import net .dv8tion .jda .api .entities .channel .concrete .TextChannel ;
77import net .dv8tion .jda .api .entities .emoji .Emoji ;
88import net .dv8tion .jda .api .events .message .react .MessageReactionAddEvent ;
9- import net .dv8tion .jda .api .requests .restaction . MessageCreateAction ;
9+ import net .dv8tion .jda .api .requests .RestAction ;
1010import org .slf4j .Logger ;
1111import org .slf4j .LoggerFactory ;
1212
1313import org .togetherjava .tjbot .config .Config ;
14- import org .togetherjava .tjbot .config .CoolMessagesBoardConfig ;
14+ import org .togetherjava .tjbot .config .QuoteBoardConfig ;
1515import org .togetherjava .tjbot .features .MessageReceiverAdapter ;
1616
1717import java .util .Optional ;
2020
2121/**
2222 * Manager for the cool messages board. It appends highly-voted text messages to a separate channel
23- * where members of the guild can see a list of all of them.
23+ * where members of the guild can see a list of all of them. User reacts to a message with a
24+ * configured emoji it then forwards this message to the configured quote board channel
2425 */
25- public final class CoolMessagesBoardManager extends MessageReceiverAdapter {
26+ public final class QuoteBoardForwarder extends MessageReceiverAdapter {
2627
27- private static final Logger logger = LoggerFactory .getLogger (CoolMessagesBoardManager .class );
28- private final Emoji coolEmoji ;
29- private final Predicate <String > boardChannelNamePredicate ;
30- private final CoolMessagesBoardConfig config ;
28+ private static final Logger logger = LoggerFactory .getLogger (QuoteBoardForwarder .class );
29+ private final Emoji triggerReaction ;
30+ private final Predicate <String > isQuoteBoardChannelName ;
31+ private final QuoteBoardConfig config ;
3132
3233 /**
33- * Constructs a new instance of CoolMessagesBoardManager .
34+ * Constructs a new instance of QuoteBoardForwarder .
3435 *
3536 * @param config the configuration containing settings specific to the cool messages board,
3637 * including the reaction emoji and the pattern to match board channel names
3738 */
38- public CoolMessagesBoardManager (Config config ) {
39+ public QuoteBoardForwarder (Config config ) {
3940 this .config = config .getCoolMessagesConfig ();
40- this .coolEmoji = Emoji .fromUnicode (this .config .reactionEmoji ());
41+ this .triggerReaction = Emoji .fromUnicode (this .config .reactionEmoji ());
4142
42- boardChannelNamePredicate =
43+ isQuoteBoardChannelName =
4344 Pattern .compile (this .config .boardChannelPattern ()).asMatchPredicate ();
4445 }
4546
4647 @ Override
4748 public void onMessageReactionAdd (MessageReactionAddEvent event ) {
4849 final MessageReaction messageReaction = event .getReaction ();
49- int originalReactionsCount = messageReaction .hasCount () ? messageReaction .getCount () : 0 ;
50- boolean isCoolEmoji = messageReaction .getEmoji ().equals (coolEmoji );
50+ boolean isCoolEmoji = messageReaction .getEmoji ().equals (triggerReaction );
5151 long guildId = event .getGuild ().getIdLong ();
52- Optional <TextChannel > boardChannel = getBoardChannel (event .getJDA (), guildId );
5352
54- if (boardChannel .isEmpty ()) {
55- logger .warn (
56- "Could not find board channel with pattern '{}' in server with ID '{}'. Skipping reaction handling..." ,
57- this .config .boardChannelPattern (), guildId );
53+ if (hasAlreadyForwardedMessage (event .getJDA (), messageReaction )) {
5854 return ;
5955 }
6056
61- // If the bot has already reacted to this message, then this means that
62- // the message has been quoted to the cool messages board, so skip it.
63- if (hasBotReacted (event .getJDA (), messageReaction )) {
64- return ;
65- }
57+ final int reactionsCount = (int ) messageReaction .retrieveUsers ().stream ().count ();
58+ if (isCoolEmoji && reactionsCount >= config .minimumReactions ()) {
59+ Optional <TextChannel > boardChannel = findQuoteBoardChannel (event .getJDA (), guildId );
60+
61+ if (boardChannel .isEmpty ()) {
62+ logger .warn (
63+ "Could not find board channel with pattern '{}' in server with ID '{}'. Skipping reaction handling..." ,
64+ this .config .boardChannelPattern (), guildId );
65+ return ;
66+ }
6667
67- final int newReactionsCount = originalReactionsCount + 1 ;
68- if (isCoolEmoji && newReactionsCount >= config .minimumReactions ()) {
6968 event .retrieveMessage ()
70- .queue (message -> message .addReaction (coolEmoji )
71- .flatMap (v -> insertCoolMessage (boardChannel .get (), message ))
72- .queue (),
73- e -> logger .warn ("Tried to retrieve cool message but got: {}" ,
74- e .getMessage ()));
69+ .queue (message -> markAsProcessed (message ).flatMap (v -> message
70+ .forwardTo (boardChannel .orElseThrow ())).queue (), e -> logger .warn (
71+ "Unknown error while attempting to retrieve and forward message for quote-board, message is ignored." ,
72+ e ));
7573 }
7674 }
7775
76+ private RestAction <Void > markAsProcessed (Message message ) {
77+ return message .addReaction (triggerReaction );
78+ }
79+
7880 /**
7981 * Gets the board text channel where the quotes go to, wrapped in an optional.
8082 *
8183 * @param jda the JDA
8284 * @param guildId the guild ID
8385 * @return the board text channel
8486 */
85- private Optional <TextChannel > getBoardChannel (JDA jda , long guildId ) {
87+ private Optional <TextChannel > findQuoteBoardChannel (JDA jda , long guildId ) {
8688 return jda .getGuildById (guildId )
8789 .getTextChannelCache ()
8890 .stream ()
89- .filter (channel -> boardChannelNamePredicate .test (channel .getName ()))
91+ .filter (channel -> isQuoteBoardChannelName .test (channel .getName ()))
9092 .findAny ();
9193 }
9294
@@ -95,16 +97,12 @@ private Optional<TextChannel> getBoardChannel(JDA jda, long guildId) {
9597 *
9698 * @return a {@link MessageCreateAction} of the call to make
9799 */
98- private static MessageCreateAction insertCoolMessage (TextChannel boardChannel ,
99- Message message ) {
100- return message .forwardTo (boardChannel );
101- }
102100
103101 /**
104102 * Checks a {@link MessageReaction} to see if the bot has reacted to it.
105103 */
106- private boolean hasBotReacted (JDA jda , MessageReaction messageReaction ) {
107- if (!coolEmoji .equals (messageReaction .getEmoji ())) {
104+ private boolean hasAlreadyForwardedMessage (JDA jda , MessageReaction messageReaction ) {
105+ if (!triggerReaction .equals (messageReaction .getEmoji ())) {
108106 return false ;
109107 }
110108
0 commit comments