-
-
Notifications
You must be signed in to change notification settings - Fork 105
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
0a53bbc
5db7cff
d2c1b29
0946b54
a6efb2c
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 |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package org.togetherjava.tjbot.config; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.fasterxml.jackson.annotation.JsonRootName; | ||
| import org.apache.logging.log4j.LogManager; | ||
|
|
||
| import org.togetherjava.tjbot.features.basic.QuoteBoardForwarder; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Configuration for the quote board feature, see {@link QuoteBoardForwarder}. | ||
| */ | ||
| @JsonRootName("quoteBoardConfig") | ||
| public record QuoteBoardConfig( | ||
| @JsonProperty(value = "minimumReactionsToTrigger", required = true) int minimumReactions, | ||
| @JsonProperty(required = true) String channel, | ||
| @JsonProperty(value = "reactionEmoji", required = true) String reactionEmoji) { | ||
|
|
||
| /** | ||
| * Creates a QuoteBoardConfig. | ||
| * | ||
| * @param minimumReactions the minimum amount of reactions | ||
| * @param channel the pattern for the board channel | ||
| * @param reactionEmoji the emoji with which users should react to | ||
| */ | ||
| public QuoteBoardConfig { | ||
|
Member
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.
|
||
| if (minimumReactions <= 0) { | ||
| throw new IllegalArgumentException("minimumReactions must be greater than zero"); | ||
| } | ||
| Objects.requireNonNull(channel); | ||
| if (channel.isBlank()) { | ||
| throw new IllegalArgumentException("channel must not be empty or blank"); | ||
| } | ||
| Objects.requireNonNull(reactionEmoji); | ||
| if (reactionEmoji.isBlank()) { | ||
| throw new IllegalArgumentException("reactionEmoji must not be empty or blank"); | ||
| } | ||
| LogManager.getLogger(QuoteBoardConfig.class) | ||
| .debug("Quote-Board configs loaded: minimumReactions={}, channel='{}', reactionEmoji='{}'", | ||
| minimumReactions, channel, reactionEmoji); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| 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.List; | ||
| 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". | ||
| * <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; | ||
|
Member
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. As said below, i don't see a reason for this field |
||
| 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.getQuoteBoardConfig(); | ||
| this.triggerReaction = Emoji.fromUnicode(this.config.reactionEmoji()); | ||
|
|
||
| this.isQuoteBoardChannelName = Pattern.compile(this.config.channel()).asMatchPredicate(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onMessageReactionAdd(MessageReactionAddEvent event) { | ||
| logger.debug("Received MessageReactionAddEvent: messageId={}, channelId={}, userId={}", | ||
| event.getMessageId(), event.getChannel().getId(), event.getUserId()); | ||
|
|
||
| final MessageReaction messageReaction = event.getReaction(); | ||
|
|
||
| if (!messageReaction.getEmoji().equals(triggerReaction)) { | ||
| logger.debug("Reaction emoji '{}' does not match trigger emoji '{}'. Ignoring.", | ||
| messageReaction.getEmoji(), triggerReaction); | ||
| return; | ||
| } | ||
|
|
||
| if (hasAlreadyForwardedMessage(event.getJDA(), messageReaction)) { | ||
| logger.debug("Message has already been forwarded by the bot. Skipping."); | ||
| return; | ||
| } | ||
|
|
||
| long reactionCount = messageReaction.retrieveUsers().stream().count(); | ||
| if (reactionCount < config.minimumReactions()) { | ||
| logger.debug("Reaction count {} is less than minimum required {}. Skipping.", | ||
| reactionCount, config.minimumReactions()); | ||
| return; | ||
| } | ||
|
|
||
| final long guildId = event.getGuild().getIdLong(); | ||
|
|
||
| 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.channel(), guildId); | ||
| return; | ||
| } | ||
|
|
||
| logger.debug("Forwarding message to quote board channel: {}", boardChannel.get().getName()); | ||
|
|
||
| event.retrieveMessage() | ||
| .queue(message -> markAsProcessed(message) | ||
| .flatMap(v -> message.forwardTo(boardChannel.orElseThrow())) | ||
| .queue(_ -> logger.debug("Message forwarded to quote board channel: {}", | ||
| boardChannel.get().getName())), | ||
|
|
||
| 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) { | ||
|
Member
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.
|
||
| var guild = jda.getGuildById(guildId); | ||
|
|
||
| if (guild == null) { | ||
| throw new IllegalStateException( | ||
| String.format("Guild with ID '%d' not found.", guildId)); | ||
| } | ||
|
|
||
| List<TextChannel> matchingChannels = guild.getTextChannelCache() | ||
| .stream() | ||
| .filter(channel -> isQuoteBoardChannelName.test(channel.getName())) | ||
| .toList(); | ||
|
|
||
| if (matchingChannels.size() > 1) { | ||
| logger.warn( | ||
| "Multiple quote board channels found matching pattern '{}' in guild with ID '{}'. Selecting the first one anyway.", | ||
| this.config.channel(), guildId); | ||
| } | ||
|
|
||
| return matchingChannels.stream().findFirst(); | ||
| } | ||
|
|
||
| /** | ||
| * 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.
triggerReaction, or simplyreaction.:star:? I think it's cleaner