Files
SupportBot/Commands/closeticket.js
T

174 lines
7.6 KiB
JavaScript
Raw Normal View History

2021-10-04 20:41:05 +01:00
// SupportBot | Emerald Services
// Close Ticket Command
2019-11-15 20:27:10 +00:00
2020-01-26 22:35:45 +00:00
const fs = require("fs");
2021-10-04 20:41:05 +01:00
const Discord = require("discord.js");
2020-06-15 23:58:37 +01:00
const yaml = require('js-yaml');
2021-10-04 20:41:05 +01:00
const supportbot = yaml.load(fs.readFileSync('./Data/supportbot.yml', 'utf8'));
2021-10-07 20:46:18 +01:00
const cmdconfig = yaml.load(fs.readFileSync('./Data/commands.yml', 'utf8'));
2021-10-04 20:41:05 +01:00
const Command = require("../Structures/Command.js");
const TicketNumberID = require('../Structures/TicketID.js');
2019-06-09 23:52:41 +01:00
2021-10-04 20:41:05 +01:00
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
module.exports = new Command({
2021-10-07 20:46:18 +01:00
name: cmdconfig.CloseTicket,
description: cmdconfig.CloseTicketDesc,
2021-10-04 20:41:05 +01:00
type: "BOTH",
slashCommandOptions: [],
permission: "SEND_MESSAGES",
2020-01-26 22:35:45 +00:00
2021-10-04 20:41:05 +01:00
async run(message, args, client) {
2020-07-27 10:43:51 -04:00
2020-07-04 00:54:34 +01:00
if (!message.channel.name.startsWith( `${supportbot.TicketChannel}-` )) {
2020-06-24 09:50:14 +01:00
const Exists = new Discord.MessageEmbed()
2020-07-14 19:42:46 +01:00
.setTitle("No Ticket Found!")
.setDescription(`${supportbot.NoValidTicket}`)
2020-06-24 09:50:14 +01:00
.setColor(supportbot.WarningColour);
message.channel.send({ embed: Exists });
return;
2020-06-21 15:56:16 +01:00
}
2021-05-23 10:39:24 +02:00
if (supportbot.CloseConfirmation) {
2020-06-24 09:50:14 +01:00
const CloseTicketRequest = new Discord.MessageEmbed()
2020-07-04 00:54:34 +01:00
.setTitle(`**${supportbot.ClosingTicket}**`)
.setDescription(`Please confirm by repeating the following word.. \`${supportbot.ClosingConfirmation_Word}\` `)
.setColor(supportbot.EmbedColour);
message.channel.send(CloseTicketRequest).then(m => {
message.channel.awaitMessages( response => response.content.toLowerCase() === supportbot.ClosingConfirmation_Word, {
max: 1,
time: 20000,
errors: [ 'time' ],
}).then( collected => {
2021-05-23 10:39:24 +02:00
let logChannel = message.guild.channels.cache.find(channel => channel.name === supportbot.TranscriptLog) || message.guild.channels.cache.find(channel => channel.id === supportbot.TranscriptLog)
2020-07-04 00:54:34 +01:00
let user = message.author;
let reason = args.join(" ") || "No Reason Provided.";
let name = message.channel.name;
let ticketChannel = message.channel;
message.channel.send(`**${supportbot.ClosingTicket}**`)
.then(() => {
const logEmbed = new Discord.MessageEmbed()
.setTitle(supportbot.TranscriptTitle)
.setColor(supportbot.EmbedColour)
.setFooter(supportbot.EmbedFooter)
.addField("Closed By", message.author.tag)
.addField("Reason", reason);
message.channel.messages.fetch({ limit: 100 }).then( msgs => {
2021-08-05 01:03:58 +01:00
let html = '';
2020-07-04 00:54:34 +01:00
msgs = msgs.sort( ( a, b ) => a.createdTimestamp - b.createdTimestamp );
2021-08-05 01:03:58 +01:00
html += `<strong>Server Name:</strong> ${message.guild.name}<br>`;
html += `<strong>Ticket:</strong> ${ticketChannel.name}<br>`;
html += `<strong>Message:</strong> ${msgs.size} Messages<br><br><br>`;
2020-07-04 00:54:34 +01:00
msgs.forEach( msg => {
if ( msg.content ) {
2021-08-05 01:03:58 +01:00
html += `<strong>User:</strong> ${msg.author.tag}<br>`;
html += `<strong>Message:</strong> ${msg.content}<br>`;
html += `-----<br><br>`;
2020-07-04 00:54:34 +01:00
}
});
2021-09-11 15:29:21 +01:00
logChannel.send({embeds: [logEmbed]}).catch(err => {
2020-07-04 00:54:34 +01:00
message.reply(err)
})
2021-09-11 15:29:21 +01:00
let file = new Discord.MessageAttachment( Buffer.from( html ), `${name}.html` )
logChannel.send({embeds: [logEmbed], files: [file]}).catch(err => {
message.reply(err)
})
message.channel.delete().catch(error => {
if (error.code !== Discord.Constants.APIErrors.UNKNOWN_CHANNEL) {
console.error('Failed to delete the message:', error);
}
});
2020-07-04 00:54:34 +01:00
})
})
})
}).catch( () => {
2021-09-11 15:29:21 +01:00
interaction.m.edit({content: 'The request to close the ticket has timed out.'}).then(m2 => setTimeout(() => channel.delete(3000)) );
2020-07-04 00:54:34 +01:00
});
2020-06-21 15:56:16 +01:00
}
2020-06-24 09:50:14 +01:00
2021-05-23 10:39:24 +02:00
if (!supportbot.CloseConfirmation) {
2020-07-04 00:54:34 +01:00
2021-05-23 10:39:24 +02:00
let logChannel = message.guild.channels.cache.find(channel => channel.name === supportbot.TranscriptLog) || message.guild.channels.cache.find(channel => channel.id === supportbot.TranscriptLog)
2020-07-04 00:54:34 +01:00
let user = message.author;
let reason = args.join(" ") || "No Reason Provided.";
let name = message.channel.name;
let ticketChannel = message.channel;
message.channel.send(`**${supportbot.ClosingTicket}**`)
.then(() => {
const logEmbed = new Discord.MessageEmbed()
.setTitle(supportbot.TranscriptTitle)
.setColor(supportbot.EmbedColour)
.setFooter(supportbot.EmbedFooter)
.addField("Closed By", message.author.tag)
.addField("Reason", reason);
message.channel.messages.fetch({ limit: 100 }).then( msgs => {
2021-08-05 01:03:58 +01:00
let html = '';
2020-07-04 00:54:34 +01:00
msgs = msgs.sort( ( a, b ) => a.createdTimestamp - b.createdTimestamp );
2021-08-05 01:03:58 +01:00
html += `<strong>Server Name:</strong> ${message.guild.name}<br>`;
html += `<strong>Ticket:</strong> ${ticketChannel.name}<br>`;
html += `<strong>Message:</strong> ${msgs.size} Messages<br><br><br>`;
2020-07-04 00:54:34 +01:00
msgs.forEach( msg => {
if ( msg.content ) {
2021-08-05 01:03:58 +01:00
html += `<strong>User:</strong> ${msg.author.tag}<br>`;
html += `<strong>Message:</strong> ${msg.content}<br>`;
html += `-----<br><br>`;
2020-07-04 00:54:34 +01:00
}
});
2021-09-11 15:29:21 +01:00
logChannel.send({embeds: [logEmbed]}).catch(err => {
2020-07-04 00:54:34 +01:00
message.reply(err)
})
2021-09-11 15:29:21 +01:00
let file = new Discord.MessageAttachment( Buffer.from( html ), `${name}.html` )
logChannel.send({embeds: [logEmbed], files: [file]}).catch(err => {
message.reply(err)
})
2020-07-04 00:54:34 +01:00
2021-09-11 15:29:21 +01:00
message.channel.delete().catch(error => {
if (error.code !== Discord.Constants.APIErrors.UNKNOWN_CHANNEL) {
console.error('Failed to delete the message:', error);
}
});
2020-07-04 00:54:34 +01:00
})
}).catch( () => {
2021-09-11 15:29:21 +01:00
interaction.m.edit({content: 'The request to close the ticket has timed out.'}).then(m2 => setTimeout(() => channel.delete(3000)) );
2020-07-04 00:54:34 +01:00
});
}
2020-06-24 09:50:14 +01:00
2021-10-04 20:41:05 +01:00
}
});