const fs = require('fs'); const path = require('path'); const { EmbedBuilder, StickerFormatType } = require('discord.js'); const { closeTicket } = require('./database'); const { config } = require('./configLoader'); async function generateTranscript(channel, user) { const messages = await channel.messages.fetch({ limit: 100 }); const sorted = [...messages.values()].reverse(); let html = ` Transcript - ${channel.name}

Transcript - ${channel.name}

`; for (const msg of sorted) { const time = msg.createdAt.toLocaleString(); const avatar = msg.author.displayAvatarURL(); let content = msg.content || ''; // Mentions if (content) { content = content.replace(/<@!?(\d+)>/g, (match, id) => { const u = channel.client.users.cache.get(id); return u ? `@${u.username}` : match; }); // Emojis custom content = content.replace(//g, (match, id) => { const isAnimated = match.startsWith('`; }); } html += `
${msg.author.tag} (${time})
${content}
`; // Pièces jointes if (msg.attachments.size > 0) { msg.attachments.forEach((att) => { if (att.contentType?.startsWith('image/')) { html += `
`; } else { html += ``; } }); } // Stickers if (msg.stickers.size > 0) { msg.stickers.forEach((sticker) => { if (sticker.format === StickerFormatType.LOTTIE) { html += `
[Sticker Lottie non supporté]
`; return; } const url = `https://cdn.discordapp.com/stickers/${sticker.id}.png`; html += `
${sticker.name}
${sticker.name}
`; }); } // Embeds if (msg.embeds.length > 0) { msg.embeds.forEach((embed) => { html += `
`; if (embed.title) html += `
${embed.title}
`; if (embed.description) html += `
${embed.description}
`; if (embed.url) html += `
🔗 Lien
`; if (embed.thumbnail?.url) { html += ``; } html += `
`; }); } html += `
`; } html += ``; const transcriptsDir = path.join(__dirname, '../transcripts'); if (!fs.existsSync(transcriptsDir)) fs.mkdirSync(transcriptsDir); const filePath = path.join(transcriptsDir, `${channel.id}.html`); fs.writeFileSync(filePath, html, 'utf8'); // === Lecture domaine/port depuis .env === const DOMAIN = process.env.TRANSCRIPT_DOMAIN || 'http://localhost'; const PORT = process.env.TRANSCRIPT_PORT || 3000; // Logs const logChannel = await channel.client.channels .fetch(config.logsChannel) .catch(() => null); if (logChannel) { const embed = new EmbedBuilder() .setTitle('🔒 Ticket fermé') .addFields( { name: 'Salon', value: `${channel.name}`, inline: true }, { name: 'Fermé par', value: user ? `${user.tag} (${user.id})` : 'Automatique', inline: true, }, { name: 'Transcript', value: `${DOMAIN}:${PORT}/transcript/${channel.id}`, } ) .setColor(0xe74c3c) .setTimestamp(); await logChannel.send({ embeds: [embed] }); } } async function closeTicketWithTranscript(channel, user) { await generateTranscript(channel, user); closeTicket(channel.id); await channel.delete().catch(() => {}); } module.exports = { closeTicketWithTranscript };