Home > Back-end >  Add roles to user from DMs in new discordjs
Add roles to user from DMs in new discordjs

Time:12-16

I wanted to know how I could add a role to a specific user within DMs. With the new Discord.js update, it's tricky, and can't find a way around it. Thanks.

My attempt:

var guild = client.guilds.cache.get("[GUILDID]")
var buyerRole = guild.roles.cache.get("[ROLE ID]")
console.log(message.author.id) // Works
var guildMember = guild.members.fetch(message.author.id)
console.log(guildMember.displayName) // Returns 'undefined'
guildMember.setNickname(guildMember.username " | Buyer") // Error
console.log(buyerRole.color) // Works

Output: guildMember.setNickname is not a function

CodePudding user response:

That's because you fetch the member but never await it. .fetch returns a promise so the right way to get guildMember is like this:

const guildMember = await guild.members.fetch(message.author.id)
  • Related