|
| 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