mirror of
https://github.com/Hessenuk/DiscordTickets.git
synced 2024-12-23 00:03:09 +02:00
feat(archives): add transcript command
This commit is contained in:
parent
7864c8d544
commit
92d5a7ed96
3
.gitignore
vendored
3
.gitignore
vendored
@ -5,7 +5,7 @@ node_modules/
|
|||||||
prisma/
|
prisma/
|
||||||
|
|
||||||
# files
|
# files
|
||||||
.env
|
*.env*
|
||||||
*.db
|
*.db
|
||||||
*.db-journal
|
*.db-journal
|
||||||
*.log
|
*.log
|
||||||
@ -13,3 +13,4 @@ prisma/
|
|||||||
user/config.yml
|
user/config.yml
|
||||||
user/**/*.*
|
user/**/*.*
|
||||||
!user/**/.gitkeep
|
!user/**/.gitkeep
|
||||||
|
!user/templates/*
|
@ -55,9 +55,11 @@
|
|||||||
"leeks.js": "^0.2.4",
|
"leeks.js": "^0.2.4",
|
||||||
"leekslazylogger": "^4.1.7",
|
"leekslazylogger": "^4.1.7",
|
||||||
"ms": "^2.1.3",
|
"ms": "^2.1.3",
|
||||||
|
"mustache": "^4.2.0",
|
||||||
"node-dir": "^0.1.17",
|
"node-dir": "^0.1.17",
|
||||||
"node-emoji": "^1.11.0",
|
"node-emoji": "^1.11.0",
|
||||||
"object-diffy": "^1.0.4",
|
"object-diffy": "^1.0.4",
|
||||||
|
"pad": "^3.2.0",
|
||||||
"prisma": "^4.5.0",
|
"prisma": "^4.5.0",
|
||||||
"semver": "^7.3.8",
|
"semver": "^7.3.8",
|
||||||
"terminal-link": "^2.1.1",
|
"terminal-link": "^2.1.1",
|
||||||
|
@ -11,6 +11,13 @@ module.exports = class ReferencesCompleter extends Autocompleter {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
format(ticket) {
|
||||||
|
const date = new Date(ticket.createdAt).toLocaleString(ticket.guild.locale, { dateStyle: 'short' });
|
||||||
|
const topic = ticket.topic ? '| ' + decrypt(ticket.topic).substring(0, 50) : '';
|
||||||
|
const category = emoji.hasEmoji(ticket.category.emoji) ? emoji.get(ticket.category.emoji) + ' ' + ticket.category.name : ticket.category.name;
|
||||||
|
return `${category} #${ticket.number} - ${date} ${topic}`;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} value
|
* @param {string} value
|
||||||
* @param {*} comamnd
|
* @param {*} comamnd
|
||||||
@ -19,7 +26,6 @@ module.exports = class ReferencesCompleter extends Autocompleter {
|
|||||||
async run(value, comamnd, interaction) {
|
async run(value, comamnd, interaction) {
|
||||||
/** @type {import("client")} */
|
/** @type {import("client")} */
|
||||||
const client = this.client;
|
const client = this.client;
|
||||||
const settings = await client.prisma.guild.findUnique({ where: { id: interaction.guild.id } });
|
|
||||||
const tickets = await client.prisma.ticket.findMany({
|
const tickets = await client.prisma.ticket.findMany({
|
||||||
include: {
|
include: {
|
||||||
category: {
|
category: {
|
||||||
@ -28,6 +34,7 @@ module.exports = class ReferencesCompleter extends Autocompleter {
|
|||||||
name: true,
|
name: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
guild: true,
|
||||||
},
|
},
|
||||||
where: {
|
where: {
|
||||||
createdById: interaction.user.id,
|
createdById: interaction.user.id,
|
||||||
@ -35,23 +42,14 @@ module.exports = class ReferencesCompleter extends Autocompleter {
|
|||||||
open: false,
|
open: false,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const options = value ? tickets.filter(t =>
|
const options = value ? tickets.filter(t => this.format(t).match(new RegExp(value, 'i'))) : tickets;
|
||||||
String(t.number).match(new RegExp(value, 'i')) ||
|
|
||||||
t.topic?.match(new RegExp(value, 'i')) ||
|
|
||||||
new Date(t.createdAt).toLocaleString(settings.locale, { dateStyle: 'short' })?.match(new RegExp(value, 'i')),
|
|
||||||
) : tickets;
|
|
||||||
await interaction.respond(
|
await interaction.respond(
|
||||||
options
|
options
|
||||||
.slice(0, 25)
|
.slice(0, 25)
|
||||||
.map(t => {
|
.map(t => ({
|
||||||
const date = new Date(t.createdAt).toLocaleString(settings.locale, { dateStyle: 'short' });
|
name: this.format(t),
|
||||||
const topic = t.topic ? '| ' + decrypt(t.topic).substring(0, 50) : '';
|
value: t.id,
|
||||||
const category = emoji.hasEmoji(t.category.emoji) ? emoji.get(t.category.emoji) + ' ' + t.category.name : t.category.name;
|
})),
|
||||||
return {
|
|
||||||
name: `${category} #${t.number} - ${date} ${topic}`,
|
|
||||||
value: t.id,
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
@ -11,6 +11,13 @@ module.exports = class TicketCompleter extends Autocompleter {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
format(ticket) {
|
||||||
|
const date = new Date(ticket.createdAt).toLocaleString(ticket.guild.locale, { dateStyle: 'short' });
|
||||||
|
const topic = ticket.topic ? '| ' + decrypt(ticket.topic).substring(0, 50) : '';
|
||||||
|
const category = emoji.hasEmoji(ticket.category.emoji) ? emoji.get(ticket.category.emoji) + ' ' + ticket.category.name : ticket.category.name;
|
||||||
|
return `${category} #${ticket.number} - ${date} ${topic}`;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} value
|
* @param {string} value
|
||||||
* @param {*} command
|
* @param {*} command
|
||||||
@ -19,7 +26,6 @@ module.exports = class TicketCompleter extends Autocompleter {
|
|||||||
async run(value, command, interaction) {
|
async run(value, command, interaction) {
|
||||||
/** @type {import("client")} */
|
/** @type {import("client")} */
|
||||||
const client = this.client;
|
const client = this.client;
|
||||||
const settings = await client.prisma.guild.findUnique({ where: { id: interaction.guild.id } });
|
|
||||||
const tickets = await client.prisma.ticket.findMany({
|
const tickets = await client.prisma.ticket.findMany({
|
||||||
include: {
|
include: {
|
||||||
category: {
|
category: {
|
||||||
@ -28,6 +34,7 @@ module.exports = class TicketCompleter extends Autocompleter {
|
|||||||
name: true,
|
name: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
guild: true,
|
||||||
},
|
},
|
||||||
where: {
|
where: {
|
||||||
createdById: interaction.user.id,
|
createdById: interaction.user.id,
|
||||||
@ -35,23 +42,14 @@ module.exports = class TicketCompleter extends Autocompleter {
|
|||||||
open: ['add', 'close', 'force-close', 'remove'].includes(command.name), // false for `new`, `transcript` etc
|
open: ['add', 'close', 'force-close', 'remove'].includes(command.name), // false for `new`, `transcript` etc
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const options = value ? tickets.filter(t =>
|
const options = value ? tickets.filter(t => this.format(t).match(new RegExp(value, 'i'))) : tickets;
|
||||||
String(t.number).match(new RegExp(value, 'i')) ||
|
|
||||||
t.topic?.match(new RegExp(value, 'i')) ||
|
|
||||||
new Date(t.createdAt).toLocaleString(settings.locale, { dateStyle: 'short' })?.match(new RegExp(value, 'i')),
|
|
||||||
) : tickets;
|
|
||||||
await interaction.respond(
|
await interaction.respond(
|
||||||
options
|
options
|
||||||
.slice(0, 25)
|
.slice(0, 25)
|
||||||
.map(t => {
|
.map(t => ({
|
||||||
const date = new Date(t.createdAt).toLocaleString(settings.locale, { dateStyle: 'short' });
|
name: this.format(t),
|
||||||
const topic = t.topic ? '| ' + decrypt(t.topic).substring(0, 50) : '';
|
value: t.id,
|
||||||
const category = emoji.hasEmoji(t.category.emoji) ? emoji.get(t.category.emoji) + ' ' + t.category.name : t.category.name;
|
})),
|
||||||
return {
|
|
||||||
name: `${category} #${t.number} - ${date} ${topic}`,
|
|
||||||
value: t.id,
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
@ -1,5 +1,12 @@
|
|||||||
const { SlashCommand } = require('@eartharoid/dbf');
|
const { SlashCommand } = require('@eartharoid/dbf');
|
||||||
const { ApplicationCommandOptionType } = require('discord.js');
|
const { ApplicationCommandOptionType } = require('discord.js');
|
||||||
|
const fs = require('fs');
|
||||||
|
const { join } = require('path');
|
||||||
|
const Mustache = require('mustache');
|
||||||
|
const { AttachmentBuilder } = require('discord.js');
|
||||||
|
const Cryptr = require('cryptr');
|
||||||
|
const { decrypt } = new Cryptr(process.env.ENCRYPTION_KEY);
|
||||||
|
const pad = require('pad');
|
||||||
|
|
||||||
module.exports = class TranscriptSlashCommand extends SlashCommand {
|
module.exports = class TranscriptSlashCommand extends SlashCommand {
|
||||||
constructor(client, options) {
|
constructor(client, options) {
|
||||||
@ -41,7 +48,116 @@ module.exports = class TranscriptSlashCommand extends SlashCommand {
|
|||||||
nameLocalizations,
|
nameLocalizations,
|
||||||
options: opts,
|
options: opts,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Mustache.escape = text => text; // don't HTML-escape
|
||||||
|
this.template = fs.readFileSync(
|
||||||
|
join('./user/templates/', this.client.config.templates.transcript),
|
||||||
|
{ encoding: 'utf8' },
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async run(interaction) { }
|
async fillTemplate(ticketId) {
|
||||||
|
/** @type {import("client")} */
|
||||||
|
const client = this.client;
|
||||||
|
const ticket = await client.prisma.ticket.findUnique({
|
||||||
|
include: {
|
||||||
|
archivedChannels: true,
|
||||||
|
archivedMessages: {
|
||||||
|
orderBy: { createdAt: 'asc' },
|
||||||
|
where: { external: false },
|
||||||
|
},
|
||||||
|
archivedRoles: true,
|
||||||
|
archivedUsers: true,
|
||||||
|
category: true,
|
||||||
|
claimedBy: true,
|
||||||
|
closedBy: true,
|
||||||
|
createdBy: true,
|
||||||
|
feedback: true,
|
||||||
|
guild: true,
|
||||||
|
questionAnswers: true,
|
||||||
|
},
|
||||||
|
where: { id: ticketId },
|
||||||
|
});
|
||||||
|
if (!ticket) throw new Error(`Ticket ${ticketId} does not exist`);
|
||||||
|
|
||||||
|
ticket.claimedBy = ticket.archivedUsers.find(u => u.userId === ticket.claimedById);
|
||||||
|
ticket.closedBy = ticket.archivedUsers.find(u => u.userId === ticket.closedById);
|
||||||
|
ticket.createdBy = ticket.archivedUsers.find(u => u.userId === ticket.createdById);
|
||||||
|
|
||||||
|
if (ticket.closedReason) ticket.closedReason = decrypt(ticket.closedReason);
|
||||||
|
if (ticket.feedback?.comment) ticket.feedback.comment = decrypt(ticket.feedback.comment);
|
||||||
|
if (ticket.topic) ticket.topic = decrypt(ticket.topic);
|
||||||
|
|
||||||
|
ticket.archivedUsers.forEach((user, i) => {
|
||||||
|
if (user.displayName) user.displayName = decrypt(user.displayName);
|
||||||
|
user.username = decrypt(user.username);
|
||||||
|
ticket.archivedUsers[i] = user;
|
||||||
|
});
|
||||||
|
|
||||||
|
ticket.archivedMessages.forEach((message, i) => {
|
||||||
|
message.author = ticket.archivedUsers.find(u => u.userId === message.authorId);
|
||||||
|
message.content = JSON.parse(decrypt(message.content));
|
||||||
|
message.text = message.content.content?.replace(/\n/g, '\n\t') ?? '';
|
||||||
|
message.content.attachments?.forEach(a => (message.text += '\n\t' + a.url));
|
||||||
|
message.content.embeds?.forEach(() => (message.text += '\n\t[embedded content]'));
|
||||||
|
message.number = 'M' + pad(String(ticket.archivedMessages.length).length, i + 1, '0');
|
||||||
|
ticket.archivedMessages[i] = message;
|
||||||
|
});
|
||||||
|
|
||||||
|
ticket.pinnedMessageIds = ticket.pinnedMessageIds.map(id => ticket.archivedMessages.find(message => message.id === id)?.number);
|
||||||
|
|
||||||
|
const channelName = ticket.category.channelName
|
||||||
|
.replace(/{+\s?(user)?name\s?}+/gi, ticket.createdBy?.username)
|
||||||
|
.replace(/{+\s?(nick|display)(name)?\s?}+/gi, ticket.createdBy?.displayName)
|
||||||
|
.replace(/{+\s?num(ber)?\s?}+/gi, ticket.number);
|
||||||
|
const fileName = `${channelName}.${this.client.config.templates.transcript.split('.').slice(-1)[0]}`;
|
||||||
|
const transcript = Mustache.render(this.template, {
|
||||||
|
channelName,
|
||||||
|
closedAtFull: function () {
|
||||||
|
return new Intl.DateTimeFormat([ticket.guild.locale, 'en-GB'], {
|
||||||
|
dateStyle: 'full',
|
||||||
|
timeStyle: 'long',
|
||||||
|
timeZone: 'Etc/UTC',
|
||||||
|
}).format(this.closedAt);
|
||||||
|
},
|
||||||
|
createdAtFull: function () {
|
||||||
|
return new Intl.DateTimeFormat([ticket.guild.locale, 'en-GB'], {
|
||||||
|
dateStyle: 'full',
|
||||||
|
timeStyle: 'long',
|
||||||
|
timeZone: 'Etc/UTC',
|
||||||
|
}).format(this.createdAt);
|
||||||
|
},
|
||||||
|
createdAtTimestamp: function () {
|
||||||
|
return new Intl.DateTimeFormat([ticket.guild.locale, 'en-GB'], {
|
||||||
|
dateStyle: 'short',
|
||||||
|
timeStyle: 'long',
|
||||||
|
timeZone: 'Etc/UTC',
|
||||||
|
}).format(this.createdAt);
|
||||||
|
},
|
||||||
|
guildName: client.guilds.cache.get(ticket.guildId)?.name,
|
||||||
|
pinned: ticket.pinnedMessageIds.join(', '),
|
||||||
|
ticket,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
fileName,
|
||||||
|
transcript,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {import("discord.js").ChatInputCommandInteraction} interaction
|
||||||
|
*/
|
||||||
|
async run(interaction) {
|
||||||
|
await interaction.deferReply({ ephemeral: true });
|
||||||
|
const {
|
||||||
|
fileName,
|
||||||
|
transcript,
|
||||||
|
} = await this.fillTemplate(interaction.options.getString('ticket', true));
|
||||||
|
const attachment = new AttachmentBuilder()
|
||||||
|
.setFile(Buffer.from(transcript))
|
||||||
|
.setName(fileName);
|
||||||
|
await interaction.editReply({ files: [attachment] });
|
||||||
|
// TODO: add portal link
|
||||||
|
}
|
||||||
};
|
};
|
26
user/templates/transcript.md
Normal file
26
user/templates/transcript.md
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#{{ channelName }} ticket transcript
|
||||||
|
---
|
||||||
|
ID: {{ ticket.id }}
|
||||||
|
Number: {{ guildName }} #{{ ticket.number }}
|
||||||
|
Topic: {{ #ticket.topic }}{{ . }}{{ /ticket.topic }}
|
||||||
|
Created on: {{ #ticket }}{{ createdAtFull }}{{ /ticket }}
|
||||||
|
Created by: {{ #ticket.createdBy }}"{{ displayName }}" @{{ username }}#{{ discriminator }}{{ /ticket.createdBy }}
|
||||||
|
Closed on: {{ #ticket }}{{ closedAtFull }}{{ /ticket }}
|
||||||
|
Closed by: {{ #ticket.closedBy }}"{{ displayName }}" @{{ username }}#{{ discriminator }}{{ /ticket.closedBy }}{{ ^ticket.closedBy }}(automated){{ /ticket.closedBy }}
|
||||||
|
Closed because: {{ #ticket.closedReason }}{{ ticket.closedReason }}{{ /ticket.closedReason }}{{ ^ticket.closedReason }}(no reason){{ /ticket.closedReason }}
|
||||||
|
Claimed by: {{ #ticket.claimedBy }}"{{ displayName }}" @{{ username }}#{{ discriminator }}{{ /ticket.claimedBy }}{{ ^ticket.claimedBy }}(not claimed){{ /ticket.claimedBy }}
|
||||||
|
{{ #ticket.feedback }}
|
||||||
|
Feedback:
|
||||||
|
Rating: {{ rating }}/5
|
||||||
|
Comment: {{ comment }}{{ ^comment }}(no comment){{ /comment }}
|
||||||
|
{{ /ticket.feedback }}
|
||||||
|
Participants:
|
||||||
|
{{ #ticket.archivedUsers }}
|
||||||
|
- "{{ displayName }}" @{{ username }}#{{ discriminator }} ({{ userId }})
|
||||||
|
{{ /ticket.archivedUsers }}
|
||||||
|
Pinned messages: {{ #pinned }}{{ . }}{{ /pinned }}
|
||||||
|
---
|
||||||
|
|
||||||
|
{{ #ticket.archivedMessages }}
|
||||||
|
<{{ number }}> [{{ createdAtTimestamp }}] {{author.displayName}}: {{ text }}
|
||||||
|
{{ /ticket.archivedMessages }}
|
Loading…
Reference in New Issue
Block a user