I'm coding my own ticket bot on discord.js. I need to check if guild have a channel (by name). Here's little piece of code:
if (userTickets.has(user.id) || reaction.message.guild.channels.cache.???(c => c.name === user.username.toLowerCase() + 's-ticket')) {
user.send("You already have a ticket!");
return;
}
I tried the following:
reaction.message.guild.channels.cache.find(c => c.name === user.username.toLowerCase() + 's-ticket')
reaction.message.guild.channels.cache.has(c => c.name === user.username.toLowerCase() + 's-ticket')
reaction.message.guild.channels.cache.has(user.username.toLowerCase() + 's-ticket')
But nothing helped
SOLUTION:
As suggested by @Chiitoi, in this case it is better to use some()
function.
Also, user.username
returns string with spaces and special characters, so you need to parse it.
This code works for me:
if (userTickets.has(user.id) || reaction.message.guild.channels.cache.some((channel) => channel.name === username.replace(/[^a-zA-Z0-9-]/ig, "") + 's-ticket')) {
user.send("You already have a ticket!");
return;
}