-
-
Notifications
You must be signed in to change notification settings - Fork 91
Implement Quotes Board #1029
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Implement Quotes Board #1029
Changes from all commits
e1dcd79
26e7811
fb4fb5d
1ade409
a6085db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ public final class Config { | |
private final RSSFeedsConfig rssFeedsConfig; | ||
private final String selectRolesChannelPattern; | ||
private final String memberCountCategoryPattern; | ||
private final QuoteBoardConfig coolMessagesConfig; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename here as well |
||
|
||
@SuppressWarnings("ConstructorWithTooManyParameters") | ||
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES) | ||
|
@@ -100,7 +101,9 @@ private Config(@JsonProperty(value = "token", required = true) String token, | |
required = true) FeatureBlacklistConfig featureBlacklistConfig, | ||
@JsonProperty(value = "rssConfig", required = true) RSSFeedsConfig rssFeedsConfig, | ||
@JsonProperty(value = "selectRolesChannelPattern", | ||
required = true) String selectRolesChannelPattern) { | ||
required = true) String selectRolesChannelPattern, | ||
@JsonProperty(value = "coolMessagesConfig", | ||
required = true) QuoteBoardConfig coolMessagesConfig) { | ||
Comment on lines
+104
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename here as well |
||
this.token = Objects.requireNonNull(token); | ||
this.githubApiKey = Objects.requireNonNull(githubApiKey); | ||
this.databasePath = Objects.requireNonNull(databasePath); | ||
|
@@ -135,6 +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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename here as well |
||
} | ||
|
||
/** | ||
|
@@ -428,6 +432,15 @@ public String getSelectRolesChannelPattern() { | |
return selectRolesChannelPattern; | ||
} | ||
|
||
/** | ||
* The configuration of the cool messages config. | ||
* | ||
* @return configuration of cool messages config | ||
*/ | ||
public QuoteBoardConfig getCoolMessagesConfig() { | ||
return coolMessagesConfig; | ||
} | ||
Comment on lines
+435
to
+442
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename here as well id also appreciate if we add a second sentence to the javadoc here to summarize the feature quickly. perhaps:
|
||
|
||
/** | ||
* Gets the pattern matching the category that is used to display the total member count. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.togetherjava.tjbot.config; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonRootName; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Configuration for the cool messages board feature, see {@link ``QuoteBoardForwarder``}. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably also add that extra sentence here:
That link looks a bit weird, are those backticks necessary? |
||
*/ | ||
@JsonRootName("coolMessagesConfig") | ||
public record QuoteBoardConfig( | ||
@JsonProperty(value = "minimumReactions", required = true) int minimumReactions, | ||
@JsonProperty(value = "boardChannelPattern", required = true) String boardChannelPattern, | ||
@JsonProperty(value = "reactionEmoji", required = true) String reactionEmoji) { | ||
|
||
/** | ||
* Creates a QuoteBoardConfig. | ||
* | ||
* @param minimumReactions the minimum amount of reactions | ||
* @param boardChannelPattern the pattern for the board channel | ||
* @param reactionEmoji the emoji with which users should react to | ||
*/ | ||
public QuoteBoardConfig { | ||
Objects.requireNonNull(boardChannelPattern); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is that possibly missing checks on the other params as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like the checks for But there is a possibility that there might not be a channel on the server with the name contained by the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. they are all (rightfully) marked as required though. so we need to check they are actually set to a non null value There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds fair There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aye. but the other one is a string 👍 |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package org.togetherjava.tjbot.features.basic; | ||
|
||
import net.dv8tion.jda.api.JDA; | ||
import net.dv8tion.jda.api.entities.Message; | ||
import net.dv8tion.jda.api.entities.MessageReaction; | ||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; | ||
import net.dv8tion.jda.api.entities.emoji.Emoji; | ||
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; | ||
import net.dv8tion.jda.api.requests.RestAction; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.togetherjava.tjbot.config.Config; | ||
import org.togetherjava.tjbot.config.QuoteBoardConfig; | ||
import org.togetherjava.tjbot.features.MessageReceiverAdapter; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Listens for reaction-add events and turns popular messages into “quotes”. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no smart-quotes please. just |
||
* <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 | ||
* not processed the message before, it copies (forwards) the message to the first text channel | ||
* whose name matches the configured quote-board pattern, then reacts to the original message itself | ||
* to mark it as handled (and to not let people spam react a message and give a way to the bot to | ||
* know that a message has been quoted before). | ||
* <p> | ||
* Key points: - Trigger emoji, minimum vote count and quote-board channel pattern are supplied via | ||
* {@code QuoteBoardConfig}. | ||
*/ | ||
public final class QuoteBoardForwarder extends MessageReceiverAdapter { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(QuoteBoardForwarder.class); | ||
private final Emoji triggerReaction; | ||
private final Predicate<String> isQuoteBoardChannelName; | ||
private final QuoteBoardConfig config; | ||
|
||
/** | ||
* Constructs a new instance of QuoteBoardForwarder. | ||
* | ||
* @param config the configuration containing settings specific to the cool messages board, | ||
* including the reaction emoji and the pattern to match board channel names | ||
*/ | ||
public QuoteBoardForwarder(Config config) { | ||
this.config = config.getCoolMessagesConfig(); | ||
this.triggerReaction = Emoji.fromUnicode(this.config.reactionEmoji()); | ||
|
||
isQuoteBoardChannelName = | ||
Pattern.compile(this.config.boardChannelPattern()).asMatchPredicate(); | ||
} | ||
|
||
@Override | ||
public void onMessageReactionAdd(MessageReactionAddEvent event) { | ||
final MessageReaction messageReaction = event.getReaction(); | ||
boolean isCoolEmoji = messageReaction.getEmoji().equals(triggerReaction); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. id rename that to sth like |
||
long guildId = event.getGuild().getIdLong(); | ||
|
||
if (hasAlreadyForwardedMessage(event.getJDA(), messageReaction)) { | ||
return; | ||
} | ||
|
||
final int reactionsCount = (int) messageReaction.retrieveUsers().stream().count(); | ||
if (isCoolEmoji && reactionsCount >= config.minimumReactions()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. personally id favor early-return to reduce the nesting here.
Comment on lines
+58
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is no need to do the |
||
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) { | ||
return message.addReaction(triggerReaction); | ||
} | ||
|
||
/** | ||
* Gets the board text channel where the quotes go to, wrapped in an optional. | ||
* | ||
* @param jda the JDA | ||
* @param guildId the guild ID | ||
* @return the board text channel | ||
*/ | ||
private Optional<TextChannel> findQuoteBoardChannel(JDA jda, long guildId) { | ||
return jda.getGuildById(guildId) | ||
.getTextChannelCache() | ||
.stream() | ||
.filter(channel -> isQuoteBoardChannelName.test(channel.getName())) | ||
.findAny(); | ||
} | ||
|
||
/** | ||
* Inserts a message to the specified text channel | ||
* | ||
* @return a {@link MessageCreateAction} of the call to make | ||
*/ | ||
|
||
/** | ||
* Checks a {@link MessageReaction} to see if the bot has reacted to it. | ||
*/ | ||
private boolean hasAlreadyForwardedMessage(JDA jda, MessageReaction messageReaction) { | ||
if (!triggerReaction.equals(messageReaction.getEmoji())) { | ||
return false; | ||
} | ||
|
||
return messageReaction.retrieveUsers() | ||
.parallelStream() | ||
.anyMatch(user -> jda.getSelfUser().getIdLong() == user.getIdLong()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please rename it here as well, thanks