22
33import net .dv8tion .jda .api .JDA ;
44import net .dv8tion .jda .api .entities .*;
5- import net .dv8tion .jda .api .entities .channel .concrete .TextChannel ;
5+ import net .dv8tion .jda .api .entities .channel .concrete .ForumChannel ;
66import net .dv8tion .jda .api .entities .emoji .Emoji ;
77import net .dv8tion .jda .api .events .message .MessageReceivedEvent ;
88import net .dv8tion .jda .api .events .message .react .GenericMessageReactionEvent ;
2323 * on whether a message should stay in the channel.
2424 */
2525@ RequiredArgsConstructor
26- public abstract class MessageVoteListener extends ListenerAdapter {
26+ public abstract class ForumPostVoteListener extends ListenerAdapter {
2727 /**
2828 * The main configuration of the bot.
2929 */
@@ -36,7 +36,7 @@ public abstract class MessageVoteListener extends ListenerAdapter {
3636 * @param guild The guild to get the channel for.
3737 * @return The text channel that this vote listener should listen in.
3838 */
39- protected abstract TextChannel getChannel (Guild guild );
39+ protected abstract ForumChannel getChannel (Guild guild );
4040
4141 /**
4242 * Gets the threshold needed to remove a message. If a message has <code>U</code>
@@ -56,17 +56,6 @@ protected int getMessageDeleteVoteThreshold(Guild guild) {
5656 return 5 ;
5757 }
5858
59- /**
60- * Determines if a given message is eligible for voting. Only eligible
61- * messages will have voting reactions applied.
62- *
63- * @param message The message to check.
64- * @return True if the message is eligible for voting, or false if not.
65- */
66- protected boolean isMessageEligibleForVoting (Message message ) {
67- return true ;
68- }
69-
7059 /**
7160 * Gets the emote that's used for casting upvotes.
7261 *
@@ -127,9 +116,10 @@ private boolean isMessageReceivedEventValid(@NotNull MessageReceivedEvent event)
127116 if (event .getAuthor ().isBot () || event .getAuthor ().isSystem () || event .getMessage ().getType () == MessageType .THREAD_CREATED ) {
128117 return false ;
129118 }
119+ if (!event .isFromThread ()) return false ;
120+ if (event .getMessageIdLong () != event .getChannel ().getIdLong ()) return false ;
130121 if (getChannel (event .getGuild ()) == null ) return false ;
131- return event .getChannel ().getId ().equals (getChannel (event .getGuild ()).getId ()) &&
132- isMessageEligibleForVoting (event .getMessage ());
122+ return event .getChannel ().asThreadChannel ().getParentChannel ().getId ().equals (getChannel (event .getGuild ()).getId ());
133123 }
134124
135125 /**
@@ -142,7 +132,9 @@ private boolean isMessageReceivedEventValid(@NotNull MessageReceivedEvent event)
142132 * proceed to check the votes on the message.
143133 */
144134 private boolean isReactionEventValid (@ NotNull GenericMessageReactionEvent event ) {
145- if (!event .getChannel ().getId ().equals (getChannel (event .getGuild ()).getId ())) return false ;
135+ if (!event .isFromThread ()) return false ;
136+ if (!event .getChannel ().asThreadChannel ().getParentChannel ().getId ().equals (getChannel (event .getGuild ()).getId ())) return false ;
137+ if (event .getMessageIdLong () != event .getChannel ().getIdLong ()) return false ;
146138 Emoji reaction = event .getEmoji ();
147139 if (
148140 !reaction .equals (getUpvoteEmote (event .getJDA ())) &&
@@ -151,7 +143,10 @@ private boolean isReactionEventValid(@NotNull GenericMessageReactionEvent event)
151143 return false ;
152144 }
153145
154- User user = event .retrieveUser ().complete ();
146+ User user = event .getUser ();
147+ if (user == null ) {
148+ user = event .retrieveUser ().complete ();
149+ }
155150 return !user .isBot () && !user .isSystem ();
156151 }
157152
@@ -164,9 +159,7 @@ private boolean isReactionEventValid(@NotNull GenericMessageReactionEvent event)
164159 private void handleReactionEvent (GenericMessageReactionEvent event ) {
165160 if (isReactionEventValid (event )) {
166161 Message message = event .retrieveMessage ().complete ();
167- if (isMessageEligibleForVoting (message )) {
168- checkVotes (message , event .getGuild ());
169- }
162+ checkVotes (message , event .getGuild ());
170163 }
171164 }
172165
@@ -179,10 +172,10 @@ private void checkVotes(Message msg, @NotNull Guild guild) {
179172 int downvoteDifference = downvotes - upvotes ;
180173
181174 if (downvoteDifference >= getMessageDeleteVoteThreshold (guild )) {
182- msg .delete ( ).queue ();
175+ msg .getChannel (). asThreadChannel (). getManager (). setArchived ( true ). setLocked ( true ).queue ();
183176 msg .getAuthor ().openPrivateChannel ()
184177 .queue (
185- s -> s .sendMessageFormat ("Your message in %s has been removed due to community feedback." , getChannel (guild ).getAsMention ()).queue (),
178+ s -> s .sendMessageFormat ("Your post %s in %s has been closed due to community feedback." , msg . getChannel (). getAsMention () , getChannel (guild ).getAsMention ()).queue (),
186179 e -> {}
187180 );
188181 }
0 commit comments