0

I have a discord bot that I'm attempting to assign a user a role with. From some searching around, I've discovered that the code let role = message.guild.roles.cache.get("[role_id]") message.author.roles.add(role) is supposed to work.

The issue is that message.author.roles doesn't exist. Is there a new way to do this for discord.js v13, or am I missing something?

TheLazySquid
  • 69
  • 1
  • 8
  • 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) – MrMythical Oct 25 '21 at 12:21

3 Answers3

3

As per the discord.js docs, Message#author is a User object, which doesn't have the .roles property. What you want is Message#member, which is a GuildMember and has the property you want in order to give and remove roles.

Give this a shot:

let role = message.guild.roles.cache.get("[role_id]")
message.member.roles.add(role)
Pedro Fracassi
  • 3,342
  • 2
  • 16
  • 33
0

I use another method and it works as good as this, try it out

let role = message.guild.roles.cache.find((role) => role.name === "[role name here]")
message.member.roles.add(role)
MrMythical
  • 8,908
  • 2
  • 17
  • 45
0
const mention = (await message.mentions.members.first()) || message.guild.members.cache.get(args[0]) || message.guild.members.cache.find((r) => r.user.username.toLowerCase().includes() === args.join(" ").toLocaleLowerCase()) || message.guild.members.cache.find((r) => r.displayName.toLowerCase().includes() === args.join(" ").toLocaleLowerCase()); // get member on mention
const role = message.mentions.roles.first(); // get role on mention
await mention.roles.add(role)

{prefix} {command} {mention a user} {mention a role}

Don't forget, this is in async function