JaBa/commands/Tickets/createticketembed.js

232 lines
9.5 KiB
JavaScript
Raw Normal View History

const { SlashCommandBuilder, PermissionsBitField, EmbedBuilder, ButtonBuilder, ButtonStyle, ActionRowBuilder, ChannelType } = require("discord.js");
const BaseCommand = require("../../base/BaseCommand");
class CreateTicketEmbed extends BaseCommand {
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../../base/Client")} client
*/
constructor(client) {
super({
command: new SlashCommandBuilder()
.setName("createticketembed")
.setDescription(client.translate("tickets/createticketembed:DESCRIPTION"))
.setDescriptionLocalizations({
uk: client.translate("tickets/createticketembed:DESCRIPTION", null, "uk-UA"),
ru: client.translate("tickets/createticketembed:DESCRIPTION", null, "ru-RU"),
})
.setDMPermission(false)
.setDefaultMemberPermissions(PermissionsBitField.Flags.ManageGuild),
aliases: [],
dirname: __dirname,
ownerOnly: false,
});
}
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../../base/Client")} client
*/
async onLoad(client) {
client.on("interactionCreate", async interaction => {
if (!interaction.isButton()) return;
2023-10-15 15:05:10 +05:00
const guildData = await client.findOrCreateGuild(interaction.guildId);
const ticketsCategory = guildData.plugins.tickets.ticketsCategory,
ticketLogs = guildData.plugins.tickets.ticketLogs,
transcriptionLogs = guildData.plugins.tickets.transcriptionLogs;
if (interaction.isButton()) {
const button = interaction.component;
if (button.customId === "support_ticket") {
2023-11-30 17:36:02 +05:00
if (interaction.guild.channels.cache.get(ticketsCategory).children.cache.size >= 50) {
const sorted = interaction.guild.channels.cache.get(ticketsCategory).children.cache.sort((ch1, ch2) => ch1.createdTimestamp - ch2.createdTimestamp);
await sorted.first().delete();
}
if (guildData.plugins.tickets.count === undefined) guildData.plugins.tickets.count = 0;
guildData.plugins.tickets.count++;
2023-10-30 21:45:10 +05:00
guildData.markModified("plugins.tickets");
2023-10-17 08:33:41 +05:00
await guildData.save();
const channel = await interaction.guild.channels.create({
name: `${interaction.user.username}-support-${guildData.plugins.tickets.count}`,
topic: interaction.user.id,
type: ChannelType.GuildText,
parent: ticketsCategory,
permissionOverwrites: [
{
id: interaction.user.id,
allow: [PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.SendMessages],
},
{
id: interaction.guild.roles.everyone,
deny: [PermissionsBitField.Flags.ViewChannel, PermissionsBitField.Flags.SendMessages],
},
],
});
const logChannel = interaction.guild.channels.cache.get(ticketLogs);
const logEmbed = new EmbedBuilder()
.setTitle(interaction.translate("tickets/createticketembed:TICKET_CREATED_TITLE"))
.setDescription(`${interaction.user.toString()} (${channel.toString()})`)
.setColor(client.config.embed.color)
.setFooter(client.config.embed.footer)
.setTimestamp();
await logChannel.send({ embeds: [logEmbed] });
await interaction.success("tickets/createticketembed:TICKET_CREATED", {
channel: channel.toString(),
}, { ephemeral: true });
await channel.send(`<@${interaction.user.id}>`);
const embed = new EmbedBuilder()
.setTitle("Support Ticket")
.setAuthor({ name: interaction.user.getUsername(), iconURL: interaction.user.displayAvatarURL() })
.setDescription(interaction.translate("tickets/createticketembed:TICKET_CREATED_DESC"))
.setColor(client.config.embed.color)
.setFooter(client.config.embed.footer)
.setTimestamp();
const closeButton = new ButtonBuilder()
.setCustomId("close_ticket")
.setLabel(interaction.translate("tickets/closeticket:CLOSE_TICKET"))
.setStyle(ButtonStyle.Danger);
const transcriptButton = new ButtonBuilder()
.setCustomId("transcript_ticket")
.setLabel(interaction.translate("tickets/closeticket:TRANSCRIPT_TICKET"))
.setStyle(ButtonStyle.Secondary);
const row = new ActionRowBuilder().addComponents(closeButton, transcriptButton);
await channel.send({ embeds: [embed], components: [row] });
2023-11-19 22:43:17 +05:00
} else if (button.customId === "close_ticket") {
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(),
},
)
.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);
const row = new ActionRowBuilder().addComponents(button);
await interaction.reply({
embeds: [embed],
components: [row],
});
const filter = i => i.customId === "cancel_closing";
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 reversedMessages = (await interaction.channel.messages.fetch()).filter(m => !m.author.bot);
const messages = Array.from(reversedMessages.values()).reverse();
let transcript = "---- TICKET CREATED ----\n";
messages.forEach(message => {
2023-07-10 20:13:23 +05:00
transcript += `[${client.functions.printDate(client, message.createdTimestamp, null, interaction.guild.data.language)}] ${message.author.getUsername()}: ${message.content}\n`;
});
transcript += "---- TICKET CLOSED ----";
2023-11-24 22:09:17 +05:00
if (transcriptionLogs !== null) 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` }] });
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!");
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 });
}
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-11-19 22:43:17 +05:00
} else if (button.customId === "transcript_ticket") {
await interaction.deferUpdate();
const reversedMessages = (await interaction.channel.messages.fetch()).filter(m => !m.author.bot);
const messages = Array.from(reversedMessages.values()).reverse();
let transcript = "---- TICKET CREATED ----\n";
messages.forEach(message => {
2023-07-10 20:13:23 +05:00
transcript += `[${client.functions.printDate(client, message.createdTimestamp, null, interaction.guild.data.language)}] ${message.author.getUsername()}: ${message.content}\n`;
});
transcript += "---- TICKET CLOSED ----";
try {
await interaction.user.send({
content: interaction.translate("tickets/closeticket:TRANSCRIPT", { channel: `<#${interaction.channelId}>` }),
files: [{ attachment: Buffer.from(transcript), name: `${interaction.channel.name}.txt` }],
});
} catch (error) {
await interaction.followUp({ content: interaction.translate("misc:CANT_DM"), ephemeral: true });
}
}
}
});
}
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../../base/Client")} client
* @param {import("discord.js").ChatInputCommandInteraction} interaction
* @param {Object} data
*/
async execute(client, interaction, data) {
if (!data.guildData.plugins.tickets.ticketsCategory) return interaction.error("tickets/createticketembed:NO_CATEGORY");
await interaction.deferReply({ ephemeral: true });
const embed = new EmbedBuilder()
.setTitle(interaction.translate("tickets/createticketembed:TICKET_TITLE"))
.setDescription(interaction.translate("tickets/createticketembed:TICKET_DESC"))
.setColor(client.config.embed.color)
.setFooter(client.config.embed.footer);
const supportButton = new ButtonBuilder().setCustomId("support_ticket").setLabel(interaction.translate("tickets/createticketembed:TICKET_SUPPORT")).setStyle(ButtonStyle.Primary);
const row = new ActionRowBuilder().addComponents(supportButton);
await interaction.channel.send({ embeds: [embed], components: [row] });
interaction.success("tickets/createticketembed:SUCCESS", null, { edit: true });
}
}
module.exports = CreateTicketEmbed;