How to get the name of a closed channel for ticket system so it can be logged?
I'm trying to think of a way to get the channel name that the c!close
command will be ran in so the channel can be logged in the logs channel when it gets deleted or closed.
This might be an easy task to do, but I can't think of the solution to this off the top of my head, and was unable to find anything on google to help.
const { MessageEmbed } = require("discord.js");
module.exports = {
name: "close",
aliases: ["delete"],
description: "Closes an open ticket",
usage: "close",
run: async (client, message, args) => {
if (!message.member.permissions.has("MANAGE_CHANNELS")) {
return message.reply("You do not have perms to use this command. - `[MANAGE_CHANNELS]`")
}
if (message.channel.name.includes("ticket-")) {
message.reply("Closing ticket, please wait...").then(msg => {
setTimeout(() => msg.channel.delete(), 3000)
}).catch();
} else {
return message.reply("This is not a ticket channel, please run this command inside of an open ticket channel.");
}
const logchannel = message.guild.channels.cache.find(channel => channel.name === "action-log");
if (logchannel) {
const ticketLogs = new MessageEmbed()
.setTitle("Ticket Closed")
.addField("Name", `<:CL_Reply:909436090413363252> \`CHANNEL NAME\``) //I want to be able to get the name of the channel that is being deleted so it can be logged in my logs channel.
.addField("Closed by", `<:CL_Reply:909436090413363252> \`${message.author.tag}\``)
.addField("Closed", `<:CL_Reply:909436090413363252> <t:${Math.floor(Date.now() / 1000)}:R>`)
.setFooter("Crimson - Ticket Logger", client.user.displayAvatarURL())
.setColor("RED")
.setTimestamp();
logchannel.send({ embeds: [ticketLogs] });
}
}
};
this command is for my ticket system for my bot, so it will be helpful to get info off of a closed channel whenever this command is executed inside of the channel.
Solution 1:
What you could do is have a temporary variable outside of the ticket check code which can be set as the channel name.
if (!message.member.permissions.has("MANAGE_CHANNELS")) {
...
}
let deletedTicketName;
if (message.channel.name.includes("ticket-")) {
message.reply("Closing ticket, please wait...").then(msg => {
deletedTicketName = message.channel.name;
setTimeout(() => msg.channel.delete(), 3000)
}).catch();
} else {
...
}
and then reference that in the logging part.
IF you just assign a the variable as the channel, it may not exist as you do .delete(), however I am not 100% sure of that.
There may be another way of doing it but this works and you get any of the channel information.