Redact0r
Redact0r

Reputation: 41

Discord.js (V12) messageReactionAdd not working for new users until they type something

This is weird. I have a message reaction add function that adds your name to a list if you react with a certain emoji. However, once the bot starts, no user has any functionality with the bot until they type something in the server (any channel). I am not sure why. This is an issue for new users who just want to join the server and react to the message, as well as update days when I need to re-launch the bot.

My console.log at the top of the event listener does not trigger at all for any user until they send something in the chat, after the bot has ran.

This is the event listener:

bot.on("messageReactionAdd", (reaction, user) => {
  console.log(`emoji added by ${user.tag}`);
  if (reaction.message.reactions.cache.size > 1) {
    reaction.remove();
  }

  function getNickname(array) {
    for (let i = 0; i < array.length; i++) {
      if (array[i].id === user.id) {
        let nickname = array[i].nickname;
        return nickname;
      }
    }
  }

  if (
    reaction.emoji.name === "🕵️‍♂️" &&
    reaction.message.author.bot &&
    user.id !== "773710233977618464"
  ) {
    const spotsLeft = reaction.message.embeds[0].fields[1].value;

    if (spotsLeft == 0) {
      return reaction.users.remove(user.id);
    }

    let newEmbed = reaction.message.embeds[0];

    let guildMembers = reaction.message.guild.members.cache.map((member, i) => {
      let rMember = {};
      rMember.nickname = member.nickname;
      rMember.id = i;
      return rMember;
    });

    let newParticipant = getNickname(guildMembers) || user.tag;
    if (newEmbed.fields[2].value[0] == "0") {
      newEmbed.fields[2].value = [
        `${newEmbed.fields[2].value.length}. ${newParticipant}`,
      ];
    } else {
      let participants = newEmbed.fields[2].value;
      let num =
        typeof participants === "string" ? participants.split("\n").length : 0;
      let newEntry = `\n${num + 1}. ${newParticipant}`;
      participants += newEntry;

      newEmbed.fields[2] = { name: "Participants", value: participants };
    }

    newEmbed.fields[1] = {
      name: "Spots Left",
      value: newEmbed.fields[1].value - 1,
    };
    let newEmbedObj = new Discord.MessageEmbed(newEmbed);
    return reaction.message.edit(newEmbedObj);
  }
});

The "host" message is sent after the bot is ran, and the reactions happen to the new message after the bot is on, for clarification. It's just any user who has not typed in the server cannot trigger the event. Very strange!

Upvotes: 0

Views: 1239

Answers (1)

mobatome
mobatome

Reputation: 1

First, you'll need to find your Bot in your Discord developer portal. Look for the "Privileged Gateway Intents" section and enable "Server Members Intent".

Next, in your app you'll need to initialize the client with intents like this:

const intents = new Discord.Intents(Discord.Intents.ALL);
const discordClient = new Discord.Client({ws: { intents }});

Upvotes: 0

Related Questions