Files
SupportBot/Commands/close.js
T

182 lines
6.3 KiB
JavaScript
Raw Permalink Normal View History

2021-10-28 18:03:27 -05:00
// SupportBot | Emerald Services
// Close Ticket Command
const fs = require("fs");
const Discord = require("discord.js");
const yaml = require("js-yaml");
const supportbot = yaml.load(
fs.readFileSync("./Configs/supportbot.yml", "utf8")
);
const cmdconfig = yaml.load(fs.readFileSync("./Configs/commands.yml", "utf8"));
const Command = require("../Structures/Command.js");
module.exports = new Command({
name: cmdconfig.CloseTicket,
description: cmdconfig.CloseTicketDesc,
options: [
2021-10-28 18:03:27 -05:00
{
name: "reason",
description: "Ticket Close Reason",
type: "STRING",
},
],
permissions: ["SEND_MESSAGES"],
2021-10-28 18:03:27 -05:00
async run(interaction) {
const { getRole, getChannel, getCategory } = interaction.client;
2021-12-18 19:39:59 -06:00
if (supportbot.StaffOnly) {
let SupportStaff = await getRole(supportbot.Staff, interaction.guild);
let Admin = await getRole(supportbot.Admin, interaction.guild);
if (!SupportStaff || !Admin)
return interaction.reply(
"Some roles seem to be missing!\nPlease check for errors when starting the bot."
);
const NoPerms = new Discord.MessageEmbed()
.setTitle("Invalid Permissions!")
.setDescription(
`${supportbot.IncorrectPerms}\n\nRole Required: \`${supportbot.Staff}\` or \`${supportbot.Admin}\``
)
.setColor(supportbot.WarningColour);
if (
!interaction.member.roles.cache.has(SupportStaff.id) &&
!interaction.member.roles.cache.has(Admin.id)
)
return interaction.reply({ embeds: [NoPerms] });
}
2021-12-04 20:57:23 -06:00
await interaction.deferReply();
2021-12-18 20:54:01 -06:00
let tickets = await JSON.parse(
2021-11-23 15:49:37 -06:00
fs.readFileSync("./Data/TicketData.json", "utf8")
).tickets.find((t) => t.id === interaction.channel.id);
2021-12-18 20:54:01 -06:00
let TicketData = await tickets.tickets.findIndex(
(t) => t.id == ticketChannel.id
);
2021-12-14 18:04:38 -06:00
if (!TicketData) {
2021-10-28 18:03:27 -05:00
const Exists = new Discord.MessageEmbed()
.setTitle("No Ticket Found!")
.setDescription(`${supportbot.NoValidTicket}`)
.setColor(supportbot.WarningColour);
2021-12-04 20:57:23 -06:00
return interaction.followUp({ embeds: [Exists] });
2021-10-28 18:03:27 -05:00
}
2021-11-23 17:37:25 -06:00
let tUser = interaction.client.users.cache.get(TicketData.user);
let transcriptChannel = await getChannel(
supportbot.TranscriptLog,
interaction.guild
2021-11-23 15:49:37 -06:00
);
let logChannel = await getChannel(supportbot.TicketLog, interaction.guild);
2021-11-23 15:49:37 -06:00
let reason =
2021-11-23 18:09:06 -06:00
(await interaction.options?.getString("reason")) || "No Reason Provided.";
2021-11-23 15:49:37 -06:00
if (!transcriptChannel || !logChannel)
2021-12-04 20:57:23 -06:00
return interaction.followUp("Some Channels seem to be missing!");
2021-10-28 18:03:27 -05:00
if (supportbot.CloseConfirmation) {
const CloseTicketRequest = new Discord.MessageEmbed()
.setTitle(`**${supportbot.ClosingTicket}**`)
.setDescription(
`Please confirm by repeating the following word.. \`${supportbot.ClosingConfirmation_Word}\` `
)
.setColor(supportbot.EmbedColour);
2021-12-04 20:57:23 -06:00
await interaction.followUp({ embeds: [CloseTicketRequest] });
let filter = (m) =>
m.content.toLowerCase() == supportbot.ClosingConfirmation_Word &&
m.author.id == interaction.user.id;
await interaction.channel.awaitMessages({
filter,
max: 1,
time: 20000,
errors: ["time"],
});
2021-10-28 18:03:27 -05:00
}
2021-11-23 15:49:37 -06:00
try {
2021-12-18 20:54:01 -06:00
tickets.tickets[TicketData].open = false;
fs.writeFileSync(
"./Data/TicketData.json",
JSON.stringify(tickets, null, 4),
(err) => {
if (err) console.error(err);
}
);
2021-12-04 20:57:23 -06:00
await interaction.followUp(`**${supportbot.ClosingTicket}**`);
2021-11-23 15:49:37 -06:00
const transcriptEmbed = new Discord.MessageEmbed()
.setTitle(supportbot.TranscriptTitle)
.setColor(supportbot.EmbedColour)
.setFooter(supportbot.EmbedFooter)
.addField(
"Ticket",
`${interaction.channel.name} (${interaction.channel.id})`
)
.addField(
"User",
`${tUser.username}#${tUser.discriminator} (${tUser.id})`
)
.addField("Closed By", interaction.user.tag)
.addField("Reason", reason);
const logEmbed = new Discord.MessageEmbed()
.setTitle(supportbot.TicketLog_Title)
.setColor(supportbot.EmbedColour)
.setFooter(supportbot.EmbedFooter)
.addField(
"Ticket",
`${interaction.channel.name} (${interaction.channel.id})`
)
.addField(
"User",
`${tUser.username}#${tUser.discriminator} (${tUser.id})`
)
.addField("Closed By", interaction.user.tag)
.addField("Reason", reason);
let msgs = await interaction.channel.messages.fetch();
let html = "";
msgs = msgs.sort((a, b) => a.createdTimestamp - b.createdTimestamp);
html += `<style>* {background-color: #2c2f33;color: #fff;font-family: Arial, Helvetica, sans-serif;}</style>`;
html += `<strong>Server Name:</strong> ${interaction.guild.name}<br>`;
html += `<strong>Ticket:</strong> ${interaction.channel.name}<br>`;
html += `<strong>Message:</strong> ${msgs.size} Messages<br><br><br>`;
await msgs.forEach((msg) => {
if (msg.content) {
html += `<strong>User:</strong> ${msg.author.tag}<br>`;
html += `<strong>Message:</strong> ${msg.content}<br>`;
html += `-----<br><br>`;
}
});
await logChannel.send({ embeds: [logEmbed] }).catch((err) => {
console.error(err);
});
let file = new Discord.MessageAttachment(
Buffer.from(html),
`${interaction.channel.name}.html`
2021-11-21 18:42:18 -06:00
);
2021-11-23 15:49:37 -06:00
await transcriptChannel
.send({ embeds: [transcriptEmbed], files: [file] })
.catch(async (err) => {
2021-11-23 18:09:06 -06:00
console.error(err);
2021-11-23 15:49:37 -06:00
});
await tUser
.send({ embeds: [transcriptEmbed], files: [file] })
.catch(async (err) => {
2021-11-23 18:09:06 -06:00
console.error(err);
2021-10-28 18:03:27 -05:00
});
if (TicketData.subUsers) {
TicketData.subUsers.forEach(async (subUser) => {
interaction.client.users.cache
.get(subUser)
.send({ embeds: [transcriptEmbed], files: [file] });
});
}
2021-11-23 15:49:37 -06:00
await interaction.channel.delete().catch(async (error) => {
console.error(error);
if (error.code !== Discord.Constants.APIErrors.UNKNOWN_CHANNEL) {
console.error("Failed to delete the interaction:", error);
}
});
} catch (error) {
console.error(error);
2021-10-28 18:03:27 -05:00
}
},
});