2

I wanna make my bot give automatically role when my friends join for first time to my server: I have tried this one my VPS node version: 12.19.0v:?

client.on('guildMemberAdd', member => {
console.log('User @' + member.user.tag + ' has joined the server!');
var role = member.guild.roles.cache.find(role => role.name == "Newbie")
let user = member.user
user.roles.add(role);
});

but nothing is working ! help me pls

Sun Jin
  • 21
  • 3
  • Does the console.log() writes on console? – Cursed Oct 27 '20 at 14:25
  • what do you mean ? it what in my visual: https://prnt.sc/v7z2m8 – Sun Jin Oct 27 '20 at 14:35
  • Does this answer your question? [What is the difference between a User and a GuildMember in discord.js?](https://stackoverflow.com/questions/63979076/what-is-the-difference-between-a-user-and-a-guildmember-in-discord-js) You should be using [`message.member`](https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=member) – Lioness100 Oct 27 '20 at 14:37
  • Also, "does not work" is not sufficient information to provide to receive help. Do you get any errors? – Lioness100 Oct 27 '20 at 14:40
  • literally without any errors just nothing happening when my friend is join ! – Sun Jin Oct 27 '20 at 14:42
  • Does it log `User @Someone#0000 has joined the server`? – Lioness100 Oct 27 '20 at 14:44
  • No it doesn't say something my log only when i run it say: bot is online – Sun Jin Oct 27 '20 at 15:07

2 Answers2

1

Discord is now enforcing privileged intents. The GUILD_MEMBERS intent is required to receive events such as guildMemberAdd and guildMemberUpdate.

To learn how intents work and how to use them, check out discord.js' detailed guide.

Also, as stated in the comments, you need to use message.member instead of message.user

Lioness100
  • 8,260
  • 6
  • 18
  • 49
0

You need to know the difference between Members and Users. A user represents a Discord account, a member represents a user being in a server. So for example to get someone's tag, you need to do user.tag, member.tag wouldn't work. It's the same with nicknames, user.displayName woudln't work, because a user is an account, a member is a user in a server. You'd do member.displayName instead.

Anyway, where you put

let user = member.user
user.roles.add(role);

it should be replaced with

member.roles.add(role);

and that should hopefully solve your problem.

Yankue Team
  • 109
  • 1
  • 10
  • i tried this one before doesn't work as well i don't receive when someone join to my server on log too ! – Sun Jin Oct 27 '20 at 15:11