【问题标题】:Discord.js bot: How can I check if the person who pings has certain roleDiscord.js bot:如何检查 ping 的人是否具有特定角色
【发布时间】:2021-07-07 05:11:38
【问题描述】:

我试图创建一个关于 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 员工”

【问题讨论】:

  • 它应该可以正常工作。那么角色ID肯定是错误的。同样在回复don't ping staff 后,message.delete() 也不起作用。您需要使用.then()删除已发送的消息。

标签: javascript discord discord.js bots


【解决方案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
}

【讨论】:

    猜你喜欢
    • 2021-10-25
    • 2020-09-21
    • 2021-02-24
    • 2023-03-03
    • 2020-12-25
    • 2021-08-16
    • 2020-10-12
    • 1970-01-01
    • 2021-04-06
    相关资源
    最近更新 更多