Files
SupportBot/Commands/ticket.js
T

329 lines
10 KiB
JavaScript
Raw Normal View History

2021-10-24 22:46:36 +01:00
// SupportBot | Emerald Services
// Ticket Command
const fs = require("fs");
2021-12-14 18:04:38 -06:00
const {
MessageEmbed,
Permissions,
MessageButton,
MessageActionRow,
} = require("discord.js");
2021-10-24 22:46:36 +01:00
const yaml = require("js-yaml");
2021-10-27 19:30:10 -05:00
const supportbot = yaml.load(
fs.readFileSync("./Configs/supportbot.yml", "utf8")
);
const cmdconfig = yaml.load(fs.readFileSync("./Configs/commands.yml", "utf8"));
2021-10-24 22:46:36 +01:00
const Command = require("../Structures/Command.js");
const TicketNumberID = require("../Structures/TicketID.js");
module.exports = new Command({
name: cmdconfig.OpenTicket,
description: cmdconfig.OpenTicketDesc,
options: [
2021-10-24 22:46:36 +01:00
{
name: "reason",
description: "Ticket Reason",
type: "STRING",
},
],
permissions: ["SEND_MESSAGES"],
2021-10-24 22:46:36 +01:00
async run(interaction) {
2021-12-18 20:54:01 -06:00
let TicketData = await JSON.parse(
fs.readFileSync("./Data/TicketData.json", "utf8")
);
const { getRole, getChannel, getCategory } = interaction.client;
2021-12-18 20:54:01 -06:00
let User = interaction.guild.members.cache.get(interaction.user.id);
2021-10-24 22:46:36 +01:00
if (
2021-12-18 20:54:01 -06:00
supportbot.MaxAllowedTickets &&
TicketData.tickets.filter((t) => t.user == interaction.user.id && t.open)
.length >= supportbot.MaxAllowedTickets
) {
return interaction.reply({
embeds: [
{
title: "Too Many Tickets!",
description: `You can't have more than ${supportbot.MaxAllowedTickets} open tickets!`,
color: supportbot.WarningColour,
},
],
2021-12-18 20:56:30 -06:00
ephemeral: true,
2021-12-18 20:54:01 -06:00
});
}
if (
User.roles.cache.has(
getRole(supportbot.TicketBlackListRole, interaction.guild).id
2021-10-24 22:46:36 +01:00
)
) {
2021-12-18 20:56:30 -06:00
return interaction.reply({
content: supportbot.TicketBlackListMessage,
ephemeral: true,
});
2021-10-24 22:46:36 +01:00
}
// Ticket ID
2021-11-23 15:48:45 -06:00
let ticketNumberID = await TicketNumberID.pad();
2021-10-24 22:46:36 +01:00
// Ticket Subject
const TicketSubject =
interaction.options?.getString("reason") || supportbot.NoTicketSubject;
2021-12-14 18:04:38 -06:00
const TicketExists = new MessageEmbed()
2021-10-24 22:46:36 +01:00
.setTitle("Ticket Exists!")
.setDescription(`${supportbot.TicketExists}`)
.setColor(supportbot.WarningColour);
if (
2021-10-27 20:18:06 -05:00
await interaction.guild.channels.cache.find((ticketChannel) => {
ticketChannel.name === `${supportbot.TicketPrefix}${ticketNumberID}`;
})
2021-10-24 22:46:36 +01:00
) {
2021-12-04 20:57:23 -06:00
return await interaction.reply({ embeds: [TicketExists] });
2021-10-24 22:46:36 +01:00
}
const Staff = await getRole(supportbot.Staff, interaction.guild);
const Admin = await getRole(supportbot.Admin, interaction.guild);
const DeptRole1 = await getRole(
supportbot.DepartmentRole_1,
interaction.guild
2021-11-21 18:42:18 -06:00
);
const DeptRole2 = await getRole(
supportbot.DepartmentRole_2,
interaction.guild
2021-11-21 18:42:18 -06:00
);
const DeptRole3 = await getRole(
supportbot.DepartmentRole_3,
interaction.guild
2021-11-21 18:42:18 -06:00
);
if (!Staff || !Admin || !DeptRole1 || !DeptRole2 || !DeptRole3)
2021-11-23 16:51:28 -06:00
return interaction.reply({
content:
"Some roles seem to be missing!\nPlease check for errors when starting the bot.",
ephemeral: true,
});
2021-10-24 22:46:36 +01:00
const Author = interaction.user;
let TicketCategory = await getCategory(
supportbot.TicketCategory,
interaction.guild
2021-12-14 18:04:38 -06:00
);
2021-10-24 22:46:36 +01:00
const ticketChannel = await interaction.guild.channels.create(
2021-10-27 19:48:00 -05:00
`${supportbot.TicketPrefix}${ticketNumberID}`,
2021-10-24 22:46:36 +01:00
{
type: "GUILD_TEXT",
2021-12-14 18:04:38 -06:00
parent: TicketCategory.id,
permissionOverwrites: [
{
id: Admin.id,
2021-12-14 18:04:38 -06:00
allow: [Permissions.FLAGS.VIEW_CHANNEL],
},
{
id: Author.id,
allow: [Permissions.FLAGS.VIEW_CHANNEL],
},
{
id: interaction.guild.id,
deny: [Permissions.FLAGS.VIEW_CHANNEL],
},
],
2021-10-24 22:46:36 +01:00
}
);
2021-12-14 18:04:38 -06:00
if (supportbot.AllowStaff) {
ticketChannel.permissionOverwrites.edit(Staff.id, {
VIEW_CHANNEL: true,
});
}
2021-12-19 13:02:43 -06:00
if (supportbot.TicketDepartments) {
ticketChannel.permissionOverwrites.edit(interaction.user.id, {
SEND_MESSAGES: false,
});
}
2021-11-23 15:48:45 -06:00
await TicketData.tickets.push({
id: ticketChannel.id,
name: ticketChannel.name,
user: Author.id,
number: ticketNumberID,
reason: TicketSubject,
2021-12-18 20:54:01 -06:00
open: true,
2021-12-14 18:04:38 -06:00
subUsers: [],
2021-11-23 15:48:45 -06:00
});
fs.writeFileSync(
"./Data/TicketData.json",
JSON.stringify(TicketData, null, 4),
(err) => {
if (err) console.error(err);
}
);
2021-10-24 22:46:36 +01:00
2021-12-14 18:04:38 -06:00
const CreatedTicket = new MessageEmbed()
.setDescription(
supportbot.TicketCreatedAlert.replace(
/%ticketauthor%/g,
interaction.user.id
2021-10-24 22:46:36 +01:00
)
2021-12-14 18:04:38 -06:00
.replace(/%ticketid%/g, ticketChannel.id)
.replace(/%ticketusername%/g, interaction.user.username)
)
.setColor(supportbot.EmbedColour);
await interaction.reply({ embeds: [CreatedTicket], ephemeral: true });
2021-10-24 22:46:36 +01:00
if (supportbot.AllowTicketMentions) {
await ticketChannel.send(`${interaction.user}`);
2021-10-24 22:46:36 +01:00
}
2021-12-14 18:04:38 -06:00
const TicketMessage = new MessageEmbed()
2021-10-24 22:46:36 +01:00
.setTitle(
supportbot.Ticket_Title.replace(/%ticketauthor%/g, interaction.user.id)
.replace(/%ticketid%/g, ticketChannel.id)
.replace(/%ticketusername%/g, interaction.user.username)
)
.setDescription(
supportbot.TicketMessage.replace(/%ticketauthor%/g, interaction.user.id)
.replace(/%ticketid%/g, ticketChannel.id)
.replace(/%ticketusername%/g, interaction.user.username)
)
.setColor(supportbot.EmbedColour);
if (supportbot.TicketSubject === "embed") {
if (TicketSubject != "No Reason Provided.") {
TicketMessage.addFields({ name: "Reason", value: TicketSubject });
}
}
if (supportbot.TicketSubject === "description") {
if (TicketSubject != "No Reason Provided.") {
2021-12-04 20:57:23 -06:00
await ticketChannel.setTopic(
2021-10-24 22:46:36 +01:00
`Reason: ${TicketSubject} - User ID: ${interaction.user.id} - Ticket: ${ticketChannel.name}`
);
}
}
if (supportbot.TicketDepartments) {
TicketMessage.addFields({
name: "Departments",
value: ` **${supportbot.DepartmentTitle_1}**\n **${supportbot.DepartmentTitle_2}**\n **${supportbot.DepartmentTitle_3}**`,
});
}
2021-12-14 18:04:38 -06:00
const CloseButton = new MessageButton()
2021-10-24 22:46:36 +01:00
.setCustomId("ticketclose")
.setLabel("Close")
.setStyle(supportbot.TicketCloseColour)
.setEmoji(supportbot.TicketCloseEmoji);
2021-12-14 18:04:38 -06:00
const LockButton = new MessageButton()
2021-10-24 22:46:36 +01:00
.setCustomId("ticketlock")
.setLabel("Lock")
.setStyle(supportbot.TicketLockColour)
.setEmoji(supportbot.TicketLockEmoji);
2021-12-14 18:04:38 -06:00
const row2 = new MessageActionRow().addComponents(CloseButton, LockButton);
2021-10-24 22:46:36 +01:00
if (supportbot.TicketDepartments) {
2021-12-14 18:04:38 -06:00
try {
const Department1Button = new MessageButton()
.setCustomId("Department1")
.setLabel(supportbot.DepartmentTitle_1)
.setStyle(supportbot.TicketDept1Colour)
.setEmoji(supportbot.TicketDept1Emoji);
const Department2Button = new MessageButton()
.setCustomId("Department2")
.setLabel(supportbot.DepartmentTitle_2)
.setStyle(supportbot.TicketDept2Colour)
.setEmoji(supportbot.TicketDept2Emoji);
const Department3Button = new MessageButton()
.setCustomId("Department3")
.setLabel(supportbot.DepartmentTitle_3)
.setStyle(supportbot.TicketDept3Colour)
.setEmoji(supportbot.TicketDept3Emoji);
const row = new MessageActionRow().addComponents(
Department1Button,
Department2Button,
Department3Button
);
2021-12-04 20:57:23 -06:00
2021-12-14 18:04:38 -06:00
const m = await ticketChannel.send({
embeds: [TicketMessage],
components: [row],
2021-12-04 20:57:23 -06:00
});
2021-10-24 22:46:36 +01:00
2021-12-18 20:54:01 -06:00
let collector;
try {
2021-12-18 22:59:05 -06:00
const filter = (i) => i.user.id === interaction.user.id;
2021-12-18 20:54:01 -06:00
collector = await m.awaitMessageComponent({
filter,
max: 1,
componentType: "BUTTON",
time: supportbot.Timeout * 60000,
});
} catch (e) {
2021-12-18 22:42:51 -06:00
if (e.code == "INTERACTION_COLLECTOR_ERROR") {
2021-12-18 20:54:01 -06:00
try {
2021-12-18 22:23:18 -06:00
TicketData = await JSON.parse(
fs.readFileSync("./Data/TicketData.json", "utf8")
);
2021-12-18 20:54:01 -06:00
let ticket = await TicketData.tickets.findIndex(
(t) => t.id == ticketChannel.id
);
TicketData.tickets[ticket].open = false;
fs.writeFileSync(
"./Data/TicketData.json",
JSON.stringify(TicketData, null, 4),
(err) => {
if (err) console.error(err);
}
);
interaction.user.send(
"Your ticket has timed out. Please open a new one, and select a department."
);
} catch (e) {}
2021-12-18 22:42:51 -06:00
return await ticketChannel.delete();
}
2021-12-18 20:54:01 -06:00
}
2021-12-14 18:04:38 -06:00
let role;
let title;
if (collector.customId === "Department1") {
role = DeptRole1;
title = supportbot.DepartmentTitle_1;
2021-12-04 20:57:23 -06:00
}
2021-12-14 18:04:38 -06:00
if (collector.customId === "Department2") {
role = DeptRole2;
title = supportbot.DepartmentTitle_2;
}
if (collector.customId === "Department3") {
role = DeptRole3;
title = supportbot.DepartmentTitle_3;
2021-10-24 22:46:36 +01:00
}
2021-12-04 20:57:23 -06:00
2021-12-14 18:04:38 -06:00
ticketChannel.permissionOverwrites.edit(role.id, {
2021-12-04 20:57:23 -06:00
VIEW_CHANNEL: true,
2021-12-14 18:04:38 -06:00
});
ticketChannel.permissionOverwrites.edit(Author.id, {
2021-12-04 20:57:23 -06:00
SEND_MESSAGES: true,
});
2021-12-14 18:04:38 -06:00
await collector.reply({
embeds: [
{
description: `> Thank for reaching out to the **${title} Department**. Please provide us information regarding your query.`,
color: supportbot.EmbedColour,
},
],
});
2021-10-24 22:46:36 +01:00
2021-12-04 20:57:23 -06:00
if (supportbot.AllowTicketMentions) {
await ticketChannel.send("@here");
2021-10-24 22:46:36 +01:00
}
2021-12-04 20:57:23 -06:00
await collector.message.edit({
embeds: [TicketMessage],
components: [row2],
});
2021-12-14 18:04:38 -06:00
} catch (error) {}
2021-10-24 22:46:36 +01:00
} else {
2021-12-04 20:57:23 -06:00
await ticketChannel.send({ embeds: [TicketMessage], components: [row2] });
2021-10-24 22:46:36 +01:00
}
},
});