bot(tickets): delete attachments when message is deleted
vi did:web:vt3e.cat
Fri, 03 Jul 2026 21:39:01 +0100
2 files changed,
19 insertions(+),
0 deletions(-)
M
apps/bot/src/feats/tickets/listeners.ts
→
apps/bot/src/feats/tickets/listeners.ts
@@ -8,6 +8,7 @@ createTicketMessageWithAttachments,
deleteTicketMessage, editTicket, editTicketMessage, + getTicketAttachmentsByMessageId, getTicketByChannelId, getTicketMessageByMessageId, } from "@stealth-developers/db";@@ -220,6 +221,20 @@ if (message.flags.has("Ephemeral")) return;
const dbMessage = await getTicketMessageByMessageId(message.id); if (!dbMessage) return; + + const attachments = await getTicketAttachmentsByMessageId(dbMessage.id); + if (attachments && attachments.length > 0) { + for (const attachment of attachments) { + const file = s3.file(attachment.key); + const deleteRes = await Result.fromAsync(file.delete()); + if (deleteRes.isErr()) { + logger.error( + deleteRes.unwrapErr(), + `Failed to delete S3 attachment file with key: ${attachment.key}`, + ); + } + } + } await deleteTicketMessage(dbMessage.id); });
M
pkgs/db/src/utils/ticket.ts
→
pkgs/db/src/utils/ticket.ts
@@ -141,6 +141,10 @@ with: ticketWithRelations,
}); } +export const getTicketAttachmentsByMessageId = async (messageId: string) => { + return db.select().from(ticketAttachments).where(eq(ticketAttachments.messageId, messageId)); +}; + export async function getOpenTickets(): Promise<TicketWithRelations[]> { return await db.query.tickets.findMany({ where: {