3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-25 18:25:03 +00:00
zeppelin/backend/src/plugins/ChannelArchiver/rehostAttachment.ts
metal 59bf98f928
remove unused imports & add prettier plugin
Signed-off-by: GitHub <noreply@github.com>
2023-03-20 20:13:30 +00:00

30 lines
1,020 B
TypeScript

import { Attachment, GuildTextBasedChannel, MessageCreateOptions } from "discord.js";
import fs from "fs";
import { downloadFile } from "../../utils";
const fsp = fs.promises;
const MAX_ATTACHMENT_REHOST_SIZE = 1024 * 1024 * 8;
export async function rehostAttachment(attachment: Attachment, targetChannel: GuildTextBasedChannel): Promise<string> {
if (attachment.size > MAX_ATTACHMENT_REHOST_SIZE) {
return "Attachment too big to rehost";
}
let downloaded;
try {
downloaded = await downloadFile(attachment.url, 3);
} catch {
return "Failed to download attachment after 3 tries";
}
try {
const content: MessageCreateOptions = {
content: `Rehost of attachment ${attachment.id}`,
files: [{ name: attachment.name ? attachment.name : undefined, attachment: await fsp.readFile(downloaded.path) }],
};
const rehostMessage = await targetChannel.send(content);
return rehostMessage.attachments.values()[0].url;
} catch {
return "Failed to rehost attachment";
}
}