0

For some reason, the bot I wrote using Node.js and Discord.js does not assign roles to members when they join. Can someone please help?

bot.on("guildMemberAdd", function (member) {
    member.guild.channels
        .find("name", "general")
        .send(
            member.toString() +
                " Welcome to the server. All your base are belong to us."
        );
    member.add(member.guild.roles.find("name", "Honorable Member"));
});

I've tried both member.add and member.addRole, nothing I do seems to have any effect. I'm not even getting an error message of any kind, the code just doesn't seem to be executing.

kmoser
  • 8,780
  • 3
  • 24
  • 40
Dogfoger
  • 11
  • 2
  • Are you using discord.js v12 or v11? – Liam Feb 06 '21 at 14:24
  • I'm using discord.js v11.6.2 – Dogfoger Feb 06 '21 at 20:27
  • Does this answer your question? [None of my discord.js guildmember events are emitting, my user caches are basically empty, and my functions are timing out?](https://stackoverflow.com/questions/64559390/none-of-my-discord-js-guildmember-events-are-emitting-my-user-caches-are-basica) – Zsolt Meszaros Feb 07 '21 at 19:02
  • @Zsolt Meszaros Yes!! That worked for me, thank you so much! – Dogfoger Feb 08 '21 at 00:53

1 Answers1

-1

How to figure out what discord.js version you have:

Open up Command Prompt in the current directory (the same folder as your bot.js file). Then within that type npm list discord.js and it will return something similar to this: example of npm list command

It will either start with 11 or 12, that is how you can tell if your discord.js is version 11 or version 12.

How to fix your problem on discord.js v11:

Since you may be using discord.js v11, I'll give an example on how to fix it there:

member.addRole(member.guild.roles.find("name", "Honorable Member").id);

If you are using discord.js v12:

You need to use modern techniques to accomplish this.

const roleToAdd = member.guild.roles.cache.find(r => r.name === "Honorable Member");
member.roles.add(roleToAdd);
Liam
  • 1,179
  • 1
  • 7
  • 22
  • I'm using version 11.6.4, tried both options and neither made a difference. The bot doesn't do anything at all when a person joins a server, not even with a console.log command – Dogfoger Feb 06 '21 at 20:26
  • I eventually managed to fix it, I had to enable some settings in the Discord Application portal and then your code worked for me. Thank you! – Dogfoger Feb 08 '21 at 00:53