Discord.js Get user from reactions **Updated**

Viewed 4863

Update

I have updated the code that is currently working for me, It will now create the channel and then check to see if the channel is create. It will only allow 1 user to create a channel you will not be able to create a T5 channel and a T4 channel as the same user.

Thank you and I hope you all have a great Christmas.

Client.on("message", async (message) => {
  if (message.author.Client || message.channel.type === "dm") return;

  let messageArray = message.content.split(" ");
  let cmd = messageArray[0];
  let args = message.content.substring(message.content.indexOf(" ") + 1);

  if (cmd === `${prefix}battle`) {
    let embed = new Discord.MessageEmbed()
      .setTitle("⚔️ 1657 Battles! ⚔️")
      .setDescription("React to this message to join the battle.")
      .addFields(
        { name: "", value: "T5" },
        { name: "", value: "T4" },
        { name: "", value: "T3" },
        { name: "", value: "T2" },
        { name: "", value: "T1" }
      )
      .setColor("#0099ff")
      .setTimestamp()
      .setFooter("Please pick the troop type you want to battle with!");

    let msgEmbed = await message.channel.send(embed);
    msgEmbed.react(""); // T5
    msgEmbed.react(""); // T4
    msgEmbed.react(""); // T3
    msgEmbed.react(""); // T2
    msgEmbed.react(""); // T1
  }
});

Client.on("messageReactionAdd", async (reaction, user, message) => {
  //Add an event listener
  if (reaction.message.partial) await reaction.message.fetch();
  if (user.id === Client.user.id) return; //If the reaction was from the bot, return
  if (!reaction.message.guild) return; //If the reaction was not in a guild
  const guild = Client.guilds.cache.get("644295524404428832");
  if (
    guild.channels.cache.find((channel) => channel.name === "t5-battle-channel")
  )
    return;

  if (reaction.emoji.name === "") {
    let guild = reaction.message.guild;
    guild.channels.create("T5 Battle Channel", {
      //Creating the channel
      type: "text", //Make sure the channel type is text
      permissionOverwrites: [
        //Set overwrites
        {
          id: guild.id,
          deny: "VIEW_CHANNEL",
        },
        {
          id: "788400016736780338",
          allow: ["VIEW_CHANNEL"],
        },
      ],
    });
  }
  if (
    guild.channels.cache.find((channel) => channel.name === "t4-battle-channel")
  )
    return;

  if (reaction.emoji.name === "") {
    let guild = reaction.message.guild;
    guild.channels.create("T4 Battle Channel", {
      //Creating the channel
      type: "text", //Make sure the channel type is text
      permissionOverwrites: [
        //Set overwrites
        {
          id: guild.id,
          deny: "VIEW_CHANNEL",
        },
        {
          id: "788400619114463275",
          allow: ["VIEW_CHANNEL"],
        },
      ],
    });
  }
  if (
    guild.channels.cache.find((channel) => channel.name === "t3-battle-channel")
  )
    return;

  if (reaction.emoji.name === "") {
    let guild = reaction.message.guild;
    guild.channels.create("T3 Battle Channel", {
      //Creating the channel
      type: "text", //Make sure the channel type is text
      permissionOverwrites: [
        //Set overwrites
        {
          id: guild.id,
          deny: "VIEW_CHANNEL",
        },
        {
          id: "788400701130670110",
          allow: ["VIEW_CHANNEL"],
        },
      ],
    });
  }
  if (
    guild.channels.cache.find((channel) => channel.name === "t2-battle-channel")
  )
    return;

  if (reaction.emoji.name === "") {
    let guild = reaction.message.guild;
    guild.channels.create("T2 Battle Channel", {
      //Creating the channel
      type: "text", //Make sure the channel type is text
      permissionOverwrites: [
        //Set overwrites
        {
          id: guild.id,
          deny: "VIEW_CHANNEL",
        },
        {
          id: "788400738727624704",
          allow: ["VIEW_CHANNEL"],
        },
      ],
    });
  }
  if (
    guild.channels.cache.find((channel) => channel.name === "t1-battle-channel")
  )
    return;

  if (reaction.emoji.name === "") {
    let guild = reaction.message.guild;
    guild.channels.create("T1 Battle Channel", {
      //Creating the channel
      type: "text", //Make sure the channel type is text
      permissionOverwrites: [
        //Set overwrites
        {
          id: guild.id,
          deny: "VIEW_CHANNEL",
        },
        {
          id: "788400784420372490",
          allow: ["VIEW_CHANNEL"],
        },
      ],
    });
  }
});

2 Answers

If you want to get all of the reactions on a message, you can use the reactions property on a message, if you already have the message object. This will return a ReactionManager which you can use.

You should use a reaction collector.

These work quite similarly to message collectors, except that you apply them on a message rather than a channel. The following is an example taken from the documentation, with slightly better variable names for clarification. The filter will check for the emoji - in the default skin tone specifically, so be wary of that. It will also check that the person who reacted shares the same id as the author of the original message that the collector was assigned to.

const filter = (reaction, user) => {
    return reaction.emoji.name === '' && user.id === message.author.id;
};

const collector = message.createReactionCollector(filter, { time: 15000 });

collector.on('collect', (reaction, user) => {
    console.log(`Collected ${reaction.emoji.name} from ${user.tag}`);
});

collector.on('end', collected => {
    console.log(`Collected ${collected.size} items`);
});

Read it's documentation here.

I would like to also know if it is possible to the randomly pair each users that have reacted to a specific role.

The collected property should be handy for this task. As for adding a role/roles to a GuildMember, use the add method. Read about how you can add a role to a member here.

Related