Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set ticket Category #485

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Update ticket.js
DarthMayTriX authored Feb 27, 2024
commit 7a12f3b7e9594435de3a4c8eaa9f75d80b53d16d
91 changes: 64 additions & 27 deletions src/handlers/ticket.js
Original file line number Diff line number Diff line change
@@ -18,7 +18,6 @@ const { error } = require("@helpers/Logger");

const OPEN_PERMS = ["ManageChannels"];
const CLOSE_PERMS = ["ManageChannels", "ReadMessageHistory"];

/**
* @param {import('discord.js').Channel} channel
*/
@@ -76,7 +75,7 @@ async function closeTicket(channel, closedBy, reason) {

let content = "";
reversed.forEach((m) => {
content += `[${new Date(m.createdAt).toLocaleString("en-US")}] - ${m.author.username}\n`;
content += `[${new Date(m.createdAt).toLocaleString("en-US")}] - ${m.author.tag}\n`;
if (m.cleanContent !== "") content += `${m.cleanContent}\n`;
if (m.attachments.size > 0) content += `${m.attachments.map((att) => att.proxyURL).join(", ")}\n`;
content += "\n";
@@ -96,19 +95,19 @@ async function closeTicket(channel, closedBy, reason) {

if (channel.deletable) await channel.delete();

const embed = new EmbedBuilder().setAuthor({ name: "Ticket Closed" }).setColor(TICKET.CLOSE_EMBED);
const embed = new EmbedBuilder().setAuthor({ name: "Ticket Closed & deleted" }).setColor(TICKET.CLOSE_EMBED);
const fields = [];

if (reason) fields.push({ name: "Reason", value: reason, inline: false });
fields.push(
{
name: "Opened By",
value: ticketDetails.user ? ticketDetails.user.username : "Unknown",
value: ticketDetails.user ? ticketDetails.user.tag : "Unknown",
inline: true,
},
{
name: "Closed By",
value: closedBy ? closedBy.username : "Unknown",
value: closedBy ? closedBy.tag : "Unknown",
inline: true,
}
);
@@ -170,7 +169,16 @@ async function handleTicketOpen(interaction) {
if (alreadyExists) return interaction.followUp(`You already have an open ticket`);

const settings = await getSettings(guild);
// Retrieve the category ID from guild settings
const categoryId = settings.ticket.category_channel;

// Get the category channel by ID
const categoryChannel = guild.channels.cache.get(categoryId);

// Ensure that the category channel exists and is a category
if (!categoryChannel || categoryChannel.type !== 4) {
return interaction.followUp("Invalid category ID set for ticket creation.");
}
// limit check
const existing = getTicketChannels(guild).size;
if (existing > settings.ticket.limit) return interaction.followUp("There are too many open tickets. Try again later");
@@ -204,23 +212,41 @@ async function handleTicketOpen(interaction) {
catName = res.values[0];
catPerms = categories.find((cat) => cat.name === catName)?.staff_roles || [];
}

// Retrieve category channel ID from guild settings
let catChannel = null;
if (categoryId) {
catChannel = guild.channels.cache.get(categoryId);
}
try {
const ticketNumber = (existing + 1).toString();
const permissionOverwrites = [
{
id: guild.roles.everyone,
deny: ["ViewChannel"],
},
{
id: user.id,
allow: ["ViewChannel", "SendMessages", "ReadMessageHistory"],
},
{
id: guild.members.me.roles.highest.id,
allow: ["ViewChannel", "SendMessages", "ReadMessageHistory"],
},
];
const permissionOverwrites = [
{
id: guild.roles.everyone.id,
deny: ["ViewChannel"],
},
{
id: user.id,
allow: ["ViewChannel", "SendMessages", "ReadMessageHistory"],
},
];

// Get staff roles from settings
const staffRoles = settings.ticket?.staff_roles || [];


// Loop through each staff role ID
staffRoles.forEach(roleId => {
const role = guild.roles.cache.get(roleId);
if (role) {
permissionOverwrites.push({
id: role.id,
allow: ["ViewChannel", "SendMessages", "ReadMessageHistory"],
});
}
});




if (catPerms?.length > 0) {
catPerms?.forEach((roleId) => {
@@ -233,32 +259,43 @@ async function handleTicketOpen(interaction) {
});
}

const username = interaction.user.username;

const tktChannel = await guild.channels.create({
name: `tіcket-${ticketNumber}`,
name: `${username}-${ticketNumber}`,
type: ChannelType.GuildText,
topic: `tіcket|${user.id}|${catName || "Default"}`,
topic: `${username}|${user.id}|${catName || "Default"}`,
permissionOverwrites,
parent: categoryId,
});

const staffRolesPing = staffRoles.map(roleId => `<@&${roleId}>`).join(' ');
const embed = new EmbedBuilder()
.setAuthor({ name: `Ticket #${ticketNumber}` })
.setDescription(
`Hello ${user.toString()}
Support will be with you shortly
${staffRolesPing} will be with you shortly
${catName ? `\n**Category:** ${catName}` : ""}
`
)
.setFooter({ text: "You may close your ticket anytime by clicking the button below" });

let buttonsRow = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setLabel("Close Ticket")
.setLabel("Close & delete Ticket")
.setCustomId("TICKET_CLOSE")
.setEmoji("🔒")
.setStyle(ButtonStyle.Primary)
);

const sent = await tktChannel.send({ content: user.toString(), embeds: [embed], components: [buttonsRow] });
// Ping staff roles if present
if (staffRoles.length > 0) {
const staffRolesPing = staffRoles.map(roleId => `<@&${roleId}>`).join(' ');
const messageContent = `**New ticket**\n${staffRolesPing}`;
await tktChannel.send({ content: messageContent, allowedMentions: { parse: ["everyone", "roles", "users"] } });
} else {
await tktChannel.send("**New ticket**");
}
const sent = await tktChannel.send({ content: user.toString(), embeds: [embed], components: [buttonsRow], allowedMentions: { parse: ["users"] } });

const dmEmbed = new EmbedBuilder()
.setColor(TICKET.CREATE_EMBED)
@@ -276,7 +313,7 @@ async function handleTicketOpen(interaction) {

user.send({ embeds: [dmEmbed], components: [row] }).catch((ex) => {});

await interaction.editReply(`Ticket created! 🔥`);
await interaction.editReply(`💯 Ticket created! in\n${tktChannel}`);
} catch (ex) {
error("handleTicketOpen", ex);
return interaction.editReply("Failed to create ticket channel, an error occurred!");