Skip to content

Commit 9c2ee35

Browse files
committed
Reupload attachments from other deleted messages
1 parent f43a76a commit 9c2ee35

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/main/java/net/discordjug/javabot/data/h2db/message_cache/MessageCache.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ public void sendDeletedMessageToLog(Guild guild, MessageChannel channel, CachedM
161161
guild.getJDA().retrieveUserById(message.getAuthorId()).queue(author -> {
162162
MessageCreateAction action = config.getMessageCacheLogChannel()
163163
.sendMessageEmbeds(buildMessageDeleteEmbed(guild, author, channel, message));
164+
addAttachmentsToCreateAction(action, message);
164165
if (message.getMessageContent().length() > MessageEmbed.VALUE_MAX_LENGTH) {
165166
action.addFiles(FileUpload.fromData(buildDeletedMessageFile(author, message), messageId + ".txt"));
166167
}
@@ -184,6 +185,28 @@ public void sendBlacklistedAttachmentsMessageToLog(Guild guild, MessageChannel c
184185
action.complete();
185186
}
186187

188+
private void addAttachmentsToCreateAction(MessageCreateAction action, CachedMessage cached) {
189+
for (String urlString : cached.getAttachments()) {
190+
try {
191+
URL url = URI.create(urlString).toURL();
192+
String attachmentName = url.getFile();
193+
194+
int slashIndex = attachmentName.lastIndexOf('/');
195+
if (slashIndex > -1) attachmentName = attachmentName.substring(slashIndex + 1);
196+
197+
int questionIndex = attachmentName.indexOf('?');
198+
if (questionIndex > -1) attachmentName = attachmentName.substring(0, questionIndex);
199+
200+
InputStream attachmentStream = url.openStream();
201+
FileUpload upload = FileUpload.fromData(attachmentStream, attachmentName).asSpoiler();
202+
action.addFiles(upload);
203+
} catch (IOException e) {
204+
ExceptionLogger.capture(e, getClass().getSimpleName());
205+
log.error("Something went wrong while retrieving attachments for a deleted message");
206+
}
207+
}
208+
}
209+
187210
private void addAttachmentsToCreateAction(MessageCreateAction action, Message original) {
188211
for (Attachment attachment : original.getAttachments()) {
189212
try {
@@ -194,7 +217,7 @@ private void addAttachmentsToCreateAction(MessageCreateAction action, Message or
194217
action.addFiles(upload);
195218
} catch (IOException e) {
196219
ExceptionLogger.capture(e, getClass().getSimpleName());
197-
log.error("Something went wrong during retrieval of stored messages.");
220+
log.error("Something went wrong while retrieving attachments for a deleted message");
198221
}
199222
}
200223
}

0 commit comments

Comments
 (0)