UNPKG

djs-systems

Version:

The simplest way to build complex Discord bots.

347 lines (346 loc) â€ĸ 18.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.manageTicket = void 0; const discord_js_1 = require("discord.js"); const misc_1 = require("../misc"); const error_1 = require("../error"); // ------------------------------ // ------ F U N C T I O N ------- // ------------------------------ /** * A Ticket Handler for **simplydjs ticket system.** * @param button * @param options * @link `Documentation:` https://simplyd.js.org/docs/handler/manageTicket * @example simplydjs.manageTicket(interaction) */ async function manageTicket(button, options = {}) { return new Promise(async (resolve) => { const { client } = button; if (button.isButton()) { try { const member = button.member; // ------------------------------ // ---- T I C K E T - S Y S ----- // ------------------------------ if (button.customId === 'create_ticket') { await button.deferReply({ ephemeral: true }); let name = options?.ticketname || `ticket_{tag}`; name = name .replaceAll('{username}', member.user.username) .replaceAll('{tag}', member.user.username) .replaceAll('{id}', member.user.id); const topic = `Ticket has been opened by <@${member.user.id}>`; const existing = button.guild.channels.cache.find((ch) => ch.topic === topic); if (existing) { await button.editReply({ content: `You have an existing ticket opened (${existing.toString()}). Close it before creating a new one.` }); } else if (!existing) { let chparent; const category = button.guild.channels.cache.get(options?.category); if (!category) { chparent = null; } chparent = category; const pingRolePermissions = []; const roles = []; options?.pingRoles?.forEach((r) => { roles.push(r); pingRolePermissions.push({ id: r, allow: [ discord_js_1.PermissionFlagsBits.ViewChannel, discord_js_1.PermissionFlagsBits.SendMessages, discord_js_1.PermissionFlagsBits.ReadMessageHistory ] }); }); const channel = await button.guild.channels.create({ name: name, type: discord_js_1.ChannelType.GuildText, topic: topic, parent: chparent, nsfw: false, permissionOverwrites: [ ...pingRolePermissions, { id: button.guild.roles.everyone, deny: [ discord_js_1.PermissionFlagsBits.ViewChannel, discord_js_1.PermissionFlagsBits.SendMessages, discord_js_1.PermissionFlagsBits.ReadMessageHistory ] //Deny permissions }, { id: client.user.id, allow: [ discord_js_1.PermissionFlagsBits.ViewChannel, discord_js_1.PermissionFlagsBits.SendMessages, discord_js_1.PermissionFlagsBits.ReadMessageHistory ] }, { id: member, allow: [ discord_js_1.PermissionFlagsBits.ViewChannel, discord_js_1.PermissionFlagsBits.SendMessages, discord_js_1.PermissionFlagsBits.ReadMessageHistory ] } ] }); await button.editReply({ content: `đŸŽĢ Opened your support ticket in ${channel.toString()}.` }); const embed = new discord_js_1.EmbedBuilder() .setTitle(options?.embed?.title || 'Ticket Created') .setDescription((options?.embed?.description || `Ticket has been raised by {user}. The support will reach you shortly.\n\n**User ID**: \`{id}\` | **User Tag**: \`{tag}\``) .replaceAll('{user}', member.user.toString()) .replaceAll('{tag}', member.user.username) .replaceAll('{id}', member.user.id) .replaceAll('{guild}', button.guild.name)) .setThumbnail(options.embed?.thumbnail || button.guild.iconURL()) .setTimestamp() .setColor(options?.embed?.color || (0, misc_1.toRgb)('#406DBC')) .setFooter(options?.embed?.footer ? options?.embed?.footer : { text: 'ÂŠī¸ Rahuletto. npm i simply-djs', iconURL: 'https://i.imgur.com/XFUIwPh.png' }); if (options?.embed?.fields) embed.setFields(options?.embed?.fields); if (options?.embed?.author) embed.setAuthor(options?.embed?.author); if (options?.embed?.image) embed.setImage(options?.embed?.image); if (options?.embed?.thumbnail) embed.setThumbnail(options?.embed?.thumbnail); if (options?.embed?.timestamp) embed.setTimestamp(options?.embed?.timestamp); if (options?.embed?.title) embed.setTitle(options?.embed?.title); if (options?.embed?.url) embed.setURL(options?.embed?.url); if (options?.buttons?.close?.style) options.buttons.close.style = (0, misc_1.toButtonStyle)(options?.buttons?.close?.style); if (options?.buttons?.reopen?.style) options.buttons.reopen.style = (0, misc_1.toButtonStyle)(options?.buttons?.reopen?.style); if (options?.buttons?.delete?.style) options.buttons.delete.style = (0, misc_1.toButtonStyle)(options?.buttons?.delete?.style); if (options?.buttons?.transcript?.style) options.buttons.transcript.style = (0, misc_1.toButtonStyle)(options?.buttons?.transcript?.style); const close = new discord_js_1.ButtonBuilder() .setStyle(options?.buttons?.close?.style || discord_js_1.ButtonStyle.Danger) .setEmoji(options?.buttons?.close?.emoji || '🔒') .setLabel(options?.buttons?.close?.label || 'Close') .setCustomId('close_ticket'); const row = new discord_js_1.ActionRowBuilder().addComponents([ close ]); channel .send({ content: `Here is your ticket ${member.user.toString()}. | ${roles.join(',')}`, embeds: [embed], components: [row] }) .then(async (msg) => { await msg.pin(); }); } } else if (button.customId === 'close_ticket') { await button.reply({ content: 'Locking the channel.', ephemeral: true }); button.channel.permissionOverwrites .edit(member, { SendMessages: false }) .catch(() => { }); const deleteBtn = new discord_js_1.ButtonBuilder() .setStyle(options?.buttons?.delete?.style || discord_js_1.ButtonStyle.Danger) .setEmoji(options?.buttons?.delete?.emoji || '❌') .setLabel(options?.buttons?.delete?.label || 'Delete') .setCustomId('delete_ticket'); const reopenBtn = new discord_js_1.ButtonBuilder() .setStyle(options?.buttons?.reopen?.style || discord_js_1.ButtonStyle.Success) .setEmoji(options?.buttons?.reopen?.emoji || '🔓') .setLabel(options?.buttons?.delete?.label || 'Reopen') .setCustomId('open_ticket'); const transcriptBtn = new discord_js_1.ButtonBuilder() .setStyle(options?.buttons?.transcript?.style || discord_js_1.ButtonStyle.Primary) .setEmoji(options?.buttons?.transcript?.emoji || '📜') .setLabel(options?.buttons?.transcript?.label || 'Transcript') .setCustomId('tr_ticket'); const row = new discord_js_1.ActionRowBuilder().addComponents([ reopenBtn, deleteBtn, transcriptBtn ]); await button.message.edit({ components: [row] }); } else if (button.customId === 'tr_ticket') { await button.deferReply({ ephemeral: true }); let messages = await button.channel.messages.fetch({ limit: 100 }); messages = messages.sort((a, b) => a.createdTimestamp - b.createdTimestamp); const response = []; messages.forEach((m) => { if (m.author.bot) return; const attachment = m.attachments.first(); const url = attachment ? attachment.url : null; if (url !== null) { m.content = url; } response.push(`${m.author.username} (ID: ${m.author.id}) => ${m.content}`); }); await button.editReply({ content: 'Collecting messages to create logs' }); let user = button.channel.topic .replace(`Ticket has been opened by <@`, '') .replace('>', ''); user = await button.guild.members.fetch(user); const attach = new discord_js_1.AttachmentBuilder(Buffer.from(response.join(`\n`), 'utf-8'), { name: `${user.user.username}.txt` }); setTimeout(async () => { await button.followUp({ content: 'Done. Generated the logs', files: [attach], embeds: [], ephemeral: false }); }, (0, misc_1.ms)('3s')); } else if (button.customId === 'delete_ticket') { await button.deferReply({ ephemeral: false }); const del = new discord_js_1.ButtonBuilder() .setCustomId('yes_delete') .setLabel('Delete') .setStyle(discord_js_1.ButtonStyle.Danger); const cancel = new discord_js_1.ButtonBuilder() .setCustomId('cancel') .setLabel('Cancel') .setStyle(discord_js_1.ButtonStyle.Success); const row = new discord_js_1.ActionRowBuilder().addComponents([ del, cancel ]); button.editReply({ content: 'Are you sure ?? This process is not reversible !', components: [row] }); } else if (button.customId === 'yes_delete') { await button.message.edit({ content: 'Deleting the channel..', embeds: [], components: [] }); let messages = await button.channel.messages.fetch({ limit: 100 }); const response = []; messages = messages.sort((a, b) => a.createdTimestamp - b.createdTimestamp); messages.forEach((m) => { if (m.author.bot) return; const attachment = m.attachments.first(); const url = attachment ? attachment.url : null; if (url !== null) { m.content = url; } response.push(`${m.author.username} (ID: ${m.author.id}) => ${m.content}`); }); let user = button.channel.topic .replace(`Ticket has been opened by <@`, '') .replace('>', ''); user = await button.guild.members.fetch(user); const attach = new discord_js_1.AttachmentBuilder(Buffer.from(response.join(`\n`), 'utf-8'), { name: `${user.user.username}.txt` }); resolve({ type: 'Delete', channelId: button.channel.id, user: user.user, data: attach }); if (options?.logChannelId) { const ch = await client.channels.cache.get(options?.logChannelId); if (ch) { const log = new discord_js_1.EmbedBuilder() .setTitle('Ticket deleted') .setDescription(`Ticket with the name: \`${button.channel.name}\` got deleted. Opened by: ${user.user}.`) .setTimestamp() .setColor('Red'); await ch.send({ embeds: [log], files: [attach] }); } else return; } setTimeout(async () => { await button.channel.delete(); }, (0, misc_1.ms)('5s')); } else if (button.customId === 'cancel') { await button.deferUpdate(); button.message.edit({ content: 'You cancelled to delete', components: [] }); } else if (button.customId === 'open_ticket') { await button.reply({ content: 'Unlocking the channel.', ephemeral: true }); button.channel.permissionOverwrites .edit(member, { SendMessages: true }) .catch(() => { }); const close = new discord_js_1.ButtonBuilder() .setStyle(options?.buttons?.close?.style || discord_js_1.ButtonStyle.Danger) .setEmoji(options?.buttons?.close?.emoji || '🔒') .setLabel(options?.buttons?.close?.label || 'Close') .setCustomId('close_ticket'); const row = new discord_js_1.ActionRowBuilder().addComponents([ close ]); button.message.edit({ components: [row] }); } } catch (err) { if (options?.strict) throw new error_1.SimplyError({ function: 'manageTicket', title: 'An Error occured when running the function ', tip: err.stack }); else console.log(`SimplyError - manageTicket | Error: ${err.stack}`); } } else return; }); } exports.manageTicket = manageTicket;