Elyas Behroozizade
Elyas Behroozizade

Reputation: 292

Send message to users who reacted to a message discrod js

I want to create a command to send DM to users who reacted (all reactions) to a message from ID. I want to filter the bots and don't send two messages for one user if they reacted twice.

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

Upvotes: 1

Views: 71

Answers (1)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23161

I would probably create a dmSent object with the user IDs as keys, so I could easily check if a DM was already sent to them. When you loop over the users reacted to the message, you can check if the ID already exists or if the user is a bot. If the user is not found in the dmSent object, you can send a message and add the user ID to the object.

I've just checked it, the following sends only one message to each user who reacted at least to the message:

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

Upvotes: 1

Related Questions