【问题标题】:Send message to users who reacted to a message discrod js向对消息不和谐 js 做出反应的用户发送消息
【发布时间】:2021-05-08 19:13:31
【问题描述】:

我想创建一个命令,将 DM 发送给对来自 ID 的消息做出反应(所有反应)的用户。我想过滤机器人,如果他们反应两次,就不要为一个用户发送两条消息。

if (command === 'gopv') {
  message.channel.messages.fetch("806866144597770280").then((reactionMessage) => {
    console.log(
      reactionMessage.reactions.cache
      .each(async(reaction) => await reaction.users.fetch())
      .map((reaction) => reaction.users.cache.filter((user) => !user.bot))
      .flat()
    );
  });
}

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    我可能会创建一个dmSent 对象,并将用户 ID 作为键,这样我就可以轻松检查是否已经向他们发送了 DM。当您遍历对消息做出反应的用户时,您可以检查 ID 是否已经存在或者用户是否是机器人。如果在dmSent 对象中没有找到用户,您可以发送消息并将用户ID 添加到对象中。

    我刚刚检查过,以下仅向每个至少对该消息做出反应的用户发送一条消息:

    if (command === 'gopv') {
      const dmSent = {};
    
      try {
        const { reactions } = await message.channel.messages.fetch('806866144597770280');
    
        reactions.cache.each(async (reaction) => {
          const usersReacted = await reaction.users.fetch();
    
          usersReacted.each((user) => {
            if (dmSent[user.id] || user.bot) return;
    
            dmSent[user.id] = true;
            user.send('You sent at least one reaction, so here is a DM.');
          });
        });
      } catch (err) {
        console.log(err);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-08
      • 2021-08-28
      • 1970-01-01
      • 2020-06-13
      • 2021-10-31
      • 2020-11-20
      • 1970-01-01
      • 2021-04-02
      • 2020-07-31
      相关资源
      最近更新 更多