|
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.channel.ChannelType; |
| 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.utils.messages.MessageCreateBuilder; |
9 | 10 | import net.dv8tion.jda.api.utils.messages.MessageCreateData; |
@@ -112,4 +113,48 @@ private MessageReceivedEvent sendMessage(MessageCreateData message, |
112 | 113 | mediaOnlyChannelListener.onMessageReceived(event); |
113 | 114 | return event; |
114 | 115 | } |
| 116 | + |
| 117 | + |
| 118 | + @Test |
| 119 | + void keepsForwardedMessageWithAttachment() { |
| 120 | + // GIVEN a forwarded message that contains an attachment inside the snapshot |
| 121 | + MessageCreateData message = new MessageCreateBuilder().setContent("any").build(); |
| 122 | + |
| 123 | + MessageSnapshot snapshot = mock(MessageSnapshot.class); |
| 124 | + when(snapshot.getAttachments()).thenReturn(List.of(mock(Message.Attachment.class))); |
| 125 | + when(snapshot.getEmbeds()).thenReturn(List.of()); |
| 126 | + when(snapshot.getContentRaw()).thenReturn(""); |
| 127 | + |
| 128 | + // WHEN sending the forwarded message |
| 129 | + MessageReceivedEvent event = sendMessageWithSnapshots(message, List.of(snapshot)); |
| 130 | + |
| 131 | + // THEN it does not get deleted |
| 132 | + verify(event.getMessage(), never()).delete(); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + void deletesForwardedMessageWithoutMedia() { |
| 137 | + // GIVEN a forwarded message that contains no media inside the snapshot |
| 138 | + MessageCreateData message = new MessageCreateBuilder().setContent("any").build(); |
| 139 | + |
| 140 | + MessageSnapshot snapshot = mock(MessageSnapshot.class); |
| 141 | + when(snapshot.getAttachments()).thenReturn(List.of()); |
| 142 | + when(snapshot.getEmbeds()).thenReturn(List.of()); |
| 143 | + when(snapshot.getContentRaw()).thenReturn("just some text, no media"); |
| 144 | + |
| 145 | + // WHEN sending the forwarded message |
| 146 | + MessageReceivedEvent event = sendMessageWithSnapshots(message, List.of(snapshot)); |
| 147 | + |
| 148 | + // THEN it gets deleted |
| 149 | + verify(event.getMessage()).delete(); |
| 150 | + } |
| 151 | + |
| 152 | + private MessageReceivedEvent sendMessageWithSnapshots(MessageCreateData message, |
| 153 | + List<MessageSnapshot> snapshots) { |
| 154 | + MessageReceivedEvent event = |
| 155 | + jdaTester.createMessageReceiveEvent(message, List.of(), ChannelType.TEXT); |
| 156 | + when(event.getMessage().getMessageSnapshots()).thenReturn(snapshots); |
| 157 | + mediaOnlyChannelListener.onMessageReceived(event); |
| 158 | + return event; |
| 159 | + } |
115 | 160 | } |
0 commit comments