Reputation: 27
I have an issue with give member a role/rank when they are joining my discord, I have just various code statments and none of them has help me so far. Some of the the code that i tried is from Stackoverflow.
my last try is this:
client.on('GuildMemberAdd', member => {
console.log('User: ' + member.user.username + ' has joined the server!');
var role = member.guild.roles.cache.find(role => role.id === "<My role ID>");
member.roles.add(role);
});
This is what I get out of the discord.js documentation and from google when I brows around for a solution for my problem.
I do not know if the problem is that it can not read the role.id
and it has to be the name.
The issue is just that the ID is a better solution then role.name
, just in case the name on the role should be changed, since the ID is bound to that role with out dependency to the name of the role.
I do hope that someone can help me out with this issue.
Best Regards
Upvotes: 1
Views: 2514
Reputation: 761
You cannot have the same role id across multiple guilds, though you can have the same role names.
Trying to use the ID instead means it's locked to one and specific ID.
Say, the ID belongs to your guild, and the member that joined wasn't joining your server, but someone else's, then role
would return undefined as the role of the role ID doesn't exist.
If you're using this only for your server, then this should work, just make sure to check that the member joined your
server. So, other than your server it would use the names instead
client.on('GuildMemberAdd', member => {
if (member.guild.id === "<My guild ID>") {
// <My guild ID> represents an ID of a guild that has a role with the ID <My role ID>
console.log('User: ' + member.user.username + ' has joined the server!');
var role = member.guild.roles.cache.find(role => role.id === "<My role ID>");
member.roles.add(role);
} else {
console.log('User: ' + member.user.username + ' has joined ' + member.guild.name + ' server!');
var role = member.guild.roles.cache.find(role => role.name === "<Whatever>");
member.roles.add(role);
}
});
If you want them to be able to save different IDs for each guild, then I suggest you learn how databases work and try to work on them accordingly.
Upvotes: 1
Reputation: 502
Try to directly add the role using the id:
client.on('GuildMemberAdd', member => {
console.log('User: ' + member.user.username + ' has joined the server!');
member.roles.add(ROLE_ID);
});
Upvotes: 0