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) {
2022-02-11 23:34:38 -06:00
let disableCommand = true;
if (interaction.isCommand() && disableCommand)
return interaction.reply({
content: "This command is disabled",
ephemeral: true,
});
let department = interaction.customId?.split("-")[1] || null;
2021-12-18 20:54:01 -06:00
let TicketData = await JSON.parse(
fs.readFileSync("./Data/TicketData.json", "utf8")
);
2022-02-04 17:55:02 -06:00
const { getRole, 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 &&
2022-02-04 17:55:02 -06:00
TicketData.tickets.filter((t) => t.user === interaction.user.id && t.open)
2021-12-18 20:54:01 -06:00
.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!")
2022-02-11 23:47:06 -06:00
.setDescription(supportbot.TicketExists);
2021-10-24 22:46:36 +01:00
if (
2022-02-04 17:55:02 -06:00
await interaction.guild.channels.cache.find(
(ticketChannel) =>
ticketChannel.name === `${supportbot.TicketPrefix}${ticketNumberID}`
)
2021-10-24 22:46:36 +01:00
) {
2022-02-04 17:55:02 -06:00
return await interaction.reply({
embeds: [TicketExists],
ephemeral: true,
});
2021-10-24 22:46:36 +01:00
}
const Staff = await getRole(supportbot.Staff, interaction.guild);
const Admin = await getRole(supportbot.Admin, interaction.guild);
2022-02-11 23:34:38 -06:00
if (!Staff || !Admin)
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,
});
let TicketCategory = await getCategory(
2022-06-22 23:30:59 +01:00
supportbot.Departments[department].category,
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],
},
{
2022-02-17 19:57:11 -06:00
id: interaction.user.id,
2021-12-14 18:04:38 -06:00
allow: [Permissions.FLAGS.VIEW_CHANNEL],
},
{
id: interaction.guild.id,
deny: [Permissions.FLAGS.VIEW_CHANNEL],
},
],
2021-10-24 22:46:36 +01:00
}
);
2022-02-11 23:34:38 -06:00
if (supportbot.AllowAllStaff) {
await ticketChannel.permissionOverwrites.edit(Staff.id, {
2021-12-14 18:04:38 -06:00
VIEW_CHANNEL: true,
});
}
2022-06-23 00:05:08 +01:00
if (!department) {
2022-02-11 23:34:38 -06:00
await ticketChannel.permissionOverwrites.edit(interaction.user.id, {
2021-12-19 13:02:43 -06:00
SEND_MESSAGES: false,
});
}
2021-11-23 15:48:45 -06:00
await TicketData.tickets.push({
id: ticketChannel.id,
name: ticketChannel.name,
2022-02-17 19:57:11 -06:00
user: interaction.user.id,
2021-11-23 15:48:45 -06:00
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") {
2022-02-04 17:55:02 -06:00
if (TicketSubject !== "No Reason Provided.") {
2021-10-24 22:46:36 +01:00
TicketMessage.addFields({ name: "Reason", value: TicketSubject });
}
}
if (supportbot.TicketSubject === "description") {
2022-02-04 17:55:02 -06:00
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}`
);
}
}
2022-06-23 00:05:08 +01:00
if (!department) {
2021-10-24 22:46:36 +01:00
TicketMessage.addFields({
name: "Departments",
2022-02-11 23:34:38 -06:00
value: ` **${supportbot.Departments.map((X) => X.title).join("\n")}**`,
2021-10-24 22:46:36 +01:00
});
}
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);
2022-06-23 00:05:08 +01:00
if (!department) {
2021-12-14 18:04:38 -06:00
try {
2022-02-11 23:34:38 -06:00
let buttons = await supportbot.Departments.map((x) =>
new MessageButton()
.setCustomId("Department" + supportbot.Departments.indexOf(x))
.setLabel(x.title)
.setStyle(x.color)
.setEmoji(x.emoji)
2021-12-14 18:04:38 -06:00
);
2022-02-11 23:34:38 -06:00
const row = new MessageActionRow().addComponents(buttons);
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) {
2022-02-04 17:55:02 -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(
2022-02-04 17:56:11 -06:00
(t) => t.id === ticketChannel.id
2021-12-18 20:54:01 -06:00
);
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
}
2022-02-11 23:34:38 -06:00
let num = collector.customId.split("Department")[1];
let role = getRole(supportbot.Departments[num].role, interaction.guild);
let title = supportbot.Departments[num].title;
2021-12-04 20:57:23 -06:00
2022-02-04 17:55:02 -06:00
await ticketChannel.permissionOverwrites.edit(role.id, {
2021-12-04 20:57:23 -06:00
VIEW_CHANNEL: true,
2021-12-14 18:04:38 -06:00
});
2022-02-17 19:57:11 -06:00
await ticketChannel.permissionOverwrites.edit(interaction.user.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 {
2022-02-11 23:34:38 -06:00
await ticketChannel.send({
embeds: [TicketMessage],
components: [row2],
});
if (department) {
let role = await getRole(
supportbot.Departments[department].role,
interaction.guild
);
let title = supportbot.Departments[department].title;
await ticketChannel.permissionOverwrites.edit(role.id, {
VIEW_CHANNEL: true,
});
let TicketCat = await getCategory(
2022-06-22 23:30:59 +01:00
supportbot.Departments[department].category,
2022-02-11 23:34:38 -06:00
interaction.guild
);
2022-02-25 21:51:22 -06:00
await ticketChannel.setParent(TicketCat.id, { lockPermissions: false });
if (supportbot.AllowThanksForReachingOutMessage) {
await ticketChannel.send({
embeds: [
{
description: `> Thank for reaching out to the **${title} Department**. Please provide us information regarding your query.`,
color: supportbot.EmbedColour,
},
],
});
}
2022-02-11 23:34:38 -06:00
}
if (supportbot.AllowTicketMentions) {
2022-06-23 00:05:08 +01:00
await ticketChannel.send(supportbot.TicketRoleMention);
2022-02-11 23:34:38 -06:00
}
2021-10-24 22:46:36 +01:00
}
},
});