[英]discord.js v12 check if channel exists
I'm coding my own ticket bot on discord.js.我在 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.解决方案:正如@Chiitoi所建议的那样,在这种情况下,最好使用
some()
function。 Also, user.username
returns string with spaces and special characters, so you need to parse it.此外,
user.username
返回带有空格和特殊字符的字符串,因此您需要对其进行解析。
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;
}
I think the some() method might be good here.我认为some() 方法在这里可能很好。 And what does
user.username
return? user.username
返回什么? I'm wondering if the returned value has any special or unique characters.我想知道返回的值是否有任何特殊或唯一字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.