From e7d9d3558a420bab6941d571d5b9fca38cf40f40 Mon Sep 17 00:00:00 2001 From: o5pxels Date: Sun, 20 Dec 2020 19:49:41 -0600 Subject: [PATCH] Options for confirm on close/delete on/off for new --- src/commands/close.js | 203 +++++++++++++++++-------------- src/commands/delete.js | 161 ++++++++++++------------ src/commands/new.js | 6 +- src/events/messageReactionAdd.js | 2 +- user/config.js | 15 ++- 5 files changed, 207 insertions(+), 180 deletions(-) diff --git a/src/commands/close.js b/src/commands/close.js index eadb383..c859380 100644 --- a/src/commands/close.js +++ b/src/commands/close.js @@ -82,46 +82,115 @@ module.exports = { let pre = fs.existsSync(paths.text) || fs.existsSync(paths.log) ? `You will be able to view an archived version later with \`${config.prefix}transcript ${ticket.id}\`` : ''; - - let confirm = await message.channel.send( - new MessageEmbed() - .setColor(config.colour) - .setAuthor(message.author.username, message.author.displayAvatarURL()) - .setTitle('❔ Are you sure?') - .setDescription(`${pre}\n**React with ✅ to confirm.**`) - .setFooter(guild.name + ' | Expires in 15 seconds', guild.iconURL()) - ); - - await confirm.react('✅'); - - const collector = confirm.createReactionCollector( - (r, u) => r.emoji.name === '✅' && u.id === message.author.id, { - time: 15000 - }); - - collector.on('collect', async () => { - let users = []; - if (channel.id !== message.channel.id) { - channel.send( - new MessageEmbed() - .setColor(config.colour) - .setAuthor(message.author.username, message.author.displayAvatarURL()) - .setTitle('**Ticket closed**') - .setDescription(`Ticket closed by ${message.author}`) - .setFooter(guild.name, guild.iconURL()) - ); - } - - confirm.reactions.removeAll(); - confirm.edit( + if (config.commands.close.confirmation) { + let confirm = await message.channel.send( new MessageEmbed() .setColor(config.colour) .setAuthor(message.author.username, message.author.displayAvatarURL()) - .setTitle(`✅ **Ticket ${ticket.id} closed**`) - .setDescription('The channel will be automatically deleted in a few seconds, once the contents have been archived.') - .setFooter(guild.name, guild.iconURL()) + .setTitle('❔ Are you sure?') + .setDescription(`${pre}\n**React with ✅ to confirm.**`) + .setFooter(guild.name + ' | Expires in 15 seconds', guild.iconURL()) ); + await confirm.react('✅'); + + const collector = confirm.createReactionCollector( + (r, u) => r.emoji.name === '✅' && u.id === message.author.id, { + time: 15000 + }); + + collector.on('collect', async () => { + let users = []; + if (channel.id !== message.channel.id) { + channel.send( + new MessageEmbed() + .setColor(config.colour) + .setAuthor(message.author.username, message.author.displayAvatarURL()) + .setTitle('**Ticket closed**') + .setDescription(`Ticket closed by ${message.author}`) + .setFooter(guild.name, guild.iconURL()) + ); + } + + confirm.reactions.removeAll(); + confirm.edit( + new MessageEmbed() + .setColor(config.colour) + .setAuthor(message.author.username, message.author.displayAvatarURL()) + .setTitle(`✅ **Ticket ${ticket.id} closed**`) + .setDescription('The channel will be automatically deleted in a few seconds, once the contents have been archived.') + .setFooter(guild.name, guild.iconURL()) + ); + success = true; + closeTicket(); + if (config.commands.close.send_transcripts) { + sendTranscript(); + } + }); + + + collector.on('end', () => { + if (!success) { + confirm.reactions.removeAll(); + confirm.edit( + new MessageEmbed() + .setColor(config.err_colour) + .setAuthor(message.author.username, message.author.displayAvatarURL()) + .setTitle('❌ **Expired**') + .setDescription('You took too long to react; confirmation failed.') + .setFooter(guild.name, guild.iconURL())); + + message.delete({ + timeout: 10000 + }) + .then(() => confirm.delete()); + } + }); + } else { + success = true; + closeTicket(); + } + + async function closeTicket() { + // update database + ticket.update({ + open: false + }, { + where: { + channel: channel.id + } + }); + + // delete messages and channel + setTimeout(() => { + channel.delete(); + if (channel.id !== message.channel.id) + message.delete() + .then(() => confirm.delete()); + }, 5000); + + log.info(`${message.author.tag} closed a ticket (#ticket-${ticket.id})`); + + if (config.logs.discord.enabled) { + let embed = new MessageEmbed() + .setColor(config.colour) + .setAuthor(message.author.username, message.author.displayAvatarURL()) + .setTitle(`Ticket ${ticket.id} closed`) + .addField('Creator', `<@${ticket.creator}>`, true) + .addField('Closed by', message.author, true) + .setFooter(guild.name, guild.iconURL()) + .setTimestamp(); + + if (users.length > 1) + embed.addField('Members', users.map(u => `<@${u}>`).join('\n')); + + client.channels.cache.get(config.logs.discord.channel).send(embed); + } + } + if (config.commands.close.send_transcripts) { + sendTranscript(); + } + async function sendTranscript() { if (config.transcripts.text.enabled || config.transcripts.web.enabled) { let u = await client.users.fetch(ticket.creator); @@ -160,71 +229,17 @@ module.exports = { } res.embed = embed; - try { - dm.send(res); - if (config.transcripts.channel.length > 1) client.channels.cache.get(config.transcripts.channel).send(res); + if (success) { + dm.send(res); + if (config.transcripts.channel.length > 1) client.channels.cache.get(config.transcripts.channel).send(res); + } } catch (e) { message.channel.send('❌ Couldn\'t send DM or transcript log message'); } } } - - // update database - success = true; - ticket.update({ - open: false - }, { - where: { - channel: channel.id - } - }); - - // delete messages and channel - setTimeout(() => { - channel.delete(); - if (channel.id !== message.channel.id) - message.delete() - .then(() => confirm.delete()); - }, 5000); - - log.info(`${message.author.tag} closed a ticket (#ticket-${ticket.id})`); - - if (config.logs.discord.enabled) { - let embed = new MessageEmbed() - .setColor(config.colour) - .setAuthor(message.author.username, message.author.displayAvatarURL()) - .setTitle(`Ticket ${ticket.id} closed`) - .addField('Creator', `<@${ticket.creator}>`, true) - .addField('Closed by', message.author, true) - .setFooter(guild.name, guild.iconURL()) - .setTimestamp(); - - if (users.length > 1) - embed.addField('Members', users.map(u => `<@${u}>`).join('\n')); - - client.channels.cache.get(config.logs.discord.channel).send(embed); - } - }); - - - collector.on('end', () => { - if (!success) { - confirm.reactions.removeAll(); - confirm.edit( - new MessageEmbed() - .setColor(config.err_colour) - .setAuthor(message.author.username, message.author.displayAvatarURL()) - .setTitle('❌ **Expired**') - .setDescription('You took too long to react; confirmation failed.') - .setFooter(guild.name, guild.iconURL())); - - message.delete({ - timeout: 10000 - }) - .then(() => confirm.delete()); - } - }); + } } }; diff --git a/src/commands/delete.js b/src/commands/delete.js index 42d236a..8be94f9 100644 --- a/src/commands/delete.js +++ b/src/commands/delete.js @@ -64,7 +64,7 @@ module.exports = { } } - if (message.author.id !== ticket.creator && !message.member.roles.cache.has(config.staff_role)) + if (message.author.id !== ticket.creator && !message.member.roles.cache.has(config.staff_role)) return channel.send( new MessageEmbed() .setColor(config.err_colour) @@ -75,103 +75,100 @@ module.exports = { .addField('Help', `Type \`${config.prefix}help ${this.name}\` for more information`) .setFooter(guild.name, guild.iconURL()) ); - + let success; - - let confirm = await message.channel.send( - new MessageEmbed() - .setColor(config.colour) - .setAuthor(message.author.username, message.author.displayAvatarURL()) - .setTitle('❔ Are you sure?') - .setDescription( - `:warning: This action is **irreversible**, the ticket will be completely removed from the database. - You will **not** be able to view a transcript/archive of the channel later. - Use the \`close\` command instead if you don't want this behaviour.\n**React with ✅ to confirm.**`) - .setFooter(guild.name + ' | Expires in 15 seconds', guild.iconURL()) - ); - - await confirm.react('✅'); - - const collector = confirm.createReactionCollector( - (r, u) => r.emoji.name === '✅' && u.id === message.author.id, { - time: 15000 - }); - - collector.on('collect', async () => { - if (channel.id !== message.channel.id) - channel.send( - new MessageEmbed() - .setColor(config.colour) - .setAuthor(message.author.username, message.author.displayAvatarURL()) - .setTitle('**Ticket deleted**') - .setDescription(`Ticket deleted by ${message.author}`) - .setFooter(guild.name, guild.iconURL()) - ); - - confirm.reactions.removeAll(); - confirm.edit( + if (config.commands.delete.confirmation) { + let confirm = await message.channel.send( new MessageEmbed() .setColor(config.colour) .setAuthor(message.author.username, message.author.displayAvatarURL()) - .setTitle(`✅ **Ticket ${ticket.id} deleted**`) - .setDescription('The channel will be automatically deleted in a few seconds.') - .setFooter(guild.name, guild.iconURL()) + .setTitle('❔ Are you sure?') + .setDescription( + `:warning: This action is **irreversible**, the ticket will be completely removed from the database. + You will **not** be able to view a transcript/archive of the channel later. + Use the \`close\` command instead if you don't want this behaviour.\n**React with ✅ to confirm.**`) + .setFooter(guild.name + ' | Expires in 15 seconds', guild.iconURL()) ); - let txt = join(__dirname, `../../user/transcripts/text/${ticket.get('channel')}.txt`), - raw = join(__dirname, `../../user/transcripts/raw/${ticket.get('channel')}.log`), - json = join(__dirname, `../../user/transcripts/raw/entities/${ticket.get('channel')}.json`); + await confirm.react('✅'); - if (fs.existsSync(txt)) fs.unlinkSync(txt); - if (fs.existsSync(raw)) fs.unlinkSync(raw); - if (fs.existsSync(json)) fs.unlinkSync(json); + const collector = confirm.createReactionCollector( + (r, u) => r.emoji.name === '✅' && u.id === message.author.id, { + time: 15000 + }); - // update database - success = true; - ticket.destroy(); // remove ticket from database - - // delete messages and channel - setTimeout(() => { - channel.delete(); + collector.on('collect', async () => { if (channel.id !== message.channel.id) - message.delete() - .then(() => confirm.delete()); - }, 5000); + channel.send( + new MessageEmbed() + .setColor(config.colour) + .setAuthor(message.author.username, message.author.displayAvatarURL()) + .setTitle('**Ticket deleted**') + .setDescription(`Ticket deleted by ${message.author}`) + .setFooter(guild.name, guild.iconURL()) + ); - log.info(`${message.author.tag} deleted a ticket (#ticket-${ticket.id})`); - - if (config.logs.discord.enabled) { - client.channels.cache.get(config.logs.discord.channel).send( - new MessageEmbed() - .setColor(config.colour) - .setAuthor(message.author.username, message.author.displayAvatarURL()) - .setTitle('Ticket deleted') - .addField('Creator', `<@${ticket.creator}>`, true) - .addField('Deleted by', message.author, true) - .setFooter(guild.name, guild.iconURL()) - .setTimestamp() - ); - } - }); - - - collector.on('end', () => { - if (!success) { confirm.reactions.removeAll(); confirm.edit( new MessageEmbed() - .setColor(config.err_colour) + .setColor(config.colour) .setAuthor(message.author.username, message.author.displayAvatarURL()) - .setTitle('❌ **Expired**') - .setDescription('You took too long to react; confirmation failed.') - .setFooter(guild.name, guild.iconURL())); + .setTitle(`✅ **Ticket ${ticket.id} deleted**`) + .setDescription('The channel will be automatically deleted in a few seconds.') + .setFooter(guild.name, guild.iconURL()) + ); + }); + collector.on('end', () => { + if (!success) { + confirm.reactions.removeAll(); + confirm.edit( + new MessageEmbed() + .setColor(config.err_colour) + .setAuthor(message.author.username, message.author.displayAvatarURL()) + .setTitle('❌ **Expired**') + .setDescription('You took too long to react; confirmation failed.') + .setFooter(guild.name, guild.iconURL())); - message.delete({ - timeout: 10000 - }) + message.delete({ + timeout: 10000 + }) + .then(() => confirm.delete()); + } + }); + } + let txt = join(__dirname, `../../user/transcripts/text/${ticket.get('channel')}.txt`), + raw = join(__dirname, `../../user/transcripts/raw/${ticket.get('channel')}.log`), + json = join(__dirname, `../../user/transcripts/raw/entities/${ticket.get('channel')}.json`); + + if (fs.existsSync(txt)) fs.unlinkSync(txt); + if (fs.existsSync(raw)) fs.unlinkSync(raw); + if (fs.existsSync(json)) fs.unlinkSync(json); + + // update database + success = true; + ticket.destroy(); // remove ticket from database + + // delete messages and channel + setTimeout(() => { + channel.delete(); + if (channel.id !== message.channel.id) + message.delete() .then(() => confirm.delete()); - } - }); + }, 5000); + log.info(`${message.author.tag} deleted a ticket (#ticket-${ticket.id})`); + + if (config.logs.discord.enabled) { + client.channels.cache.get(config.logs.discord.channel).send( + new MessageEmbed() + .setColor(config.colour) + .setAuthor(message.author.username, message.author.displayAvatarURL()) + .setTitle('Ticket deleted') + .addField('Creator', `<@${ticket.creator}>`, true) + .addField('Deleted by', message.author, true) + .setFooter(guild.name, guild.iconURL()) + .setTimestamp() + ); + } } }; \ No newline at end of file diff --git a/src/commands/new.js b/src/commands/new.js index b4a545f..2e76bc2 100644 --- a/src/commands/new.js +++ b/src/commands/new.js @@ -23,7 +23,8 @@ module.exports = { const guild = client.guilds.cache.get(config.guild); const supportRole = guild.roles.cache.get(config.staff_role); - if (!supportRole) + if (config.commands.new.enabled) { + if (!supportRole) return message.channel.send( new MessageEmbed() .setColor(config.err_colour) @@ -75,7 +76,7 @@ module.exports = { .setDescription('Please limit your ticket topic to less than 256 characters. A short sentence will do.') .setFooter(guild.name, guild.iconURL()) ); - else if (topic.length < 1) topic = 'No topic given'; + else if (topic.length < 1) topic = config.default_topic; let ticket = await Ticket.create({ channel: '', @@ -192,5 +193,6 @@ module.exports = { }).catch(log.error); + } }, }; diff --git a/src/events/messageReactionAdd.js b/src/events/messageReactionAdd.js index 80da3e7..250006f 100644 --- a/src/events/messageReactionAdd.js +++ b/src/events/messageReactionAdd.js @@ -89,7 +89,7 @@ module.exports = { } } - let topic = 'No topic given (created via panel)'; + let topic = config.default_topic; let ticket = await Ticket.create({ channel: '', diff --git a/user/config.js b/user/config.js index 67623e5..f5fff1e 100644 --- a/user/config.js +++ b/user/config.js @@ -47,10 +47,23 @@ module.exports = { colour: '#009999', err_colour: 'RED', cooldown: 3, - + default_topic: 'Support Needed', guild: '', // ID of your guild (REQUIRED) staff_role: '', // ID of your Support Team role (REQUIRED) + commands: { + close: { + confirmation: true, + send_transcripts: true + }, + delete: { + confirmation: true + }, + new: { + enabled: true + }, + }, + tickets: { category: '', // ID of your tickets category (REQUIRED) send_img: true,