all repos — stealth-developers @ 652ff34ecfa201e32014b65eaa7ca0c4d4a4794a

src/interactions/buttons/transcript-btn.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
import type { ButtonInteraction, Client, GuildMember } from "discord.js";

import { hasManagerPermissions } from "@/utils/permissions";
import { getTicketById } from "@/utils/queries";
import { generateTranscript } from "@/utils/transcripts";

import { db, ticketAccessLogs } from "@/database";

export default {
	buttonExecute: async (_client: Client, interaction: ButtonInteraction) => {
		const [, id] = interaction.customId.split(":");

		const { data: ticket, exists } = await getTicketById(null, Number(id));
		if (!exists) {
			await interaction.reply({
				content: "Ticket not found.",
				ephemeral: true,
			});
			return;
		}

		if (
			interaction.member &&
			(await hasManagerPermissions(interaction.member as GuildMember))
		) {
			await db.insert(ticketAccessLogs).values({
				ticketId: ticket.id,
				userId: interaction.user.id,
				action: "view_transcript",
			});
		}

		const [transcriptText] = await generateTranscript(ticket);
		await interaction.reply({
			files: [
				{
					attachment: Buffer.from(transcriptText, "utf-8"),
					name: `transcript-${ticket.id}.txt`,
				},
			],
			flags: ["Ephemeral"],
		});
	},
};