Skip to content

Commit 0a53bbc

Browse files
suryatejesschristolis
authored andcommitted
refactor: code review addressed by zabuzard
* rename coolMessagesConfig to quoteMessagesConfig * removed backticks for QuoteBoardForwarder and added a qualifier statement for QuoteBoardForwarder * param check for reactionEmoji * straight quotes instead of smart quotes * rename isCoolEmoji to isTriggerEmoji * early return when reactionsCount < config.minimumReactions() * early return for isCoolEmoji
1 parent a6085db commit 0a53bbc

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

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

Lines changed: 7 additions & 6 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 QuoteBoardConfig coolMessagesConfig;
51+
private final QuoteBoardConfig quoteMessagesConfig;
5252

5353
@SuppressWarnings("ConstructorWithTooManyParameters")
5454
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
@@ -102,8 +102,8 @@ private Config(@JsonProperty(value = "token", required = true) String token,
102102
@JsonProperty(value = "rssConfig", required = true) RSSFeedsConfig rssFeedsConfig,
103103
@JsonProperty(value = "selectRolesChannelPattern",
104104
required = true) String selectRolesChannelPattern,
105-
@JsonProperty(value = "coolMessagesConfig",
106-
required = true) QuoteBoardConfig coolMessagesConfig) {
105+
@JsonProperty(value = "quoteMessagesConfig",
106+
required = true) QuoteBoardConfig quoteMessagesConfig) {
107107
this.token = Objects.requireNonNull(token);
108108
this.githubApiKey = Objects.requireNonNull(githubApiKey);
109109
this.databasePath = Objects.requireNonNull(databasePath);
@@ -138,7 +138,7 @@ private Config(@JsonProperty(value = "token", required = true) String token,
138138
this.featureBlacklistConfig = Objects.requireNonNull(featureBlacklistConfig);
139139
this.rssFeedsConfig = Objects.requireNonNull(rssFeedsConfig);
140140
this.selectRolesChannelPattern = Objects.requireNonNull(selectRolesChannelPattern);
141-
this.coolMessagesConfig = Objects.requireNonNull(coolMessagesConfig);
141+
this.quoteMessagesConfig = Objects.requireNonNull(quoteMessagesConfig);
142142
}
143143

144144
/**
@@ -433,12 +433,13 @@ public String getSelectRolesChannelPattern() {
433433
}
434434

435435
/**
436-
* The configuration of the cool messages config.
436+
* The configuration of the cool messages config. The configuration of the quote board feature.
437+
* Quotes user selected messages
437438
*
438439
* @return configuration of cool messages config
439440
*/
440441
public QuoteBoardConfig getCoolMessagesConfig() {
441-
return coolMessagesConfig;
442+
return quoteMessagesConfig;
442443
}
443444

444445
/**

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import com.fasterxml.jackson.annotation.JsonProperty;
44
import com.fasterxml.jackson.annotation.JsonRootName;
55

6+
import org.togetherjava.tjbot.features.basic.QuoteBoardForwarder;
7+
68
import java.util.Objects;
79

810
/**
9-
* Configuration for the cool messages board feature, see {@link ``QuoteBoardForwarder``}.
11+
* Configuration for the quote board feature, see {@link QuoteBoardForwarder}.
1012
*/
1113
@JsonRootName("coolMessagesConfig")
1214
public record QuoteBoardConfig(
@@ -23,5 +25,6 @@ public record QuoteBoardConfig(
2325
*/
2426
public QuoteBoardConfig {
2527
Objects.requireNonNull(boardChannelPattern);
28+
Objects.requireNonNull(reactionEmoji);
2629
}
2730
}

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.regex.Pattern;
2020

2121
/**
22-
* Listens for reaction-add events and turns popular messages into quotes.
22+
* Listens for reaction-add events and turns popular messages into "quotes".
2323
* <p>
2424
* When someone reacts to a message with the configured emoji, the listener counts how many users
2525
* have used that same emoji. If the total meets or exceeds the minimum threshold and the bot has
@@ -55,30 +55,38 @@ public QuoteBoardForwarder(Config config) {
5555
@Override
5656
public void onMessageReactionAdd(MessageReactionAddEvent event) {
5757
final MessageReaction messageReaction = event.getReaction();
58-
boolean isCoolEmoji = messageReaction.getEmoji().equals(triggerReaction);
58+
boolean isTriggerEmoji = messageReaction.getEmoji().equals(triggerReaction);
5959
long guildId = event.getGuild().getIdLong();
6060

61+
if (!isTriggerEmoji) {
62+
return;
63+
}
64+
6165
if (hasAlreadyForwardedMessage(event.getJDA(), messageReaction)) {
6266
return;
6367
}
6468

6569
final int reactionsCount = (int) messageReaction.retrieveUsers().stream().count();
66-
if (isCoolEmoji && reactionsCount >= config.minimumReactions()) {
67-
Optional<TextChannel> boardChannel = findQuoteBoardChannel(event.getJDA(), guildId);
68-
69-
if (boardChannel.isEmpty()) {
70-
logger.warn(
71-
"Could not find board channel with pattern '{}' in server with ID '{}'. Skipping reaction handling...",
72-
this.config.boardChannelPattern(), guildId);
73-
return;
74-
}
75-
76-
event.retrieveMessage()
77-
.queue(message -> markAsProcessed(message).flatMap(v -> message
78-
.forwardTo(boardChannel.orElseThrow())).queue(), e -> logger.warn(
79-
"Unknown error while attempting to retrieve and forward message for quote-board, message is ignored.",
80-
e));
70+
71+
if (reactionsCount < config.minimumReactions()) {
72+
return;
8173
}
74+
75+
Optional<TextChannel> boardChannel = findQuoteBoardChannel(event.getJDA(), guildId);
76+
77+
if (boardChannel.isEmpty()) {
78+
logger.warn(
79+
"Could not find board channel with pattern '{}' in server with ID '{}'. Skipping reaction handling...",
80+
this.config.boardChannelPattern(), guildId);
81+
return;
82+
}
83+
84+
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));
89+
8290
}
8391

8492
private RestAction<Void> markAsProcessed(Message message) {

0 commit comments

Comments
 (0)