2021-10-28 18:03:27 -05:00
const fs = require ( "fs" );
const Discord = require ( "discord.js" );
const yaml = require ( "js-yaml" );
2022-08-12 01:35:36 +01:00
2024-10-10 19:14:45 +01:00
const supportbot = yaml . load ( fs . readFileSync ( "./Configs/supportbot.yml" , "utf8" ));
const cmdconfig = yaml . load ( fs . readFileSync ( "./Configs/commands.yml" , "utf8" ));
const msgconfig = yaml . load ( fs . readFileSync ( "./Configs/messages.yml" , "utf8" ));
2021-10-28 18:03:27 -05:00
const Command = require ( "../Structures/Command.js" );
module . exports = new Command ({
2022-07-23 07:50:12 +01:00
name : cmdconfig . CloseTicket . Command ,
description : cmdconfig . CloseTicket . Description ,
2022-07-22 13:19:05 +01:00
type : Discord . ApplicationCommandType . ChatInput ,
2021-12-18 18:57:26 -06:00
options : [
2021-10-28 18:03:27 -05:00
{
name : "reason" ,
description : "Ticket Close Reason" ,
2022-07-22 13:19:05 +01:00
type : Discord . ApplicationCommandOptionType . String ,
2021-10-28 18:03:27 -05:00
},
],
2022-07-23 07:50:12 +01:00
permissions : cmdconfig . CloseTicket . Permission ,
2021-10-28 18:03:27 -05:00
async run ( interaction ) {
2022-01-30 00:07:04 -06:00
const { getRole , getChannel } = interaction . client ;
2024-06-12 12:54:31 +01:00
2022-08-12 01:35:36 +01:00
if ( supportbot . Ticket . Close . StaffOnly ) {
let SupportStaff = await getRole ( supportbot . Roles . StaffMember . Staff , interaction . guild );
let Admin = await getRole ( supportbot . Roles . StaffMember . Admin , interaction . guild );
2024-06-12 12:54:31 +01:00
2024-11-09 00:37:51 +00:00
if ( ! SupportStaff || ! Admin ) {
return interaction . reply ( "Some roles seem to be missing! Please check for errors when starting the bot." );
}
2022-08-12 01:35:36 +01:00
2024-06-12 12:54:31 +01:00
const NoPerms = new Discord . EmbedBuilder ()
. setTitle ( "Invalid Permissions!" )
2024-10-10 19:14:45 +01:00
. setDescription ( ` ${ msgconfig . Error . IncorrectPerms } \n\nRole Required: \` ${ supportbot . Roles . StaffMember . Staff } \` or \` ${ supportbot . Roles . StaffMember . Admin } \`` )
2024-11-09 00:37:51 +00:00
. setColor ( supportbot . Embed . Colours . Warn );
2021-12-18 19:39:59 -06:00
2024-11-09 00:37:51 +00:00
if ( ! interaction . member . roles . cache . has ( SupportStaff . id ) && ! interaction . member . roles . cache . has ( Admin . id )) {
2021-12-18 19:39:59 -06:00
return interaction . reply ({ embeds : [ NoPerms ] });
2024-11-09 00:37:51 +00:00
}
2021-12-18 19:39:59 -06:00
}
2024-06-12 12:54:31 +01:00
2024-10-10 19:14:45 +01:00
if (
( supportbot . Ticket . TicketType === "threads" && interaction . channel . type !== Discord . ChannelType . PrivateThread ) ||
( supportbot . Ticket . TicketType === "channels" && interaction . channel . type !== Discord . ChannelType . GuildText )
) {
2024-06-12 12:54:31 +01:00
const NotTicketChannel = new Discord . EmbedBuilder ()
. setTitle ( "Invalid Channel!" )
2024-10-10 19:14:45 +01:00
. setDescription ( `This command can only be used in a ${ supportbot . Ticket . TicketType === "threads" ? "ticket thread" : "ticket channel" } .` )
2024-11-09 00:37:51 +00:00
. setColor ( supportbot . Embed . Colours . Warn ); // Ensure valid color
2024-06-12 12:54:31 +01:00
return interaction . reply ({ embeds : [ NotTicketChannel ], ephemeral : true });
}
2024-11-09 00:37:51 +00:00
await interaction . deferReply (); // Deferring to avoid double replies
// Reading TicketData.json once for efficiency
let tickets ;
try {
tickets = JSON . parse ( fs . readFileSync ( "./Data/TicketData.json" , "utf8" ));
} catch ( err ) {
console . error ( 'Error reading ticket data file:' , err );
return interaction . followUp ({ content : "There was an error loading ticket data." });
}
2024-06-12 12:54:31 +01:00
let TicketData = tickets . tickets . findIndex (( t ) => t . id === interaction . channel . id );
2021-12-18 22:14:11 -06:00
let ticket = tickets . tickets [ TicketData ];
2024-06-12 12:54:31 +01:00
2022-01-29 23:57:10 -06:00
if ( TicketData === - 1 ) {
2022-07-22 13:19:05 +01:00
const Exists = new Discord . EmbedBuilder ()
2021-10-28 18:03:27 -05:00
. setTitle ( "No Ticket Found!" )
2022-08-12 01:35:36 +01:00
. setDescription ( msgconfig . Error . NoValidTicket )
2024-11-09 00:37:51 +00:00
. setColor ( supportbot . Embed . Colours . Warn ); // Ensure valid color
2021-12-04 20:57:23 -06:00
return interaction . followUp ({ embeds : [ Exists ] });
2021-10-28 18:03:27 -05:00
}
2024-06-12 12:54:31 +01:00
let reason = interaction . options ? . getString ( "reason" ) || "No Reason Provided." ;
2024-11-09 00:37:51 +00:00
if ( supportbot . Ticket . ReviewSystem . Enabled ) {
const rating = await collectReviewRating ( interaction );
const comment = await collectReviewComment ( interaction );
const reviewChannel = await getChannel ( supportbot . Ticket . ReviewSystem . Channel , interaction . guild );
const reviewer = supportbot . Ticket . ClaimTickets ? `<@ ${ ticket . claimedBy } >` : null ;
const reviewEmbed = new Discord . EmbedBuilder ()
. setTitle ( msgconfig . ReviewSystem . ReviewEmbed . Title )
. addFields (
{
name : msgconfig . ReviewSystem . ReviewEmbed . RatingTitle ,
value : ` ${ msgconfig . ReviewSystem . ReviewEmbed . ReviewEmoji . repeat ( rating ) } ` ,
inline : false
},
{
name : msgconfig . ReviewSystem . ReviewEmbed . CommentTitle ,
value : `\`\`\` ${ comment } \`\`\`` ,
inline : false
}
)
. setColor ( msgconfig . ReviewSystem . ReviewEmbed . Color );
if ( supportbot . Ticket . ClaimTickets . Enabled ) {
reviewEmbed . setDescription (
`** ${ msgconfig . ReviewSystem . ReviewEmbed . ReviewedStaffTitle } ** ${ reviewer } \n** ${ msgconfig . ReviewSystem . ReviewEmbed . ReviewedByTitle } ** <@ ${ interaction . user . id } >` || `N/A`
);
} else {
reviewEmbed . setDescription (
`** ${ msgconfig . ReviewSystem . ReviewEmbed . ReviewedByTitle } ** <@ ${ interaction . user . id } >`
);
}
2024-06-12 12:54:31 +01:00
2024-11-09 00:37:51 +00:00
if ( reviewChannel ) {
await reviewChannel . send ({ embeds : [ reviewEmbed ] });
}
2024-06-12 12:54:31 +01:00
2024-11-09 00:37:51 +00:00
if ( ! interaction . replied && ! interaction . deferred ) {
await interaction . followUp ({ embeds : [ reviewEmbed ] });
}
2024-06-12 12:54:31 +01:00
2024-11-09 00:37:51 +00:00
await handleCloseTicket ( interaction , reason , ticket , TicketData );
} else {
await handleCloseTicket ( interaction , reason , ticket , TicketData );
}
2024-06-12 12:54:31 +01:00
},
});
2024-11-09 00:37:51 +00:00
async function collectReviewRating ( interaction ) {
const reviewPrompt = new Discord . EmbedBuilder ()
. setTitle ( msgconfig . ReviewSystem . Rate . Title )
. setDescription ( msgconfig . ReviewSystem . Rate . Description )
. setColor ( supportbot . Embed . Colours . General ); // Ensure valid color
const stars = new Discord . ActionRowBuilder (). addComponents (
new Discord . StringSelectMenuBuilder ()
. setCustomId ( "starRating" )
. setPlaceholder ( "Select a rating" )
. addOptions ([
{ label : msgconfig . ReviewSystem . Stars . One , value : "1" },
{ label : msgconfig . ReviewSystem . Stars . Two , value : "2" },
{ label : msgconfig . ReviewSystem . Stars . Three , value : "3" },
{ label : msgconfig . ReviewSystem . Stars . Four , value : "4" },
{ label : msgconfig . ReviewSystem . Stars . Five , value : "5" },
])
);
await interaction . followUp ({
embeds : [ reviewPrompt ],
components : [ stars ],
ephemeral : true ,
});
const starRating = await interaction . channel . awaitMessageComponent ({
componentType : Discord . ComponentType . StringSelect ,
filter : ( i ) => i . customId === "starRating" && i . user . id === interaction . user . id ,
time : 30000 ,
});
await starRating . deferUpdate ();
return starRating . values [ 0 ];
}
async function collectReviewComment ( interaction ) {
const commentPrompt = new Discord . EmbedBuilder ()
. setTitle ( msgconfig . ReviewSystem . Comment . Title )
. setDescription ( msgconfig . ReviewSystem . Comment . Description )
. setColor ( supportbot . Embed . Colours . General ); // Ensure valid color
await interaction . followUp ({
embeds : [ commentPrompt ],
ephemeral : true ,
});
const filter = ( response ) => response . author . id === interaction . user . id ;
const commentCollection = await interaction . channel . awaitMessages ({ filter , max : 1 , time : 30000 });
const comment = commentCollection . first () ? . content ;
return comment && comment . toLowerCase () !== "no" ? comment : "No Comment Provided" ;
}
2024-06-12 12:54:31 +01:00
async function handleCloseTicket ( interaction , reason , ticket , TicketData ) {
const { getChannel } = interaction . client ;
let tickets = JSON . parse ( fs . readFileSync ( "./Data/TicketData.json" , "utf8" ));
let tUser = interaction . client . users . cache . get ( ticket . user );
2024-10-10 19:14:45 +01:00
let transcriptChannel = await getChannel ( supportbot . Ticket . Log . TicketDataLog , interaction . guild );
2024-06-12 12:54:31 +01:00
2024-10-10 19:14:45 +01:00
if ( ! transcriptChannel )
2024-11-09 00:37:51 +00:00
return console . log ( "Ticket Log Missing" );
2024-06-12 12:54:31 +01:00
try {
tickets . tickets [ TicketData ]. open = false ;
2024-11-09 00:37:51 +00:00
fs . writeFileSync ( "./Data/TicketData.json" , JSON . stringify ( tickets , null , 4 ));
2024-06-12 12:54:31 +01:00
const transcriptEmbed = new Discord . EmbedBuilder ()
2024-10-10 19:14:45 +01:00
. setTitle ( msgconfig . TicketLog . Title )
2024-11-09 00:37:51 +00:00
. setColor ( msgconfig . TicketLog . Colour ) // Ensure valid color
. setFooter ({ text : supportbot . Embed . Footer , iconURL : interaction . user . displayAvatarURL () })
. setDescription ( `> **Ticket:** ${ interaction . channel . name } (\` ${ interaction . channel . id } \`)\n> **User:** ${ tUser ? . username || "N/A" } # ${ tUser ? . discriminator || "N/A" } (\` ${ tUser ? . id || ticket . user } \`)\n> **Closed by:** <@ ${ interaction . user . id } >` )
. addFields ({ name : "Reason" , value : `\`\`\` ${ reason } \`\`\`` , inline : true });
2024-06-12 12:54:31 +01:00
let msgs = await interaction . channel . messages . fetch ({ limit : 100 });
2024-11-09 00:37:51 +00:00
let html = createTranscriptHTML ( interaction , msgs );
2024-06-12 12:54:31 +01:00
const fileName = ` ${ interaction . channel . name } .html` ;
2024-11-09 00:37:51 +00:00
let file = new Discord . AttachmentBuilder ( Buffer . from ( html ), { name : `SPOILER_ ${ fileName } ` });
2024-06-12 12:54:31 +01:00
2024-11-09 00:37:51 +00:00
transcriptChannel . send ({
embeds : [ transcriptEmbed ],
files : [ file ],
2024-06-12 12:54:31 +01:00
});
2024-11-09 00:37:51 +00:00
} catch ( err ) {
console . log ( "Error closing ticket:" , err );
2024-06-12 12:54:31 +01:00
}
2024-11-09 00:37:51 +00:00
await interaction . channel . delete ();
}
2024-06-12 12:54:31 +01:00
2024-11-09 00:37:51 +00:00
function createTranscriptHTML ( interaction , msgs ) {
let html = `
<html>
<head>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.message { padding: 10px; margin: 5px 0; border: 1px solid #ddd; }
.username { font-weight: bold; }
</style>
</head>
<body>
<h1>Ticket Transcript</h1>
<h2>Ticket: ${ interaction . channel . name } </h2>
<h3>User: ${ interaction . user . tag } </h3>
<div id="messages">
` ;
msgs . reverse (). forEach (( msg ) => {
html += `
<div class="message">
<span class="username"> ${ msg . author . tag } </span>: ${ msg . content }
</div>
` ;
});
html += `
</div>
</body>
</html>
` ;
return html ;
2024-06-12 12:54:31 +01:00
}