From 33f7675aacbf5262e232865bc7acbb2436ffd6da Mon Sep 17 00:00:00 2001 From: Noah Date: Sat, 18 Dec 2021 20:54:01 -0600 Subject: [PATCH] Added Max Ticket Count + Timeout --- Commands/close.js | 13 ++++++++- Commands/ticket.js | 61 +++++++++++++++++++++++++++++++++--------- Configs/supportbot.yml | 7 ++--- 3 files changed, 65 insertions(+), 16 deletions(-) diff --git a/Commands/close.js b/Commands/close.js index 22a5718..22a251d 100644 --- a/Commands/close.js +++ b/Commands/close.js @@ -47,9 +47,12 @@ module.exports = new Command({ return interaction.reply({ embeds: [NoPerms] }); } await interaction.deferReply(); - let TicketData = await JSON.parse( + let tickets = await JSON.parse( fs.readFileSync("./Data/TicketData.json", "utf8") ).tickets.find((t) => t.id === interaction.channel.id); + let TicketData = await tickets.tickets.findIndex( + (t) => t.id == ticketChannel.id + ); if (!TicketData) { const Exists = new Discord.MessageEmbed() .setTitle("No Ticket Found!") @@ -87,6 +90,14 @@ module.exports = new Command({ }); } try { + tickets.tickets[TicketData].open = false; + fs.writeFileSync( + "./Data/TicketData.json", + JSON.stringify(tickets, null, 4), + (err) => { + if (err) console.error(err); + } + ); await interaction.followUp(`**${supportbot.ClosingTicket}**`); const transcriptEmbed = new Discord.MessageEmbed() .setTitle(supportbot.TranscriptTitle) diff --git a/Commands/ticket.js b/Commands/ticket.js index 03a9289..c7904a4 100644 --- a/Commands/ticket.js +++ b/Commands/ticket.js @@ -31,11 +31,29 @@ module.exports = new Command({ permissions: ["SEND_MESSAGES"], async run(interaction) { + let TicketData = await JSON.parse( + fs.readFileSync("./Data/TicketData.json", "utf8") + ); const { getRole, getChannel, getCategory } = interaction.client; - let reactionUser = interaction.guild.members.cache.get(interaction.user.id); + let User = interaction.guild.members.cache.get(interaction.user.id); if ( - reactionUser.roles.cache.has( + 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, + }, + ], + }); + } + if ( + User.roles.cache.has( getRole(supportbot.TicketBlackListRole, interaction.guild).id ) ) { @@ -49,10 +67,6 @@ module.exports = new Command({ // Ticket ID let ticketNumberID = await TicketNumberID.pad(); - let TicketData = await JSON.parse( - fs.readFileSync("./Data/TicketData.json", "utf8") - ); - // Ticket Subject const TicketSubject = interaction.options?.getString("reason") || supportbot.NoTicketSubject; @@ -127,6 +141,7 @@ module.exports = new Command({ user: Author.id, number: ticketNumberID, reason: TicketSubject, + open: true, subUsers: [], }); fs.writeFileSync( @@ -232,12 +247,34 @@ module.exports = new Command({ }); const filter = (i) => i.user.id === interaction.user.id; - const collector = await m.awaitMessageComponent({ - filter, - max: 1, - componentType: "BUTTON", - time: 60000, - }); + let collector; + try { + collector = await m.awaitMessageComponent({ + filter, + max: 1, + componentType: "BUTTON", + time: supportbot.Timeout * 60000, + }); + } catch (e) { + if (e.code == "INTERACTION_COLLECTOR_ERROR") + try { + 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) {} + return await ticketChannel.delete(); + } let role; let title; if (collector.customId === "Department1") { diff --git a/Configs/supportbot.yml b/Configs/supportbot.yml index bbe5b06..762c84b 100644 --- a/Configs/supportbot.yml +++ b/Configs/supportbot.yml @@ -17,7 +17,6 @@ SupportBot_Version: "7.4.0" # ------------------------- Name: "SupportBot" -# No Prefix, as everything uses the new Slash (/) Commands feature! Token: "BOT_TOKEN" # Guild ID Ex: 123456789123456789 Guild: "" @@ -95,6 +94,8 @@ TranscriptTitle: "Ticket Transcript" TicketLog_Title: "Ticket Closed" TicketBlackListMessage: "You are not allowed to open a ticket" +MaxAllowedTickets: 2 + TicketSubject: "description" # "description" will displays the ticket reason within the channel description # "embed" will displays the ticket reason within the ticket embed @@ -153,13 +154,13 @@ TicketDept3Emoji: "3️⃣" TranscriptSavedMessage: "📝 **Transcript Saved!**" TicketLockMessage: "❌ **Ticket Locked!**" TicketDeleteMessage: "⚠ **Ticket Deleting!**" # Tickets deleting after 5 seconds by default. -TicketDeleteTime: 5000 # Time must be set in milliseconds. # --- Departments Config --- # --- Start --- TicketDepartments: true - +# Delete the ticket if no department selected. TIME IN MINUTES +Timeout: 5 AllowStaff: true # If set to false users with the "Staff" role will not be able to view tickets # without a valid department role set below.