From 02ef4129605f4165e012d4e875ce466a201e22fd Mon Sep 17 00:00:00 2001 From: StormPacer Date: Tue, 24 Dec 2024 17:09:39 +0100 Subject: [PATCH] Change transcript handling --- interactions/buttons/operation/confirm.js | 35 ++++++++++++++++++---- interactions/slash/operation/forceClose.js | 35 +++++++++++++++++----- package.json | 1 + yarn.lock | 5 ++++ 4 files changed, 63 insertions(+), 13 deletions(-) diff --git a/interactions/buttons/operation/confirm.js b/interactions/buttons/operation/confirm.js index dae6c44..3117b65 100644 --- a/interactions/buttons/operation/confirm.js +++ b/interactions/buttons/operation/confirm.js @@ -1,6 +1,8 @@ const { EmbedBuilder } = require("discord.js"); const db = require("../../../connectDb"); const discordTranscripts = require("discord-html-transcripts"); +const { FormData, File } = require("formdata-node"); +const fs = require("fs"); module.exports = { id: "confirm_close", @@ -32,11 +34,15 @@ module.exports = { const transcript = await discordTranscripts.createTranscript(channel, { limit: -1, filename: `transcript-${channel.name}.html`, - saveImages: false, + saveImages: true, poweredBy: false, - ssr: false + ssr: false, + returnType: "string" }); + const transcriptPath = `./temp/${channel.name}.html`; + fs.writeFileSync(transcriptPath, transcript); + const ticket = await db("tickets") .select("*") .where("channel_id", channel.id) @@ -47,6 +53,25 @@ module.exports = { .where("id", ticket.ticket_id) .first(); + const username = (await interaction.guild.members.fetch(ticket.user_id)).user.username; + + const blob = await fs.openAsBlob(transcriptPath); + + const form = new FormData(); + const file = new File([blob], `transcript-${channel.name}.html`, { type: "text/html" }); + form.set("file", file); + form.set("jsonData", JSON.stringify({ type: category.label, username, ticket: channel.name })); + + await fetch(`${process.env.API_URL}/transcript`, { + method: "POST", + headers: { + Authorization: process.env.API_KEY + }, + body: form + }); + + fs.unlinkSync(transcriptPath); + const logChannel = interaction.guild.channels.cache.get(ticket.log_channel_id); const embed = new EmbedBuilder() @@ -58,12 +83,10 @@ module.exports = { { name: "Ticket owner", value: `ID: ${ticket.user_id}` } ) .setColor("Red") - .setFooter({ text: `Transcript is attached below this message` }) - logChannel.send({ embeds: [embed] }); - logChannel.send({ files: [transcript] }); + logChannel.send({ content: `Transcript available at ${process.env.WEB_URL}/transcript/${channel.name}`, embeds: [embed] }); - interaction.followUp({ content: `Close Request Confirmed!`}) + interaction.followUp({ content: `Close Request Confirmed!` }) interaction.followUp({ content: `Closing this ticket in 5 seconds...`, ephemeral: false }); setTimeout(async () => { diff --git a/interactions/slash/operation/forceClose.js b/interactions/slash/operation/forceClose.js index 663d74c..b021863 100644 --- a/interactions/slash/operation/forceClose.js +++ b/interactions/slash/operation/forceClose.js @@ -29,11 +29,15 @@ module.exports = { const transcript = await discordTranscripts.createTranscript(channel, { limit: -1, filename: `transcript-${channel.name}.html`, - saveImages: false, + saveImages: true, poweredBy: false, - ssr: false + ssr: false, + returnType: "string" }); + const transcriptPath = `./temp/${channel.name}.html`; + fs.writeFileSync(transcriptPath, transcript); + const ticket = await db("tickets") .select("*") .where("channel_id", channel.id) @@ -44,7 +48,24 @@ module.exports = { .where("id", ticket.ticket_id) .first(); - const logChannel = interaction.guild.channels.cache.get(ticket.log_channel_id); + const username = (await interaction.guild.members.fetch(ticket.user_id)).user.username; + + const blob = await fs.openAsBlob(transcriptPath); + + const form = new FormData(); + const file = new File([blob], `transcript-${channel.name}.html`, { type: "text/html" }); + form.set("file", file); + form.set("jsonData", JSON.stringify({ type: category.label, username, ticket: channel.name })); + + await fetch(`${process.env.API_URL}/transcript`, { + method: "POST", + headers: { + Authorization: process.env.API_KEY + }, + body: form + }); + + fs.unlinkSync(transcriptPath); const embed = new EmbedBuilder() .setTitle(`Ticket #${ticket.id} closed`) @@ -54,11 +75,11 @@ module.exports = { { name: "Ticket type", value: `${category.label}` }, { name: "Ticket owner", value: `ID: ${ticket.user_id}` } ) - .setColor("Red") - .setFooter({ text: `Transcript is attached below this message` }) + .setColor("Red"); + + const logChannel = interaction.guild.channels.cache.get(ticket.log_channel_id); - logChannel.send({ embeds: [embed] }); - logChannel.send({ files: [transcript] }); + logChannel.send({ content: `Transcript available at ${process.env.WEB_URL}/transcript/${channel.name}`, embeds: [embed] }); interaction.followUp({ content: `Closing this ticket in 5 seconds...` }); diff --git a/package.json b/package.json index 1092b54..45dd30d 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "discord-html-transcripts": "^3.2.0", "discord.js": "^14.0.2", "dotenv": "^16.4.5", + "formdata-node": "^6.0.3", "knex": "^3.1.0", "pg": "^8.11.3" } diff --git a/yarn.lock b/yarn.lock index 3363aa3..ced1033 100644 --- a/yarn.lock +++ b/yarn.lock @@ -304,6 +304,11 @@ file-type@^18.3.0: strtok3 "^7.0.0" token-types "^5.0.1" +formdata-node@^6.0.3: + version "6.0.3" + resolved "https://registry.yarnpkg.com/formdata-node/-/formdata-node-6.0.3.tgz#48f8e2206ae2befded82af621ef015f08168dc6d" + integrity sha512-8e1++BCiTzUno9v5IZ2J6bv4RU+3UKDmqWUQD0MIMVCd9AdhWkO1gw57oo1mNEX1dMq2EGI+FbWz4B92pscSQg== + fs-extra@^8.0.1: version "8.1.0" resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-8.1.0.tgz#49d43c45a88cd9677668cb7be1b46efdb8d2e1c0"