問題描述
我正在 discord.js 上編寫我自己的票務機器人.我需要檢查公會是否有頻道(按名稱).這是一小段代碼:
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;
}
我嘗試了以下方法:
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')
但沒有任何幫助
解決方案:正如 @Chiitoi 所建議的,在這種情況下最好使用 some()
函數.另外,user.username
返回的是帶有空格和特殊字符的字符串,所以需要解析一下.
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.
此代碼適用于我:
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;
}
推薦答案
我認為 some() 方法 在這里可能很好.user.username
返回什么?我想知道返回的值是否有任何特殊或唯一字符.
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.
這篇關于discord.js v12 檢查頻道是否存在的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!