Files
SupportBot/commands/close.js
T

113 lines
4.7 KiB
JavaScript
Raw Normal View History

2019-06-09 23:52:41 +01:00
// SupportBot
2020-01-26 22:35:45 +00:00
// Created by © 2020 Emerald Services
// Command: Close
2019-11-15 20:27:10 +00:00
2020-01-26 22:35:45 +00:00
const Discord = require("discord.js");
const bot = new Discord.Client();
2019-06-09 23:52:41 +01:00
2020-01-26 22:35:45 +00:00
const fs = require("fs");
const yaml = require('js-yaml');
const supportbot = yaml.load(fs.readFileSync('./supportbot-config.yml', 'utf8'));
2019-06-09 23:52:41 +01:00
2019-11-17 21:27:13 +00:00
/**
* @param {Discord.Message} message
*/
2019-06-09 23:52:41 +01:00
2020-01-26 22:35:45 +00:00
exports.run = (bot, message, args) => {
console.log(`\u001b[33m`, `[${supportbot.Bot_Name}] > `, `\u001b[31;1m`, `${message.author.tag}`, `\u001b[32;1m`, `has executed`, `\u001b[31;1m`, `${supportbot.Prefix}${supportbot.Close_Command}`);
2019-11-17 21:27:13 +00:00
2020-01-26 22:35:45 +00:00
if ( !message.channel.name.startsWith( `${supportbot.Ticket_Channel_Name}-` ) ) {
2019-11-17 21:27:13 +00:00
const embed = new Discord.RichEmbed()
2020-01-26 22:35:45 +00:00
.setDescription(`:x: Cannot use this command becase you are outside a ticket channel.`)
.setColor(supportbot.EmbedColour);
2019-11-17 21:27:13 +00:00
2020-01-26 22:35:45 +00:00
message.channel.send(embed);
2019-11-17 21:27:13 +00:00
return;
}
const closeRequestEmbed = new Discord.RichEmbed()
2020-01-26 22:35:45 +00:00
.setDescription(`Looks like you have come to the end of your support ticket\nPlease confirm that you want to close your ticket by saying ||**confirm**||`)
.setFooter("Your request will be avoided in 20 seconds")
.setColor(supportbot.EmbedColour)
2019-11-17 21:27:13 +00:00
2020-01-26 22:35:45 +00:00
message.channel.send(closeRequestEmbed).then( m => {
2019-11-17 21:27:13 +00:00
message.channel.awaitMessages( response => response.content.toLowerCase() === `confirm`, {
max: 1,
time: 20 * 1000,
errors: [ 'time' ],
} ).then( collected => {
2020-01-26 22:35:45 +00:00
let logChannel = message.guild.channels.find( channel => channel.name === supportbot.Transcript_Logs );
2019-11-17 21:27:13 +00:00
let user = message.author;
2020-01-26 22:35:45 +00:00
let reason = args.join(" ") || "No reason provided.";
2019-11-17 21:27:13 +00:00
let name = message.channel.name;
let ticketChannel = message.channel;
2020-01-26 22:35:45 +00:00
message.channel.send(supportbot.Ticket_Closing)
2019-11-17 21:27:13 +00:00
.then( () => {
const logEmbed = new Discord.RichEmbed()
2020-01-26 22:35:45 +00:00
.setTitle(supportbot.Transcript_Title)
.setColor(supportbot.EmbedColour)
.setFooter(supportbot.EmbedFooter)
.addField("Ticket Author", user)
.addField("Closed By", message.author.tag)
.addField("Reason", reason);
message.channel.fetchMessages({ limit: 100 })
2019-11-17 21:27:13 +00:00
.then( msgs => {
let txt = '';
msgs = msgs.sort( ( a, b ) => a.createdTimestamp - b.createdTimestamp );
txt += `${message.guild.name}\n`;
txt += `#${ticketChannel.name}\n`;
txt += `${msgs.size} messages\n\n\n`;
msgs.forEach( msg => {
if ( msg.content ) {
txt += `----- ${msg.author.tag} at ${msg.createdAt}\n`;
txt += `${msg.content}\n`;
txt += `-----\n\n`;
}
} );
2020-01-05 15:22:50 +00:00
// message.author.send( `A transcript has been generated for the ticket you closed.` );
// message.author.send( new Discord.Attachment( Buffer.from( txt ), `${name}.txt` ) );
2019-11-17 21:27:13 +00:00
logChannel.send( logEmbed );
logChannel.send( new Discord.Attachment( Buffer.from( txt ), `${name}.txt` ) );
message.channel.delete();
// message.channel.fetchMessages( { limit: 100, before: msgs.last().id } )
// .then( msg => {
// const merged = msgs.concat( msg );
// const output = merged.reduce( ( out, msg ) => {
// out += `[${message.createdAt}] ${message.author.tag}: ${message.cleanContent ? message.cleanContent.replace( /\n/g, '\r\n' ) : ''}\r\n`;
// return out;
// }, '' );
// log.send( { files: [ { attachment: Buffer.from( output, 'utf8' ), name: `${name}.txt` } ] } ).then( c.delete() ).catch( console.error ); // sends the file to logs
// return log.send( embed1 ).catch( console.error );
// } )
2020-01-26 22:35:45 +00:00
});
})
2019-07-24 16:52:30 -06:00
2020-01-26 22:35:45 +00:00
}).catch( () => {
m.edit('The request to close the ticket has timed out.').then( m2 => m2.delete( 3000 ) );
});
});
2019-07-24 16:52:30 -06:00
2019-06-09 23:52:41 +01:00
}
2019-11-15 21:53:39 +00:00
2019-06-09 23:52:41 +01:00
exports.help = {
2020-01-26 22:35:45 +00:00
name: supportbot.Close_Command,
2019-11-15 13:58:24 -06:00
}