|
4 | 4 | import net.dv8tion.jda.api.entities.Message; |
5 | 5 | import net.dv8tion.jda.api.entities.MessageEmbed; |
6 | 6 | import net.dv8tion.jda.api.entities.MessageType; |
| 7 | +import net.dv8tion.jda.api.entities.messages.MessageSnapshot; |
7 | 8 | import net.dv8tion.jda.api.events.message.MessageReceivedEvent; |
8 | 9 | import net.dv8tion.jda.api.requests.RestAction; |
9 | 10 | import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder; |
|
13 | 14 | import org.togetherjava.tjbot.features.MessageReceiverAdapter; |
14 | 15 |
|
15 | 16 | import java.awt.Color; |
| 17 | +import java.util.List; |
16 | 18 | import java.util.concurrent.TimeUnit; |
17 | 19 | import java.util.regex.Pattern; |
18 | 20 |
|
@@ -51,9 +53,49 @@ public void onMessageReceived(MessageReceivedEvent event) { |
51 | 53 | } |
52 | 54 | } |
53 | 55 |
|
| 56 | +// private boolean messageHasNoMediaAttached(Message message) { |
| 57 | +// return message.getAttachments().isEmpty() && message.getEmbeds().isEmpty() |
| 58 | +// && !message.getContentRaw().contains("http"); |
| 59 | +// } |
| 60 | + |
| 61 | + /** |
| 62 | + * Checks whether the given message has no media attached. |
| 63 | + * <p> |
| 64 | + * A message is considered to have media if it contains attachments, embeds, |
| 65 | + * or a URL in its text content. For forwarded messages, the snapshots are also |
| 66 | + * checked for media. |
| 67 | + * |
| 68 | + * @param message the message to check |
| 69 | + * @return {@code true} if the message has no media, {@code false} otherwise |
| 70 | + */ |
54 | 71 | private boolean messageHasNoMediaAttached(Message message) { |
55 | | - return message.getAttachments().isEmpty() && message.getEmbeds().isEmpty() |
56 | | - && !message.getContentRaw().contains("http"); |
| 72 | + if (hasMedia(message.getAttachments(), message.getEmbeds(), message.getContentRaw())) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + // checks forwarded snapshots |
| 76 | + for (MessageSnapshot snapshot : message.getMessageSnapshots()) { |
| 77 | + if (hasMedia(snapshot.getAttachments(), snapshot.getEmbeds(), snapshot.getContentRaw())) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + } |
| 81 | + return true; |
| 82 | + } |
| 83 | + /** |
| 84 | + * Checks whether the given content contains any media. |
| 85 | + * <p> |
| 86 | + * Media is considered present if there are attachments, embeds, |
| 87 | + * or a URL (identified by {@code "http"}) in the text content. |
| 88 | + * |
| 89 | + * @param attachments the attachments of the message or snapshot |
| 90 | + * @param embeds the embeds of the message or snapshot |
| 91 | + * @param content the raw text content of the message or snapshot |
| 92 | + */ |
| 93 | + |
| 94 | + private boolean hasMedia(List<Message.Attachment> attachments, |
| 95 | + List<MessageEmbed> embeds, String content) { |
| 96 | + return !attachments.isEmpty() |
| 97 | + || !embeds.isEmpty() |
| 98 | + || content.contains("http"); |
57 | 99 | } |
58 | 100 |
|
59 | 101 | private MessageCreateData createNotificationMessage(Message message) { |
|
0 commit comments