0

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;
}

1 Answers1

0

I think the some() method might be good here. And what does user.username return? I'm wondering if the returned value has any special or unique characters.

Chiitoi
  • 71
  • 2
  • Thank you VERY MUCH! Yes, *user.username* returned string that had special characters and spaces. So, the solution is like this: `reaction.message.guild.channels.cache.some((channel) => channel.name === username.replace(/[^a-zA-Z0-9-]/ig, "") + 's-ticket')` – Nikita Emelianov Jul 25 '20 at 06:13