Discord.js bot:如何检查 ping 的人是否具有特定角色

时间:2021-04-12 05:17:53

标签: javascript discord discord.js bots

我试图创建一个关于 ping 员工的警告,我当前的代码是:

const roleId = "761874957705674762";

client.on("message", async message => {
    if (message.author.bot) return false;

    if (message.mentions.has(roleId)) {
        await message.delete();
        message.reply(`dont ping staff!`);
        message.delete(5000);
    };
});

我没有收到任何错误信息,但我也没有得到想要的回复“不要 ping 员工”

1 个答案:

答案 0 :(得分:1)

您可以通过角色 ID 检查用户的角色:

// assuming role.id is an actual ID of a valid role:
if(message.member.roles.cache.has(role.id)) {
  console.log(`Yay, the author of the message has the role!`);
} else {
  console.log(`Nope, noppers, nadda.`);
}

如果你想检查他是否有多个角色之一,你可以这样做:

// Check if they have one of many roles
if(message.member.roles.cache.some(r=>["Dev", "Mod", "Server Staff", "Proficient"].includes(r.name)) ) {
  // has one of the roles
} else {
  // has none of the roles
}