JaBa/commands/Tickets/closeticket.js

130 lines
4.8 KiB
JavaScript
Raw Normal View History

2024-09-14 19:52:56 +05:00
const { SlashCommandBuilder, PermissionsBitField, ButtonBuilder, ButtonStyle, ActionRowBuilder, InteractionContextType } = require("discord.js");
2023-07-07 17:34:23 +05:00
const BaseCommand = require("../../base/BaseCommand");
class CloseTicket extends BaseCommand {
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../../base/Client")} client
2023-07-07 17:34:23 +05:00
*/
constructor(client) {
super({
command: new SlashCommandBuilder()
.setName("closeticket")
.setDescription(client.translate("tickets/closeticket:DESCRIPTION"))
.setDescriptionLocalizations({
uk: client.translate("tickets/closeticket:DESCRIPTION", null, "uk-UA"),
ru: client.translate("tickets/closeticket:DESCRIPTION", null, "ru-RU"),
})
2024-09-14 19:52:56 +05:00
.setContexts([InteractionContextType.Guild])
2023-07-07 17:34:23 +05:00
.setDefaultMemberPermissions(PermissionsBitField.Flags.ManageMessages),
dirname: __dirname,
ownerOnly: false,
});
}
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../../base/Client")} client
2023-07-07 17:34:23 +05:00
* @param {import("discord.js").ChatInputCommandInteraction} interaction
*/
2024-02-09 23:26:57 +05:00
async execute(client, interaction) {
2023-07-07 17:34:23 +05:00
await interaction.deferReply();
2024-02-09 23:26:57 +05:00
const guildData = interaction.data.guild;
if (!interaction.channel.name.includes("support")) return interaction.error("tickets/adduser:NOT_TICKET", null, { ephemeral: true, edit: true });
2023-07-07 17:34:23 +05:00
const embed = client.embed({
title: interaction.translate("tickets/closeticket:CLOSING_TITLE"),
description: interaction.translate("tickets/closeticket:CLOSING_DESC"),
fields: [
2023-07-07 17:34:23 +05:00
{
name: interaction.translate("common:TICKET"),
value: interaction.channel.name,
},
{
name: interaction.translate("tickets/closeticket:CLOSING_BY"),
value: interaction.user.getUsername(),
2023-07-07 17:34:23 +05:00
},
],
});
2023-07-07 17:34:23 +05:00
const button = new ButtonBuilder().setCustomId("cancel_closing").setLabel(interaction.translate("common:CANCEL")).setStyle(ButtonStyle.Danger);
2023-07-07 17:34:23 +05:00
const row = new ActionRowBuilder().addComponents(button);
await interaction.editReply({
2023-07-07 17:34:23 +05:00
embeds: [embed],
components: [row],
});
const filter = i => i.customId === "cancel_closing";
2023-07-07 17:34:23 +05:00
const collector = interaction.channel.createMessageComponentCollector({ filter, time: 5000 });
collector.on("collect", async i => {
await i.update({ content: interaction.translate("tickets/closeticket:CLOSING_CANCELED"), components: [] });
collector.stop("canceled");
});
collector.on("end", async (_, reason) => {
if (reason !== "canceled") {
2024-02-09 23:26:57 +05:00
const transcriptionLogs = guildData.plugins.tickets.transcriptionLogs,
ticketLogs = guildData.plugins.tickets.ticketLogs;
const reversedMessages = (await interaction.channel.messages.fetch()).filter(m => !m.author.bot);
2023-07-07 17:34:23 +05:00
const messages = Array.from(reversedMessages.values()).reverse();
let transcript = "---- TICKET CREATED ----\n";
2023-07-07 17:34:23 +05:00
messages.forEach(message => {
2023-12-14 18:58:19 +05:00
transcript += `[${client.functions.printDate(client, message.createdTimestamp, null, interaction.getLocale())}] ${message.author.getUsername()}: ${message.content}\n`;
2023-07-07 17:34:23 +05:00
});
transcript += "---- TICKET CLOSED ----";
if (transcriptionLogs) interaction.guild.channels.cache.get(transcriptionLogs).send({ content: interaction.translate("tickets/closeticket:TRANSCRIPT", { channel: `<#${interaction.channelId}>` }), files: [{ attachment: Buffer.from(transcript), name: `${interaction.channel.name}.txt` }] });
if (ticketLogs) {
const logChannel = interaction.guild.channels.cache.get(ticketLogs);
const logEmbed = client.embed({
title: interaction.translate("tickets/createticketembed:TICKET_CLOSED_TITLE"),
description: `${interaction.user.toString()} (${interaction.channel.toString()})`,
});
logChannel.send({ embeds: [logEmbed] });
}
interaction.channel.send("Closed!");
2023-07-07 17:34:23 +05:00
try {
await interaction.user.send({
content: interaction.translate("tickets/closeticket:TRANSCRIPT", { channel: interaction.channel.name }),
files: [{ attachment: Buffer.from(transcript), name: `${interaction.channel.name}.txt` }],
});
} catch (e) {
interaction.reply({ content: interaction.translate("misc:CANT_DM"), ephemeral: true });
2023-07-07 17:34:23 +05:00
}
const member = interaction.guild.members.cache.find(u => u.user.id === interaction.channel.topic);
2023-08-08 22:22:06 +05:00
await interaction.channel.permissionOverwrites.edit(member, { ViewChannel: false, SendMessages: null });
2023-08-08 17:28:03 +05:00
await interaction.channel.setName(`${interaction.channel.name}-closed`);
2023-07-07 17:34:23 +05:00
}
});
2024-02-09 23:26:57 +05:00
const ticketLogs = guildData.plugins.tickets.ticketLogs;
const logEmbed = client.embed({
title: interaction.translate("tickets/closeticket:CLOSED_TITLE"),
fields: [
2023-07-07 17:34:23 +05:00
{
name: interaction.translate("common:TICKET"),
value: interaction.channel.name,
},
{
name: interaction.translate("tickets/closeticket:CLOSING_BY"),
value: interaction.user.getUsername(),
2023-07-07 17:34:23 +05:00
},
],
});
2023-07-07 17:34:23 +05:00
if (ticketLogs) interaction.guild.channels.cache.get(ticketLogs).send({ embeds: [logEmbed] });
}
}
module.exports = CloseTicket;