From ae1d24236f35fd96275c44f04a17dcc42921632a Mon Sep 17 00:00:00 2001 From: "Isaac (eartharoid)" Date: Fri, 1 Jan 2021 15:41:12 +0000 Subject: [PATCH] Improvements and fixes --- src/commands/close.js | 117 +++++++++++++++---------------- src/commands/delete.js | 79 ++++++++++++--------- src/commands/help.js | 2 +- src/commands/new.js | 12 +++- src/events/messageReactionAdd.js | 6 +- user/config.js | 30 ++++---- 6 files changed, 128 insertions(+), 118 deletions(-) diff --git a/src/commands/close.js b/src/commands/close.js index c859380..79ecdc8 100644 --- a/src/commands/close.js +++ b/src/commands/close.js @@ -78,11 +78,13 @@ module.exports = { .setFooter(guild.name, guild.iconURL()) ); - let success; - 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}\`` - : ''; + if (config.commands.close.confirmation) { + let success; + 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) @@ -96,11 +98,10 @@ module.exports = { const collector = confirm.createReactionCollector( (r, u) => r.emoji.name === '✅' && u.id === message.author.id, { - time: 15000 - }); + time: 15000 + }); collector.on('collect', async () => { - let users = []; if (channel.id !== message.channel.id) { channel.send( new MessageEmbed() @@ -121,11 +122,15 @@ module.exports = { .setDescription('The channel will be automatically deleted in a few seconds, once the contents have been archived.') .setFooter(guild.name, guild.iconURL()) ); + + + if (channel.id !== message.channel.id) + message.delete({ + timeout: 5000 + }).then(() => confirm.delete()); + success = true; - closeTicket(); - if (config.commands.close.send_transcripts) { - sendTranscript(); - } + close(); }); @@ -142,58 +147,19 @@ module.exports = { message.delete({ timeout: 10000 - }) - .then(() => confirm.delete()); + }).then(() => confirm.delete()); } }); } else { - success = true; - closeTicket(); + close(); } - async function closeTicket() { - // update database - ticket.update({ - open: false - }, { - where: { - channel: channel.id - } - }); + + async function close () { + let users = []; - // 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); - if (u) { let dm; try { @@ -202,7 +168,6 @@ module.exports = { log.warn(`Could not create DM channel with ${u.tag}`); } - let res = {}; const embed = new MessageEmbed() .setColor(config.colour) @@ -229,17 +194,47 @@ module.exports = { } res.embed = embed; + try { - if (success) { - dm.send(res); - if (config.transcripts.channel.length > 1) client.channels.cache.get(config.transcripts.channel).send(res); - } + if (config.commands.close.send_transcripts) 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 + ticket.update({ + open: false + }, { + where: { + channel: channel.id + } + }); + + // delete channel + channel.delete({ + timeout: 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); + } + } } }; diff --git a/src/commands/delete.js b/src/commands/delete.js index 8be94f9..a5347cd 100644 --- a/src/commands/delete.js +++ b/src/commands/delete.js @@ -76,8 +76,9 @@ module.exports = { .setFooter(guild.name, guild.iconURL()) ); - let success; + if (config.commands.delete.confirmation) { + let success; let confirm = await message.channel.send( new MessageEmbed() .setColor(config.colour) @@ -94,8 +95,8 @@ module.exports = { const collector = confirm.createReactionCollector( (r, u) => r.emoji.name === '✅' && u.id === message.author.id, { - time: 15000 - }); + time: 15000 + }); collector.on('collect', async () => { if (channel.id !== message.channel.id) @@ -117,7 +118,16 @@ module.exports = { .setDescription('The channel will be automatically deleted in a few seconds.') .setFooter(guild.name, guild.iconURL()) ); + + if (channel.id !== message.channel.id) + message.delete({ + timeout: 5000 + }).then(() => confirm.delete()); + + success = true; + del(); }); + collector.on('end', () => { if (!success) { confirm.reactions.removeAll(); @@ -131,44 +141,47 @@ module.exports = { message.delete({ timeout: 10000 - }) - .then(() => confirm.delete()); + }).then(() => confirm.delete()); } }); + } else { + del(); } - 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 + async function del () { + 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`); - // delete messages and channel - setTimeout(() => { - channel.delete(); - if (channel.id !== message.channel.id) - message.delete() - .then(() => confirm.delete()); - }, 5000); + if (fs.existsSync(txt)) fs.unlinkSync(txt); + if (fs.existsSync(raw)) fs.unlinkSync(raw); + if (fs.existsSync(json)) fs.unlinkSync(json); - log.info(`${message.author.tag} deleted a ticket (#ticket-${ticket.id})`); + // update database + ticket.destroy(); // remove ticket from database - 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() - ); + // channel + channel.delete({ + timeout: 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/help.js b/src/commands/help.js index 8366065..ff27d12 100644 --- a/src/commands/help.js +++ b/src/commands/help.js @@ -26,7 +26,7 @@ module.exports = { let cmds = []; for (let command of commands) { - if (command.hide) continue; + if (command.hide || command.disabled) continue; if (command.permission && !message.member.hasPermission(command.permission)) continue; let desc = command.description; diff --git a/src/commands/new.js b/src/commands/new.js index f5795c9..b3170d3 100644 --- a/src/commands/new.js +++ b/src/commands/new.js @@ -11,6 +11,7 @@ const log = new Logger(); const { MessageEmbed } = require('discord.js'); const fs = require('fs'); const { join } = require('path'); +const config = require(join(__dirname, '../../user/', require('../').config)); module.exports = { name: 'new', @@ -19,12 +20,17 @@ module.exports = { aliases: ['ticket', 'open'], example: 'new my server won\'t start', args: false, + disabled: !config.commands.new.enabled, async execute(client, message, args, {config, Ticket}) { + + if (!config.commands.new.enabled) return; // stop if the command is disabled + + const guild = client.guilds.cache.get(config.guild); const supportRole = guild.roles.cache.get(config.staff_role); - if (config.commands.new.enabled) { - if (!supportRole) + + if (!supportRole) return message.channel.send( new MessageEmbed() .setColor(config.err_colour) @@ -196,6 +202,6 @@ module.exports = { }).catch(log.error); - } + }, }; diff --git a/src/events/messageReactionAdd.js b/src/events/messageReactionAdd.js index 707df13..3ca336a 100644 --- a/src/events/messageReactionAdd.js +++ b/src/events/messageReactionAdd.js @@ -89,11 +89,7 @@ module.exports = { } } - if (!/("|'|`)("|'|`)/.test(config.default_topic)) { - topic = config.tickets.default_topic.panel; - } else { - topic = 'No topic given (created via panel)'; - } + let topic = config.tickets.default_topic.command; let ticket = await Ticket.create({ channel: '', diff --git a/user/config.js b/user/config.js index 0813b3a..7e182e5 100644 --- a/user/config.js +++ b/user/config.js @@ -50,6 +50,21 @@ module.exports = { guild: '', // ID of your guild (REQUIRED) staff_role: '', // ID of your Support Team role (REQUIRED) + tickets: { + category: '', // ID of your tickets category (REQUIRED) + send_img: true, + ping: 'here', + text: `Hello there, {{ tag }}! + A member of staff will assist you shortly. + In the mean time, please describe your issue in as much detail as possible! :)`, + pin: false, + max: 3, + default_topic: { + command: 'No topic given', + panel: 'Created via panel' + } + }, + commands: { close: { confirmation: true, @@ -63,21 +78,6 @@ module.exports = { }, }, - tickets: { - default_topic: { - command: 'No topic given', - panel: 'Created via panel' - }, - category: '', // ID of your tickets category (REQUIRED) - send_img: true, - ping: 'here', - text: `Hello there, {{ tag }}! - A member of staff will assist you shortly. - In the mean time, please describe your issue in as much detail as possible! :)`, - pin: false, - max: 3 - }, - transcripts: { text: { enabled: true,