|
| 1 | +package net.javadiscord.javabot.listener; |
| 2 | + |
| 3 | +import net.dv8tion.jda.api.entities.*; |
| 4 | +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; |
| 5 | +import net.dv8tion.jda.api.events.message.react.GenericMessageReactionEvent; |
| 6 | +import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; |
| 7 | +import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent; |
| 8 | +import net.dv8tion.jda.api.hooks.ListenerAdapter; |
| 9 | +import net.javadiscord.javabot.Bot; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | + |
| 12 | +/** |
| 13 | + * Generic listener that can be extended to add the ability for users to vote |
| 14 | + * on whether a message should stay in the channel. |
| 15 | + */ |
| 16 | +public abstract class MessageVoteListener extends ListenerAdapter { |
| 17 | + /** |
| 18 | + * Gets the text channel in which this vote listener operates. |
| 19 | + * |
| 20 | + * @param guild The guild to get the channel for. |
| 21 | + * @return The text channel that this vote listener should listen in. |
| 22 | + */ |
| 23 | + protected abstract TextChannel getChannel(Guild guild); |
| 24 | + |
| 25 | + /** |
| 26 | + * Gets the threshold needed to remove a message. If a message has <code>U</code> |
| 27 | + * upvotes and <code>D</code> downvotes, we compute the difference as |
| 28 | + * <code>D - U</code> to get a number that indicates how many more |
| 29 | + * downvotes there are than upvotes. If this value is higher than or equal |
| 30 | + * to the threshold value returned by this method, the message is deleted. |
| 31 | + * <p> |
| 32 | + * Note that usually, you want to return a positive value, to indicate |
| 33 | + * that the message should have <em>more</em> downvotes than upvotes. |
| 34 | + * </p> |
| 35 | + * |
| 36 | + * @param guild The guild to get the threshold for. |
| 37 | + * @return The delete threshold value. |
| 38 | + */ |
| 39 | + protected int getMessageDeleteVoteThreshold(Guild guild) { |
| 40 | + return 5; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Determines if a given message is eligible for voting. Only eligible |
| 45 | + * messages will have voting reactions applied. |
| 46 | + * |
| 47 | + * @param message The message to check. |
| 48 | + * @return True if the message is eligible for voting, or false if not. |
| 49 | + */ |
| 50 | + protected boolean isMessageEligibleForVoting(Message message) { |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Gets the emote that's used for casting upvotes. |
| 56 | + * |
| 57 | + * @param guild The guild to get the emote for. |
| 58 | + * @return The emote. |
| 59 | + */ |
| 60 | + protected Emote getUpvoteEmote(Guild guild) { |
| 61 | + return Bot.config.get(guild).getEmote().getUpvoteEmote(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Gets the emote that's used for casting downvotes. |
| 66 | + * |
| 67 | + * @param guild The guild to get the emote for. |
| 68 | + * @return The emote. |
| 69 | + */ |
| 70 | + protected Emote getDownvoteEmote(Guild guild) { |
| 71 | + return Bot.config.get(guild).getEmote().getDownvoteEmote(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Whether the bot should add the first upvote and downvote emotes to |
| 76 | + * messages that are eligible for voting. |
| 77 | + * |
| 78 | + * @param guild The guild to get this setting for. |
| 79 | + * @return True if the bot should add emotes. |
| 80 | + */ |
| 81 | + protected boolean shouldAddInitialEmotes(Guild guild) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void onMessageReceived(@NotNull MessageReceivedEvent event) { |
| 87 | + if (isMessageReceivedEventValid(event) && shouldAddInitialEmotes(event.getGuild())) { |
| 88 | + event.getMessage().addReaction(getUpvoteEmote(event.getGuild())).queue(); |
| 89 | + event.getMessage().addReaction(getDownvoteEmote(event.getGuild())).queue(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event) { |
| 95 | + Bot.asyncPool.submit(() -> handleReactionEvent(event)); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void onMessageReactionRemove(@NotNull MessageReactionRemoveEvent event) { |
| 100 | + Bot.asyncPool.submit(() -> handleReactionEvent(event)); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Checks if a message received event is valid for this vote listener. |
| 105 | + * |
| 106 | + * @param event The event to check. |
| 107 | + * @return True if the event is valid, meaning that it is relevant for this |
| 108 | + * vote listener to add the voting emotes to it. |
| 109 | + */ |
| 110 | + private boolean isMessageReceivedEventValid(MessageReceivedEvent event) { |
| 111 | + if (event.getAuthor().isBot() || event.getAuthor().isSystem() || event.getMessage().getType() == MessageType.THREAD_CREATED) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + return event.getChannel().getId().equals(getChannel(event.getGuild()).getId()) && |
| 115 | + isMessageEligibleForVoting(event.getMessage()); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Checks if a reaction event is valid for this vote listener. Note that |
| 120 | + * this method may use blocking calls to check if the user who sent the |
| 121 | + * reaction is valid. |
| 122 | + * |
| 123 | + * @param event The event to check. |
| 124 | + * @return True if the event is valid, meaning that this listener should |
| 125 | + * proceed to check the votes on the message. |
| 126 | + */ |
| 127 | + private boolean isReactionEventValid(GenericMessageReactionEvent event) { |
| 128 | + if (!event.getChannel().getId().equals(getChannel(event.getGuild()).getId())) return false; |
| 129 | + String reactionId = event.getReactionEmote().getId(); |
| 130 | + if ( |
| 131 | + !reactionId.equals(getUpvoteEmote(event.getGuild()).getId()) && |
| 132 | + !reactionId.equals(getDownvoteEmote(event.getGuild()).getId()) |
| 133 | + ) { |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + User user = event.retrieveUser().complete(); |
| 138 | + return !user.isBot() && !user.isSystem(); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Handles voting reaction events, including both the addition and removal |
| 143 | + * of votes. Note that this is a blocking method. |
| 144 | + * |
| 145 | + * @param event The reaction event to handle. |
| 146 | + */ |
| 147 | + private void handleReactionEvent(GenericMessageReactionEvent event) { |
| 148 | + if (isReactionEventValid(event)) { |
| 149 | + Message message = event.retrieveMessage().complete(); |
| 150 | + if (isMessageEligibleForVoting(message)) { |
| 151 | + checkVotes(message, event.getGuild()); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private void checkVotes(Message msg, Guild guild) { |
| 157 | + String upvoteId = getUpvoteEmote(guild).getId(); |
| 158 | + String downvoteId = getDownvoteEmote(guild).getId(); |
| 159 | + |
| 160 | + int upvotes = countReactions(msg, upvoteId); |
| 161 | + int downvotes = countReactions(msg, downvoteId); |
| 162 | + int downvoteDifference = downvotes - upvotes; |
| 163 | + |
| 164 | + if (downvoteDifference >= getMessageDeleteVoteThreshold(guild)) { |
| 165 | + msg.delete().queue(); |
| 166 | + msg.getAuthor().openPrivateChannel() |
| 167 | + .queue( |
| 168 | + s -> s.sendMessageFormat( |
| 169 | + "Your message in %s has been removed due to community feedback.", |
| 170 | + getChannel(guild).getAsMention() |
| 171 | + ).queue(), |
| 172 | + e -> {} |
| 173 | + ); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private int countReactions(Message msg, String id) { |
| 178 | + MessageReaction reaction = msg.getReactionById(id); |
| 179 | + if (reaction == null) return 0; |
| 180 | + return (int) reaction.retrieveUsers().stream() |
| 181 | + .filter(user -> !user.isBot() && !user.isSystem()) |
| 182 | + .count(); |
| 183 | + } |
| 184 | +} |
0 commit comments