Skip to content

Commit 21c7f62

Browse files
authored
add pinned announcement listener (#938)
* listens for pinned announcement in helper forum * removes those announcements * resolves #921 * improved javadoc and method name * refactor class name
1 parent 3301ea5 commit 21c7f62

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

application/src/main/java/org/togetherjava/tjbot/features/Features.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
118118
features.add(new CodeMessageAutoDetection(config, codeMessageHandler));
119119
features.add(new CodeMessageManualDetection(codeMessageHandler));
120120
features.add(new SlashCommandEducator());
121+
features.add(new PinnedNotificationRemover(config));
121122

122123
// Event receivers
123124
features.add(new RejoinModerationRoleListener(actionsStore, config));
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.togetherjava.tjbot.features.help;
2+
3+
import net.dv8tion.jda.api.entities.Message;
4+
import net.dv8tion.jda.api.entities.MessageType;
5+
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
6+
import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion;
7+
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
8+
9+
import org.togetherjava.tjbot.config.Config;
10+
import org.togetherjava.tjbot.features.MessageReceiverAdapter;
11+
12+
import java.util.function.Predicate;
13+
import java.util.regex.Pattern;
14+
15+
/**
16+
* Removes notification-announcements about pinned messages from the helper forum.
17+
*/
18+
public final class PinnedNotificationRemover extends MessageReceiverAdapter {
19+
20+
private final Predicate<String> isHelpForumName;
21+
22+
/**
23+
* Creates a new listener to receive all pinned announcement messages sent in help channels.
24+
*
25+
* @param config the config to use for this
26+
*/
27+
public PinnedNotificationRemover(Config config) {
28+
isHelpForumName =
29+
Pattern.compile(config.getHelpSystem().getHelpForumPattern()).asMatchPredicate();
30+
}
31+
32+
@Override
33+
public void onMessageReceived(MessageReceivedEvent event) {
34+
handlePinnedAnnouncement(event);
35+
}
36+
37+
private boolean isHelpThread(MessageChannelUnion channel) {
38+
if (!channel.getType().isThread()) {
39+
return false;
40+
}
41+
ThreadChannel thread = channel.asThreadChannel();
42+
String rootChannelName = thread.getParentChannel().getName();
43+
return isHelpForumName.test(rootChannelName);
44+
}
45+
46+
private boolean isPinnedAnnouncement(Message message) {
47+
return message.getType() == MessageType.CHANNEL_PINNED_ADD;
48+
}
49+
50+
private void handlePinnedAnnouncement(MessageReceivedEvent event) {
51+
if (isPinnedAnnouncement(event.getMessage()) && isHelpThread(event.getChannel())) {
52+
event.getMessage().delete().queue();
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)