Skip to content

Commit

Permalink
Merge pull request #1 from NejcZivic/main
Browse files Browse the repository at this point in the history
Changed transcript handling
  • Loading branch information
bloodcloak authored Jan 7, 2025
2 parents cc09c26 + 49001ab commit 1dc4fa6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
3 changes: 2 additions & 1 deletion example.env
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ DB_USER=postgres
DB_PASS=postgrespw
DB_HOST=db
DB_PORT=5432
DB_NAME=postgres
DB_NAME=postgres
AUTH_KEY=changeme
35 changes: 29 additions & 6 deletions interactions/buttons/operation/confirm.js
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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)
Expand All @@ -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.AUTH_KEY
},
body: form
});

fs.unlinkSync(transcriptPath);

const logChannel = interaction.guild.channels.cache.get(ticket.log_channel_id);

const embed = new EmbedBuilder()
Expand All @@ -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 () => {
Expand Down
35 changes: 28 additions & 7 deletions interactions/slash/operation/forceClose.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.AUTH_KEY
},
body: form
});

fs.unlinkSync(transcriptPath);

const embed = new EmbedBuilder()
.setTitle(`Ticket #${ticket.id} closed`)
Expand All @@ -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...` });

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 1dc4fa6

Please sign in to comment.