-
-
Notifications
You must be signed in to change notification settings - Fork 102
Allow forwarding of media, to media channels #1266
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
base: develop
Are you sure you want to change the base?
Changes from 1 commit
d20f984
2742573
a21f294
869da01
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| import net.dv8tion.jda.api.entities.Message; | ||
| import net.dv8tion.jda.api.entities.MessageEmbed; | ||
| import net.dv8tion.jda.api.entities.MessageType; | ||
| import net.dv8tion.jda.api.entities.messages.MessageSnapshot; | ||
| import net.dv8tion.jda.api.events.message.MessageReceivedEvent; | ||
| import net.dv8tion.jda.api.requests.RestAction; | ||
| import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder; | ||
|
|
@@ -13,6 +14,7 @@ | |
| import org.togetherjava.tjbot.features.MessageReceiverAdapter; | ||
|
|
||
| import java.awt.Color; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.regex.Pattern; | ||
|
|
||
|
|
@@ -25,6 +27,13 @@ | |
| */ | ||
| public final class MediaOnlyChannelListener extends MessageReceiverAdapter { | ||
|
|
||
| private static final Pattern MEDIA_URL_PATTERN = Pattern.compile( | ||
| ".*https?://\\S+\\.(png|jpe?g|gif|bmp|webp|mp4|mov|avi|webm|mp3|wav|ogg|youtube\\.com/|youtu\\.com|imgur\\.com/).*", | ||
| Pattern.CASE_INSENSITIVE); | ||
|
|
||
| private final ConcurrentHashMap<Long, Long> lastValidForwardedMediaMessageTime = | ||
| new ConcurrentHashMap<>(); | ||
|
|
||
| /** | ||
| * Creates a MediaOnlyChannelListener to receive all message sent in MediaOnly channel. | ||
| * | ||
|
|
@@ -45,19 +54,60 @@ public void onMessageReceived(MessageReceivedEvent event) { | |
| return; | ||
| } | ||
|
|
||
| if (messageHasNoMediaAttached(message)) { | ||
| message.delete().flatMap(any -> dmUser(message)).queue(any -> { | ||
| }, failure -> tempNotifyUserInChannel(message)); | ||
| long userId = event.getAuthor().getIdLong(); | ||
|
|
||
| boolean isForwardedWithMedia = | ||
| !message.getMessageSnapshots().isEmpty() && !messageHasNoMediaAttached(message); | ||
|
|
||
| if (isForwardedWithMedia) { | ||
| lastValidForwardedMediaMessageTime.put(userId, System.currentTimeMillis()); | ||
| return; | ||
| } | ||
|
|
||
| boolean isNormalMediaUpload = | ||
| message.getMessageSnapshots().isEmpty() && !messageHasNoMediaAttached(message); | ||
| if (isNormalMediaUpload) { | ||
| return; | ||
| } | ||
|
|
||
| Long lastForwardedMediaTime = lastValidForwardedMediaMessageTime.get(userId); | ||
| long gracePeriodMillis = TimeUnit.SECONDS.toMillis(1); | ||
|
|
||
| if (lastForwardedMediaTime != null | ||
| && (System.currentTimeMillis() - lastForwardedMediaTime) <= gracePeriodMillis) { | ||
| lastValidForwardedMediaMessageTime.remove(userId); | ||
| return; | ||
| } | ||
|
|
||
| message.delete().queue(deleteSuccess -> dmUser(message).queue(dmSuccess -> { | ||
| }, dmFailure -> tempNotifyUserInChannel(message)), | ||
| deleteFailure -> tempNotifyUserInChannel(message)); | ||
| } | ||
|
|
||
| private boolean messageHasNoMediaAttached(Message message) { | ||
| return message.getAttachments().isEmpty() && message.getEmbeds().isEmpty() | ||
| && !message.getContentRaw().contains("http"); | ||
| if (!message.getAttachments().isEmpty() || !message.getEmbeds().isEmpty() | ||
| || MEDIA_URL_PATTERN.matcher(message.getContentRaw()).matches()) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!message.getMessageSnapshots().isEmpty()) { | ||
| for (MessageSnapshot snapshot : message.getMessageSnapshots()) { | ||
| if (!snapshot.getAttachments().isEmpty() || !snapshot.getEmbeds().isEmpty() | ||
| || MEDIA_URL_PATTERN.matcher(snapshot.getContentRaw()).matches()) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| return true; | ||
|
||
| } | ||
|
|
||
| private MessageCreateData createNotificationMessage(Message message) { | ||
| String originalMessageContent = message.getContentRaw(); | ||
| if (originalMessageContent.trim().isEmpty()) { | ||
| originalMessageContent = "(Original message had no visible text content)"; | ||
|
||
| } | ||
|
|
||
| MessageEmbed originalMessageEmbed = | ||
| new EmbedBuilder().setDescription(originalMessageContent) | ||
|
|
||



Uh oh!
There was an error while loading. Please reload this page.