Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public final class Config {
private final RSSFeedsConfig rssFeedsConfig;
private final String selectRolesChannelPattern;
private final String memberCountCategoryPattern;
private final QuoteBoardConfig coolMessagesConfig;
private final QuoteBoardConfig quoteMessagesConfig;

@SuppressWarnings("ConstructorWithTooManyParameters")
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
Expand Down Expand Up @@ -102,8 +102,8 @@ private Config(@JsonProperty(value = "token", required = true) String token,
@JsonProperty(value = "rssConfig", required = true) RSSFeedsConfig rssFeedsConfig,
@JsonProperty(value = "selectRolesChannelPattern",
required = true) String selectRolesChannelPattern,
@JsonProperty(value = "coolMessagesConfig",
required = true) QuoteBoardConfig coolMessagesConfig) {
@JsonProperty(value = "quoteMessagesConfig",
required = true) QuoteBoardConfig quoteMessagesConfig) {
this.token = Objects.requireNonNull(token);
this.githubApiKey = Objects.requireNonNull(githubApiKey);
this.databasePath = Objects.requireNonNull(databasePath);
Expand Down Expand Up @@ -138,7 +138,7 @@ private Config(@JsonProperty(value = "token", required = true) String token,
this.featureBlacklistConfig = Objects.requireNonNull(featureBlacklistConfig);
this.rssFeedsConfig = Objects.requireNonNull(rssFeedsConfig);
this.selectRolesChannelPattern = Objects.requireNonNull(selectRolesChannelPattern);
this.coolMessagesConfig = Objects.requireNonNull(coolMessagesConfig);
this.quoteMessagesConfig = Objects.requireNonNull(quoteMessagesConfig);
}

/**
Expand Down Expand Up @@ -433,12 +433,13 @@ public String getSelectRolesChannelPattern() {
}

/**
* The configuration of the cool messages config.
* The configuration of the cool messages config. The configuration of the quote board feature.
* Quotes user selected messages
*
* @return configuration of cool messages config
*/
public QuoteBoardConfig getCoolMessagesConfig() {
return coolMessagesConfig;
return quoteMessagesConfig;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;

import org.togetherjava.tjbot.features.basic.QuoteBoardForwarder;

import java.util.Objects;

/**
* Configuration for the cool messages board feature, see {@link ``QuoteBoardForwarder``}.
* Configuration for the quote board feature, see {@link QuoteBoardForwarder}.
*/
@JsonRootName("coolMessagesConfig")
public record QuoteBoardConfig(
Expand All @@ -23,5 +25,6 @@ public record QuoteBoardConfig(
*/
public QuoteBoardConfig {
Objects.requireNonNull(boardChannelPattern);
Objects.requireNonNull(reactionEmoji);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.regex.Pattern;

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

if (!isTriggerEmoji) {
return;
}

if (hasAlreadyForwardedMessage(event.getJDA(), messageReaction)) {
return;
}

final int reactionsCount = (int) messageReaction.retrieveUsers().stream().count();
if (isCoolEmoji && reactionsCount >= config.minimumReactions()) {
Optional<TextChannel> boardChannel = findQuoteBoardChannel(event.getJDA(), guildId);

if (boardChannel.isEmpty()) {
logger.warn(
"Could not find board channel with pattern '{}' in server with ID '{}'. Skipping reaction handling...",
this.config.boardChannelPattern(), guildId);
return;
}

event.retrieveMessage()
.queue(message -> markAsProcessed(message).flatMap(v -> message
.forwardTo(boardChannel.orElseThrow())).queue(), e -> logger.warn(
"Unknown error while attempting to retrieve and forward message for quote-board, message is ignored.",
e));

if (reactionsCount < config.minimumReactions()) {
return;
}

Optional<TextChannel> boardChannel = findQuoteBoardChannel(event.getJDA(), guildId);

if (boardChannel.isEmpty()) {
logger.warn(
"Could not find board channel with pattern '{}' in server with ID '{}'. Skipping reaction handling...",
this.config.boardChannelPattern(), guildId);
return;
}

event.retrieveMessage()
.queue(message -> markAsProcessed(message).flatMap(v -> message
.forwardTo(boardChannel.orElseThrow())).queue(), e -> logger.warn(
"Unknown error while attempting to retrieve and forward message for quote-board, message is ignored.",
e));

}

private RestAction<Void> markAsProcessed(Message message) {
Expand Down
Loading