Skip to content

Commit 1ade409

Browse files
refactor: code review addressed by Zabuzard
* minimumReactions-5, star symbol instead of encoding * requests changes by zabuzard except for moving getBoardChannel down and markMessageAsProcessed * Following JavaDocs guidelines of making the first letter capital * code refactoring * refactor: use correct method for reactionsCount It turns out that for each event fired, every *single* damn time, messageReaction.hasCount() would always return false. No matter what. Terrible documentation from JDA's side. As a result, because of the ternary operator: messageReaction.hasCount() ? messageReaction.getCount() + 1 : 1 the result of `reactionsCount` would always end up holding the value of one. In the following changes, we use `messageReaction.retrieveUsers()` to get a list of the people reacted, get a `Stream<User>` from that and get its count. Much more reliable this way and it also happens to be more readable. Signed-off-by: Chris Sdogkos <[email protected]> Co-authored-by: Chris Sdogkos <[email protected]> Co-authored-by: Surya Tejess <[email protected]>
1 parent fb4fb5d commit 1ade409

File tree

5 files changed

+48
-51
lines changed

5 files changed

+48
-51
lines changed

application/config.json.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@
176176
"pollIntervalInMinutes": 10
177177
},
178178
"coolMessagesConfig": {
179-
"minimumReactions": 5,
179+
"minimumReactions": 2,
180180
"boardChannelPattern": "quotes",
181-
"reactionEmoji": "U+2B50"
181+
"reactionEmoji": ""
182182
},
183183
"memberCountCategoryPattern": "Info"
184184
}

application/src/main/java/org/togetherjava/tjbot/config/Config.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public final class Config {
4848
private final RSSFeedsConfig rssFeedsConfig;
4949
private final String selectRolesChannelPattern;
5050
private final String memberCountCategoryPattern;
51-
private final CoolMessagesBoardConfig coolMessagesConfig;
51+
private final QuoteBoardConfig coolMessagesConfig;
5252

5353
@SuppressWarnings("ConstructorWithTooManyParameters")
5454
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
@@ -103,7 +103,7 @@ private Config(@JsonProperty(value = "token", required = true) String token,
103103
@JsonProperty(value = "selectRolesChannelPattern",
104104
required = true) String selectRolesChannelPattern,
105105
@JsonProperty(value = "coolMessagesConfig",
106-
required = true) CoolMessagesBoardConfig coolMessagesConfig) {
106+
required = true) QuoteBoardConfig coolMessagesConfig) {
107107
this.token = Objects.requireNonNull(token);
108108
this.githubApiKey = Objects.requireNonNull(githubApiKey);
109109
this.databasePath = Objects.requireNonNull(databasePath);
@@ -437,7 +437,7 @@ public String getSelectRolesChannelPattern() {
437437
*
438438
* @return configuration of cool messages config
439439
*/
440-
public CoolMessagesBoardConfig getCoolMessagesConfig() {
440+
public QuoteBoardConfig getCoolMessagesConfig() {
441441
return coolMessagesConfig;
442442
}
443443

application/src/main/java/org/togetherjava/tjbot/config/CoolMessagesBoardConfig.java renamed to application/src/main/java/org/togetherjava/tjbot/config/QuoteBoardConfig.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,22 @@
66
import java.util.Objects;
77

88
/**
9-
* Configuration for the cool messages board feature, see
10-
* {@link org.togetherjava.tjbot.features.basic.CoolMessagesBoardManager}.
9+
* Configuration for the cool messages board feature, see {@link ``QuoteBoardForwarder``}.
1110
*/
1211
@JsonRootName("coolMessagesConfig")
13-
public record CoolMessagesBoardConfig(
12+
public record QuoteBoardConfig(
1413
@JsonProperty(value = "minimumReactions", required = true) int minimumReactions,
1514
@JsonProperty(value = "boardChannelPattern", required = true) String boardChannelPattern,
1615
@JsonProperty(value = "reactionEmoji", required = true) String reactionEmoji) {
1716

1817
/**
19-
* Creates a CoolMessagesBoardConfig.
18+
* Creates a QuoteBoardConfig.
2019
*
2120
* @param minimumReactions the minimum amount of reactions
2221
* @param boardChannelPattern the pattern for the board channel
2322
* @param reactionEmoji the emoji with which users should react to
2423
*/
25-
public CoolMessagesBoardConfig {
24+
public QuoteBoardConfig {
2625
Objects.requireNonNull(boardChannelPattern);
2726
}
2827
}

application/src/main/java/org/togetherjava/tjbot/features/Features.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import org.togetherjava.tjbot.config.FeatureBlacklist;
77
import org.togetherjava.tjbot.config.FeatureBlacklistConfig;
88
import org.togetherjava.tjbot.db.Database;
9-
import org.togetherjava.tjbot.features.basic.CoolMessagesBoardManager;
109
import org.togetherjava.tjbot.features.basic.MemberCountDisplayRoutine;
1110
import org.togetherjava.tjbot.features.basic.PingCommand;
11+
import org.togetherjava.tjbot.features.basic.QuoteBoardForwarder;
1212
import org.togetherjava.tjbot.features.basic.RoleSelectCommand;
1313
import org.togetherjava.tjbot.features.basic.SlashCommandEducator;
1414
import org.togetherjava.tjbot.features.basic.SuggestionsUpDownVoter;
@@ -155,7 +155,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
155155
features.add(new CodeMessageManualDetection(codeMessageHandler));
156156
features.add(new SlashCommandEducator());
157157
features.add(new PinnedNotificationRemover(config));
158-
features.add(new CoolMessagesBoardManager(config));
158+
features.add(new QuoteBoardForwarder(config));
159159

160160
// Event receivers
161161
features.add(new RejoinModerationRoleListener(actionsStore, config));

application/src/main/java/org/togetherjava/tjbot/features/basic/CoolMessagesBoardManager.java renamed to application/src/main/java/org/togetherjava/tjbot/features/basic/QuoteBoardForwarder.java

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
77
import net.dv8tion.jda.api.entities.emoji.Emoji;
88
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;
1010
import org.slf4j.Logger;
1111
import org.slf4j.LoggerFactory;
1212

1313
import org.togetherjava.tjbot.config.Config;
14-
import org.togetherjava.tjbot.config.CoolMessagesBoardConfig;
14+
import org.togetherjava.tjbot.config.QuoteBoardConfig;
1515
import org.togetherjava.tjbot.features.MessageReceiverAdapter;
1616

1717
import java.util.Optional;
@@ -20,73 +20,75 @@
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

Comments
 (0)