Parent
Parent

Reputation: 11

Add roles to user from DMs in new discordjs

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

Upvotes: 0

Views: 44

Answers (1)

MrMythical
MrMythical

Reputation: 9041

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)

Upvotes: 1

Related Questions