Skip to content

Commit 9b8d844

Browse files
committed
refactor(bot.js): Updated Discord Attachment handle
1 parent 2157f44 commit 9b8d844

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

src/bot.js

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ class PulchowkBot {
10921092
.setFooter({ text: `Source: ${notice.source}` })
10931093
.setTimestamp(new Date(notice.date));
10941094

1095-
let filesToSend = [];
1095+
let allFilesForNotice = []; // Collect all files here
10961096
let tempFilesOnDisk = [];
10971097
let description = `A new notice has been published.`;
10981098

@@ -1160,7 +1160,7 @@ class PulchowkBot {
11601160
const pngFilePath = convertResponse.path;
11611161
const pngFileName = path.basename(pngFilePath);
11621162
tempFilesOnDisk.push(pngFilePath);
1163-
filesToSend.push(new AttachmentBuilder(pngFilePath, { name: pngFileName }));
1163+
allFilesForNotice.push(new AttachmentBuilder(pngFilePath, { name: pngFileName }));
11641164
console.log(`Converted PDF ${fileName} page ${pageNum} to PNG and prepared for sending.`);
11651165
pageConvertedCount++;
11661166
} else {
@@ -1177,7 +1177,7 @@ class PulchowkBot {
11771177

11781178
if (pageConvertedCount === 0) {
11791179
console.warn(`No pages converted for PDF ${fileName}. Sending original PDF.`);
1180-
filesToSend.push(new AttachmentBuilder(tempFilePath, { name: fileName }));
1180+
allFilesForNotice.push(new AttachmentBuilder(tempFilePath, { name: fileName }));
11811181
} else if (pageConvertedCount < totalPdfPages) {
11821182
console.log(`\n(Sent ${pageConvertedCount} of ${totalPdfPages} pages from ${fileName} as images.)`);
11831183
} else {
@@ -1187,10 +1187,10 @@ class PulchowkBot {
11871187
} catch (pdfProcessError) {
11881188
console.error(`Error processing PDF ${fileName}:`, pdfProcessError.message);
11891189
description += `\n\n⚠️ Could not process PDF attachment: ${fileName}`;
1190-
filesToSend.push(new AttachmentBuilder(tempFilePath, { name: fileName }));
1190+
allFilesForNotice.push(new AttachmentBuilder(tempFilePath, { name: fileName }));
11911191
}
11921192
} else {
1193-
filesToSend.push(new AttachmentBuilder(tempFilePath, { name: fileName }));
1193+
allFilesForNotice.push(new AttachmentBuilder(tempFilePath, { name: fileName }));
11941194
console.log(`Prepared attachment: ${fileName}`);
11951195
}
11961196
} catch (downloadError) {
@@ -1201,13 +1201,40 @@ class PulchowkBot {
12011201
}
12021202
noticeEmbed.setDescription(description);
12031203

1204-
try {
1205-
await noticeChannel.send({ embeds: [noticeEmbed], files: filesToSend });
1206-
console.log(`Sent notice and attachments for "${notice.title}" to Discord.`);
1207-
} catch (discordSendError) {
1208-
console.error(`Error sending notice or files to channel ${TARGET_NOTICE_CHANNEL_ID}:`, discordSendError);
1209-
if (adminChannel) await adminChannel.send(`❌ Error sending notice/files for "${notice.title}": ${discordSendError.message}`).catch(e => console.error("Error sending admin error:", e));
1210-
} finally {
1204+
// --- NEW LOGIC FOR SPLITTING ATTACHMENTS ---
1205+
const ATTACHMENT_LIMIT = 10;
1206+
if (allFilesForNotice.length > 0) {
1207+
let sentFirstMessage = false;
1208+
for (let i = 0; i < allFilesForNotice.length; i += ATTACHMENT_LIMIT) {
1209+
const chunk = allFilesForNotice.slice(i, i + ATTACHMENT_LIMIT);
1210+
try {
1211+
if (!sentFirstMessage) {
1212+
// Send the initial embed with the first chunk of attachments
1213+
await noticeChannel.send({ embeds: [noticeEmbed], files: chunk });
1214+
sentFirstMessage = true;
1215+
} else {
1216+
// For subsequent chunks, send only attachments with a follow-up message
1217+
await noticeChannel.send({ content: `(Continued attachments for "${notice.title}")`, files: chunk });
1218+
}
1219+
console.log(`Sent chunk of ${chunk.length} attachments for "${notice.title}" to Discord.`);
1220+
} catch (discordSendError) {
1221+
console.error(`Error sending notice or files to channel ${TARGET_NOTICE_CHANNEL_ID} (chunk ${i / ATTACHMENT_LIMIT + 1}):`, discordSendError);
1222+
if (adminChannel) await adminChannel.send(`❌ Error sending notice/files for "${notice.title}" (chunk ${i / ATTACHMENT_LIMIT + 1}): ${discordSendError.message}`).catch(e => console.error("Error sending admin error:", e));
1223+
}
1224+
}
1225+
} else {
1226+
// If no attachments, just send the embed
1227+
try {
1228+
await noticeChannel.send({ embeds: [noticeEmbed] });
1229+
console.log(`Sent notice for "${notice.title}" to Discord (no attachments).`);
1230+
} catch (discordSendError) {
1231+
console.error(`Error sending notice to channel ${TARGET_NOTICE_CHANNEL_ID}:`, discordSendError);
1232+
if (adminChannel) await adminChannel.send(`❌ Error sending notice for "${notice.title}": ${discordSendError.message}`).catch(e => console.error("Error sending admin error:", e));
1233+
}
1234+
}
1235+
// --- END NEW LOGIC ---
1236+
1237+
finally {
12111238
for (const filePath of tempFilesOnDisk) {
12121239
try {
12131240
await fsPromises.unlink(filePath);
@@ -1310,7 +1337,7 @@ class PulchowkBot {
13101337
firstBirthdayUserAvatarUrl = member.user.displayAvatarURL({ dynamic: true, size: 128 });
13111338
}
13121339
} catch (fetchErr) {
1313-
console.warn(`Could not fetch birthday user ${row.user_id}):`, fetchErr.message);
1340+
console.warn(`Could not fetch birthday user ${row.user.id}):`, fetchErr.message);
13141341
birthdayUsers.push(`• Unknown User (ID: ${row.user.id})`);
13151342
}
13161343
}

0 commit comments

Comments
 (0)