我正在 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模板网!
discord.js v12:我如何等待 DM 频道中的消息?discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 频道中的消息?)
如何让我的机器人提及发出该机器人命令的人how to make my bot mention the person who gave that bot command(如何让我的机器人提及发出该机器人命令的人)
如何修复必须使用导入来加载 ES 模块 discord.jsHow to fix Must use import to load ES Module discord.js(如何修复必须使用导入来加载 ES 模块 discord.js)
如何列出来自特定服务器的所有成员?How to list all members from a specific server?(如何列出来自特定服务器的所有成员?)
Discord bot:修复“找不到 FFMPEG"Discord bot: Fix ‘FFMPEG not found’(Discord bot:修复“找不到 FFMPEG)
使用 discord.js 加入 discord 服务器时的欢迎消息Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服务器时的欢迎消息)