Skip to content

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

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,6 +26,10 @@
*/
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);

/**
* Creates a MediaOnlyChannelListener to receive all message sent in MediaOnly channel.
*
Expand All @@ -36,38 +41,62 @@ public MediaOnlyChannelListener(Config config) {

@Override
public void onMessageReceived(MessageReceivedEvent event) {
if (event.getAuthor().isBot() || event.isWebhookMessage()) {
if (event.getAuthor().isBot() || event.isWebhookMessage()
|| event.getMessage().getType() == MessageType.THREAD_CREATED) {
return;
}

Message message = event.getMessage();
if (message.getType() == MessageType.THREAD_CREATED) {

if (!messageHasNoMediaAttached(message)) {
return;
}

if (messageHasNoMediaAttached(message)) {
message.delete().flatMap(any -> dmUser(message)).queue(any -> {
}, failure -> tempNotifyUserInChannel(message));
}
deleteAndNotify(message);
}

private void deleteAndNotify(Message message) {
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();

MessageEmbed originalMessageEmbed =
new EmbedBuilder().setDescription(originalMessageContent)
.setColor(Color.ORANGE)
.build();
MessageCreateBuilder messageBuilder = new MessageCreateBuilder();
messageBuilder.setContent(message.getAuthor().getAsMention()
+ " Hey there, you posted a message without media (image, video, link) in a media-only channel. Please see the description of the channel for details and then repost with media attached, thanks 😀");

// Conditionally add the embed only if the original message content is NOT empty
if (!originalMessageContent.trim().isEmpty()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.isBlank is probably what you want here

MessageEmbed originalMessageEmbed =
new EmbedBuilder().setDescription(originalMessageContent)
.setColor(Color.ORANGE)
.build();
messageBuilder.setEmbeds(originalMessageEmbed);
}

return new MessageCreateBuilder().setContent(message.getAuthor().getAsMention()
+ " Hey there, you posted a message without media (image, video, link) in a media-only channel. Please see the description of the channel for details and then repost with media attached, thanks 😀")
.setEmbeds(originalMessageEmbed)
.build();
return messageBuilder.build();
}

private RestAction<Message> dmUser(Message message) {
Expand Down