-
-
Notifications
You must be signed in to change notification settings - Fork 89
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
Open
christolis
wants to merge
3
commits into
Together-Java:develop
Choose a base branch
from
christolis:feature/cool-messages
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Implement Quotes Board #1029
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
application/src/main/java/org/togetherjava/tjbot/config/CoolMessagesBoardConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
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 org.togetherjava.tjbot.features.basic.CoolMessagesBoardManager}. | ||
*/ | ||
@JsonRootName("coolMessagesConfig") | ||
public record CoolMessagesBoardConfig( | ||
@JsonProperty(value = "minimumReactions", required = true) int minimumReactions, | ||
@JsonProperty(value = "boardChannelPattern", required = true) String boardChannelPattern, | ||
@JsonProperty(value = "reactionEmoji", required = true) String reactionEmoji) { | ||
|
||
/** | ||
* Creates a CoolMessagesBoardConfig. | ||
* | ||
* @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 CoolMessagesBoardConfig { | ||
Objects.requireNonNull(boardChannelPattern); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...ication/src/main/java/org/togetherjava/tjbot/features/basic/CoolMessagesBoardManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
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.MessageCreateAction; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.togetherjava.tjbot.config.Config; | ||
import org.togetherjava.tjbot.config.CoolMessagesBoardConfig; | ||
import org.togetherjava.tjbot.features.MessageReceiverAdapter; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Manager for the cool messages board. It appends highly-voted text messages to a separate channel | ||
* where members of the guild can see a list of all of them. | ||
*/ | ||
public final class CoolMessagesBoardManager extends MessageReceiverAdapter { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(CoolMessagesBoardManager.class); | ||
private final Emoji coolEmoji; | ||
private final Predicate<String> boardChannelNamePredicate; | ||
private final CoolMessagesBoardConfig config; | ||
|
||
/** | ||
* Constructs a new instance of CoolMessagesBoardManager. | ||
* | ||
* @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 CoolMessagesBoardManager(Config config) { | ||
this.config = config.getCoolMessagesConfig(); | ||
this.coolEmoji = Emoji.fromUnicode(this.config.reactionEmoji()); | ||
|
||
boardChannelNamePredicate = | ||
Pattern.compile(this.config.boardChannelPattern()).asMatchPredicate(); | ||
} | ||
|
||
@Override | ||
public void onMessageReactionAdd(MessageReactionAddEvent event) { | ||
final MessageReaction messageReaction = event.getReaction(); | ||
int originalReactionsCount = messageReaction.hasCount() ? messageReaction.getCount() : 0; | ||
boolean isCoolEmoji = messageReaction.getEmoji().equals(coolEmoji); | ||
long guildId = event.getGuild().getIdLong(); | ||
Optional<TextChannel> boardChannel = getBoardChannel(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; | ||
} | ||
|
||
// If the bot has already reacted to this message, then this means that | ||
// the message has been quoted to the cool messages board, so skip it. | ||
if (hasBotReacted(event.getJDA(), messageReaction)) { | ||
return; | ||
} | ||
|
||
final int newReactionsCount = originalReactionsCount + 1; | ||
if (isCoolEmoji && newReactionsCount >= config.minimumReactions()) { | ||
event.retrieveMessage() | ||
.queue(message -> message.addReaction(coolEmoji) | ||
.flatMap(v -> insertCoolMessage(boardChannel.get(), message)) | ||
.queue(), | ||
e -> logger.warn("Tried to retrieve cool message but got: {}", | ||
e.getMessage())); | ||
} | ||
} | ||
|
||
/** | ||
* 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> getBoardChannel(JDA jda, long guildId) { | ||
return jda.getGuildById(guildId) | ||
.getTextChannelCache() | ||
.stream() | ||
.filter(channel -> boardChannelNamePredicate.test(channel.getName())) | ||
.findAny(); | ||
} | ||
|
||
/** | ||
* Inserts a message to the specified text channel | ||
* | ||
* @return a {@link MessageCreateAction} of the call to make | ||
*/ | ||
private static MessageCreateAction insertCoolMessage(TextChannel boardChannel, | ||
Message message) { | ||
return message.forwardTo(boardChannel); | ||
} | ||
|
||
/** | ||
* Checks a {@link MessageReaction} to see if the bot has reacted to it. | ||
*/ | ||
private boolean hasBotReacted(JDA jda, MessageReaction messageReaction) { | ||
if (!coolEmoji.equals(messageReaction.getEmoji())) { | ||
return false; | ||
} | ||
|
||
return messageReaction.retrieveUsers() | ||
.parallelStream() | ||
.anyMatch(user -> jda.getSelfUser().getIdLong() == user.getIdLong()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.