简体   繁体   中英

Discord.js bot: How can I check if the person who pings has certain role

I was trying to create a warning about pinging staff, my current code is:

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);
    };
});

I don't get any errors, but I also don't get the desired respond "don't ping staff"

You can check a user for roles by the role 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.`);
}

And if you want to check if he has one of multiple roles, you can do this:

// 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM