-
-
Notifications
You must be signed in to change notification settings - Fork 104
add pinned announcement listener #938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ankitsmt211
merged 3 commits into
Together-Java:develop
from
ankitsmt211:improved-delete-pinned
Nov 3, 2023
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...cation/src/main/java/org/togetherjava/tjbot/features/help/PinnedAnnouncementListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package org.togetherjava.tjbot.features.help; | ||
|
|
||
| import net.dv8tion.jda.api.entities.Message; | ||
| import net.dv8tion.jda.api.entities.MessageType; | ||
| import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; | ||
| import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion; | ||
| import net.dv8tion.jda.api.events.message.MessageReceivedEvent; | ||
|
|
||
| import org.togetherjava.tjbot.config.Config; | ||
| import org.togetherjava.tjbot.features.MessageReceiverAdapter; | ||
|
|
||
| import java.util.function.Predicate; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| /** | ||
| * Listener that receives all pinned announcement messages from helper forum so that it can be | ||
| * removed | ||
| */ | ||
| public final class PinnedAnnouncementListener extends MessageReceiverAdapter { | ||
ankitsmt211 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private final Predicate<String> isHelpForumName; | ||
|
|
||
| /** | ||
| * Creates a new listener to receive all pinned announcement messages sent in help channels. | ||
| * | ||
| * @param config the config to use for this | ||
| */ | ||
| public PinnedAnnouncementListener(Config config) { | ||
| isHelpForumName = | ||
| Pattern.compile(config.getHelpSystem().getHelpForumPattern()).asMatchPredicate(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onMessageReceived(MessageReceivedEvent event) { | ||
| removePinnedAnnouncement(event); | ||
| } | ||
ankitsmt211 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private boolean isHelpThread(MessageChannelUnion channel) { | ||
| if (!channel.getType().isThread()) { | ||
| return false; | ||
| } | ||
| ThreadChannel thread = channel.asThreadChannel(); | ||
| String rootChannelName = thread.getParentChannel().getName(); | ||
| return isHelpForumName.test(rootChannelName); | ||
| } | ||
|
|
||
| private boolean isPinnedAnnouncement(Message message) { | ||
| return message.getType() == MessageType.CHANNEL_PINNED_ADD; | ||
| } | ||
|
|
||
| private void removePinnedAnnouncement(MessageReceivedEvent event) { | ||
| if (isPinnedAnnouncement(event.getMessage()) && isHelpThread(event.getChannel())) { | ||
| event.getMessage().delete().queue(); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.