Skip to content

Commit

Permalink
reaction-emoji: read from configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
christolis committed Apr 4, 2024
1 parent b04c8f7 commit 6d86c8f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
@JsonRootName("coolMessagesConfig")
public record CoolMessagesBoardConfig(
@JsonProperty(value = "minimumReactions", required = true) int minimumReactions,
@JsonProperty(value = "boardChannelPattern", required = true) String boardChannelPattern) {
@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
public final class CoolMessagesBoardManager extends MessageReceiverAdapter {

private static final Logger logger = LoggerFactory.getLogger(CoolMessagesBoardManager.class);
private static final Emoji REACT_EMOJI = Emoji.fromUnicode("🌟");
private Emoji coolEmoji;
private final Predicate<String> boardChannelNamePredicate;
private final CoolMessagesBoardConfig config;

public CoolMessagesBoardManager(Config config) {
this.config = config.getCoolMessagesConfig();
this.coolEmoji = Emoji.fromUnicode(this.config.reactionEmoji());

boardChannelNamePredicate =
Pattern.compile(this.config.boardChannelPattern()).asMatchPredicate();
Expand All @@ -44,7 +45,7 @@ public CoolMessagesBoardManager(Config config) {
public void onMessageReactionAdd(MessageReactionAddEvent event) {
final MessageReaction messageReaction = event.getReaction();
int originalReactionsCount = messageReaction.hasCount() ? messageReaction.getCount() : 0;
boolean isCoolEmoji = messageReaction.getEmoji().getName().equals(REACT_EMOJI.getName());
boolean isCoolEmoji = messageReaction.getEmoji().equals(coolEmoji);
long guildId = event.getGuild().getIdLong();
Optional<TextChannel> boardChannel = getBoardChannel(event.getJDA(), guildId);

Expand All @@ -64,7 +65,7 @@ public void onMessageReactionAdd(MessageReactionAddEvent event) {
final int newReactionsCount = originalReactionsCount + 1;
if (isCoolEmoji && newReactionsCount >= config.minimumReactions()) {
event.retrieveMessage().queue(message -> {
message.addReaction(REACT_EMOJI).queue();
message.addReaction(coolEmoji).queue();

insertCoolMessage(boardChannel.get(), message);
}, e -> logger.warn("Tried to retrieve cool message but got: {}", e.getMessage()));
Expand Down Expand Up @@ -123,8 +124,8 @@ private static MessageEmbed createQuoteEmbed(Message message) {
/**
* Checks a {@link MessageReaction} to see if the bot has reacted to it.
*/
private static boolean hasBotReacted(JDA jda, MessageReaction messageReaction) {
if (!REACT_EMOJI.equals(messageReaction.getEmoji())) {
private boolean hasBotReacted(JDA jda, MessageReaction messageReaction) {
if (!coolEmoji.equals(messageReaction.getEmoji())) {
return false;
}

Expand Down

0 comments on commit 6d86c8f

Please sign in to comment.