【问题标题】:Send a message if there is a role Discord.js如果有角色 Discord.js 发送消息
【发布时间】:2021-05-20 13:12:51
【问题描述】:

Bot 如果点击反应的 User 具有 ROLE "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)

【问题讨论】:

  • 什么是sentMessage
  • 对消息做出反应
  • sentMessage 是如何定义的?

标签: javascript discord discord.js


【解决方案1】:

您应该检查做出反应的成员的角色(在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(),
      },
    });
  }
})

【讨论】:

    猜你喜欢
    • 2019-12-04
    • 2021-08-05
    • 2019-02-15
    • 2019-07-26
    • 2021-03-14
    • 1970-01-01
    • 2020-07-17
    • 2020-12-22
    • 1970-01-01
    相关资源
    最近更新 更多