DiscordTickets/src/commands/close.js

214 lines
6.6 KiB
JavaScript
Raw Normal View History

/**
2020-10-03 17:08:10 +03:00
*
* @name DiscordTickets
* @author eartharoid <contact@eartharoid.me>
* @license GNU-GPLv3
2020-10-03 17:08:10 +03:00
*
*/
const ChildLogger = require('leekslazylogger').ChildLogger;
const log = new ChildLogger();
const {
MessageEmbed
} = require('discord.js');
2020-08-17 16:46:23 +03:00
const fs = require('fs');
2020-08-23 17:35:24 +03:00
const archive = require('../modules/archive');
module.exports = {
name: 'close',
description: 'Close a ticket; either a specified (mentioned) channel, or the channel the command is used in.',
usage: '[ticket]',
aliases: ['none'],
example: 'close #ticket-17',
args: false,
2020-10-03 19:18:07 +03:00
async execute(client, message, args, { config, Ticket }) {
2020-08-17 16:46:23 +03:00
const guild = client.guilds.cache.get(config.guild);
2020-08-18 00:07:05 +03:00
const notTicket = new MessageEmbed()
.setColor(config.err_colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(':x: **This isn\'t a ticket channel**')
.setDescription('Use this command in the ticket channel you want to close, or mention the channel.')
.addField('Usage', `\`${config.prefix}${this.name} ${this.usage}\`\n`)
.addField('Help', `Type \`${config.prefix}help ${this.name}\` for more information`)
2020-08-17 16:46:23 +03:00
.setFooter(guild.name, guild.iconURL());
let ticket;
2020-08-17 16:46:23 +03:00
let channel = message.mentions.channels.first();
// || client.channels.resolve(await Ticket.findOne({ where: { id: args[0] } }).channel) // channels.fetch()
if (!channel) {
2020-08-17 16:46:23 +03:00
channel = message.channel;
ticket = await Ticket.findOne({
where: {
channel: channel.id
}
});
2020-10-03 19:18:07 +03:00
if (!ticket) return channel.send(notTicket);
} else {
ticket = await Ticket.findOne({
where: {
channel: channel.id
}
});
if (!ticket) {
notTicket
.setTitle(':x: **Channel is not a ticket**')
.setDescription(`${channel} is not a ticket channel.`);
2020-08-24 00:01:21 +03:00
return message.channel.send(notTicket);
}
if (message.author.id !== ticket.creator && !message.member.roles.cache.has(config.staff_role))
2020-08-17 16:46:23 +03:00
return channel.send(
2020-08-18 00:07:05 +03:00
new MessageEmbed()
.setColor(config.err_colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(':x: **No permission**')
.setDescription(`You don't have permission to close ${channel} as it does not belong to you and you are not staff.`)
.addField('Usage', `\`${config.prefix}${this.name} ${this.usage}\`\n`)
.addField('Help', `Type \`${config.prefix}help ${this.name}\` for more information`)
2020-08-17 16:46:23 +03:00
.setFooter(guild.name, guild.iconURL())
);
2020-08-17 16:46:23 +03:00
}
2020-08-17 16:46:23 +03:00
let success;
let pre = fs.existsSync(`user/transcripts/text/${channel.id}.txt`) ||
fs.existsSync(`user/transcripts/raw/${channel.id}.log`) ?
2020-08-24 00:01:21 +03:00
`You will be able to view an archived version later with \`${config.prefix}transcript ${ticket.id}\`` :
'';
2020-08-17 16:46:23 +03:00
let confirm = await message.channel.send(
2020-08-18 00:07:05 +03:00
new MessageEmbed()
2020-08-17 16:46:23 +03:00
.setColor(config.colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(':grey_question: Are you sure?')
.setDescription(`${pre}\n**React with :white_check_mark: to confirm.**`)
.setFooter(guild.name + ' | Expires in 15 seconds', guild.iconURL())
);
await confirm.react('✅');
2020-08-17 16:46:23 +03:00
const collector = confirm.createReactionCollector(
(r, u) => r.emoji.name === '✅' && u.id === message.author.id, {
time: 15000
});
collector.on('collect', async () => {
2020-10-03 19:18:07 +03:00
if (channel.id !== message.channel.id) {
2020-08-17 16:46:23 +03:00
channel.send(
2020-08-18 00:07:05 +03:00
new MessageEmbed()
2020-08-17 16:46:23 +03:00
.setColor(config.colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle('**Ticket closed**')
.setDescription(`Ticket closed by ${message.author}`)
.setFooter(guild.name, guild.iconURL())
);
2020-10-03 19:18:07 +03:00
}
2020-08-17 16:46:23 +03:00
confirm.reactions.removeAll();
confirm.edit(
2020-08-18 00:07:05 +03:00
new MessageEmbed()
.setColor(config.colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(`:white_check_mark: **Ticket ${ticket.id} closed**`)
2020-08-17 16:46:23 +03:00
.setDescription('The channel will be automatically deleted in a few seconds, once the contents have been archived.')
.setFooter(guild.name, guild.iconURL())
);
if (config.transcripts.text.enabled || config.transcripts.web.enabled) {
2020-08-24 00:01:21 +03:00
let u = await client.users.fetch(ticket.creator);
if (u) {
let dm;
try {
dm = u.dmChannel || await u.createDM();
} catch (e) {
log.warn(`Could not create DM channel with ${u.tag}`);
}
2020-08-24 00:01:21 +03:00
let res = {};
const embed = new MessageEmbed()
.setColor(config.colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(`Ticket ${ticket.id}`)
.setFooter(guild.name, guild.iconURL());
if (fs.existsSync(`user/transcripts/text/${ticket.get('channel')}.txt`)) {
embed.addField('Text transcript', 'See attachment');
res.files = [{
attachment: `user/transcripts/text/${ticket.get('channel')}.txt`,
name: `ticket-${ticket.id}-${ticket.get('channel')}.txt`
}];
}
2020-10-03 19:18:07 +03:00
if (fs.existsSync(`user/transcripts/raw/${ticket.get('channel')}.log`) && fs.existsSync(`user/transcripts/raw/entities/${ticket.get('channel')}.json`)) {
embed.addField('Web archive', await archive.export(Ticket, channel));
2020-10-03 19:18:07 +03:00
}
2020-10-03 17:08:10 +03:00
2020-10-03 19:18:07 +03:00
if (embed.fields.length < 1) {
2020-08-24 00:01:21 +03:00
embed.setDescription(`No text transcripts or archive data exists for ticket ${ticket.id}`);
2020-10-03 19:18:07 +03:00
}
2020-08-24 00:01:21 +03:00
res.embed = embed;
dm.send(res).then();
}
}
2020-08-24 00:01:21 +03:00
// 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})`);
2020-10-03 19:18:07 +03:00
if (config.logs.discord.enabled) {
2020-08-17 16:46:23 +03:00
client.channels.cache.get(config.logs.discord.channel).send(
2020-08-18 00:07:05 +03:00
new MessageEmbed()
2020-08-17 16:46:23 +03:00
.setColor(config.colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle('Ticket closed')
2020-08-24 00:01:21 +03:00
.addField('Creator', `<@${ticket.creator}>`, true)
2020-08-17 16:46:23 +03:00
.addField('Closed by', message.author, true)
.setFooter(guild.name, guild.iconURL())
.setTimestamp()
);
2020-10-03 19:18:07 +03:00
}
2020-08-17 16:46:23 +03:00
});
2020-08-17 16:46:23 +03:00
collector.on('end', () => {
if (!success) {
2020-08-17 16:46:23 +03:00
confirm.reactions.removeAll();
confirm.edit(
2020-08-18 00:07:05 +03:00
new MessageEmbed()
2020-08-17 16:46:23 +03:00
.setColor(config.err_colour)
.setAuthor(message.author.username, message.author.displayAvatarURL())
.setTitle(':x: **Expired**')
2020-09-07 12:13:09 +03:00
.setDescription('You took too long to react; confirmation failed.')
2020-08-17 16:46:23 +03:00
.setFooter(guild.name, guild.iconURL()));
message.delete({
timeout: 10000
})
2020-08-17 16:46:23 +03:00
.then(() => confirm.delete());
}
2020-08-17 16:46:23 +03:00
});
2020-08-17 16:46:23 +03:00
}
};