6
6
import net .dv8tion .jda .api .entities .channel .concrete .TextChannel ;
7
7
import net .dv8tion .jda .api .entities .emoji .Emoji ;
8
8
import 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 ;
10
10
import org .slf4j .Logger ;
11
11
import org .slf4j .LoggerFactory ;
12
12
13
13
import org .togetherjava .tjbot .config .Config ;
14
- import org .togetherjava .tjbot .config .CoolMessagesBoardConfig ;
14
+ import org .togetherjava .tjbot .config .QuoteBoardConfig ;
15
15
import org .togetherjava .tjbot .features .MessageReceiverAdapter ;
16
16
17
17
import java .util .Optional ;
20
20
21
21
/**
22
22
* 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
24
25
*/
25
- public final class CoolMessagesBoardManager extends MessageReceiverAdapter {
26
+ public final class QuoteBoardForwarder extends MessageReceiverAdapter {
26
27
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 ;
31
32
32
33
/**
33
- * Constructs a new instance of CoolMessagesBoardManager .
34
+ * Constructs a new instance of QuoteBoardForwarder .
34
35
*
35
36
* @param config the configuration containing settings specific to the cool messages board,
36
37
* including the reaction emoji and the pattern to match board channel names
37
38
*/
38
- public CoolMessagesBoardManager (Config config ) {
39
+ public QuoteBoardForwarder (Config config ) {
39
40
this .config = config .getCoolMessagesConfig ();
40
- this .coolEmoji = Emoji .fromUnicode (this .config .reactionEmoji ());
41
+ this .triggerReaction = Emoji .fromUnicode (this .config .reactionEmoji ());
41
42
42
- boardChannelNamePredicate =
43
+ isQuoteBoardChannelName =
43
44
Pattern .compile (this .config .boardChannelPattern ()).asMatchPredicate ();
44
45
}
45
46
46
47
@ Override
47
48
public void onMessageReactionAdd (MessageReactionAddEvent event ) {
48
49
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 );
51
51
long guildId = event .getGuild ().getIdLong ();
52
- Optional <TextChannel > boardChannel = getBoardChannel (event .getJDA (), guildId );
53
52
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 )) {
58
54
return ;
59
55
}
60
56
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
+ }
66
67
67
- final int newReactionsCount = originalReactionsCount + 1 ;
68
- if (isCoolEmoji && newReactionsCount >= config .minimumReactions ()) {
69
68
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 ));
75
73
}
76
74
}
77
75
76
+ private RestAction <Void > markAsProcessed (Message message ) {
77
+ return message .addReaction (triggerReaction );
78
+ }
79
+
78
80
/**
79
81
* Gets the board text channel where the quotes go to, wrapped in an optional.
80
82
*
81
83
* @param jda the JDA
82
84
* @param guildId the guild ID
83
85
* @return the board text channel
84
86
*/
85
- private Optional <TextChannel > getBoardChannel (JDA jda , long guildId ) {
87
+ private Optional <TextChannel > findQuoteBoardChannel (JDA jda , long guildId ) {
86
88
return jda .getGuildById (guildId )
87
89
.getTextChannelCache ()
88
90
.stream ()
89
- .filter (channel -> boardChannelNamePredicate .test (channel .getName ()))
91
+ .filter (channel -> isQuoteBoardChannelName .test (channel .getName ()))
90
92
.findAny ();
91
93
}
92
94
@@ -95,16 +97,12 @@ private Optional<TextChannel> getBoardChannel(JDA jda, long guildId) {
95
97
*
96
98
* @return a {@link MessageCreateAction} of the call to make
97
99
*/
98
- private static MessageCreateAction insertCoolMessage (TextChannel boardChannel ,
99
- Message message ) {
100
- return message .forwardTo (boardChannel );
101
- }
102
100
103
101
/**
104
102
* Checks a {@link MessageReaction} to see if the bot has reacted to it.
105
103
*/
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 ())) {
108
106
return false ;
109
107
}
110
108
0 commit comments