首页 > 解决方案 > 如果有角色 Discord.js 发送消息

问题描述

如果单击反应的用户具有角色,机器人会发送消息 "ID"

我决定试试这个,但没有成功

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

====这里是主要代码====

       sentMessage.react("✅");
        message.delete({ timeout: 100 });
        const filter = (reaction, user) => {
          return !user.bot && ["✅"].includes(reaction.emoji.name);
        };

        sentMessage
          .awaitReactions(filter, {
            max: 1,
            time: 60000,
          })
          .then((collected) => {
            const reaction = collected.first();

            if (reaction.emoji.name === "✅") {
              const member = reaction.users.cache.find((user) => !user.bot);
              
              message.author.send(Hello)

标签: javascriptdiscorddiscord.js

解决方案


您应该检查做出反应的成员(在 中找到的成员reaction.users.cache)的角色。reaction.users.cache返回一个用户,你需要一个公会成员来获取他们的角色。您可以为此使用message.guild.members.fetch()或。message.guild.member()现在您可以检查返回的成员是否具有角色:

sentMessage.awaitReactions(filter, {
  max: 1,
  time: 60000,
})
.then(async (collected) => {
  const reaction = collected.first();

  if (reaction.emoji.name === '1️⃣') {
    // find the first user who reacted and is not a bot
    const userReacted = reaction.users.cache.find((user) => !user.bot);
    // get the guild member
    const member = await message.guild.member(userReacted);

    if (!member.roles.cache.has('ROLE_ID')) return;

    message.author.send({
      embed: {
        color: 3447003,
        title: 'Вызов принят',
        description: `**Сотрудник:** ${member}`,
        timestamp: new Date(),
      },
    });
  }
})

推荐阅读