Currently, I have been working on a Discord.js bot for a discord server I have. I want to make a command so that a user can open a ticket using !oticket and it will make a new channel that all of their dms to the bot will come through. I have working code for the channel creation, but when I tried to add an if statement to make it so that if someone has a ticket already opened they couldn’t open another one, I couldn’t find a working way to select a channel by its name.
Here is my current code:
var test = msg.guild.channels.find(channel => channel.name === msg.author.username); //Outputs null even if a channel with their username already exists
console.log(test);
if (!msg.guild.channels.find(channel => channel.name === msg.author.username)) { // Passes even if a channel with their username already exists
var server = msg.guild; //Works fine
var name = msg.author.username; //Works fine
server.createChannel(name, { //Works fine
type: "text",
parent: client.channels.get("834963781342724096"),
}); //Works fine
} else {
msg.reply("You already have an open ticket!")
}
I know that finding a channel by its name is impractical but this bot will only be on my server.
Advertisement
Answer
You can use a map where you can set a value when the user has created a ticket what would look like this:
const ticketmap = new Map();
if(ticketmap.get(msg.author.id)) return message.reply(`You already have an open ticket!`);
var server = msg.guild; //Works fine
var name = msg.author.username; //Works fine
server.createChannel(name, { //Works fine
type: "text",
parent: client.channels.get("834963781342724096"),
}).then(ch => {
ticketmap.set(msg.author.id, ch.id);
});