我该如何提及discord.js v13中的两个角色


const role = message.guild.roles.cache.find(role => role.name === ['Muted' || 'muted'])

上面的代码用于静音(和取消静音(命令。通常情况下,服务器具有M资本或较小资本,这会影响编码中的角色。对于上面的代码,它返回一个错误,称为Unknown code

对于初学者,让我们看看您的代码实际在做什么。

const role = message.guild.roles.cache.find(role => role.name === ['Muted' || 'muted'])

此代码相当于

const role = message.guild.roles.cache.find(role => role.name === ['Muted'])

因为CCD_ 3评估为CCD_。请注意,这种比较总是会失败,因为无法使用===运算符比较不同的数组。

["muted"] === ["muted"] // this is false

如果要对数组进行检查,则需要自己实现一些迭代逻辑,或者使用Array#someArray#includes等有用的助手。

进行案例无意义比较的正确方法可能是类似的方法

const role = 
message.guild.roles.cache.find(role => role.name.toLowerCase() === "muted")

如果您真的只想支持Mutedmuted(例如不支持mUtEd(,那么您的方法只需进行一个小的调整即可工作。

const role 
= message.guild.roles.cache.find(role => ["muted","Muted"].includes(role.name))
//                                         ^ anonymous array of acceptable values 

降低角色名称的大小写并将其与小写字符串进行比较

const role = message.guild.roles.cache
.find(role => role.name.toLowerCase() == 'muted');

如果您确实需要检查字符串数组中可能的角色名称,请使用Array#includes()

const role = message.guild.roles.cache
.find(role => ['foo', 'bar', 'baz'].includes(role.name));

最新更新