JaBa/commands/Tickets/closeticket.js

143 lines
5.2 KiB
JavaScript
Raw Normal View History

2023-07-07 17:34:23 +05:00
const { SlashCommandBuilder, PermissionsBitField, EmbedBuilder, ButtonBuilder, ButtonStyle, ActionRowBuilder } = require("discord.js");
const BaseCommand = require("../../base/BaseCommand");
class CloseTicket extends BaseCommand {
/**
*
* @param {import("../../base/JaBa")} client
*/
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"),
})
.setDMPermission(false)
.setDefaultMemberPermissions(PermissionsBitField.Flags.ManageMessages),
aliases: [],
dirname: __dirname,
ownerOnly: false,
});
}
/**
*
* @param {import("../../base/JaBa")} client
*/
async onLoad() {
//...
}
/**
*
* @param {import("../../base/JaBa")} client
* @param {import("discord.js").ChatInputCommandInteraction} interaction
* @param {Object} data
*/
async execute(client, interaction, data) {
await interaction.deferReply();
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 = new EmbedBuilder()
.setTitle(interaction.translate("tickets/closeticket:CLOSING_TITLE"))
.setDescription(interaction.translate("tickets/closeticket:CLOSING_DESC"))
.addFields(
{
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
},
)
.setColor(client.config.embed.color)
.setFooter(client.config.embed.footer)
.setTimestamp();
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.reply({
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") {
const transcriptionLogs = data.guildData.plugins.tickets.transcriptionLogs,
ticketLogs = data.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-07-10 20:13:23 +05:00
transcript += `[${client.functions.printDate(client, message.createdTimestamp, null, data.guildData.language)}] ${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 = new EmbedBuilder()
.setTitle(interaction.translate("tickets/createticketembed:TICKET_CLOSED_TITLE"))
.setDescription(`${interaction.user.toString()} (${interaction.channel.toString()})`)
.setColor(client.config.embed.color)
.setFooter(client.config.embed.footer)
.setTimestamp();
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) {
await interaction.reply({ content: interaction.translate("misc:CANT_DM"), ephemeral: true });
2023-07-07 17:34:23 +05:00
}
2023-08-16 20:46:27 +05:00
const member = interaction.guild.members.cache.find(u => u.user.username.includes(interaction.channel.name.split("-")[0]));
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
}
});
const ticketLogs = data.guildData.plugins.tickets.ticketLogs;
const logEmbed = new EmbedBuilder()
.setTitle(interaction.translate("tickets/closeticket:CLOSED_TITLE"))
.addFields(
{
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
},
)
.setColor(client.config.embed.color)
.setFooter(client.config.embed.footer)
.setTimestamp();
if (ticketLogs) interaction.guild.channels.cache.get(ticketLogs).send({ embeds: [logEmbed] });
}
}
module.exports = CloseTicket;