APRK01 commited on
Commit
8bc3069
Β·
1 Parent(s): c8f3880

fix: re-upload drop files as local attachments to prevent discord CDN expiration

Browse files
Files changed (1) hide show
  1. src/systems/drops.js +53 -5
src/systems/drops.js CHANGED
@@ -3,6 +3,7 @@ const {
3
  ButtonBuilder,
4
  ButtonStyle,
5
  StringSelectMenuBuilder,
 
6
  } = require('discord.js');
7
  const { createEmbed } = require('../utils/embeds');
8
  const { Colors } = require('../config');
@@ -183,7 +184,8 @@ function buildDropEmbed(session) {
183
  };
184
 
185
  if (session.image) {
186
- embedData.image = session.image;
 
187
  }
188
 
189
  return createEmbed(embedData);
@@ -232,7 +234,13 @@ async function handleDropMessage(message) {
232
  return true;
233
  }
234
  const att = message.attachments.first();
235
- session.file = { url: att.url, name: att.name, size: att.size };
 
 
 
 
 
 
236
  session.step = 'warnings';
237
  await message.reply(getPrompt(session));
238
  return true;
@@ -253,7 +261,12 @@ async function handleDropMessage(message) {
253
  if (content.toLowerCase() === 'none') {
254
  session.image = null;
255
  } else if (message.attachments.size > 0) {
256
- session.image = message.attachments.first().url;
 
 
 
 
 
257
  } else {
258
  session.image = null;
259
  }
@@ -289,11 +302,46 @@ async function handleDropMessage(message) {
289
  const channel = await guild.channels.fetch(channelId);
290
  if (!channel) throw new Error('Channel not found');
291
 
292
- const dropEmbed = buildDropEmbed(session);
293
- const downloadRow = buildDownloadButton(session);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294
  await channel.send({
295
  embeds: [dropEmbed],
296
  components: [downloadRow],
 
297
  });
298
 
299
  // Log the drop for rate limiting
 
3
  ButtonBuilder,
4
  ButtonStyle,
5
  StringSelectMenuBuilder,
6
+ AttachmentBuilder,
7
  } = require('discord.js');
8
  const { createEmbed } = require('../utils/embeds');
9
  const { Colors } = require('../config');
 
184
  };
185
 
186
  if (session.image) {
187
+ // Handle both object-style (from re-upload) and string-style (from direct URL/preview)
188
+ embedData.image = typeof session.image === 'object' ? session.image.url : session.image;
189
  }
190
 
191
  return createEmbed(embedData);
 
234
  return true;
235
  }
236
  const att = message.attachments.first();
237
+ // Store the full attachment object to re-upload it later
238
+ session.file = {
239
+ url: att.url,
240
+ name: att.name,
241
+ size: att.size,
242
+ attachment: att // Save reference to original attachment
243
+ };
244
  session.step = 'warnings';
245
  await message.reply(getPrompt(session));
246
  return true;
 
261
  if (content.toLowerCase() === 'none') {
262
  session.image = null;
263
  } else if (message.attachments.size > 0) {
264
+ const imgAtt = message.attachments.first();
265
+ session.image = {
266
+ url: imgAtt.url,
267
+ name: imgAtt.name,
268
+ attachment: imgAtt
269
+ };
270
  } else {
271
  session.image = null;
272
  }
 
302
  const channel = await guild.channels.fetch(channelId);
303
  if (!channel) throw new Error('Channel not found');
304
 
305
+ // We must re-upload the files so they don't expire
306
+ const filesToUpload = [];
307
+ let fileDndUrl = session.file.url;
308
+ let imageUrl = session.image?.url;
309
+
310
+ // 1. The main dropped file
311
+ if (session.file.attachment) {
312
+ const mainFile = new AttachmentBuilder(session.file.attachment.url, { name: session.file.name });
313
+ filesToUpload.push(mainFile);
314
+ fileDndUrl = `attachment://${session.file.name}`;
315
+ }
316
+
317
+ // 2. The preview image (if any)
318
+ if (session.image && session.image.attachment) {
319
+ const imgFile = new AttachmentBuilder(session.image.attachment.url, { name: session.image.name });
320
+ filesToUpload.push(imgFile);
321
+ imageUrl = `attachment://${session.image.name}`;
322
+ }
323
+
324
+ // Build embed with local attachment references
325
+ const dropEmbed = buildDropEmbed({
326
+ ...session,
327
+ image: imageUrl ? { url: imageUrl } : null
328
+ });
329
+
330
+ // Update the download button to use the new attachment URL
331
+ // We'll actually remove the URL from the button and let them click the raw file attached to the message
332
+ // Alternatively, we send the files WITH the embed, and the button becomes a dummy/informational
333
+ const downloadRow = new ActionRowBuilder().addComponents(
334
+ new ButtonBuilder()
335
+ .setCustomId('dummy_download')
336
+ .setLabel('πŸ“₯ Download Attached File')
337
+ .setStyle(ButtonStyle.Secondary)
338
+ .setDisabled(true)
339
+ );
340
+
341
  await channel.send({
342
  embeds: [dropEmbed],
343
  components: [downloadRow],
344
+ files: filesToUpload,
345
  });
346
 
347
  // Log the drop for rate limiting